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 5356EE77188 for ; Mon, 13 Jan 2025 01:47:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B44C06B0083; Sun, 12 Jan 2025 20:47:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF5216B0085; Sun, 12 Jan 2025 20:47:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9957C6B0088; Sun, 12 Jan 2025 20:47:36 -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 7B0106B0083 for ; Sun, 12 Jan 2025 20:47:36 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3769FA1A48 for ; Mon, 13 Jan 2025 01:47:36 +0000 (UTC) X-FDA: 83000741712.13.ACC7FA4 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by imf02.hostedemail.com (Postfix) with ESMTP id 16AFC80002 for ; Mon, 13 Jan 2025 01:47:33 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nHJCzZg0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736732854; a=rsa-sha256; cv=none; b=pYiI65gKS0g12b4MSy0S59zlZ+Ai8xmJe3Wn8J9DPzuit072IxbjxyzXTUqKfotZk1H/TF fnLF3L7AsDeGO5kjcIAEh5/vNrrytvTiu6ZcWKJKDRQc8suYh8N8sdkMWvU7lyY+Q+8szB 63+Xq4KF2JSGY01t8zYeFdLI8BPvPM4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nHJCzZg0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736732854; h=from:from:sender:reply-to: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=2q3eg2wOXFrgqi2Nz726h6b3xnrqpDXRd+fF7QvymjY=; b=78EZ2OPGHjWGFww0Z/zlwckJqQl6fJ5kfBwlCoY4DHWOCPtSSnG5vXnJNSRE/gVlX9m9V5 ZeacQ99rv9uvpMzWWtk45FaAWncNPVM58QeOj/+9Z4T6kcpt2UXsc/dJ1ar7v6BAp49WDI Oi/ywnEAogiLrwxPIJHMNxVbfUM/YBY= Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-ab2bb0822a4so789580366b.3 for ; Sun, 12 Jan 2025 17:47:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736732852; x=1737337652; darn=kvack.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=2q3eg2wOXFrgqi2Nz726h6b3xnrqpDXRd+fF7QvymjY=; b=nHJCzZg0Mol/RUQmqfhGHK0Z/aQcYhcM74riX/VbRUW839RKJwgWn90UKmc611Zd9D vp9dBHaEJaknKLz4FTUVycqowskYOQPSOdNt2riz5mCQ0qGiq41JOuu5NwPXi5EdEylI G1YzMyosNgGMqEoF143Tvg1/HnxFdidbFIH3M6YOlYDr+ulJRSUvPvmZAcX2pL6Oy9YD PhesH3GyQzgwBAOu7DcOT2cjRQDxeh7K6WUujix0P4gzm3L/Y+O0BSO0RqIVNfemacdr aJD7B+a+sdRatT7keVl3hMq4ahXsJICoWRYwVqtvzqmWvUiT86qot3iCzTCJ/1u9L4Br PB3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736732852; x=1737337652; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2q3eg2wOXFrgqi2Nz726h6b3xnrqpDXRd+fF7QvymjY=; b=XCbt5wIal5ZIZj0rGpNo8q/NITAZxdrddi+Czn2GY9dLIDJwBaeew64b3GOYM7RDQx 3t1r3nPitx/SNp/VtxtwVtEBBiUh+5D7Wzq+8+62o2mmyutxX3istolX1s+Y2PKpj0WR CCdDQQAsz8mGDJtsA1HCmNwcs8vUHVxHtkNvv6rR96oC0LfU3yuXNcvgvri/kvY9ttH1 7T0P8JTEgCg78MACEDKmnYOHpoNJz4M2px4APk/UzqJhbYyf5eNGWQnrMaxQ83hdMUCK pD2Gk4dvO9VcKBEvejfGOpqWgvr1iU68jpwhfY6nbuFuErHY77r4/b+vxUBnvsMbGp03 B9Pw== X-Forwarded-Encrypted: i=1; AJvYcCW/y3n2KD/gddAgHWN7WHbN7fVVXGDYOwQaqtJeekqeFq4PtUbJ60G33BD4X5LNsBN4azzNVCbUDg==@kvack.org X-Gm-Message-State: AOJu0YyniEZ1TeVJExCZIshdmVHwgxN6AG07Lu1MzNoZy3S6Se+j+GOZ /hKy9y1W/u4gPM7S8oZPIVtddZybfaHTA4iINjrT59EQbOPrr4HF X-Gm-Gg: ASbGncurl7ttZ9FUfz7njTR5/8EfWaVd5sIUivS5DqdNvKUizHcpJuzcrdcm8dqi/EK 3URCW7Wa7N8hi00ccTcRZKPzu31LC2EdERKb8Zz+7qa79uyIyI/brFX2SY2k9DqRDJ4jCxadf1W ELxMqGXudh3hfmH9AZSpKcJEHj4auqtXLtpkVP2Lg6LvKbzh9bOF4m5g/fZGqp31ZNihEO4ozeQ oXTPOhKjKLm6XfxD4CfX7YdfUhK/XZFBxPNnO3bSfiOJ55HaWVoTLxG X-Google-Smtp-Source: AGHT+IF1M62OwdYusr98DprkJhSZCaxMgffu8BXUtDKfpuWMW8yfJ3xMwXwv94WNxtXFU0DMhC8IVA== X-Received: by 2002:a17:907:7ea2:b0:aa6:7737:1991 with SMTP id a640c23a62f3a-ab2ab16a902mr1742356066b.2.1736732852312; Sun, 12 Jan 2025 17:47:32 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c906004fsm435421266b.30.2025.01.12.17.47.29 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 12 Jan 2025 17:47:30 -0800 (PST) Date: Mon, 13 Jan 2025 01:47:29 +0000 From: Wei Yang To: Suren Baghdasaryan Cc: Mateusz Guzik , 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 Subject: Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count Message-ID: <20250113014729.ms5sdfnhynlamgrk@master> Reply-To: Wei Yang References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-12-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 16AFC80002 X-Stat-Signature: aouq4tyuuhzc3qpheh1h7jmgawxtmt8g X-Rspam-User: X-HE-Tag: 1736732853-321338 X-HE-Meta: U2FsdGVkX1+ctiK3Um5W/1CN51K8xCwvcY3OwvVCsAydh+rEWPYIrUJgV7WmU3d1EUKt7bQFIUpo4E9aDrc/Iw3EH8M6wM060+Loh+15LtrbVsvhhy/3/kBAWDR92uvD10vpfC11r6Gmhb8ttPzza9beMWsK/SEJeVq5Wz0Aj9OgXqe6XI2ijflOhRD6pSbFLt0GTJB+5HsKU3I40S/50ZMxnyFbCkW7IJexTdDkyoTUIczNo8RtKZS8q2pb8PqJN4n+HPtfZjCU0gTLzaCMGrdCdKDPWt+hHsyPprM/iMyZvBfscLH3s94SoYpxWh8keLq9iZFhMzKLhULsQfKhsxyJDhkxqlejx6K+OXDOMUzWvrWNZn+xIYcV4EuIgiECDotmekHSvyFajgthzhoreXgjFPyZgTJ1ya6cKm0HaEn2/Ttl/e5vybmmexZiMyJ5bDj/WCVvSucDBI8syk0uOV46mtruJH+6dK0RHZT+EitFYrsRGCOV0o+p287/qeD8ZOoz1iAg0Uwtv9QpARqM1Ivfkf1xtMrkRAHcg90lkuP7Z7fkDEiH8slohrhp+okr+INwt5bWPXAiGlBadjqtffUW0paA9+U+it/tnaE1CDWKRDihgzZDnrU2SI6LrM1PGJBFIwSkRpsyw14Y/wI0osAWtVPJJdpowrc3lZwaz8JnUudFEQAhkRBdmh3luClZ6iac8S3CLapYUSA4yg4jHWcwiwr4CGCrXAZG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, spamicity=0.000006, 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:47PM -0800, Suren Baghdasaryan wrote: >On Sat, Jan 11, 2025 at 3:24 AM 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. :) >> Hi, I am new to memory barriers. Hope not bothering. >> > /* >> > * Try to read-lock a vma. The function is allowed to occasionally yield 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_struct *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 need >> > @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma) >> > if (READ_ONCE(vma->vm_lock_seq) == READ_ONCE(vma->vm_mm->mm_lock_seq.sequence)) >> > return false; >> > >> > - if (unlikely(down_read_trylock(&vma->vm_lock.lock) == 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 == mm_lock_seq) // check got reordered > return false; > vm_refcnt += VMA_LOCK_OFFSET > vm_lock_seq == mm_lock_seq > vm_refcnt -= 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. > Here what we plan to do is define __refcount_inc_not_zero_limited() with acquire fence, e.g. with atomic_try_cmpxchg_acquire(), right? >> >> > + 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 check >> > - * 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_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 == raw_read_seqcount(&vma->vm_mm->mm_lock_seq))) { One question here is would compiler optimize the read of vm_lock_seq here, since we have read it at the beginning? Or with the acquire fence added above, compiler won't optimize it. Or we should use REACE_ONCE(vma->vm_lock_seq) here? >> >> 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. > Hmm.. refcount_t is defined with atomic_t. I am lost why replacing refcount_t with atomic_t would help. >> >> 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) <= 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) <= 1 && > !__is_vma_write_locked(vma, >&mm_lock_seq), vma); >} > >Will make the change. > >Thanks for the feedback! -- Wei Yang Help you, Help me