Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * shadow.c - Shadow Variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2014 Josh Poimboeuf <jpoimboe@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * DOC: Shadow variable API concurrency notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * The shadow variable API provides a simple relationship between an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * <obj, id> pair and a pointer value.  It is the responsibility of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * caller to provide any mutual exclusion required of the shadow data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * Once a shadow variable is attached to its parent object via the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * klp_shadow_*alloc() API calls, it is considered live: any subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * call to klp_shadow_get() may then return the shadow variable's data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * pointer.  Callers of klp_shadow_*alloc() should prepare shadow data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * The klp_shadow_*alloc() API calls may allocate memory for new shadow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * variable structures.  Their implementation does not call kmalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * inside any spinlocks, but API callers should pass GFP flags according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * to their specific needs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * The klp_shadow_hash is an RCU-enabled hashtable and is safe against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * concurrent klp_shadow_free() and klp_shadow_get() operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/livepatch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static DEFINE_HASHTABLE(klp_shadow_hash, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * klp_shadow_lock provides exclusive access to the klp_shadow_hash and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * the shadow variables it references.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static DEFINE_SPINLOCK(klp_shadow_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * struct klp_shadow - shadow variable structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @node:	klp_shadow_hash hash table node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * @rcu_head:	RCU is used to safely free this structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * @data:	data area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct klp_shadow {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct rcu_head rcu_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	void *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	unsigned long id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	char data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * klp_shadow_match() - verify a shadow variable matches given <obj, id>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * @shadow:	shadow variable to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * Return: true if the shadow variable matches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static inline bool klp_shadow_match(struct klp_shadow *shadow, void *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				unsigned long id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return shadow->obj == obj && shadow->id == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * klp_shadow_get() - retrieve a shadow variable data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * Return: the shadow variable data element, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) void *klp_shadow_get(void *obj, unsigned long id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct klp_shadow *shadow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	hash_for_each_possible_rcu(klp_shadow_hash, shadow, node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				   (unsigned long)obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		if (klp_shadow_match(shadow, obj, id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			return shadow->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) EXPORT_SYMBOL_GPL(klp_shadow_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				       size_t size, gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				       klp_shadow_ctor_t ctor, void *ctor_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				       bool warn_on_exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct klp_shadow *new_shadow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	void *shadow_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* Check if the shadow variable already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	shadow_data = klp_shadow_get(obj, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (shadow_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto exists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * Allocate a new shadow variable.  Fill it with zeroes by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * More complex setting can be done by @ctor function.  But it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * called only when the buffer is really used (under klp_shadow_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (!new_shadow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* Look for <obj, id> again under the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	spin_lock_irqsave(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	shadow_data = klp_shadow_get(obj, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (unlikely(shadow_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		 * Shadow variable was found, throw away speculative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		 * allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		spin_unlock_irqrestore(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		kfree(new_shadow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto exists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	new_shadow->obj = obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	new_shadow->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (ctor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		err = ctor(obj, new_shadow->data, ctor_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			spin_unlock_irqrestore(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			kfree(new_shadow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			pr_err("Failed to construct shadow variable <%p, %lx> (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			       obj, id, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* No <obj, id> found, so attach the newly allocated one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	hash_add_rcu(klp_shadow_hash, &new_shadow->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		     (unsigned long)new_shadow->obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	spin_unlock_irqrestore(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return new_shadow->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) exists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (warn_on_exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		WARN(1, "Duplicate shadow variable <%p, %lx>\n", obj, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return shadow_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * klp_shadow_alloc() - allocate and add a new shadow variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * @size:	size of attached data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * @gfp_flags:	GFP mask for allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * @ctor:	custom constructor to initialize the shadow data (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * @ctor_data:	pointer to any data needed by @ctor (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Allocates @size bytes for new shadow variable data using @gfp_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * The data are zeroed by default.  They are further initialized by @ctor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * function if it is not NULL.  The new shadow variable is then added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * to the global hashtable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * If an existing <obj, id> shadow variable can be found, this routine will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * issue a WARN, exit early and return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * This function guarantees that the constructor function is called only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * the variable did not exist before.  The cost is that @ctor is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * in atomic context under a spin lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * Return: the shadow variable data element, NULL on duplicate or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void *klp_shadow_alloc(void *obj, unsigned long id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		       size_t size, gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		       klp_shadow_ctor_t ctor, void *ctor_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 					 ctor, ctor_data, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) EXPORT_SYMBOL_GPL(klp_shadow_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * klp_shadow_get_or_alloc() - get existing or allocate a new shadow variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * @size:	size of attached data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * @gfp_flags:	GFP mask for allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * @ctor:	custom constructor to initialize the shadow data (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * @ctor_data:	pointer to any data needed by @ctor (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * Returns a pointer to existing shadow data if an <obj, id> shadow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * variable is already present.  Otherwise, it creates a new shadow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * variable like klp_shadow_alloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * This function guarantees that only one shadow variable exists with the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * @id for the given @obj.  It also guarantees that the constructor function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * will be called only when the variable did not exist before.  The cost is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * that @ctor is called in atomic context under a spin lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * Return: the shadow variable data element, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			      size_t size, gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			      klp_shadow_ctor_t ctor, void *ctor_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 					 ctor, ctor_data, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) EXPORT_SYMBOL_GPL(klp_shadow_get_or_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static void klp_shadow_free_struct(struct klp_shadow *shadow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				   klp_shadow_dtor_t dtor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	hash_del_rcu(&shadow->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (dtor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		dtor(shadow->obj, shadow->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	kfree_rcu(shadow, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * klp_shadow_free() - detach and free a <obj, id> shadow variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * @obj:	pointer to parent object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * @dtor:	custom callback that can be used to unregister the variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  *		and/or free data that the shadow variable points to (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  * This function releases the memory for this <obj, id> shadow variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * instance, callers should stop referencing it accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct klp_shadow *shadow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	spin_lock_irqsave(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* Delete <obj, id> from hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	hash_for_each_possible(klp_shadow_hash, shadow, node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			       (unsigned long)obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		if (klp_shadow_match(shadow, obj, id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			klp_shadow_free_struct(shadow, dtor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	spin_unlock_irqrestore(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) EXPORT_SYMBOL_GPL(klp_shadow_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * klp_shadow_free_all() - detach and free all <*, id> shadow variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * @id:		data identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * @dtor:	custom callback that can be used to unregister the variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  *		and/or free data that the shadow variable points to (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * This function releases the memory for all <*, id> shadow variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * instances, callers should stop referencing them accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct klp_shadow *shadow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	spin_lock_irqsave(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	/* Delete all <*, id> from hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	hash_for_each(klp_shadow_hash, i, shadow, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (klp_shadow_match(shadow, shadow->obj, id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			klp_shadow_free_struct(shadow, dtor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	spin_unlock_irqrestore(&klp_shadow_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) EXPORT_SYMBOL_GPL(klp_shadow_free_all);