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 A29F6E7719A for ; Sat, 11 Jan 2025 20:24:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 131066B0088; Sat, 11 Jan 2025 15:24:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DFC26B0089; Sat, 11 Jan 2025 15:24:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE9E86B008C; Sat, 11 Jan 2025 15:24:26 -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 D16B46B0088 for ; Sat, 11 Jan 2025 15:24:26 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 51BDA445B5 for ; Sat, 11 Jan 2025 20:24:26 +0000 (UTC) X-FDA: 82996298532.12.289FCD3 Received: from mail-il1-f181.google.com (mail-il1-f181.google.com [209.85.166.181]) by imf20.hostedemail.com (Postfix) with ESMTP id 6F5F31C0007 for ; Sat, 11 Jan 2025 20:24:24 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=WX13FcSq; spf=pass (imf20.hostedemail.com: domain of surenb@google.com designates 209.85.166.181 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=1736627064; 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=RPliOu4wmquCrykh16+WHroOJnf657+LJmEsYzkugJQ=; b=1HQJ5cWHrAVP6EOdVn95UyocZ/sRi9hbHgbZXTW7RbcoAhEQEMGIlCdlqHWLMC13ql5J/8 Ab/vgK9XSyusGio+1imawiDQS5wuQd062F/NP9oZOlF6jD4OaqOQr1qdhn1mz3eELGJoQL VW/vu8TuA7kcVpe2zbXx8NMfSQC0QDU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736627064; a=rsa-sha256; cv=none; b=Sh/M/DNSj26la3CztfcqftTD0h7OzhPZQz/4njNyfgk5N0u3MFGRxiAti5mRL9Tf4uV4dZ fWqAvVBrWihayqZWrjGd0KjIlxSdt7NyOVM4rvw1M6TUpbnLuxWcSeju1IY81f4mtZzc+b GmX/XdqZ2O0wRlr5M/gSiJzrX0AwEY4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=WX13FcSq; spf=pass (imf20.hostedemail.com: domain of surenb@google.com designates 209.85.166.181 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-il1-f181.google.com with SMTP id e9e14a558f8ab-3a814c54742so66395ab.1 for ; Sat, 11 Jan 2025 12:24:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736627063; x=1737231863; 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=RPliOu4wmquCrykh16+WHroOJnf657+LJmEsYzkugJQ=; b=WX13FcSqCeSMDlkJW9dD6Zx2U8WjQvXy1tYsdvaBXdpU9Kd0ZeHwmjyVQDzJzSqqyg yET2mLtRJmDrLRzyFW+eV5zdy6u8dkw/KukJ1UFS3jhl5sJiNMiYdt/aOy1GUhnOiusG Vh9i66lQHS5xFcUd2AHtoOnmjciuYIW8lr3gem2cfEXoq3hBEASgtppO9FgJ0EqtJLQw wf/uOK+yRua7PQu4eJqo8JDFS7uKcIu81lTQ4ORJNpbFzaXpiZB71C7P7bByHtMjgA/p IBty7dGKfzMYs/pCZae0c56gKj7XMqAOEl+h6sp7uC3dN9Ys9+Ou8QFumQ7ZVJBUgN2s OKFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736627063; x=1737231863; 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=RPliOu4wmquCrykh16+WHroOJnf657+LJmEsYzkugJQ=; b=ALNVfuK2KLQhPPE/j+SmvCtGy62ffbsW/6k/ElMpGwHDUtzYOH2v5gv3EcOwQa1tgG 3LJ+uzYL9+zBeIF7AVx3f0Tm8eKE29hARn6jogZ2zOL6ojVtEmPCxy4qXhEcbId3yl3g kIQUMAKWGBRwh3VwbCoeh5AT9O8P2Eu3uZCTMBVxYqOvwHQw2sUmPeLNwwpPIim538pQ gfSB5En1BEGaSyVOCJ6d/DuqMhon1Ezsts6ICU49R9KhxozNRFSKkukFqmH/4gupAkFJ IR4636sm19fsPGmx6SEK04hR7sw5UuyxkxnpaaC5+DjYE3ug9YjTxyzgBhVG9smrbu2G YmGA== X-Forwarded-Encrypted: i=1; AJvYcCUH0woN8eNxKEISHYB20MpC7psTqCCs9T6PG6sDQkxdIza+MsfbVeTg8beI9OXINFU4nirIFXxkIw==@kvack.org X-Gm-Message-State: AOJu0YzQuZ1Lg/yo8tLeV1LB0EBZ3Z4DtZGYivnMQ3oRXkAz7RF0e/xY UaQTbLP+/KlvcVg7sjmknY5wZ4zqncTmvXhA7nXE7eQwfn6l7MRVQp8E7kGgbU3l1QiCEUu7H45 LvjgscFB8W8tD2eN3kFHzTaheOxj0lwDboddemR8mxNP7lD2NPwMw X-Gm-Gg: ASbGncttY1lf3DnUe6P0mwVY8yLQlTiHs9p44BNpGngxifSkMOLJDEL73mEIlzZOy/Z X2NNq0g1inEa2LgjCSG4jg6PUvkT2XLvFMEcCqA== X-Google-Smtp-Source: AGHT+IHOqK4Arf1s1LLHb8+kpeakurQlQs+3i64GvQBcCOBGd2C9GaBwg4+K4xNxlGQm1zZY4Cwu3FvoRVEWtrcbtI4= X-Received: by 2002:ac8:58c1:0:b0:466:a11c:cad2 with SMTP id d75a77b69052e-46c89dad58bmr5934061cf.7.1736626603098; Sat, 11 Jan 2025 12:16:43 -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:16:32 -0800 X-Gm-Features: AbW1kvbdd4oYZGo07AKqf7hBdHNqAiydZPzBGmAoqnh4-jWcEUgpWTdVgKkN6k4 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: 6F5F31C0007 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: hdii3fu1xekns4ye9boouqoiny57x1hp X-HE-Tag: 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q44EMaixpK1+O/UY+hTCjh0sC2+lpkpBq6XGq1AZ+jLklpOTwRcnbZ5w1X3U6+imWQv+gorDwCLo1V2Y4MpqdTA3uhfvR2qzxbtmjTEHlqKpb2KI1Kw9UKbXbyCDEJ4rdMONoQT1EbOZtyLZMNttNYoNg+lCbsQ8mLLSBEZS/8YKSSNsQMDPZzZypi+pqp2jYJIcdt0Hey58PQkAxfo/Denf8vUFdxUsSqpHhDYiGa1am/gSaAdadRh5z+xblEWGVKJUleqnTqx3Y6+E1oQ/TX++LnrjZHyk3CCwrfJ6FYAMlbm2OIO/NUGMKxWSyLwkSvONyNNeKt/ERCCl5Hfvx3619Ys X-Bogosity: Ham, tests=bogofilter, spamicity=0.000023, 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 12:14=E2=80=AFPM Suren Baghdasaryan wrote: > > On Sat, Jan 11, 2025 at 3:24=E2=80=AFAM Mateusz Guzik = wrote: > > > > 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 y= ield false > > > * locked result to avoid performance overhead, in which case we fal= l back to > > > @@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_s= truct *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 n= eed > > > @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_are= a_struct *vma) > > > if (READ_ONCE(vma->vm_lock_seq) =3D=3D READ_ONCE(vma->vm_mm->mm= _lock_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()= will fail > > > + * because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. > > > + */ > > > + if (unlikely(!__refcount_inc_not_zero_limited(&vma->vm_refcnt, = &oldcnt, > > > + 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 s/vm_lock_seq =3D=3D mm_lock_seq/vm_lock_seq =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 lock= ed result. > > > * False unlocked result is impossible because we modify and ch= eck > > > - * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lo= ck_seq > > > + * vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_= lock_seq > > > * modification invalidates all existing locks. > > > * > > > * We must use ACQUIRE semantics for the mm_lock_seq so that if= we are > > > @@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area= _struct *vma) > > > * This pairs with RELEASE semantics in vma_end_write_all(). > > > */ > > > if (unlikely(vma->vm_lock_seq =3D=3D raw_read_seqcount(&vma->vm= _mm->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 somethi= ng? > > > > > 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(stru= ct 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 thi= s: > > 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!