Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2022 - Google LLC
0004  * Author: Keir Fraser <keirf@google.com>
0005  */
0006 
0007 #include <linux/list.h>
0008 #include <linux/bug.h>
0009 
0010 static inline __must_check bool nvhe_check_data_corruption(bool v)
0011 {
0012     return v;
0013 }
0014 
0015 #define NVHE_CHECK_DATA_CORRUPTION(condition)                \
0016     nvhe_check_data_corruption(({                    \
0017         bool corruption = unlikely(condition);           \
0018         if (corruption) {                    \
0019             if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
0020                 BUG_ON(1);               \
0021             } else                       \
0022                 WARN_ON(1);              \
0023         }                            \
0024         corruption;                      \
0025     }))
0026 
0027 /* The predicates checked here are taken from lib/list_debug.c. */
0028 
0029 bool __list_add_valid(struct list_head *new, struct list_head *prev,
0030               struct list_head *next)
0031 {
0032     if (NVHE_CHECK_DATA_CORRUPTION(next->prev != prev) ||
0033         NVHE_CHECK_DATA_CORRUPTION(prev->next != next) ||
0034         NVHE_CHECK_DATA_CORRUPTION(new == prev || new == next))
0035         return false;
0036 
0037     return true;
0038 }
0039 
0040 bool __list_del_entry_valid(struct list_head *entry)
0041 {
0042     struct list_head *prev, *next;
0043 
0044     prev = entry->prev;
0045     next = entry->next;
0046 
0047     if (NVHE_CHECK_DATA_CORRUPTION(next == LIST_POISON1) ||
0048         NVHE_CHECK_DATA_CORRUPTION(prev == LIST_POISON2) ||
0049         NVHE_CHECK_DATA_CORRUPTION(prev->next != entry) ||
0050         NVHE_CHECK_DATA_CORRUPTION(next->prev != entry))
0051         return false;
0052 
0053     return true;
0054 }