xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/list_debug.c (revision d57cc3b9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 - Google LLC
4  * Author: Keir Fraser <keirf@google.com>
5  */
6 
7 #include <linux/list.h>
8 #include <linux/bug.h>
9 
10 static inline __must_check bool nvhe_check_data_corruption(bool v)
11 {
12 	return v;
13 }
14 
15 #define NVHE_CHECK_DATA_CORRUPTION(condition)				 \
16 	nvhe_check_data_corruption(({					 \
17 		bool corruption = unlikely(condition);			 \
18 		if (corruption) {					 \
19 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
20 				BUG_ON(1);				 \
21 			} else						 \
22 				WARN_ON(1);				 \
23 		}							 \
24 		corruption;						 \
25 	}))
26 
27 /* The predicates checked here are taken from lib/list_debug.c. */
28 
29 bool __list_add_valid(struct list_head *new, struct list_head *prev,
30 		      struct list_head *next)
31 {
32 	if (NVHE_CHECK_DATA_CORRUPTION(next->prev != prev) ||
33 	    NVHE_CHECK_DATA_CORRUPTION(prev->next != next) ||
34 	    NVHE_CHECK_DATA_CORRUPTION(new == prev || new == next))
35 		return false;
36 
37 	return true;
38 }
39 
40 bool __list_del_entry_valid(struct list_head *entry)
41 {
42 	struct list_head *prev, *next;
43 
44 	prev = entry->prev;
45 	next = entry->next;
46 
47 	if (NVHE_CHECK_DATA_CORRUPTION(next == LIST_POISON1) ||
48 	    NVHE_CHECK_DATA_CORRUPTION(prev == LIST_POISON2) ||
49 	    NVHE_CHECK_DATA_CORRUPTION(prev->next != entry) ||
50 	    NVHE_CHECK_DATA_CORRUPTION(next->prev != entry))
51 		return false;
52 
53 	return true;
54 }
55