Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ucount: replace get_ucounts_or_wrap() with atomic_inc_not_zero()

get_ucounts_or_wrap() increments the counter and if the counter is
negative then it decrements it again in order to reset the previous
increment. This statement can be replaced with atomic_inc_not_zero() to
only increment the counter if it is not yet 0.

This simplifies the get function because the put (if the get failed) can
be removed. atomic_inc_not_zero() is implement as a cmpxchg() loop which
can be repeated several times if another get/put is performed in parallel.
This will be optimized later.

Increment the reference counter only if not yet dropped to zero.

Link: https://lkml.kernel.org/r/20250203150525.456525-3-bigeasy@linutronix.de
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Reviewed-by: Paul E. McKenney <paulmck@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Joel Fernandes <joel@joelfernandes.org>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Lai jiangshan <jiangshanlai@gmail.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Mengen Sun <mengensun@tencent.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
Cc: YueHong Wu <yuehongwu@tencent.com>
Cc: Zqiang <qiang.zhang1211@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Sebastian Andrzej Siewior and committed by
Andrew Morton
328152e6 8c6bbda8

+6 -18
+6 -18
kernel/ucount.c
··· 146 146 spin_unlock_irq(&ucounts_lock); 147 147 } 148 148 149 - static inline bool get_ucounts_or_wrap(struct ucounts *ucounts) 150 - { 151 - /* Returns true on a successful get, false if the count wraps. */ 152 - return !atomic_add_negative(1, &ucounts->count); 153 - } 154 - 155 149 struct ucounts *get_ucounts(struct ucounts *ucounts) 156 150 { 157 - if (!get_ucounts_or_wrap(ucounts)) { 158 - put_ucounts(ucounts); 159 - ucounts = NULL; 160 - } 161 - return ucounts; 151 + if (atomic_inc_not_zero(&ucounts->count)) 152 + return ucounts; 153 + return NULL; 162 154 } 163 155 164 156 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid) 165 157 { 166 158 struct hlist_head *hashent = ucounts_hashentry(ns, uid); 167 - bool wrapped; 168 159 struct ucounts *ucounts, *new = NULL; 169 160 170 161 spin_lock_irq(&ucounts_lock); ··· 180 189 return new; 181 190 } 182 191 } 183 - 184 - wrapped = !get_ucounts_or_wrap(ucounts); 192 + if (!atomic_inc_not_zero(&ucounts->count)) 193 + ucounts = NULL; 185 194 spin_unlock_irq(&ucounts_lock); 186 195 kfree(new); 187 - if (wrapped) { 188 - put_ucounts(ucounts); 189 - return NULL; 190 - } 196 + 191 197 return ucounts; 192 198 } 193 199