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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/lockref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #if USE_CMPXCHG_LOCKREF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Note that the "cmpxchg()" reloads the "old" value for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * failure case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define CMPXCHG_LOOP(CODE, SUCCESS) do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 	int retry = 100;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	struct lockref old;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	BUILD_BUG_ON(sizeof(old) != 8);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	old.lock_count = READ_ONCE(lockref->lock_count);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) {  	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 		struct lockref new = old, prev = old;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 		CODE								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		old.lock_count = cmpxchg64_relaxed(&lockref->lock_count,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 						   old.lock_count,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 						   new.lock_count);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		if (likely(old.lock_count == prev.lock_count)) {		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 			SUCCESS;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		if (!--retry)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 			break;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		cpu_relax();							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	}									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * lockref_get - Increments reference count unconditionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * This operation is only valid if you already hold a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * to the object, so you know the count cannot be zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) void lockref_get(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		new.count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	lockref->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) EXPORT_SYMBOL(lockref_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * lockref_get_not_zero - Increments count unless the count is 0 or dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * Return: 1 if count updated successfully or 0 if count was zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) int lockref_get_not_zero(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		new.count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (old.count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (lockref->count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		lockref->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) EXPORT_SYMBOL(lockref_get_not_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Return: 1 if count updated successfully or 0 if count would become zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) int lockref_put_not_zero(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		new.count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		if (old.count <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (lockref->count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		lockref->count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) EXPORT_SYMBOL(lockref_put_not_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * lockref_get_or_lock - Increments count unless the count is 0 or dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * Return: 1 if count updated successfully or 0 if count was zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * and we got the lock instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int lockref_get_or_lock(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		new.count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (old.count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (lockref->count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	lockref->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL(lockref_get_or_lock);
^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)  * lockref_put_return - Decrement reference count if possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * Decrement the reference count and return the new value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * If the lockref was dead or locked, return an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int lockref_put_return(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		new.count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if (old.count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return new.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) EXPORT_SYMBOL(lockref_put_return);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * lockref_put_or_lock - decrements count unless count <= 1 before decrement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int lockref_put_or_lock(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		new.count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		if (old.count <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (lockref->count <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	lockref->count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) EXPORT_SYMBOL(lockref_put_or_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * lockref_mark_dead - mark lockref dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) void lockref_mark_dead(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	assert_spin_locked(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	lockref->count = -128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL(lockref_mark_dead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * lockref_get_not_dead - Increments count unless the ref is dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * @lockref: pointer to lockref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * Return: 1 if count updated successfully or 0 if lockref was dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int lockref_get_not_dead(struct lockref *lockref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	CMPXCHG_LOOP(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		new.count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		if (old.count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	spin_lock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (lockref->count >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		lockref->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	spin_unlock(&lockref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) EXPORT_SYMBOL(lockref_get_not_dead);