From nobody Sun Feb 8 23:25:54 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3325318FDBE for ; Sat, 11 Jan 2025 04:26:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736569591; cv=none; b=O2CmrjOSQhgYsi9NCH+MwxilTr/q52Drt5Lf4w7chCMA4shUsEgbqoTYLd+DYrAkvSbYEa+JR8h7ysTMmUe0QS7kE7Uy2hIcZC1cBYxt3YVNfPdV0VvWdlJtAdp1KXstfWCZAxX4X5W6jaD2y/4Tu8I7rFOSTUHGaunP4nNYL9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736569591; c=relaxed/simple; bh=ClFnDYClt/1mzZUhQ7YHEE3ht4KWxc0BIqAtptsG1rc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lXjnri18h2LsGJ7I4aOHk5Q2TOEc35ikX3JQz62Mp91fCEbM4UmfBNdUC754pV16uRuY3gb70mLxr8ZafjBAhM+y1BaVBD8SbrWmtChA/YezQ5pnNWD97okCRBKRN/g9/zWBQImGGGaWSNbTL/waWJugqzKQ8I/v+3+eqMPKb8c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AdvXjKeM; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AdvXjKeM" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef8c7ef51dso4834380a91.1 for ; Fri, 10 Jan 2025 20:26:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736569589; x=1737174389; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VwcbS0dFv6CgCNycrk/1CQg6vwwJAzf/LstwUATtKHM=; b=AdvXjKeMe7qWZornGiSIIUqZbtl63WNHwgGzV0QV2XAEKAdfMLJrb1BxW5Q6O4do5V 1+ZqlHWMy7WbpxQZ70vyS1sPQlY9d2RIiOwVeg3xA9sVOX8kyRxTTi0xFwg4uZbaMMVu rKiSNRR3KPSZ51DGozyEY+MZ+9KqKjpvtfxk4VTY9w+76NVGFWmz78DPCbBSbDwfKebb kqQDYNtwrCIPuUL3vTx4saUwNrQokgYA+yEHYh24gPmWBcBkKMLwhHqpF+ZfP6zvEdTZ Uyng78A+UEBzk71ZOQ3FurUQQ/hln8WvY/K44G09uMmpcmo1A4H1CjjcQDe0HEt1uvnW vqJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736569589; x=1737174389; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VwcbS0dFv6CgCNycrk/1CQg6vwwJAzf/LstwUATtKHM=; b=bSXyD3QWS3zUoARbjRVEeyoQsbs04d8LzQE7eNcmY7D2WbE0kH8V27s9YCDFHzvDGI pJ2kKytJXTJKi/TrAHDuqPQ6M0RNFDxIeoeTTuwlhvcGH5LFbXd8r8YdCT7/tvAmm3mq ypSHr5t5PDl/9kSt0Zo+tb/eOoynUlJiQb7huHds5M+2OnjYi4O6REld5FYlt0gfMHwK owgdZdG3hkvXe9cnn20+DkGNiQH+91dQsIz8c6gA/GA8VWJwb18wG0Kj/VvLSSCJOKWN jrHDzfrL7vfMtLB60TeRvtxYZDl5V0xo0Ubb+BnU0+eLurkgU6lAbUqFcvKiN/pmq/ET GMiw== X-Forwarded-Encrypted: i=1; AJvYcCXzMSk2H/K0eWrbjbV6gwWajkrkSB3dCl2olbAuLQQV1evBnMKsEOVR9gpDkjCl+PBWvDApS8q0365kLTc=@vger.kernel.org X-Gm-Message-State: AOJu0YyAuYQgIr3+Kq5/LZD/J7Xcnp1RkVRBIiX0Pz9IpQHjfd7/ICFC EEHCy0CBprYe00p5TEOX5MhWrexc3CptvBtEoIwOGIIgR31UF7yY9ktrcMDc26ml5Cv0dy+BeAq Xag== X-Google-Smtp-Source: AGHT+IHRdmdmZQsqrHxmHBpzkjciwsLRGCl3+kAbA70quP18hv457pqxyHwgZacevWuPeHpfl252t4i39B0= X-Received: from pjuj6.prod.google.com ([2002:a17:90a:d006:b0:2ea:4139:e72d]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b4f:b0:2f2:a974:fc11 with SMTP id 98e67ed59e1d1-2f554603e39mr14016432a91.17.1736569589540; Fri, 10 Jan 2025 20:26:29 -0800 (PST) Date: Fri, 10 Jan 2025 20:25:57 -0800 In-Reply-To: <20250111042604.3230628-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111042604.3230628-11-surenb@google.com> Subject: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, david.laight.linux@gmail.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, richard.weiyang@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Suren Baghdasaryan --- include/linux/refcount.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 35f039ecb272..5072ba99f05e 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcou= nt_t *r) } =20 static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) +bool __refcount_add_not_zero_limited(int i, refcount_t *r, int *oldp, + int limit) { int old =3D refcount_read(r); =20 do { if (!old) break; + + if (statically_true(limit =3D=3D INT_MAX)) + continue; + + if (i > limit - old) { + if (oldp) + *oldp =3D old; + return false; + } } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i)); =20 if (oldp) @@ -155,6 +165,12 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int= *oldp) return old; } =20 +static inline __must_check __signed_wrap +bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) +{ + return __refcount_add_not_zero_limited(i, r, oldp, INT_MAX); +} + /** * refcount_add_not_zero - add a value to a refcount unless it is 0 * @i: the value to add to the refcount @@ -213,6 +229,12 @@ static inline void refcount_add(int i, refcount_t *r) __refcount_add(i, r, NULL); } =20 +static inline __must_check bool __refcount_inc_not_zero_limited(refcount_t= *r, + int *oldp, int limit) +{ + return __refcount_add_not_zero_limited(1, r, oldp, limit); +} + static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int= *oldp) { return __refcount_add_not_zero(1, r, oldp); --=20 2.47.1.613.gc27f4b7a9f-goog