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 9764EE7719A for ; Sat, 11 Jan 2025 20:15:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D6636B0085; Sat, 11 Jan 2025 15:15:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 089996B0092; Sat, 11 Jan 2025 15:15:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E68986B0093; Sat, 11 Jan 2025 15:15:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C80236B008C for ; Sat, 11 Jan 2025 15:15:02 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1FFC5A14BE for ; Sat, 11 Jan 2025 20:15:02 +0000 (UTC) X-FDA: 82996274844.19.544292A Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) by imf13.hostedemail.com (Postfix) with ESMTP id 3AB5920007 for ; Sat, 11 Jan 2025 20:15:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q4hf2xPs; spf=pass (imf13.hostedemail.com: domain of surenb@google.com designates 209.85.160.173 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=1736626500; 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=Jnz9ncr5hFfetUylxzngbgj19VfI0erfup149Nu0s5s=; b=Br2ElCVap5mpR40yeMNKux9T/rB0kLqQIzJKblphEoSTrCKiaka2vchqOTnjmq+shyP3zm D8qx5x4WcKyICo5pf+MstXGGSQoRQvPitEx/EP+3KW3tgb7dqMkDan7CzvSvTlxEvQ1ATV 5wX/vGC4d5Ua9ipBa1UBf+c/f8kW4wE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q4hf2xPs; spf=pass (imf13.hostedemail.com: domain of surenb@google.com designates 209.85.160.173 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=1736626500; a=rsa-sha256; cv=none; b=KgYORCiIbhmUHDYELaGpiTrZWYvG5u+6Uump8lvpHrIZ8jFxA7zuimhmXb4pvJ7luB1eJ3 WNGjNt2EZv1b4S7hmY/k/htW0VGxXEFhObEnPcgUDymEYeM2+sqflmzEd/iIPC2+ayV5m6 KfQ2O2dfogat31rkcflpVl01KZ+3QMg= Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-467abce2ef9so181781cf.0 for ; Sat, 11 Jan 2025 12:14:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736626499; x=1737231299; 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=Jnz9ncr5hFfetUylxzngbgj19VfI0erfup149Nu0s5s=; b=q4hf2xPs+edMXsibOFwTWoXoVHsoXy1jKfetDLmYUG2K0BG+Q4zfoLd0937lLHwX2u nRgZuAba31vMXNq/sbwH1lEVjHPe0oK5RTBSI59oYry9RRq9Xcrq2XtCSJB+RAdH9QRb 9LtLdcHfua8AYWt9lCylewsjtJNuVH2xrx1CixMrVoeDvY2aK5znLKs1aMb0Oud/gqke Yke5e4Y4PPZZG+1PuhWcOsA+xGl940dKh1j6WciDlxJe5ueo2Fz/tZoz5mKemQwocmIT YXDKRqN3BVjHnihmWGFUaYC8wAsa02hZfjNFlkivkg/ydXosrm57gCrOjKTn4eHsq9tM 9HPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736626499; x=1737231299; 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=Jnz9ncr5hFfetUylxzngbgj19VfI0erfup149Nu0s5s=; b=ciEZnPL6/VzO0YnyzYz08heDfHZ4SP4X8cko4CuGmGJrV92Q8hY/lD+8PeiiRDlAaP 3TD+hYdKc/iuKq4uOncudw3oRa0QBAAcFXeJeUOqhQ+klK23JyNRn2IOdVXSfPYICmBZ NA3jG/S8a4D/lfrIh6hqIOLk/XSvtaNSl/22eLqsqd5OFIczGLFXV9tP6+SZmTkdn6t1 DgmbC0S+6x2qVRO0+YoIcTOHTZqlbd2XH/I9cjBZYKBk80Ea/EJJmqF+TDX1QCKY9l3o mCZgFbubxl3PDZ8u5Fxq7pu+t3E1lkUgB4RYUnFYvUTYLAxt9Xp5bgyk7C+NiMpp8sCW qurQ== X-Forwarded-Encrypted: i=1; AJvYcCXGOXMjOQX5+IOT9j2HmanUes/M/nTm9MkQZnfPne2Xc1TAURXYFsZTyQEa4cEVhY1daHHRYD46HQ==@kvack.org X-Gm-Message-State: AOJu0Yx9qbEzKCzwxHNByyN9tBcHJUH6o3o5ENATt9V9FchKkvFEkx4T kNTCHZRnPYrVD/TnmLNp3EB36JqGAPL5a+Tfx96BP1ycnCx+yL8h9P7VYbfhXfBj6XLqhTlRf0q TX+k70i7LlD7YDCr8Clpgeb5Yim6klQ0eei3D X-Gm-Gg: ASbGnctM83zePfQOBjY9lza8yX0FSM8bajqGmVyQGJjFvvJOyM4/ApwfELNauaoSRpj f3beYxzRSDBsqRZEcx0aA34ogs3WJBaefm7TVwg== X-Google-Smtp-Source: AGHT+IHdYLrprK280yzWtK34/z0njKXtISm1/ZljVlOFh7P37PL0KLGH0XZKrteej0pcMPJj16AVNg0b7cr99QKotJ4= X-Received: by 2002:a05:622a:148d:b0:463:6fc7:e7cb with SMTP id d75a77b69052e-46c89daa62fmr6117981cf.11.1736626498762; Sat, 11 Jan 2025 12:14:58 -0800 (PST) MIME-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-12-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Sat, 11 Jan 2025 12:14:47 -0800 X-Gm-Features: AbW1kvamZVh2BNeMDT1ok9i_wekAhELF8iiYuS77TpcEBLiR04KbLfJcROKThTM Message-ID: Subject: Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count To: Mateusz Guzik Cc: akpm@linux-foundation.org, peterz@infradead.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-Queue-Id: 3AB5920007 X-Stat-Signature: 98nt4kbbxrhdxywzybc7q6phm5fhxwwy X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736626500-464655 X-HE-Meta: U2FsdGVkX18e1wG+CbN5VXJHYxw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aWavMEGDziMjHGTscWetIRaxtPGJtWccUpXriRn7VGKPglai6kxQi0jEZu3fCdtDCRYZKDAVCwj2Ugg/ZqIClSVZKFRRSvpJBDYjuGVq7ORumFj2dQ+Jsu5e4CYd5af8O8Gs/2pTGf8CUZ+v+vRlLm0+lJhCvoqCKVDlIVwXZBraqq77X6s51ZFSXgk8B/AppuGP+Loq7bFEWxS24e1HKMrHiZMZEqnLTaFFF1SA0VpfoxOzVJmsMEPIwtHKPDsoCNtGu+DQbpBbZkNDs= 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: On Sat, Jan 11, 2025 at 3:24=E2=80=AFAM Mateusz Guzik w= rote: > > On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote: > > So there were quite a few iterations of the patch and I have not been > reading majority of the feedback, so it may be I missed something, > apologies upfront. :) > > > /* > > * Try to read-lock a vma. The function is allowed to occasionally yie= ld false > > * locked result to avoid performance overhead, in which case we fall = back to > > @@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_str= uct *vma) > > */ > > static inline bool vma_start_read(struct vm_area_struct *vma) > > { > > + int oldcnt; > > + > > /* > > * Check before locking. A race might cause false locked result. > > * We can use READ_ONCE() for the mm_lock_seq here, and don't nee= d > > @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_= struct *vma) > > if (READ_ONCE(vma->vm_lock_seq) =3D=3D READ_ONCE(vma->vm_mm->mm_l= ock_seq.sequence)) > > return false; > > > > - if (unlikely(down_read_trylock(&vma->vm_lock.lock) =3D=3D 0)) > > + /* > > + * If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() w= ill fail > > + * because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. > > + */ > > + if (unlikely(!__refcount_inc_not_zero_limited(&vma->vm_refcnt, &o= ldcnt, > > + VMA_REF_LIMIT))) > > return false; > > > > 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. > > > + rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_); > > /* > > - * Overflow might produce false locked result. > > + * Overflow of vm_lock_seq/mm_lock_seq might produce false locked= result. > > * False unlocked result is impossible because we modify and chec= k > > - * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock= _seq > > + * vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lo= ck_seq > > * modification invalidates all existing locks. > > * > > * We must use ACQUIRE semantics for the mm_lock_seq so that if w= e are > > @@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_s= truct *vma) > > * This pairs with RELEASE semantics in vma_end_write_all(). > > */ > > if (unlikely(vma->vm_lock_seq =3D=3D raw_read_seqcount(&vma->vm_m= m->mm_lock_seq))) { > > The previous modification of this spot to raw_read_seqcount loses the > acquire fence, making the above comment not line up with the code. Is it? From reading the seqcount code (https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#= L211): raw_read_seqcount() seqprop_sequence() __seqprop(s, sequence) __seqprop_sequence() smp_load_acquire() smp_load_acquire() still provides the acquire fence. Am I missing something= ? > > I don't know if the stock code (with down_read_trylock()) is correct as > is -- looks fine for cursory reading fwiw. However, if it indeed works, > the acquire fence stemming from the lock routine is a mandatory part of > it afaics. > > I think the best way forward is to add a new refcount routine which > ships with an acquire fence. I plan on replacing refcount_t usage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure the down_read_trylock() replacement will provide an acquire fence. > > Otherwise I would suggest: > 1. a comment above __refcount_inc_not_zero_limited saying there is an > acq fence issued later > 2. smp_rmb() slapped between that and seq accesses > > If the now removed fence is somehow not needed, I think a comment > explaining it is necessary. > > > @@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct= vm_area_struct *vma) > > > > static inline void vma_assert_locked(struct vm_area_struct *vma) > > { > > - if (!rwsem_is_locked(&vma->vm_lock.lock)) > > + if (refcount_read(&vma->vm_refcnt) <=3D 1) > > vma_assert_write_locked(vma); > > } > > > > This now forces the compiler to emit a load from vm_refcnt even if > vma_assert_write_locked expands to nothing. iow this wants to hide > behind the same stuff as vma_assert_write_locked. True. I guess I'll have to avoid using vma_assert_write_locked() like this: static inline void vma_assert_locked(struct vm_area_struct *vma) { unsigned int mm_lock_seq; VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <=3D 1 && !__is_vma_write_locked(vma, &mm_lock_seq), vma); } Will make the change. Thanks for the feedback!