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 B95F2C3ABAC for ; Tue, 6 May 2025 09:17:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC02F6B0082; Tue, 6 May 2025 05:17:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C71686B008A; Tue, 6 May 2025 05:17:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B88066B008C; Tue, 6 May 2025 05:17:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 989466B0082 for ; Tue, 6 May 2025 05:17:12 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8CB1F161FB1 for ; Tue, 6 May 2025 09:17:12 +0000 (UTC) X-FDA: 83411929104.21.4180B56 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf02.hostedemail.com (Postfix) with ESMTP id E7A7C8000D for ; Tue, 6 May 2025 09:17:10 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf02.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746523031; a=rsa-sha256; cv=none; b=MNzMvow2sFrVlIBPRD2nQML2r90nXhAnzaLrGWj3Qyh/KjpyzqaVMlut6zm4mB2FU3UMiK F5L2tmMhvHMamefl882SkwJkrHQMB73RKyeepdrmS1CcFrp64RJ5MK6qC1snFvro3Zf/8c xzxSQ0x+fQXjr1Tks0aUfo1vI6rLguI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf02.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746523031; 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; bh=Ndk+CSdaZy4NvR0JNGUHITO+GFMdv5tl9jqpuIW6UfM=; b=TC6+6XU63iYozvWiAwzvsjJkf5LHvANnYRykGkwR50KxZJXrvM9W5g8oyQv1byDp8fKucC GkmraFOQL5Zud1AimzGbHT7wZcQI5usirLFODs5c9F1GoH30PHm2ef7+O5SxZkWbnts9jq V9t2jIYdkTVx4n2ifAhUyfaQ2qdl7Lw= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5216E113E; Tue, 6 May 2025 02:17:00 -0700 (PDT) Received: from [10.162.43.13] (K4MQJ0H1H2.blr.arm.com [10.162.43.13]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CF7EB3F5A1; Tue, 6 May 2025 02:17:02 -0700 (PDT) Message-ID: <0857761c-bb4c-4ea5-a96a-90d555673527@arm.com> Date: Tue, 6 May 2025 14:46:59 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 6/7] mm: Batch around can_change_pte_writable() To: David Hildenbrand , akpm@linux-foundation.org Cc: ryan.roberts@arm.com, willy@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, catalin.marinas@arm.com, will@kernel.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com, anshuman.khandual@arm.com, peterx@redhat.com, joey.gouly@arm.com, ioworker0@gmail.com, baohua@kernel.org, kevin.brodsky@arm.com, quic_zhenhuah@quicinc.com, christophe.leroy@csgroup.eu, yangyicong@hisilicon.com, linux-arm-kernel@lists.infradead.org, namit@vmware.com, hughd@google.com, yang@os.amperecomputing.com, ziy@nvidia.com References: <20250429052336.18912-1-dev.jain@arm.com> <20250429052336.18912-7-dev.jain@arm.com> <25dcf969-e479-4d4a-a95c-0e83706af99e@redhat.com> <9a770aec-a020-4199-a53e-eddda657999d@redhat.com> Content-Language: en-US From: Dev Jain In-Reply-To: <9a770aec-a020-4199-a53e-eddda657999d@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: E7A7C8000D X-Stat-Signature: a5ur1mhwr3gzwsx8oai9fggxxnfnyak1 X-Rspam-User: X-HE-Tag: 1746523030-665228 X-HE-Meta: U2FsdGVkX1/is1Z+8TQnK+uTLMpxGNHAciu9m3BpA/9wl2C670+2+KkKO+73RhPKJhqhVQ7G0B6N+fvmdaw/MRqrd+AQ3b08jq4hsdouGJEGkY6ctHdknVUe1nQ8RD2GiwSZePc3tAyAHmwFSnsDhZszQyUuUIrCvSJy06q34ZUVtJUVSQPd90rR4gXkZV1waZkPBL1vT/Ia093iExZaEfQmpsOVzUsrozBHdFK9Pw8ed3CYU0wqkK/jCKncwN6FXUvJaXg9yj6lNDuoowz9aZoxKrhoJvUfb6FYNlhXhYflseUTttHbzVfQxvnt6qd5UBzRqZwZgHqIycBp5GNKNdJsHa+2piAguPAxuXo77Qodf29bXBtI8pKLv7sA0wDQtQh1rZgnx6k4hbomAiJSJFPHzx+K8TPGOq+zbjvX1Oie5F8Ow0GZFhV36xw4geMNUEJ/hZVhnlnWh0f0aIJxW+3eJbSJQpApXy3R9R08Y3phNcXoQuxJVWqoZMi5P8bV3omz/+cIM/saB+ggezOMzSCUJZHyvcR+X6IHOG7l7ZieBJuO+e4BckdjujoWM7ZVi5UZk2KcAQQDcEyAv0qDXYE2ku+QRb4QOVxz6uPDJ8Sjey2CyCkaZq9mUc5vfZTl1UIBYncM5uJlgCFxtTjMbf+x/acFut1IRe4gQApAW/ax0q2p9WWWkOibs/wdfYHXt7HoYQ6kmeARU74XFJ9XYmsWJrWuigstHjqU/rRL5r7Z7eeiC8JL1Iguh5WKj404ykz1SzsqP2iF1BX0ZC/M7qmHUQwJPCwElDkgYdhBkxeswBhw0cW7mZdWsFAmiBLsuUCaLThuJKGI7sOSAsZKt1ADGlwPlbLCRbLbfP/gl3kghxXaQRsENmswOS1bn+9PGFABdvgSbf4= 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 29/04/25 2:57 pm, David Hildenbrand wrote: > On 29.04.25 11:19, David Hildenbrand wrote: >> >>>    #include "internal.h" >>> -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> -                 pte_t pte) >>> +bool can_change_ptes_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> +                  pte_t pte, struct folio *folio, unsigned int nr) >>>    { >>>        struct page *page; >>> @@ -67,8 +67,9 @@ bool can_change_pte_writable(struct vm_area_struct >>> *vma, unsigned long addr, >>>             * write-fault handler similarly would map them writable >>> without >>>             * any additional checks while holding the PT lock. >>>             */ >>> -        page = vm_normal_page(vma, addr, pte); >>> -        return page && PageAnon(page) && PageAnonExclusive(page); >>> +        if (!folio) >>> +            folio = vm_normal_folio(vma, addr, pte); >>> +        return folio_test_anon(folio) && ! >>> folio_maybe_mapped_shared(folio); >> >> Oh no, now I spot it. That is horribly wrong. >> >> Please understand first what you are doing. > > Also, would expect that the cow.c selftest would catch that: > > "vmsplice() + unmap in child with mprotect() optimization" > > After fork() we have a R/O PTE in the parent. Our child then uses > vmsplice() and unmaps the R/O PTE, meaning it is only left mapped by the > parent. > > ret = mprotect(mem, size, PROT_READ); > ret |= mprotect(mem, size, PROT_READ|PROT_WRITE); > > should turn the PTE writable, although it shouldn't. > > If that test case does not detect the issue you're introducing, we > should look into adding a test case that detects it. > Hi David, I am afraid I don't understand my mistake :( PageAnon(page) boils down to folio_test_anon(folio). Next we want to determine whether the page underlying a PTE is mapped exclusively or not. I approximate this by folio_maybe_mapped_shared -> if the folio => all pages are mapped exclusively, then I convert the entire batch to writable. If one of the pages is mapped shared, then I do not convert the batch to writable, thus missing out on the optimization. As far as I understand, the test failure points out exactly this right? Do you suggest an alternate way? My initial approach was to add a new flag to folio_pte_batch: FPB_IGNORE_ANON_EXCLUSIVE, but from an API design PoV Ryan pointed out that that looked bad.