From patchwork Thu Jan 9 02:30:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13931817 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 3914CE77188 for ; Thu, 9 Jan 2025 02:30:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92EBB6B00A3; Wed, 8 Jan 2025 21:30:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B68E6B00A4; Wed, 8 Jan 2025 21:30:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E50E6B00A5; Wed, 8 Jan 2025 21:30:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 498006B00A3 for ; Wed, 8 Jan 2025 21:30:54 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 021A8140B57 for ; Thu, 9 Jan 2025 02:30:53 +0000 (UTC) X-FDA: 82986335628.05.AEE5EC4 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf04.hostedemail.com (Postfix) with ESMTP id B227C40014 for ; Thu, 9 Jan 2025 02:30:51 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cYkuleWy; spf=pass (imf04.hostedemail.com: domain of 32jR_ZwYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=32jR_ZwYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736389851; a=rsa-sha256; cv=none; b=NPTLpnulXTRmSXKCmfHAnHzw7LnkvKFzphAV6f/Ga2OvUU+VfX7Eg9qg9HEYt4VEEX1O+8 X1D1R9nKxcpeTa79Yj3f9cdhR+m77RmwdF98+pV5JT3As4+HIO3SvsXWYyK3AWN7A1Y+Bp 1KnlxoZOVakM5klQICdVraBdAdMgyJ0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cYkuleWy; spf=pass (imf04.hostedemail.com: domain of 32jR_ZwYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=32jR_ZwYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@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=1736389851; 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=YDQWvgnM3ecpsVy1JQGtTknKWtLSYOOH2mwLaEelsWk=; b=zxxoxdnxT7qehQCPDvJuWbgelqtOkI45oxMzVO9qv26e3mMBNzxI+AirRHbou+XUr+xJ/k ZrV0jIMR4lYi2JT8urljY5fWGu015QTBnNLMVCwEzyZekLFwNV7t/DfTp8OaCeer5toQ1G 4MPRiWVS3Kpr3UW9q+oVWCCm688N6JA= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166a1a5cc4so6278205ad.3 for ; Wed, 08 Jan 2025 18:30:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389850; x=1736994650; 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=YDQWvgnM3ecpsVy1JQGtTknKWtLSYOOH2mwLaEelsWk=; b=cYkuleWygveWAfS7aJpef1GpeJR+JP+gFiQEwHvZ/S1AU2PVlacimLTDBZjkp0SXdq 2wJPocdzMku3GGS2uwAPBWzRqyHui0t6TnjGBf0oP7RahpM2lcKQPj0ddvp+xuPeeQ9l 5eHdCn+N7H39YfNtQlE09L98Vy83yhcpIMNbkJs9xWaTcHafTYQXGTYuSfDQewqh9YZ2 dYnYLjqX0xQ2jODv1fq387qiEKmFXQub+ReKKWbOiMoY+6487kAz5Rfc4pv7N0ws83dd 77DuLMpn0OkSkIbfYUJvMOXE1KChRIttIG4Icp1mUudQD1c4G2SbN/g8krGot+UyGXQO AFRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736389850; x=1736994650; 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=YDQWvgnM3ecpsVy1JQGtTknKWtLSYOOH2mwLaEelsWk=; b=ul5E4OBXct1FlzQuz/kbJ8PG4F1dSbVetiSBZHAPinUARTP5qViskFV4EcuZIPkvkJ HUkCpP8QyBiJeqS2McnGYY0KQD44t7hypEZSVPxSv3WO7oUXsoLPIR4GRdGN75ieg7Be 9GerWKy/3omgJDxv7pbzzhl5vxFBfeVXKgVm4MvoZKZxR++ejK2dnMIubvilRHrzp/Rd PC948qFJES8HLa9QhxpOcvgvhc2EgXGG5SJE24DkoPUBEcx6uem3WsYErRjr7vKuySDR 0eJ+AB8kKHlz6OHj+K2TvDU9CoGlqTrNS6B9OALK5deE7JegGlOibFC9jYjHkXSCuwv9 69yA== X-Forwarded-Encrypted: i=1; AJvYcCXdEAse17aKoIUIxPbruyTjOZtpKuhUsmwtscHZSXsL8jc1PPyvbgpBBiwa+XcI3GWdL+N6UlHiIQ==@kvack.org X-Gm-Message-State: AOJu0YxloJk3FmZXtZqgxDOWHqR3tJHuAeYyKQ2KvBupYKvJuchRF8Xk Q0Io6dQEv7CFMThSgTUxI4beKHhyVcL4YPq6mE5oZ35ELs4i0qRV8jUQiI/ZdeY6P0WBT/LNAJX dZw== X-Google-Smtp-Source: AGHT+IGU1GveoKiXtMIwX3Dsvx/aQojyG0Nha/qg6tCj4vgO60tNC7tTkojOSt/gTC0vj2nSfdWqvyuR7Jc= X-Received: from pfbea18.prod.google.com ([2002:a05:6a00:4c12:b0:725:dec7:dd47]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:93a1:b0:1e1:9f24:2e4c with SMTP id adf61e73a8af0-1e88cfa6b60mr8471434637.16.1736389850547; Wed, 08 Jan 2025 18:30:50 -0800 (PST) Date: Wed, 8 Jan 2025 18:30:19 -0800 In-Reply-To: <20250109023025.2242447-1-surenb@google.com> Mime-Version: 1.0 References: <20250109023025.2242447-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109023025.2242447-11-surenb@google.com> Subject: [PATCH v8 10/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, 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-Queue-Id: B227C40014 X-Stat-Signature: xcamxfargbx1m37137pm76qdu66rwkem X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1736389851-783042 X-HE-Meta: U2FsdGVkX1+LOvFcbcygg8wIBRM93kYOmLf6Wp8cMtwHSSaFZY8wjvfCQcBnkF6lj+AqnDukVjeLS15ta3DWWaM/watvZz4ESS+JjLszempcwFBLR5uvCEYNnvAZZahLUEx+m1rX01z9/5dJz13CHO5mEc9xf7U9a04Q1eyXyHSwz1Ne2E+KVeiUA/4rkOB7p+nmso4DgfvFUW8fdxZMvuQYam2gdvJV8nLAr5nCqQJwfSzgRdq7kzekBxo5+rGtMN9obHK0jXwpWIRBRPbbDP6ZWkaPKlI34UAVr557Law4wIPLkk3GzX7lxFWwoEgXBf3PXCtqHjSoV9w6qxkJA4I9H6VjbOzYFuc/8PmWYBKw5L7yodrOIrypzR8eN4GtrpHwOQ1LiWESdZpuBmPQRu7hKjAWbbACVunxmdbqs8U1XypEHjfN5F4md+cq0e470vcQ0ZhbPwjWu9rZ1Y+Y/Kb1iVQnR6Zo2VqlVxrozBpSuzOgBubSFXPrEy6VoU72QmwpIMaP3U4r1Bc/yXcCqWdZGd8qBVf6KoEXZBethLht8f2zhzpA7smFWu64gsPh8qnFEEawK/7XCjl//aoDWoEuRBijDUgEHDhNOgwU6d84/oL2yKRvpmkY4Q9eTnjABFR2AuXwzA4zKn93pvKID4OG3RUjtTVezz4BD1sbJ1T4hPebfgg4SrYx1iAbWacCXR/H75oo2qq71Cn+jN4Gl2hZ0fpwF7niL9BrfHRTU6vZB9Zss3ZVnhxJjE/RAFpIkz5m5pPmtVVSStKBsbY5Gwby97++kfLmegeQqUTAwRjYdKJnRE3rfdVkXMyjbcgoP55zdwFS2IOWJLJWOD+pniDXxuQUo8IsTvIlrEfU2yFwuj2CEIN6vBHPdzm53GYS9vnlm6Xkczqn7GixKYpSPzll4TQ8E8G55EGahDv4urjkjJoroAiEUAzQDRy0LmZBIUNo1JMMxa8iuhNr11P k56w036p ElMjKiELugBiI1TR5yd740wHy1dQvnZUv0V+NGHINCrcj/0dJX1Sj1Sn8AX1zz7eu9LA9cWpgQD9LiA1xZOT14ooHPrYLoVae36L+6fe6mCShZWF9PRO8v5u6a6lhQA6/9sAr3+h8qRQc9pIFM6mlmDj+YUYW75NunKYQaywvwNZLFa9SnIi6i9b2/gvkqprn9lH5x6AVAKOhSFaQCFRptdy6gdguA33JrFoOEEsRx3hHxNAuocxAScxssltw5RfeA5C+kxEPQnkctLFTTMy2vY5Bhmz4MCyxeMlp1apkVd/7gPs68E4B29H5yFh+Rhahjsb94jkAn13KuOK/2Co34FTZJE9tl+biOoYB4OqF7CsjXRwU72Ne2omC6a0fvAluLe5nxDqJd04mV0d5rArazqg0nIZzrvwinidaZPkoY/U1yvEz5pepLPDlP8YNAVVS516T1xTMZFEdby28XscJVVX8budJmkBBgKsrnNosXRJVmPmgjlbptugb29bdM9mWo59sf/qdeLDWIV9M2C2ubMXFHgS9SGj/mc2AlTprwsOEEoIjtRHRPcXfsOLvscX6NSS1qn0Q8CKBodOuDSVB1fbmz1TB9n5xHFUiOfNWT1cAzTM6smZSpKyqmg9cLrQqxdPi7nPipAJBje2cX+l429qHM0fsc9G2LTJ4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 Acked-by: Vlastimil Babka --- 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..4934247848cf 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 (i > limit - old) { + 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, 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 +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);