From mboxrd@z Thu Jan 1 00:00:00 1970 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 B5AA3C02180 for ; Wed, 15 Jan 2025 09:40:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5C5A6B007B; Wed, 15 Jan 2025 04:40:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DE5956B0082; Wed, 15 Jan 2025 04:40:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C68C66B0083; Wed, 15 Jan 2025 04:40:03 -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 A3BFF6B007B for ; Wed, 15 Jan 2025 04:40:03 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 514AC161043 for ; Wed, 15 Jan 2025 09:40:03 +0000 (UTC) X-FDA: 83009189886.16.84EDFEE Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf12.hostedemail.com (Postfix) with ESMTP id 50B9640007 for ; Wed, 15 Jan 2025 09:40:01 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=mwm9ATnA; dmarc=none; spf=none (imf12.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736934001; a=rsa-sha256; cv=none; b=c35VyuJbXEZoHLzYGMZv/bQOpr32n216j5x2Rp0RjUwMDb05jjiZZ3Y3FHZ7zj+QB2x9s3 +AfknuUzoJWVp31Lx9JKWRhN4DriqotWGD4JSBWH670sdIJ1oq7PUqFABxOkoYVRKHjjLd ojeWhPH0CI7TN9P5P3V7n6M6NayFW6U= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=mwm9ATnA; dmarc=none; spf=none (imf12.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736934001; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rQ0VMQZF4Hs6sxXl+crmmtTDrAtGUOJVVA46UF4hrDc=; b=xfxLVuY9WEuswo/lh4A2wfw7tuJsGBD6C3pA3ardXw9vj6dvY4SWK3nsr9+CzECW+/kTMR fVOiaKKKbPj4XDl7ZdORK+72hz/VB6vERkU5qCJEdaKGPp0dYZN7oCxYMKkaTT1207tJqZ 3LSagkzOmRV+Vn1BHjnJKC/Eps88BFA= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=rQ0VMQZF4Hs6sxXl+crmmtTDrAtGUOJVVA46UF4hrDc=; b=mwm9ATnA4AgfeSSoii3VUjCw3r ce5k7UxI/mrpokzcAMGSP+o10HPhcnWXObwBaw2zjB5DqhxVCq/+VQQf5H4f+H8UwH5fb6TqluA1q Qlm8CJCgC6J3ejlNjuIcBTZkc0NZ6WEWKVnhLFF08m/LmmaUM9fEQOVXmpAu5hv1betntoIup3g+r xhHH7uDs7Oh7f0XOcymouAZNEDVJR0Iray0VSQUJ7OL5nnCTxeuhkUOm2QQNxsD1barn3T/U8giDd LMD2298uTafQKYXI5uHER6jkVMq3+CRHbHFc1VSNuPd7tzZgETZ/seYv6Nj0WHz3ox0VCLraUlcD+ Z/U04CYQ==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tXzsS-0000000ApT5-2XtI; Wed, 15 Jan 2025 09:39:56 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id BF23630057A; Wed, 15 Jan 2025 10:39:55 +0100 (CET) Date: Wed, 15 Jan 2025 10:39:55 +0100 From: Peter Zijlstra To: Suren Baghdasaryan Cc: Hillf Danton , akpm@linux-foundation.org, willy@infradead.org, hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited Message-ID: <20250115093955.GW5388@noisy.programming.kicks-ass.net> References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-11-surenb@google.com> <20250111063152.1638-1-hdanton@sina.com> <20250111121320.1656-1-hdanton@sina.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Stat-Signature: kg9u9eqbxmmuuupktaqsfjcfo7d33dqx X-Rspam-User: X-Rspamd-Queue-Id: 50B9640007 X-Rspamd-Server: rspam08 X-HE-Tag: 1736934001-982446 X-HE-Meta: U2FsdGVkX19GuFEBcgVomRIjmmMTWBufLzV3ay3GjTckwBgLgv6ttoFSECevauRury5mf3slTse0loJw9p7BX4siB8U7VTOPdAOVgzTVOiWDmvfN2nvFWu05nLT/+eiK5I9wVKe4qQBGGzxIMm60KTOvaRXSCb4ROCpCWfRTq6oZZKOW2T1FSv85+X0Xhk9kVHZw+iJc6Hp3D+1hdDvDUbReBOzdZWk1Ax9G3ZX0jMXvKZiQCOc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ham, tests=bogofilter, spamicity=0.000026, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sat, Jan 11, 2025 at 09:11:52AM -0800, Suren Baghdasaryan wrote: > On Sat, Jan 11, 2025 at 4:13 AM Hillf Danton wrote: > > > > On Sat, 11 Jan 2025 01:59:41 -0800 Suren Baghdasaryan > > > On Fri, Jan 10, 2025 at 10:32 PM Hillf Danton wrote: > > > > On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan > > > > > -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)); > > > > > > > > The acquire version should be used, see atomic_long_try_cmpxchg_acquire() > > > > in kernel/locking/rwsem.c. > > > > > > This is how __refcount_add_not_zero() is already implemented and I'm > > > only adding support for a limit. If you think it's implemented wrong > > > then IMHO it should be fixed separately. > > > > > Two different things - refcount has nothing to do with locking at the > > first place, while what you are adding to the mm directory is something > > that replaces rwsem, so from the locking POV you have to mark the > > boundaries of the locking section. > > I see your point. I think it's a strong argument to use atomic > directly instead of refcount for this locking. I'll try that and see > how it looks. Thanks for the feedback! Sigh; don't let hillf confuse you. *IF* you need an acquire it will be in the part where you wait for readers to go away. But even there, think about what you're serializing against. Readers don't typically modify things. And modifications are fully serialized by mmap_sem^H^H^Hlock.