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 3BD09C4167B for ; Sat, 2 Dec 2023 08:09:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE61F8D0086; Sat, 2 Dec 2023 03:09:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C95508D007C; Sat, 2 Dec 2023 03:09:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B860F8D0086; Sat, 2 Dec 2023 03:09:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A83D98D007C for ; Sat, 2 Dec 2023 03:09:11 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7F875A0069 for ; Sat, 2 Dec 2023 08:09:11 +0000 (UTC) X-FDA: 81521152902.27.DB5C158 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) by imf12.hostedemail.com (Postfix) with ESMTP id 970BD40009 for ; Sat, 2 Dec 2023 08:09:09 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ggUl4N+v; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.180 as permitted sender) smtp.mailfrom=muchun.song@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701504550; a=rsa-sha256; cv=none; b=XNvjkiMk9SbRw+cOBo3sHAep5nZufHPm9PXIqDNZUv0dK+z+/q3ZGGDMvWeBQYy4uqm8kh lWxyLI13pX5wruVyBj57oaJN3EQNtAn4TrMnfN/6cp6kUfLO9BJDvxTm5JTnfTrHlQrSsb /c0GpZQZB9Aw1Fclo+WO686k+Ix8Als= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ggUl4N+v; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.180 as permitted sender) smtp.mailfrom=muchun.song@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701504550; 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=brrTfRqIr/zFBCw4NqIsnKtK9OdzMgGTyuDsHZLvG2I=; b=ru3CdktTWbrj9C0hfeD203SkYsatwPK3t2/4/0k66+70KZUPQ8oYdq0AV35F3WMlui8NEk JrtRWezwdDPYRN0Z5CblzRVuqeR0QHuAfIS2MmAOElI8tX+VdjGIm2TUjHD2CsrFJ1ttL4 t2FOsZSHvTBIVi6HEguzDAIQPSNT9Bk= Content-Type: text/plain; charset=us-ascii DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1701504547; 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=brrTfRqIr/zFBCw4NqIsnKtK9OdzMgGTyuDsHZLvG2I=; b=ggUl4N+vJPm0ftTWLYo3HDQeywbMbi2luvGxr270YyfxY4vs0QfgfktQKw4+9zOgGDhyK4 Wo+gf2GqkWSPhOaoGVmy3Scg3DC+7Mtv48VBtWoDrRiHA7TNaFdkDdVaNRxlonvTweMsFb vOSeEG+zEtMptLV3FnFJBWtff5DyqKg= Mime-Version: 1.0 Subject: Re: [PATCH 1/4] mm: pagewalk: assert write mmap lock only for walking the user page tables X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <9e5c199a-9b4d-4d1b-97d4-dd2b776ac85f@huawei.com> Date: Sat, 2 Dec 2023 16:08:32 +0800 Cc: Muchun Song , Mike Kravetz , Andrew Morton , Linux-MM , linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: <6AB1EE49-496E-46DE-B51E-42B06AA717D8@linux.dev> References: <20231127084645.27017-1-songmuchun@bytedance.com> <20231127084645.27017-2-songmuchun@bytedance.com> <9e5c199a-9b4d-4d1b-97d4-dd2b776ac85f@huawei.com> To: Kefeng Wang X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 970BD40009 X-Stat-Signature: 15qsbgqyafj69qsr63f5enu6zkj8do3j X-HE-Tag: 1701504549-289284 X-HE-Meta: U2FsdGVkX18zVPPeK+iSDdP3T4MTyTZIlT6RCbAdMOcQKS9CnkoHHrshhzLo/D1c+4zhlp6MYORA7h3oj/zEkGMjQpN4vowE/ST+gsJOV9wCR0pwlIz34iv8LcYeFC44vJi5bs+NgoBdByw7zSMxKIQviawGiGMCgA4IGYawTqwz21ddOdN8RzciQDKaTgI4lFnwzBSa4u4mhiDgohKY5GJy4vaK550HpJ6JZcl1W3XncUe4jW0KD9Wqah2SFjmWcOQiNnTsTb3jp1aHcOcnEW0/3Y30CF4/0k5wxpmXYOepHfDmn7lOOdHGlSyNfazRH9CPWqIBMCmFhZdO00uuyN7L0uvomp2pXwXWhdtQ1UvDW+Flyp0vZilvZellmvXTCj9BuNGxkBvk5STQ/jT9NFUtD8fmSCCH7AInlD5zu6eC/kwtVs4Esg1DXh3wwH9y9BC+La1hI+4MXut3eMbtMx1ZSIjLPAkBKsVb5DYcU3SE73qYGe//yjeTWNEdDV9nJuLfWlx/4XC/sz4RJUKZ8nur/gBhQnljjlfjn1jI4JVP+yOtRpwcvb3P1Qw0ib06WYlh8SQ2TPr5N0yrT5VqYJwpnhj5LSZr1vfoe1SZ5pR+7QRfVMkmIIuGN1Vbx8dcP1ddlQq8swFB14yaLi+O6YkR2u6GRdM5lG9RFiMhkpE8XYzMbmKq+EnBWKQ31COKXJpT6uv8no51VUNd1XENuT4x3HaZj55dr69l3/hILmS0H6zDMHfMmXD7hvDe7Pl4Ol84Np0EFnLZ4wrRD/MbhsBjydZAQW+ogTVSdYLrJ+wgD21D80uDpjR+gF+rkShaWVfqpE25sGWch5poGNK/K1NA6fLg2hxDXoKDzBGBKpHj0LPKAe/XjCf6zIvMF4ZLbjIebV/5uo3t51o43RDrAE/KlK8Cw+Cy5o3dlvactjf0yqAvLis5XVpEB+IQqLAD79OxctKtpE5nde5eEGF kNUio/5f qlRoRqiUaI3fbSlDxfPbKRtyOeDY/wwWToU0NDgFcdEtA9/PzfgKx98QONSXCAtWe8HYjUUfqqP5EV4wrA3VjCrRNVcp0a2FLp83ELC6fjq9N7JhIxZIb+L7gmZOmuiJlzljIDrV5+HSPbu8C0lSAOwEviXYNKkZJnCVd5zdYbK5G2DD6Nk2YIzGXba/qYtUpx3IG 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: > On Dec 1, 2023, at 19:09, Kefeng Wang = wrote: >=20 >=20 >=20 > On 2023/11/27 16:46, Muchun Song wrote: >> The 8782fb61cc848 ("mm: pagewalk: Fix race between unmap and page = walker") >> introduces an assertion to walk_page_range_novma() to make all the = users >> 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 = write >> lock against the changes of the kernel page tables. So only assert at = least >> mmap read lock when walking the kernel page tables. And some users = matching >> this case could downgrade to a mmap read lock to relief the = contention of >> mmap lock of init_mm, it will be nicer in hugetlb (only holding mmap = read >> 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 = function >> * will also not lock the PTEs for the pte_entry() callback. This is = useful 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 = prevent >> + * the intermediate kernel page tables belonging to the specified = address 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, = unsigned 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 = insufficient >> + * 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 struct 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: 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 >> return walk_pgd_range(start, end, &walk); >> }