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 CEF24C04FFE for ; Wed, 24 Apr 2024 03:04:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6168B6B01F1; Tue, 23 Apr 2024 23:04:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C65E6B01F2; Tue, 23 Apr 2024 23:04:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B3E46B01F3; Tue, 23 Apr 2024 23:04:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2E7A36B01F1 for ; Tue, 23 Apr 2024 23:04:59 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A70E1120BD0 for ; Wed, 24 Apr 2024 03:04:58 +0000 (UTC) X-FDA: 82042933476.02.4BEAF32 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf24.hostedemail.com (Postfix) with ESMTP id 9D0BE180006 for ; Wed, 24 Apr 2024 03:04:56 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ld+syGfV; spf=pass (imf24.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713927897; 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=5Q8EGvUA4lu8esD47DBLZ4CU9GgawJRaGEeylxgCNGE=; b=0tkdiawvRkgZrKanOJJ2NCu12hevQ241gnMUGB8rWgMORbPdhFuyzXz3X/9EVYfZcerIgK SdBlR16MIkN4ttGs59E1b3sjWvwsV9ZmS8/4egxWLP71maSvBwbiRxbmOjmaLWX87QqSJv a8MNlx3YERaDocT8X93fpa26aEoteh0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713927897; a=rsa-sha256; cv=none; b=ybDpPcrJONBu4xMCgo4SIwJDx/NHcEIWeq34dJh2TbcuZ1rO4dQhNXtrZAciSCMQv9LvQP ExXVrS5HgQAQqSKb5rF6/gTff6eF8DiwBkXjivb3UYd5cFcN5xFbeHCYIhNVpNB+rzbmQI FujH2PSyov2kxzuCZldV1LM4/m7+qSU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ld+syGfV; spf=pass (imf24.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1713927894; h=from:from: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; bh=5Q8EGvUA4lu8esD47DBLZ4CU9GgawJRaGEeylxgCNGE=; b=ld+syGfVgjrpi0pZbQafZs11J2riKDoHegikuLS+sp6Foh/YO2ebEa+OCB0K9SmN6kVKRe SAD5AEzXDi2tyRZW/7usHayuh76Ffo2zU/GItL6dmC+cznmpf0TbbIaPlk0AFAqZD7xNyP kIJLd+IEOfE/2HD8/zJmX3u/FaKvEVQ= Date: Wed, 24 Apr 2024 03:04:51 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: "Yajun Deng" Message-ID: TLS-Required: No Subject: Re: [PATCH] mm/rmap: remove unnecessary page_table_lock To: "Liam R. Howlett" Cc: "David Hildenbrand" , akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org In-Reply-To: <3v6xmyzmnw6go45riwlu7qv4c4phiexpqxldnlbgwsjhppe4oi@xdcqm4xupl4k> References: <20240422105212.1485788-1-yajun.deng@linux.dev> <3c452d5db5b3d5879160ab62a9e0ac4481a6298a@linux.dev> <3v6xmyzmnw6go45riwlu7qv4c4phiexpqxldnlbgwsjhppe4oi@xdcqm4xupl4k> X-Migadu-Flow: FLOW_OUT X-Stat-Signature: xgjruychqz8e3o6pzmzf6s54p73u5z91 X-Rspamd-Queue-Id: 9D0BE180006 X-Rspamd-Server: rspam06 X-Rspam-User: X-HE-Tag: 1713927896-274749 X-HE-Meta: U2FsdGVkX19JlwaRK0m2a1+ZT5sbOcrhoobt9+loklCHzjPoupIiZgpMOEpKfUinIDe9XBSIXZ44JRgRmIaTF0Tlqq12R1zhG4f5NNcRraj8Hg26DN4J8yvut9sIXqnGLHfc3hEr7r8X8iYkwGP1gz/R97rCcLHN9TAaXtX5RFRrLbc9OqwS6nRH03vjRwHzPRPqb+DXjyTUMnF5SJHPJ8bXn39zJS/6XM9xXuc3a2TeRUK2csUIvv5DP4BcKjNr6gYRa8Q/Lnw/lJX+548wR1Iv4jWGzMcgahUzmSgf/qmJSVrK7WBnRF2QRYNSyloqW5WUyWHMPKYPLVXai0lrtIsATKTJwF5IqFJvZ8R5F5KeMNjdzhWTq9Gmfkl72saw40HFQ3rWJOU11AcmiQSQcDb5675hguVM2XLO/BQ4HEuEC6ppmQcQ3yO07CH4j/lTMWfgq03on5l4CEkCGpVnnvM/DpbUD7lUXhSrje/BwR4lEs7rl/G5KsJAN48YrTN6Mxr7Evgngwps7aD0A7lPGl+JH9jU3z0yPFcI3f6dQZ8bIKXDE6rH3zoTB68L3lMtIrE/OzIPTPVqRgzvltNfeVNxIROu6YTPNPIlevn7FSgRkmkuSVHGzY0Jg66+bJesPmodNRmBMyEnSIe618bYLkmoVlgxJ9sxCQ2qCLnVuj7OvpwjgE412ortFnyrTLBOVLee0OqlZLXMUYzlF9KsBSnw11cG322J+N/Whv4Rqm3YJ2AHm52SDiAxLwAOOF4X0Dg/i7Wabw2GeIklI4RwxrIliJ+NZPh+VwwK/zwPWUr4gQ1QTzHHE5yc3P4ScM6QJYPp1/VxCEc2OmK6y4M4PL3dh8YpHo2EmUla0xobZftvaot7PlXPGhra+ihLIukPePwDEL0t2zOxNG4D3n3j7YaYK+0vBGXavt5E/OUSYNKL1JWa0aqEcSQQwtsMbAp8jUkcZtFsCSw/UHzlwNp vd84eGkH NqgZDhGfEKe3hiGaNOzMnVLjBqQ5gj8vlU+xL5OH2k4Oz/uawnfd1N9k2s2KsGfo8TcfGWzpNskipauY2GuljNojSZ1Bfpx9ONo8Ti4jr+rZZhqtzrR25V5Gb8/W8cvOpx1t4u0enGP5Cvp0Zw/3KYfbLbyLjLP68hB8jD/+z6Q4UmR4e94mOanm4kw== 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: April 24, 2024 at 1:11 AM, "Liam R. Howlett" wr= ote: >=20 >=20* Yajun Deng [240423 04:35]: >=20 >=20>=20 >=20> April 23, 2024 at 4:18 PM, "David Hildenbrand" w= rote: > >=20 >=20>=20=20 >=20>=20 >=20>=20=20 >=20>=20 >=20>=20=20 >=20>=20 >=20>=20=20 >=20>=20 >=20> On 23.04.24 09:53, Yajun Deng wrote: > >=20 >=20>=20=20 >=20>=20 >=20> >=20 >=20>=20 >=20> > April 22, 2024 at 7:24 PM, "David Hildenbrand" wrote: > >=20 >=20> >=20 >=20>=20 >=20> > > >> > >=20 >=20> >=20 >=20>=20 >=20> > >=20 >=20>=20 >=20> > > On 22.04.24 12:52, Yajun Deng wrote: > >=20 >=20> > >=20 >=20>=20 >=20> >=20 >=20>=20 >=20> > page_table_lock is a lock that for page table, we won't change p= age > >=20 >=20> >=20 >=20>=20 >=20> > table in __anon_vma_prepare(). As we can see, it works well in > >=20 >=20> >=20 >=20>=20 >=20> > anon_vma_clone(). They do the same operation. > >=20 >=20> >=20 >=20>=20 >=20> > >=20 >=20>=20 >=20> > > We are reusing mm->page_table_lock to serialize, not the *actu= al* low-level page table locks that really protect PTEs. > >=20 >=20> > >=20 >=20>=20 >=20> > > With that locking gone, there would be nothing protection vma-= >anon_vma. > >=20 >=20> > >=20 >=20>=20 >=20> > > Note that anon_vma_clone() is likely called with the mmap_lock= held in write mode, which is not the case for __anon_vma_prepare() ... > >=20 >=20> > >=20 >=20>=20 >=20> >=20 >=20>=20 >=20> > Yes, anon_vma_clone() is called with the mmap_lock held. I added= mmap_assert_write_locked(dst->vm_mm) to prove it. > >=20 >=20> >=20 >=20>=20 >=20> > I added mmap_assert_write_locked(vma->vm_mm) in __anon_vma_prepa= re() at the same time, it shows __anon_vma_prepare() > >=20 >=20> >=20 >=20>=20 >=20> > is also called with the mmap_lock held too. > >=20 >=20> >=20 >=20>=20 >=20>=20=20 >=20>=20 >=20> Make sure you actually have lockdep built in and enabled. > >=20 >=20>=20=20 >=20>=20 >=20>=20=20 >=20>=20 >=20> This is my config. > >=20 >=20> CONFIG_LOCKDEP=3Dn > >=20 >=20> CONFIG_DEBUG_VM=3Dy > >=20 >=20>=20=20 >=20>=20 >=20> I did another test. > >=20 >=20> I put mmap_assert_write_locked(mm) before 'set_bit(MMF_OOM_SKIP, &= mm->flags)' in mmap.c, it's outside the lock. > >=20 >=20> It will crash when on boot. I think mmap_assert_write_locked() wor= ks. > >=20 >=20 > If you are changing locks, then please test with lockdep on. >=20 It's=20my fault. It shows a warning with lockdep on. > >=20 >=20> __anon_vma_prepare() is for example called from do_anonymous_page()= where we might only hold the mmap_lock in read mode (or not at all IIRC = with VMA in read mode). > >=20 >=20 > Consider two concurrent readers getting to this function with the same >=20 >=20vma. There is no mergeable anon vma, so both create a new anon_vma. >=20 >=20You take the anon_vma lock to parallelize the linking to the vma - bu= t >=20 >=20they are different locks because they are both new anon_vma structs >=20 >=20allocated by concurrent readers. >=20 >=20You now need a lock that you know cannot allow this to happen. Lookin= g >=20 >=20at the top of mm/rmap.c and see which one works. The next in line is >=20 >=20the page table lock, so that one is used here. >=20 >=20What if we reverse the locks? We can deadlock. >=20 >=20What if we don't take the anon_vma lock? We can have two writers to t= he >=20 >=20anon_vma. >=20 >=20Thanks, >=20 >=20Liam >