From patchwork Mon Dec 16 19:24:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13910243 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 109C3E77180 for ; Mon, 16 Dec 2024 19:24:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8BBB6B00BD; Mon, 16 Dec 2024 14:24:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A3A316B00BE; Mon, 16 Dec 2024 14:24:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B5C56B00BF; Mon, 16 Dec 2024 14:24:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6CA286B00BD for ; Mon, 16 Dec 2024 14:24:45 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2C8CB1A0A0B for ; Mon, 16 Dec 2024 19:24:45 +0000 (UTC) X-FDA: 82901798448.22.0835059 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 14241A000D for ; Mon, 16 Dec 2024 19:24:21 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Nunpo0r6; spf=pass (imf25.hostedemail.com: domain of 3en5gZwYKCDsprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3en5gZwYKCDsprobkYdlldib.Zljifkru-jjhsXZh.lod@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=1734377060; 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=PJyEpzu4DU8rJRmq3ow3hqlNy2LMWwwp3HWnjTLdNPo=; b=AuWxEtdhXZfZoe/TNjDZTWsE9DdUP+9Q+/zK+I5If1IWjCCUq5uj1N8wMMo4oHj45pS/kc rMvfZLD7oHmEr+dyLDknA8OewV8IfalFaei2lWS48Qn2ykoM3lue5ZXODiWclwaMSvKZZx Ln0Rrqu1LJVy+o3db3jd0ruMPzwyxsA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Nunpo0r6; spf=pass (imf25.hostedemail.com: domain of 3en5gZwYKCDsprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3en5gZwYKCDsprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734377060; a=rsa-sha256; cv=none; b=HZiTDiDc2jiRjnaFK5D/AhQzDo8KzSoK+uhbqS0pxxiaHNqifQOrVueuep2Kw4CbjfELRZ PELX0vvGg0t3Nj3Usqg6wRs+/6ZtWtKAPdDdJZlt4nY1K+4CcIELORBfKnL7UtpXGOXgNx JKT/UhetwX3K8kvOA+u8vCD/K6gg3Hc= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee5616e986so6677821a91.2 for ; Mon, 16 Dec 2024 11:24:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734377082; x=1734981882; 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=PJyEpzu4DU8rJRmq3ow3hqlNy2LMWwwp3HWnjTLdNPo=; b=Nunpo0r6TyN3CGynG5ca3+GlNXVf0+HDL/rVWOt6dV6KCwb/hPpnJ5k7LcW9Iv8En6 HC//1NqqT5XmbGqxdkeha25NLwvw6zU+0LF3Sc0hCvcILe5KyPQhnhVeFnlrJYTerzzk 13zzu4ykrvKJrNBs56joIo8kn53yRzjFou3gafdxPSHRzfcZIzyR2sN48KcIhC2cd7EI cxEjpkWDlzJOUZtCShtB/NyMx+5zwwDez3CWq+2yl303iuZG6GKeQycFkdNIKPyRwF57 DheBV3o+6kCrWugA9ViqdeImtI8Tgu16JIpnbCbSra2mbppnPdIx7Q0blKhWQ9gn8RdO MgNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734377082; x=1734981882; 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=PJyEpzu4DU8rJRmq3ow3hqlNy2LMWwwp3HWnjTLdNPo=; b=iGulR6SwKe4llmD6ONcbXz7qH0ZYHSLMjlmWlIyzSENmSVHFbofc4MsNMH3iS1JYEv 8IPFBVSV7WXsBbvzXg9qw6MMsWCfMJesPpWnF9ACVtRVp1KyOohP/wEPo2hhtqPb8pwe iKsV01szQqKYO+eae/FDMamZDGWv6WjZQbV4lBl/LzfMe+eCvzvz1+vGIzafknfWEo3H Cyt7xdNb0Dgok3SyqpAUzlexCaU2WIO5gByi/ry3KPP2daK2c05cgz1NbJmjrLKx/UBJ 5V6EM0ftk3QkYQH6z5yxtQ+w3GkocX1nRl9aaXEOUQ5cTOXOYaxG90HVwqaUgNJsjxIJ oWXA== X-Forwarded-Encrypted: i=1; AJvYcCUSK9ZDn9fRzJ/r9UOK3FyCyL6xjVi9e6KvgOPW4VkSRjlHA5T8aG51dQl8ZlPFzlEGuwzGI3RL0Q==@kvack.org X-Gm-Message-State: AOJu0YwwdoHYbsEWVWWNsQXlChW4B1UY2tZZmHFhT8sUq2qi+CZdYypR ohIZ0z+DcUwZsV48uwcTRcaOuZBjJY6A2XjykPA/KvPU6bRZLe+REIiIKA7OS0epr57vOovPcNs AWw== X-Google-Smtp-Source: AGHT+IGSC/WDrL2DnP38qBt3Yi0DRZx0zLMWebStlpO2MkkwMeYlYALEb8oRG5sXg1v7sD7oiSjMQ3XiMF4= X-Received: from pjboi16.prod.google.com ([2002:a17:90b:3a10:b0:2e2:9021:cf53]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c4a:b0:2ee:7233:4e8c with SMTP id 98e67ed59e1d1-2f28fb522d7mr20621018a91.8.1734377082197; Mon, 16 Dec 2024 11:24:42 -0800 (PST) Date: Mon, 16 Dec 2024 11:24:12 -0800 In-Reply-To: <20241216192419.2970941-1-surenb@google.com> Mime-Version: 1.0 References: <20241216192419.2970941-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241216192419.2970941-10-surenb@google.com> Subject: [PATCH v6 09/16] 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, 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, 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-Queue-Id: 14241A000D X-Rspamd-Server: rspam12 X-Stat-Signature: r4j6z6bshmeg5s7znkumcukmwx7gnbbx X-Rspam-User: X-HE-Tag: 1734377061-85729 X-HE-Meta: U2FsdGVkX18Yr39uhsNhIbS+akYB2502G7eGiKS+dQiJ7kaiXD7CZ4kaPJtNJecTiSBxF6/CC07Tnw0G9+b3ytauSvgEoHBDDsULLd5KbeM0ntwzeffYf6q32y32mbPb2cIuOOnfoEdT4ncx+gdw+fJLmDa2M7RcEIDud1Fpsl2KgyJJkaHaJJZbqZ6j7pbRidrBjeZ578iT4pMP2Wi0dzuPxynSg4fDkz8FM5a91av5UjiiL6c2m9kvOU4uMAPmuWuc5TnsVlSfGSisis4mWRAbiR4TG/5fGv6kf0QAK9f8zjmT7WI7zWGy8Gb463c61OIsKWpaiIQkKosU8izFTStNNR8+NjgalVkw6vOn5m90K/T8vD/sSU+eZzqTLO8vFHQn2ja3uyBIXdG4o4ss9LjLx67+9KYWRyRk2+RwAj9a2W50LbhT979Ylt+JaIW/988n5cfyUnRug2fGBuT8hZc4qJa/gNx2QW334aOkDZfZKzAJ4q9VL409Tw1AyIWJ4si5Af9D0PCjEErXMO5UgP9zzlBJVL5kN7+DBd9db6KMxb8sgvADp8Smge2QhPx3cgUUzGa1LBGJ1rPxZIg3k66a23Pn4b2/d3OWUtlhXpajsCSsw+K+VXdCzTh2VPAaK/sNBENqhU98LQw4MtXpuvJ0mZUPLzX9tEhx+0zu/fZSGIaidsh+m4fynh/ctkX+lny8Tz1MuYBc3EshxZt19UEjhpnQUH+lFYPsoHSN74hfn0feca/SuAMWusrJy5BdUsLdjILpNWFsvNAp0XJ95FmogofuKXW3p+T4U7kPLG+iyAqPGSVV1X2UjOuQd4rUaIBwyC55j9bPzP9fZ+uVwgDtkKCd7EWjWNax1qqZj1VzQbPu+/70g2m/Us0fGeWirCi7FtH8eiBdo99Gy45bqIjTm3bp7uJurJL8m7hc/QGnj0Cz9N5zT3UdoIc99Hnw5N0a/6/2Ttx8gM+8SPK lQNGWVbU XUgJ7Z6rDqCGXJdTvDR/b7Wc1j2RxwPkB8ORaJDKaXZrwUQGWDpAjwrIREW9oIE5t3zR8WGAIHlNeiTZSrU00i78BSVJEuIvsuZfrjZAbTqtX0IyyO3+ZamRGG90JIVsQcclziABfN1+h9muhxyJGkb5p+OxN3Dnn9wtzvun7BRo1IHIKnTZWe+DAYcVNaMV4Rt5N3umANQazqvgDLIh0XC2YOgKws9qrm+tRiPLANtQRrmecgF2HNp7qLUKeGVUp7jRK2UZlAca9PeQCSRNKnrr+0/aavDU9xQnBf9mgCJgl0ejzDOaWC4DlorKGhaR2lLq+JAtXzRgpqMwacnMHBtVLzUUzC92yR95GnKD2tElRzdHbFbxs79/xN0IYk6KIoRfWnjeypOpskP5CcciarKvuyNQw8v1VmVPI8j10tiE8MpqK8zHY6xbuIPkd9xFe5TULAVeI5y0+pXgVJEqqr3kHFzmzQyIIv9J4YYMCgXKrzR5sqpJvVbPGEINAbfg9XQ5CbwzyN7vLtLKafQhF77wQD02g5in7GVkTcacQAqn/qhczuzWfWgMJnLV2rkbcckii2FbonQTeDZGYNDtCQvdZFQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.225839, 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. Setting the limit to 0 indicates no limit. Signed-off-by: Suren Baghdasaryan --- include/linux/refcount.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 35f039ecb272..e51a49179307 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -137,13 +137,19 @@ 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 (limit && old + i > limit) { + if (oldp) + *oldp = old; + return false; + } } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i)); if (oldp) @@ -155,6 +161,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, 0); +} + /** * refcount_add_not_zero - add a value to a refcount unless it is 0 * @i: the value to add to the refcount @@ -213,6 +225,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);