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 6BAFAC02180 for ; Wed, 15 Jan 2025 15:00:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85C9C6B007B; Wed, 15 Jan 2025 10:00:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80C116B0082; Wed, 15 Jan 2025 10:00:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4286B0083; Wed, 15 Jan 2025 10:00:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4B0D36B007B for ; Wed, 15 Jan 2025 10:00:57 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F2AF7121329 for ; Wed, 15 Jan 2025 15:00:56 +0000 (UTC) X-FDA: 83009998512.24.D18B7F8 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by imf22.hostedemail.com (Postfix) with ESMTP id 51D74C0027 for ; Wed, 15 Jan 2025 15:00:50 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Thvi09Wo; spf=pass (imf22.hostedemail.com: domain of surenb@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=surenb@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=1736953251; 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=66Q+ECsUQYkh2GFdOA9yukRjVo+GtbrrtQTFGHzSN64=; b=4S/59dBPwy53yqcTrTyBgjijN0yUg4xYNFdxTBp3lW1esDnDRmtw/PtCurBk4t0KyTlz+D g3ppE39RB97jnBKdcj/6CG/xJePcFQWtsd/g1BvCJiM+40htOMQYs1mdjGwyNK8S+SBeHD flgyyIz6QZ464TWZQI07006Z769Lwec= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Thvi09Wo; spf=pass (imf22.hostedemail.com: domain of surenb@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736953251; a=rsa-sha256; cv=none; b=YgMI67V+kgwVJXVvTSOtiCz6lQn9swJQWHu17j3aQZX1ZkoINV8cT+STDmMii9FDp+RI9M 4xMuuzZhZq7L1aVV1UvRKcwg5+1v+l98mCAA6uaPmCbfmjXFkI4lk/UqLCkSToDQpJ2y6a 8yod4+zMbdRDX37fwpiWk6g7s3wvQbc= Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-4678c9310afso516461cf.1 for ; Wed, 15 Jan 2025 07:00:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736953250; x=1737558050; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=66Q+ECsUQYkh2GFdOA9yukRjVo+GtbrrtQTFGHzSN64=; b=Thvi09Wo1G459vKuFi2hmd7dEbUkOd3ZBu66p1HFlnkXjWteECnlHYaxBlAfvyHRDh rStERRSNE2hdE4u842KTIZl2eqaSx0r1fT5Ez0kRSYyBxJLGf6tpss69QHABkVDFtIPw 22f27uYq3VctwIUT0iDpBE9SGqxaVDMwv5Z9LILSIwCZ8k2dbs64SVobJK9RXQ1YJwy6 rUcUR4XOg1g7WbH8B7ESlfKw2oBL8slD9SL+KgkDWYvYscMivDszZ63UJIJV2LoMj4mp ewDgH2ERIM0uvxTZeZisTAg2L0FRoE68ZvVPc5UtRWvAZZB0kY9X3oA376NPKudQT/my 1Hpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736953250; x=1737558050; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=66Q+ECsUQYkh2GFdOA9yukRjVo+GtbrrtQTFGHzSN64=; b=HJIj0dm+BaYfuk1KdRvMPaU9janN5mjJTwWEzbOA2i+zktIOHlo8hZCR3zM7H05qdg 1z7XclPFrqQYUU/2F5g2nNqA8WfgE8+78W2sOzPHQiEmVNxriUu8e5GjRupkqIK6d4Cj wTCQsjFJXC08P63cgpmYU+MwCvjQzM9AY1rpzNOGnHmJUoAijY8OSDY1VUwa9iIJeOrc YrrLmVyMm/DTcIWFMeZcykpXGqMPgjUtpgaFu+EPkuFjGp82/Vd520MkNe1sIphT3ins lfGDR85Y5zjsLRrVRnRbrLDkPyliLuKpQD7Juhmr54KMY7+P35MlRyJNIcOshAl0vZDy ghkg== X-Forwarded-Encrypted: i=1; AJvYcCXDuxnAcqPSNFUDOB6X3FHOiK9vZT+gzx2ZuAttg87wPH9+p0DUMFiREaKgF7J5/nbn0X+k/4Z6Rg==@kvack.org X-Gm-Message-State: AOJu0Ywr62jza9DUh8drC6TleGVWknXWV4rLXYg92V4Q3fqaQZuJ9kl/ oHhtogb6CnrtIjyeL1EDp/hu89priUqVOw/KuLGYNRLXfD+j07RgcXo0fZ2CONZnDFa0pkmMA2H YmRtogI5pjB+VcRgeSoaj5ofXl3zV85wemynq X-Gm-Gg: ASbGncsEAUt71fIWABqAqDDe+Sx83RYwmE8T65oZnDhYytpmGbL+RfrTJ+9MqKLJP6w 5SZr4+wBI/XL6FJp5j56MEpZKF9Dq7jzy94aMsw== X-Google-Smtp-Source: AGHT+IFzWOkPh/9q2N8pXZbBs3DAk6R5oO0Q3s7YwrFBOYeGwM0QBDakcObadsTeUjM0ajHT04ZgHDXe0wVJQXXcB5c= X-Received: by 2002:ac8:5708:0:b0:465:3d28:8c02 with SMTP id d75a77b69052e-46df57d575fmr4016431cf.26.1736953249300; Wed, 15 Jan 2025 07:00:49 -0800 (PST) MIME-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-12-surenb@google.com> <20250115104841.GX5388@noisy.programming.kicks-ass.net> <20250115111334.GE8385@noisy.programming.kicks-ass.net> In-Reply-To: <20250115111334.GE8385@noisy.programming.kicks-ass.net> From: Suren Baghdasaryan Date: Wed, 15 Jan 2025 07:00:37 -0800 X-Gm-Features: AbW1kvYVi7p6iuGwUc-ExzE8xKeba6NCoxPWkiolrhe8rGm0Lr5ftmP35q6f_sU Message-ID: Subject: Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count To: Peter Zijlstra Cc: Mateusz Guzik , akpm@linux-foundation.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, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam05 X-Stat-Signature: agbcf8smnsy7dk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cyaxTh+LQqNNxVGaP6DpQpTYW+FjhuSkGayRxxn1ePUaf8VS5Wbamf53gLisla1mDFoGJnHFn3QQYIaImPOg/ApQGJTx9qKos0+ffy0TtIbVbHuA+h83rbU+PGuH3XHcDGGlUCVi6dprQlWCQ+SXOqSpNnwwFZuPorlS1PjIfC+1Q6s4+qaziHFl0S/UJ9NfEOelb4QVvQd8i241zxbWDV+mwQxycUPfYbV5vsbXhoGh+gsmhK79qNE+B1iY0EQ4Tmt7aTKMwvEdf+aActoe+Y8WybiGkUX2KqIBMhwI+rmQi4XBAxUmzKAB/pEl0q2ZnmwMG2YIYoIUJ7vvZkrhYHI02xO0tBYp12A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000020, 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 Wed, Jan 15, 2025 at 3:13=E2=80=AFAM Peter Zijlstra wrote: > > On Wed, Jan 15, 2025 at 11:48:41AM +0100, Peter Zijlstra wrote: > > On Sat, Jan 11, 2025 at 12:14:47PM -0800, Suren Baghdasaryan wrote: > > > > > > Replacing down_read_trylock() with the new routine loses an acquire > > > > fence. That alone is not a problem, but see below. > > > > > > Hmm. I think this acquire fence is actually necessary. We don't want > > > the later vm_lock_seq check to be reordered and happen before we take > > > the refcount. Otherwise this might happen: > > > > > > reader writer > > > if (vm_lock_seq =3D=3D mm_lock_seq) // check got reordered > > > return false; > > > vm_refcnt +=3D VMA_LOCK_OFFSET > > > vm_lock_seq =3D=3D mm_lock_seq > > > vm_refcnt -=3D VMA_LOCK_OFFSET > > > if (!__refcount_inc_not_zero_limited()) > > > return false; > > > > > > Both reader's checks will pass and the reader would read-lock a vma > > > that was write-locked. > > > > Hmm, you're right. That acquire does matter here. > > Notably, it means refcount_t is entirely unsuitable for anything > SLAB_TYPESAFE_BY_RCU, since they all will need secondary validation > conditions after the refcount succeeds. Thanks for reviewing, Peter! Yes, I'm changing the code to use atomic_t instead of refcount_t and it comes out quite nicely I think. I had to add two small helper functions: vm_refcount_inc() - similar to refcount_add_not_zero() but with an acquired fence. vm_refcnt_sub() - similar to refcount_sub_and_test(). I could use atomic_sub_and_test() but that would add unnecessary acquire fence in the pagefault path, so I'm using refcount_sub_and_test() logic instead. For SLAB_TYPESAFE_BY_RCU I think we are ok with the __vma_enter_locked()/__vma_exit_locked() transition in the vma_mark_detached() before freeing the vma and would not need secondary validation. In __vma_enter_locked(), vm_refcount gets VMA_LOCK_OFFSET set, which prevents readers from taking the refcount. In __vma_exit_locked() vm_refcnt transitions to 0, so again that prevents readers from taking the refcount. IOW, the readers won't get to the secondary validation and will fail early on __refcount_inc_not_zero_limited(). I think this transition correctly serves the purpose of waiting for current temporary readers to exit and preventing new readers from read-locking and using the vma. > > And this is probably fine, but let me ponder this all a little more. Thanks for taking the time!