Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1589329rdb; Sat, 2 Dec 2023 01:48:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IGMZPJBXawyIKPV3FB1epk+0bnkFhWhLN9TjykC+9Od60FC0mJfuDeJay68HxcTwkrMpW/Y X-Received: by 2002:a05:6871:591:b0:1fa:f2a4:f3fa with SMTP id u17-20020a056871059100b001faf2a4f3famr1357229oan.12.1701510488545; Sat, 02 Dec 2023 01:48:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510488; cv=none; d=google.com; s=arc-20160816; b=Zd5uiYsdDvRZCKor5muhU6X6ol89Ji5DDRDdZcXC8pW+06W85hjY3xrebb9tWHnyXa jMfUQ7piXeWwViF8i5Um8c1G78C7/3SBjStQzwyXAZUnJWKdiSDO+1IYOkjeAqeTdeA4 hYLwgw03MbQyyoiobz1W+Yt1d5y9Lhk5cW7q3ajFNUsEB6Fh/C1O5w7ZEPTSday+/18A 0C8q1SbKpKzG7TuPTFbQqgRJFVBSkpZZo3Nab/W+QIS+YN/iXqMbSzTNps3hA+cFjkL1 n8lV3+WeLj8uSHvTw54d9ElHSaPOzIbJOHHQQYkJkkrMvMPK0XNgcLokpP0Gq6Qs6V/o A9Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:in-reply-to:cc:references:message-id:date :subject:mime-version:from:content-transfer-encoding:dkim-signature; bh=kWg0EL3RaVOmoE3sMtlw7YEVcWUVCDJ1DGg90IqvGCU=; fh=WP426PCulzyc/eBIKVh0aumn7Zn0Ah8cdL2hmt1aWtA=; b=Yns2lDWL/fCz4YPsOaEGTn066aHrUg7jQ4yQ0F1rQxxcQ/RqXQAbvD3UOLX00AvVEv jTBhpKp2NRj+BOc7dM3g0vxcyXpQ9VImCgauFyUgI8zCHBnspv3aPSb1aa2EAyM5dE/Z iVWWe8eNBK8e77oGFRNYNwxn8G106gHDcKW/96giN23fxkTH5KwG+pFXtn7pRu6PuBan kIcaoe9n04vRup6oDbqkIFH7q0SYdYukxmarAv91XM9o7FpjIT7Yba8W4vbRuAV75DpZ wjaoN/zijycL7hshC2c6VpYpdXs28HFQjVWpLzCKSmWI2Y/VyJiqKOrTVGkqGXye7CMT jubw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t17SwvtP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id h13-20020a170902f54d00b001cfcd4d8f2fsi4925722plf.351.2023.12.02.01.48.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:48:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t17SwvtP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 13CF38254393; Sat, 2 Dec 2023 01:48:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232373AbjLBJrr (ORCPT + 99 others); Sat, 2 Dec 2023 04:47:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232364AbjLBJrq (ORCPT ); Sat, 2 Dec 2023 04:47:46 -0500 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4D57197 for ; Sat, 2 Dec 2023 01:47:51 -0800 (PST) Content-Type: text/plain; charset=utf-8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1701510469; 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=kWg0EL3RaVOmoE3sMtlw7YEVcWUVCDJ1DGg90IqvGCU=; b=t17SwvtPvSBpQHzsL7annwyRE/ek5+7JyWpHeAunX2V05pfdxiCyjCPubq0hiFQjG1U5vE dAQAciU0FmjcGOH3h+1AvMn5R6jmJCjPRGTYqVrQoiGgnYiC5cF4YSy0tHJ8quZ6n3mwFF F9yGO4WDWlwsVSlNeJfUpimQUbkHCr8= Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song Mime-Version: 1.0 Subject: Re: [PATCH 1/4] mm: pagewalk: assert write mmap lock only for walking the user page tables Date: Sat, 2 Dec 2023 17:47:07 +0800 Message-Id: <6D1D5629-56C0-457E-A1BD-A09255AB9A3D@linux.dev> References: Cc: Muchun Song , Mike Kravetz , Andrew Morton , Linux-MM , linux-kernel@vger.kernel.org In-Reply-To: To: Kefeng Wang X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sat, 02 Dec 2023 01:48:04 -0800 (PST) > On Dec 2, 2023, at 17:25, Kefeng Wang wrote: >=20 > =EF=BB=BF >=20 > On 2023/12/2 16:08, Muchun Song wrote: >>>> On Dec 1, 2023, at 19:09, Kefeng Wang wrot= e: >>>=20 >>>=20 >>>=20 >>> On 2023/11/27 16:46, Muchun Song wrote: >>>> The 8782fb61cc848 ("mm: pagewalk: Fix race between unmap and page walke= r") >>>> introduces an assertion to walk_page_range_novma() to make all the user= s >>>> of page table walker is safe. However, the race only exists for walking= the >>>> user page tables. And it is ridiculous to hold a particular user mmap w= rite >>>> lock against the changes of the kernel page tables. So only assert at l= east >>>> mmap read lock when walking the kernel page tables. And some users matc= hing >>>> this case could downgrade to a mmap read lock to relief the contention o= f >>>> mmap lock of init_mm, it will be nicer in hugetlb (only holding mmap re= ad >>>> lock) in the next patch. >>>> Signed-off-by: Muchun Song >>>> --- >>>> mm/pagewalk.c | 29 ++++++++++++++++++++++++++++- >>>> 1 file changed, 28 insertions(+), 1 deletion(-) >>>> diff --git a/mm/pagewalk.c b/mm/pagewalk.c >>>> index b7d7e4fcfad7a..f46c80b18ce4f 100644 >>>> --- a/mm/pagewalk.c >>>> +++ b/mm/pagewalk.c >>>> @@ -539,6 +539,11 @@ int walk_page_range(struct mm_struct *mm, unsigned= long start, >>>> * not backed by VMAs. Because 'unusual' entries may be walked this fu= nction >>>> * will also not lock the PTEs for the pte_entry() callback. This is u= seful for >>>> * walking the kernel pages tables or page tables for firmware. >>>> + * >>>> + * Note: Be careful to walk the kernel pages tables, the caller may be= need to >>>> + * take other effective approache (mmap lock may be insufficient) to p= revent >>>> + * the intermediate kernel page tables belonging to the specified addr= ess range >>>> + * from being freed (e.g. memory hot-remove). >>>> */ >>>> int walk_page_range_novma(struct mm_struct *mm, unsigned long start, >>>> unsigned long end, const struct mm_walk_ops *ops, >>>> @@ -556,7 +561,29 @@ int walk_page_range_novma(struct mm_struct *mm, un= signed long start, >>>> if (start >=3D end || !walk.mm) >>>> return -EINVAL; >>>> - mmap_assert_write_locked(walk.mm); >>>> + /* >>>> + * 1) For walking the user virtual address space: >>>> + * >>>> + * The mmap lock protects the page walker from changes to the page >>>> + * tables during the walk. However a read lock is insufficient to >>>> + * protect those areas which don't have a VMA as munmap() detaches >>>> + * the VMAs before downgrading to a read lock and actually tearing >>>> + * down PTEs/page tables. In which case, the mmap write lock should >>>> + * be hold. >>>> + * >>>> + * 2) For walking the kernel virtual address space: >>>> + * >>>> + * The kernel intermediate page tables usually do not be freed, so >>>> + * the mmap map read lock is sufficient. But there are some exceptions= . >>>> + * E.g. memory hot-remove. In which case, the mmap lock is insufficien= t >>>> + * to prevent the intermediate kernel pages tables belonging to the >>>> + * specified address range from being freed. The caller should take >>>> + * other actions to prevent this race. >>>> + */ >>>> + if (mm =3D=3D &init_mm) >>>> + mmap_assert_locked(walk.mm); >>>> + else >>>> + mmap_assert_write_locked(walk.mm); >>>=20 >>> Maybe just use process_mm_walk_lock() and set correct page_walk_lock in s= truct mm_walk_ops? >> No. You also need to make sure the users do not pass the wrong >> walk_lock, so you also need to add something like following: >=20 > But all other walk_page_XX has been converted=EF=BC=8Csee more from commit= > 49b0638502da "mm: enable page walking API to lock vmas during the walk"), > there's nothing special about this one, the calls must pass the right > page_walk_lock to mm_walk_ops, If you think this one is not special, why it is not converted by that commit= at that time?=20 >=20 >> if (mm =3D=3D &init_mm) >> VM_BUG_ON(walk_lock !=3D PGWALK_RDLOCK); >> else >> VM_BUG_ON(walk_lock =3D=3D PGWALK_RDLOCK); >> I do not think the code will be simple. >=20 > or adding the above lock check into process_mm_walk_lock too. No. it=E2=80=99s wrong. walk_page_range_novma is special compared with other= variants, the check is only applicable for walk_page_range_novma, not for i= ts variants.=