From patchwork Sat Jan 11 04:25:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13935776 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F37BE7719C for ; Sat, 11 Jan 2025 04:26:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E6CB6B009F; Fri, 10 Jan 2025 23:26:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 16EDC6B00A0; Fri, 10 Jan 2025 23:26:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDD7C6B00A2; Fri, 10 Jan 2025 23:26:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C29866B009F for ; Fri, 10 Jan 2025 23:26:32 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 771F645795 for ; Sat, 11 Jan 2025 04:26:32 +0000 (UTC) X-FDA: 82993884624.24.39CAF78 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf19.hostedemail.com (Postfix) with ESMTP id B3F4C1A0004 for ; Sat, 11 Jan 2025 04:26:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QggGXiTh; spf=pass (imf19.hostedemail.com: domain of 39fKBZwYKCCYUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=39fKBZwYKCCYUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736569590; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VwcbS0dFv6CgCNycrk/1CQg6vwwJAzf/LstwUATtKHM=; b=8bVgrGtM+JZCZ2mnpEzIpKA6KZMNqdfs3IUbPD5LaBHC0TYuLMlrlGXjOBlOjHrStdxSnV pETvAO9fQhajEG8lYq+GNSqBKAfaBBqBlAjYWTM+80DQo4h387ZbSdg0usciR4wc4fzlAR 2KHskors3q7cZ+RPVI+lbxdSykwBvSQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QggGXiTh; spf=pass (imf19.hostedemail.com: domain of 39fKBZwYKCCYUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=39fKBZwYKCCYUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736569590; a=rsa-sha256; cv=none; b=i4eduAKr9WSuXy/zzgoRDGLgG+uCoS8Jg+hd/KU/40XJh4lC6M1lefFNjdS4PYvxuVuSJC rDtlNbnGJWZS8aDaZSbkeRXwzfmyEBdQ0X2WJt4jbIo4E+RJdXpVr6oHdvXioqw1L+/5P5 +bT+i0fKqJRGkEOjlqge0E+fD1kHL/0= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef6ef86607so6413326a91.0 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=kvack.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=QggGXiTh2iJEv7MbsRYVxEtv/edTStDA9byrloMSwZzv8Kes7h5JA8LMtCt5O8jR2d aPfTHEcBMysjV8v3C2EtGh0Bq87mOf6IG/W1hNGTEw1EgRbvHuaVDfjf9UB70RvBxc3d hjDJNLcUSdLuLfpE6jhU8Vxk7rNDMCWc+2xwOGVQQ//w6DZ5jde1YvnHS0j6JSkpnJ9s ZQ4NtQhXfLJke3R0lKIh/V9TnRUervEPV/3BCZtpcqZGT4AADqI+ffTJrbW4KRo5hUYW Tri+JZp/0etF0zlCXRAuSohysf103Y2iRQBt5DCYlNcKc7+mWNTVjFYhUOFWXU94la2Q U91Q== 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=J439SPxGNMMj2OaiceT74jpfT/Ju198kAAMkhyrX4wfAY0ge73jXvt9stp64Bv4Y1L 1zshX1AK5QyXgAxlpzsaQmMqFkKXF2m55uhEjk5qtA26fNF0goyTKyNiyIZ3UBph6Hzm fT4SDlQ/uRDHowAATKTZ0ZmvGQPSV5IZGX1vfsXBTuuaP1e0OM/gVI+fDKHbsMirYoZW 6ch7g80JnpJjnuhpIWRAcERQkcfuIC4IWlniJbzEHmXs1bLz0LA3nKzjAScDMVxqFGyP hmQDKKzmECXgKPsQigI5RDV4J6EGSDIY/BkFL87F/186dnaU2yBErqO/BYwqmR0xwZFx mBdw== X-Forwarded-Encrypted: i=1; AJvYcCURVObzsg4qMtp6AuK5lreYDDGLw7wrRNtOGeQ7can5TOzSRlg4HXCn0X178Z/CgwwOmBgVjCaALQ==@kvack.org X-Gm-Message-State: AOJu0YzJrmDSmsS5YxE5HyPl2ncZPJKukdxes6hVw1+WvNtwPFqbh9Ks +K9CAup29V73wEn7xSPeGE6nCq6AKiIQGDFgJhCzBKnOTJr6gY+gxghiEjKNcHxUCHQyD8WyClJ UOw== 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> 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 X-Rspamd-Server: rspam05 X-Stat-Signature: q43q73rei56otgyk5j886bwtto69enum X-Rspamd-Queue-Id: B3F4C1A0004 X-Rspam-User: X-HE-Tag: 1736569590-677493 X-HE-Meta: U2FsdGVkX1+kxmP2+9s5/m0GUWytcTwG6qM1M62SCWUeXbdD5WX8x4p1+TuJCTHPlayYgwlyd7TuoZQcNeUfTPDK2ZbrMnW3ZNGntCBudc0C/JaYnjGn0uLFAi0w3RukKsumgX7QuFEPKReWCAz/vb58zS28PGK6wALnZY6lMiTpELCqVidKKADkfH9+nflVufi3tkNSv3BuZmTRHJUtk33Wa9jhdBoq9xiEE3rw7terKvqDn49AtW/JVU9Q/1w9uba5ChO7R/ikO0J8uxG7nnT8GEWMDPT/Q1vc+o3aHxsnNb+5QhjTqmv8mVo2apqfE30U2fvZ7n5tMGb6DMBLudjjH/F5dUYD2MsmOS7N9BME15KWrg+z5b5Aa0HKRarwczRhs+Xu3SV9Fl2FhWgWvLeZ5NBS+BeRcfsBKB0NEUP6oCpsMXbJwE2x3rA1JbUIzRB+/C6hmzvG3uDuPVfsX0cNshVlMYljZ/IpdZdsEBGyDSjTMUIAvw6MQtx9nwk/QOOinhsdFgRkPW8KxrhpKvmUvHbFYNqsk78nngBay5VKKrb2t3wBnzjZ2flcKE7d3qhD11cR9HQnsPcBbtA7DFbgHgBtToCdPAgSUnLcE0k+G8amdY1AinbQ7M+v7Ee7uuZWQ1Z7azgCJheZluSg+NKg/iqcPQmYkfX/ux5q5mo7jL26kguqyYjsR2tjrkz3Eq/RUiDxPxJZz2Cn/uJz961S4mSqLbnOGHL5Za72CbxqFAbu1TOao6DyycM+BR8MkU75YLot3osBYRCj3Mq6q5d2IBDJ1gpGmRN4jI8Lz/doYKR37nBw/6WmgutzGo3876qO7g6g6FAcFQIpIWs8CKpa27rqXAPRvP9cmam0RNZKjmODdseZuZw0UMGOmtt7Oe1PFOTCL1bM0F7fpJT9YvNH+CAfXTU05lJG5MJb1bzpDaPbxi1wpyd6xZwXGOMc6exR0WEdvGX3DX2KOXU EamEcOwP HcIJeAazsU92Lt/YYBbj9MliabBTwN7l2tL9lt5RJpOxL7dXCrny+ImQ/TosflbVdKdOdQSE2023KHtVjKKdPh+t88vRCLREFpe/PLwR4R3tyQwbR3fQSUENFaHFr0krWTCW+BCf+Mq691g9IlQdq2N1k+Lsrx8aV1jkiNLvfQxun61N9ZYJE4YDXEGvDpz+PIqwFCYSaN8UYvHxxQGt0TlUPefmJr1mKxyuA1wfTVjU/n7tgj3JQRZYBv7yB+M8n+Zy7d5hlzBEwQUtttoZWptTthh9P93Ltzqq8HuEFqSIdzb1cQ+28oboKRK8rt/RJlOquRL/r94LGYtYyvd3ziQEWR2OA7d23oTflxMK2e0w9N3o5Z/MKhWZjfu8r1JD44qmegcdxJvcRkXgtmSDpB0LNAHPvUiMVjVSMM96tQ8HpbKJV0dGxYowi5JtV7a3iBldOb2lWDLfajPR3ZBSrpWzXEPzYx2Vw2lpsYCyVvvZvZS1r0PU61eox2rysHI+n1ZwxfmQ40/weCJlu2Ze48jKuXxb/42Y+0wBPRhinAm7Gahh2S3P1dmqH1ACaL9ZEQNBvPaIjxuREeNWaIfbhe4zZbSpK5kN3lRlMZUU3Vg+G8pX3HIBwqUGCJ+TUi8AXfBZKlRoGWLFJbIVaA3J3r5gDID6fOKf9fqny4Rb98BuAAeTRoO3Wdiok5/W2o9V+9QMw7LajrCLRLH4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.004666, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 refcount_t *r) } 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 = refcount_read(r); do { if (!old) break; + + if (statically_true(limit == INT_MAX)) + continue; + + if (i > limit - old) { + if (oldp) + *oldp = old; + return false; + } } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i)); if (oldp) @@ -155,6 +165,12 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) return old; } +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); } +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);