From nobody Wed Dec 17 12:11:12 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 219C71898E9 for ; Thu, 9 Jan 2025 02:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736389853; cv=none; b=Wuhy14oMUDz3MjgKWYxK5TuWaLBxVVQPNluEVQqx+54XM3vqg3piuG4KlCQ3ptM+kEim7fnHRkgTYjSGAtzF5+3WiNvQJsyRFkEgv6ZXS2OgXKWxK+GvV8d4/ItZ/h77C9CSikWhzAvePiZFDRCh0mVwovk0JHZBXk9l1bxOw5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736389853; c=relaxed/simple; bh=BuXyVw4ezCQ1JAf9BAOIg/Ukfu4Sp8IUeASwcXyb1yU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Rj40jrJu73QlY6cLvP7OJDHV/+lsOrubnNCFFiv9/JPZPOOHA8iS3SHQYSh5mLPcxMkbpluqG1gn2P7FRg83uu5E0ehEu+NRkg0t3AbZYvNeEo3cHGfUMmOJP0tP50Y/BAQHmXd1SFMcvgGklVBZqpdMqoYmWsmsDWvlfetwWgA= 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=I4xgzVAV; arc=none smtp.client-ip=209.85.214.202 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="I4xgzVAV" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2163dc0f5dbso6840305ad.2 for ; Wed, 08 Jan 2025 18:30:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389850; x=1736994650; 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=YDQWvgnM3ecpsVy1JQGtTknKWtLSYOOH2mwLaEelsWk=; b=I4xgzVAVKQGRc1ibuWjkN6nmUSDcKMo0dUxJ8V9d4uw5Xpi047A7kTNdjGC6NfM5+R U1dFJ93xuIx0eUwwAYL8S29NLTP4JIUgZs3cnE3/GEwjexNIaElNa2Vw0LAHDL2xKZkl Aywaz65GeV6bcKlFUkFsxYwAPnt1uPIyYbznfzX9AxJSc+wqVMVF9k7I7THoP6bRHzhj V+a/LXXQDl/M13Nphn/vQUrxtgrUiQXI2qRVRthUqKr2wsME5RnGKIyrnOMYIIN8e1Fi UakJq9Zu31qTkJdZ3rS2ok6ZWSyTy6jpEmqvB2j0hvhFzoxqOShNBbOhQ3nelm2pEzf3 ++Cg== 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=YNCgUjd9vRa/hiKaPbc13Yvd6CamYC3geu07samUQ54x2lKiY4G+8txDKW5mlWa52d j1ZLQzztV9vLqxJPG8VqmJO+z3pU6n4kpOKFSRv17HZnGeIV+ROnamQlaPsdcqmFYHUK JYpfJHYzhdbxtzN6kpzdbciNKJG5V8bfwqMD04bxwIgDAMEZebAI1ZGIvS4MMtUmbiF4 vsKzgf8H8c0XZnrqwS7bTBZfxdwfTyFB55etDRlrac1FXyfTZfIqEF+1HcpCBH9ouOD/ PHgnrWXDCY+CA6merL0KjPAuNWkga935UAUpOEXJhLyoTAA7zktyXWjq8rcR43UbLiqS e54A== X-Forwarded-Encrypted: i=1; AJvYcCWHg6mrznU+WIRi8zx4MmvdA1N23G7HVSuipl+s9RcPmg/HNsxRDvb2sjfs/OnWxdVQIsoKc8t+qsn/tPw=@vger.kernel.org X-Gm-Message-State: AOJu0YxT92Gz+1bA4zb8GK0n2x5eOPdB+b6rgBJ6MUaB5i8vHj01buNE gjtBARB/1m7Nix/sO9bydoCn4FYWHo1EXrstIzo+0mC5o4/uabltl6AbF9s6XLG6duKMwHdkU37 wrQ== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 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 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 (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 +161,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 +225,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