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

refcount: introduce __refcount_{add|inc}_not_zero_limited_acquire

Introduce functions to increase refcount but with a top limit above which
they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAX indicates no limit.

Link: https://lkml.kernel.org/r/20250213224655.1680278-12-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Tested-by: Shivank Garg <shivankg@amd.com>
Link: https://lkml.kernel.org/r/5e19ec93-8307-47c2-bb13-3ddf7150624e@amd.com
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Klara Modin <klarasmodin@gmail.com>
Cc: Liam R. Howlett <Liam.Howlett@Oracle.com>
Cc: Lokesh Gidra <lokeshgidra@google.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Mateusz Guzik <mjguzik@gmail.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Minchan Kim <minchan@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Sourav Panda <souravpanda@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Wei Yang <richard.weiyang@gmail.com>
Cc: Will Deacon <will@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Suren Baghdasaryan and committed by
Andrew Morton
4e0dbe10 7f8ceea0

+20 -1
+20 -1
include/linux/refcount.h
··· 213 213 } 214 214 215 215 static inline __must_check __signed_wrap 216 - bool __refcount_add_not_zero_acquire(int i, refcount_t *r, int *oldp) 216 + bool __refcount_add_not_zero_limited_acquire(int i, refcount_t *r, int *oldp, 217 + int limit) 217 218 { 218 219 int old = refcount_read(r); 219 220 220 221 do { 221 222 if (!old) 222 223 break; 224 + 225 + if (i > limit - old) { 226 + if (oldp) 227 + *oldp = old; 228 + return false; 229 + } 223 230 } while (!atomic_try_cmpxchg_acquire(&r->refs, &old, old + i)); 224 231 225 232 if (oldp) ··· 236 229 refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF); 237 230 238 231 return old; 232 + } 233 + 234 + static inline __must_check bool 235 + __refcount_inc_not_zero_limited_acquire(refcount_t *r, int *oldp, int limit) 236 + { 237 + return __refcount_add_not_zero_limited_acquire(1, r, oldp, limit); 238 + } 239 + 240 + static inline __must_check __signed_wrap 241 + bool __refcount_add_not_zero_acquire(int i, refcount_t *r, int *oldp) 242 + { 243 + return __refcount_add_not_zero_limited_acquire(i, r, oldp, INT_MAX); 239 244 } 240 245 241 246 /**