Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5820147rwe; Tue, 18 Apr 2023 12:02:38 -0700 (PDT) X-Google-Smtp-Source: AKy350Zofl8dFRUfd1jRLYLWmh7qSGtN+lwrmEIGQdibuur3owq+jIAgTkPy20rlRr/ZqEPdeqJ3 X-Received: by 2002:a17:90a:1b42:b0:249:6f89:851a with SMTP id q60-20020a17090a1b4200b002496f89851amr636642pjq.24.1681844558406; Tue, 18 Apr 2023 12:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681844558; cv=none; d=google.com; s=arc-20160816; b=tTR4qFU+uzzMUPoKcFWmZu16M8NepMRgaNOaT0Yi+QJ6cPvrPoEO1HLLjd0ugkKsyr xvtDMn1WPbsgq9J4Eqh5uhxQ/ck+KPOy1xjSHD8/OVjrMF7j6z1YYvOhpWgnGODD3nXe vfKTnWcnjZdq86XEt/lntPcv1pWLAHYd98aaPHBy2WdkyeVTYeVqSVhTbdx+a4dOdtJN elmvKI7FyyULEH3j1Ja0qK5+DTQFr6S6yzFD4n12ih2eX68Vzed4zxuEmmkfqaf/B5F9 TgmKaZi5nRXeI98Kg0K3lt6fVPm5qs1JrU7TIXviJRJIncr6bO51A/6O5BvfWeuEsVJy SQbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature:dkim-signature; bh=tqi3oI5Ezx9YV9f6lUuUN7M2X1hzJenS7E4oz2JLxDw=; b=J1h6LGg88oGLfmaeedoaFTO8PBjiTyJUNOqXHZVeni9ygUqbtttyFX8CywOhnoLObI qV1jGQlMaZ/0M8Sp8uLDl2N9lU3a+M9BO/dueQVlJKsB9vfYFvFtxGjfU3w1geZMlyGN ECEkSyH5c3r3zQJlIS3fUES0jQwClVY0CvzWVMGFkEkJQ8VbPbai74zv47fBi93csF28 lEATnC8KKrNU9BoJcUAi08KaelZLR02YIAeuZbO2zHNqhp+k5j685uYtvgIYDLAxYCc6 c2gv1/5ccqOmFvP/S7NXo6eGW4xTocxOAjGIu+whl4vhIYDcwDRtYcomXdZH6hiXgueW 06uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov.name header.s=fm1 header.b=tpxOB7Nw; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=UOIUErmm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bt10-20020a17090af00a00b00247303bfcd6si12230641pjb.168.2023.04.18.12.02.26; Tue, 18 Apr 2023 12:02:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov.name header.s=fm1 header.b=tpxOB7Nw; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=UOIUErmm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbjDRTBY (ORCPT + 99 others); Tue, 18 Apr 2023 15:01:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229813AbjDRTBW (ORCPT ); Tue, 18 Apr 2023 15:01:22 -0400 Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EE9F8E for ; Tue, 18 Apr 2023 12:01:20 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 6AA0D3200986; Tue, 18 Apr 2023 15:01:18 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 18 Apr 2023 15:01:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1681844478; x= 1681930878; bh=tqi3oI5Ezx9YV9f6lUuUN7M2X1hzJenS7E4oz2JLxDw=; b=t pxOB7NwvzZBBJLOSrfdH6NkomNJoFhTTx/7fTzb/PkB8dBn+KhKYJkGtrV4rmHsj eFv6HGQKVILhftA2cd59VIIH1Da9Ni9vI27L2QIF0OBP8VBiZOiMaZk9RGQDyK4j +2SC89wcM3WJ8VCjt/E3kdaXuwYXt21PzYlCJyMxo2KFS944vSP4PMHTiJamHpRu omtuAwRHagvQrQx+k9fyOJc7KzeIQIO4gdmXdEQCGHH52vUsu5XcLUq8SMOzOjPL yP+iXubPDxWRAl0IJiD02AfdkTEnP5w0giii6Kpk1FArpVRLxHHGEj29I00N9Zzb VGup/Xs49aqXlt+tFzOMA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1681844478; x=1681930878; bh=tqi3oI5Ezx9YV 9f6lUuUN7M2X1hzJenS7E4oz2JLxDw=; b=UOIUErmmSb5rY+eMuP0Y9+xznTbO3 ClVXiyT5naWzamZ8XASR0t6VUl++KLDf34IcXyHYtbNacKKTecVbPhaQKmil5qki tCgeR5WLquYbXH3W9B8QadI9w/PmN+mypAn4OeNverEkwCEAsd+M3IKrzfmNqxH3 LgN04x8jZPPXfJOtuHpJPaO37H8kUOrIuKegkSR3HDeQda96ASvYHl6daPDmG6CQ o55MJFFJJDVK4UjuDoUUc1HcBl15y0dSPwG5NcHIEekerU3rEuC4eW9ygjd1LZ4p U60ea5JRxFgjFG1PzlwlgCLnP5oLapS7tzgCljB8qbBDxWmdsPzQgYCcg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdelkedgudeffecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttddttddttddvnecuhfhrohhmpedfmfhi rhhilhhlucetrdcuufhhuhhtvghmohhvfdcuoehkihhrihhllhesshhhuhhtvghmohhvrd hnrghmvgeqnecuggftrfgrthhtvghrnhepkefghfefffeggefgfefhfeetieevgfeltdeu teeggefhvefhjeeifedttdetvdeunecuffhomhgrihhnpehgihhtlhgrsgdrtghomhdpkh gvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomhepkhhirhhilhhlsehshhhuthgvmhhovhdrnhgrmhgv X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 18 Apr 2023 15:01:17 -0400 (EDT) Received: by box.shutemov.name (Postfix, from userid 1000) id 63BE110C351; Tue, 18 Apr 2023 22:01:12 +0300 (+03) Date: Tue, 18 Apr 2023 22:01:12 +0300 From: "Kirill A. Shutemov" To: Peter Xu Cc: David Hildenbrand , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mel Gorman Subject: Re: [PATCH mm-unstable v1] mm: don't check VMA write permissions if the PTE/PMD indicates write permissions Message-ID: <20230418190112.2eyuhzq3hqwvlmyt@box.shutemov.name> References: <20230418142113.439494-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 18, 2023 at 11:56:07AM -0400, Peter Xu wrote: > On Tue, Apr 18, 2023 at 04:21:13PM +0200, David Hildenbrand wrote: > > Staring at the comment "Recheck VMA as permissions can change since > > migration started" in remove_migration_pte() can result in confusion, > > because if the source PTE/PMD indicates write permissions, then there > > should be no need to check VMA write permissions when restoring migration > > entries or PTE-mapping a PMD. > > > > Commit d3cb8bf6081b ("mm: migrate: Close race between migration completion > > and mprotect") introduced the maybe_mkwrite() handling in > > remove_migration_pte() in 2014, stating that a race between mprotect() and > > migration finishing would be possible, and that we could end up with > > a writable PTE that should be readable. > > > > However, mprotect() code first updates vma->vm_flags / vma->vm_page_prot > > and then walks the page tables to (a) set all present writable PTEs to > > read-only and (b) convert all writable migration entries to readable > > migration entries. While walking the page tables and modifying the > > entries, migration code has to grab the PT locks to synchronize against > > concurrent page table modifications. > > Makes sense to me. > > > > > Assuming migration would find a writable migration entry (while holding > > the PT lock) and replace it with a writable present PTE, surely mprotect() > > code didn't stumble over the writable migration entry yet (converting it > > into a readable migration entry) and would instead wait for the PT lock to > > convert the now present writable PTE into a read-only PTE. As mprotect() > > didn't finish yet, the behavior is just like migration didn't happen: a > > writable PTE will be converted to a read-only PTE. > > > > So it's fine to rely on the writability information in the source > > PTE/PMD and not recheck against the VMA as long as we're holding the PT > > lock to synchronize with anyone who concurrently wants to downgrade write > > permissions (like mprotect()) by first adjusting vma->vm_flags / > > vma->vm_page_prot to then walk over the page tables to adjust the page > > table entries. > > > > Running test cases that should reveal such races -- mprotect(PROT_READ) > > racing with page migration or THP splitting -- for multiple hours did > > not reveal an issue with this cleanup. > > > > Cc: Andrew Morton > > Cc: Mel Gorman > > Cc: Peter Xu > > Signed-off-by: David Hildenbrand > > --- > > > > This is a follow-up cleanup to [1]: > > [PATCH v1 RESEND 0/6] mm: (pte|pmd)_mkdirty() should not > > unconditionally allow for write access > > > > I wanted to be a bit careful and write some test cases to convince myself > > that I am not missing something important. Of course, there is still the > > possibility that my test cases are buggy ;) > > > > Test cases I'm running: > > https://gitlab.com/davidhildenbrand/scratchspace/-/raw/main/test_mprotect_migration.c > > https://gitlab.com/davidhildenbrand/scratchspace/-/raw/main/test_mprotect_thp_split.c > > > > > > [1] https://lkml.kernel.org/r/20230411142512.438404-1-david@redhat.com > > > > --- > > mm/huge_memory.c | 4 ++-- > > mm/migrate.c | 5 +---- > > 2 files changed, 3 insertions(+), 6 deletions(-) > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index c23fa39dec92..624671aaa60d 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -2234,7 +2234,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, > > } else { > > entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot)); > > if (write) > > - entry = maybe_mkwrite(entry, vma); > > + entry = pte_mkwrite(entry); > > This is another change besides page migration. I also don't know why it's > needed, but it's there since day 1 of thp split in eef1b3ba053, so maybe > worthwhile to copy Kirill too (which I did). I was concentrated on the correctness at the point and this small inefficency didn't catch my eyes. I was curious how we serialize here against mprotect(). Looks safe to me: CPU0 CPU1 __split_huge_pmd() pmd_lock() __split_huge_pmd_locked() pmdp_invalidate() // PMD is non-present, but huge at this point change_protection() change_pmd_range() pmd_none_or_clear_bad_unless_trans_huge() == 0 // not skipped change_huge_pmd() __pmd_trans_huge_lock() pmd_lock() // serialized against __split_huge_pmd() Acked-by: Kirill A. Shutemov -- Kiryl Shutsemau / Kirill A. Shutemov