Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp166172lqd; Tue, 23 Apr 2024 20:06:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVXEd56PWFGQiVeThRpKifB634/8zRy0AniB7hmsCfqoDCAapP/dC6CE+A8K/GNywXLTkT/8ccpxhF9R5JABI1A/2mN2haADVm0Mox/3w== X-Google-Smtp-Source: AGHT+IGZaM4y7GQEYBvIhpw6TzVT+euJZx+7GxD7uf1JMhngZK4iUhVs9Qi69Ojcb9OnMhUObBe8 X-Received: by 2002:a05:6359:7617:b0:18d:a474:9d99 with SMTP id wg23-20020a056359761700b0018da4749d99mr102722rwc.5.1713927970029; Tue, 23 Apr 2024 20:06:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713927970; cv=pass; d=google.com; s=arc-20160816; b=jhwaHXGUQ+CRrsTl670WMWiqAHOcBZXr5/aNjUKT0++2NeESqr1WNFYuqrzrn/9oI3 jy1reukD8X7oDBQsBsNvSru3QL1EymNk6uZj/ZDU2PdF7qkOlw/Wv6H6wPHmPqLQsKse dygzebyso8cap/ar5dG3of/klIyCoQqSiZE+920zxBv9moLlaE1DeL97EAxjrHOn7OgQ qY148mcwE8bqvsPuSKPaFfZvgpKruGIwpDhCHPEXLYuKetWIsFSxMEu5hvPWSfcoAs8D YgFUq2ln5subk2EF8PzPAXksR1tvTBTx5H38CG7MNLkCrY6jqNdWO+WmqJpF0Sdsr8Kr ANbA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:cc:to:subject:tls-required:message-id:from :content-transfer-encoding:date:dkim-signature:mime-version :list-unsubscribe:list-subscribe:list-id:precedence; bh=5Q8EGvUA4lu8esD47DBLZ4CU9GgawJRaGEeylxgCNGE=; fh=Xxz1mv5huxuh2eMaKUDMJHJugkmmMGbNkIE136IIvV0=; b=CESV5Xr51eTT71ASinS/zSeJo/2gR4pWSYMXYGP/JC6jIwkl5efLvHUYP1lmG1ENlq jnlHwXDXdCsifrClBfc8Rq81mzs6mqFpptmLOibeg3/Sh4Rm+B1C/8p1J/8o51GuOWn4 vMab4TEthw1AZ5tQZ+Ysyk0J37Kat1OpuUlB4uThL5/tVcbTIb9mxxze6IK7axoE9qaM 1woCuQegOhXYkr6DUOl203eRvbxWdGlSGt2SIGqTyTUDRr9TLJL+iDejciWvWTlqbl89 h94CHHPCOoBlLw5JHl8/kTUOqaISy0Yq9Lw91/X+rr7Wtv+1Se/1S/ECl5dt4YYTHIBr 3kmw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ld+syGfV; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-156198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156198-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id cx18-20020a05620a51d200b0078d6efb3d1csi13161395qkb.312.2024.04.23.20.06.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 20:06:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ld+syGfV; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-156198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156198-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BD4D51C21CE4 for ; Wed, 24 Apr 2024 03:06:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33E1714431B; Wed, 24 Apr 2024 03:05:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ld+syGfV" Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C13613D619 for ; Wed, 24 Apr 2024 03:04:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713927899; cv=none; b=DuEk4aOk8UC88V1CGNan4kxiVhVJI5YplhCEDF9btzbmbg9MH7YOSo/k+bckxzqXxSRWtXAlEDbTx+L/8o3w8DekooRhIMt4fzEOtmfz5Mo2v7KjxZNeZbIchtn8gtKPZXcv9M/SiRAwS+Xk/DvfukvaGSjBrTqvyi7xsyOx81w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713927899; c=relaxed/simple; bh=R1jmTxIBV5+sVaAK4eRBOf1ydKoXFUJi+Y0I1HTFRhg=; h=MIME-Version:Date:Content-Type:From:Message-ID:Subject:To:Cc: In-Reply-To:References; b=CCkk0CvMWwulHzgKuPnPr5XDGLnPoH/PssGRWIDb1pvU5+qFvQ4uW3v7LNMiWVQoMf/B/cN+NHM4+SDBy7dIYBXD0H3bVYfum/Pp6BeUO+lyGRc+8lZYrGzFnpBMF8rV/Q5R4iDG7+nJDRMzGh20ynySr3IWYyaDnNXh30rd8eA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ld+syGfV; arc=none smtp.client-ip=95.215.58.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 >