From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A49AE34575F for ; Thu, 19 Feb 2026 15:30:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771515003; cv=none; b=YdUI3bxH2Q8W6et4D9AgXsPiiJ6NOiLQzhMwFS960iG8RttBA1SmEI6vGwFlJZIb51F6BvIq9svrLIYF5qPYttndkGjr1R2xaDVNbakSTpvNmWAzluif2iIoAjbIQMcGWufRfulEjRLF1Ld/GAVcaaGImKjxAGRNuSYVLUbvFWU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771515003; c=relaxed/simple; bh=6VSghjQEMSGNtWa/e2MhFQ2RRzO4Osa7JV5WDWMcAys=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=jiSiiZYoNqZQLwvQdlcP81Rda5UEgM+4n4rm7ZKeVesyKNdP7Es/O9FZXdl7MP0+4UIConwUtcxePMR9eSwK07lwXH0Wii0sp62RToPsm5Ysy6/o6GMuT3t9PoN67eWszf3P70/m6oSGdPB8OXhFmR4Bo4spbBuOBNzLserBYK8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Iq4dtZIO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Iq4dtZIO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E09FFC4CEF7; Thu, 19 Feb 2026 15:30:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771515003; bh=6VSghjQEMSGNtWa/e2MhFQ2RRzO4Osa7JV5WDWMcAys=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=Iq4dtZIObnr3EuktzR/uLR2CbP0F7rxpd5uQHRXsZ+tCrjxB2R3Xz0R/io0/EPxE4 dhOb0Gr167GknR1vmS3abWwRSRUYI4/Yk96ZypdEJSQ6oxer4FhNt1dy8w+2p9tVPS P+Z7+05I42FL1+HjE6/8wkJfzgmqslFyM+J6CLClq1pZkl5rlArySjEN5RHnM0t0L9 8nUU32Wahjf1J7EjJtD4UJFrcLuqgDXnqRQ/3+uSmu2fu7hcF4cBYHocGCAFyen7/b NeTbneTUHDJQw6bZX0qZyf/rQwZyQWZd8k4ZOkXr6ARXOTLzwXgKikjl4bNIYuvl/2 MkyPgcUTq1uDA== Message-ID: Date: Thu, 19 Feb 2026 16:29:58 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [REGRESSION] mm/mprotect: 2x+ slowdown for >=400KiB regions since PTE batching (cac1db8c3aad) To: Pedro Falcato Cc: Dev Jain , Luke Yang , surenb@google.com, jhladky@redhat.com, akpm@linux-foundation.org, Liam.Howlett@oracle.com, willy@infradead.org, vbabka@suse.cz, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <8315cbde-389c-40c5-ac72-92074625489a@arm.com> <5dso4ctke4baz7hky62zyfdzyg27tcikdbg5ecnrqmnluvmxzo@sciiqgatpqqv> <340be2bc-cf9b-4e22-b557-dfde6efa9de8@kernel.org> <624496ee-4709-497f-9ac1-c63bcf4724d6@kernel.org> <9209d642-a495-4c13-9ec3-10ced1d2a04c@kernel.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZg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BdZ5eEkj1dCyx+t/ga6x2rHyc8Sl86oK1tvAkwBNsfKou3v+jP/l14a7DGBvrmlYjO 59o3t6inu6H7pt7OL6u6BQj7DoMAEQEAAcLBfAQYAQgAJgIbDBYhBBvZyq1zXEw6Rg38yk3e EPcA/4NaBQJonNqrBQkmWAihAAoJEE3eEPcA/4NaKtMQALAJ8PzprBEXbXcEXwDKQu+P/vts IfUb1UNMfMV76BicGa5NCZnJNQASDP/+bFg6O3gx5NbhHHPeaWz/VxlOmYHokHodOvtL0WCC 8A5PEP8tOk6029Z+J+xUcMrJClNVFpzVvOpb1lCbhjwAV465Hy+NUSbbUiRxdzNQtLtgZzOV Zw7jxUCs4UUZLQTCuBpFgb15bBxYZ/BL9MbzxPxvfUQIPbnzQMcqtpUs21CMK2PdfCh5c4gS sDci6D5/ZIBw94UQWmGpM/O1ilGXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/19/26 16:00, Pedro Falcato wrote: > On Thu, Feb 19, 2026 at 02:02:42PM +0100, David Hildenbrand (Arm) wrote: >> On 2/19/26 13:15, Pedro Falcato wrote: >>> >>> I don't know, perhaps there isn't a will-it-scale test for this. That's >>> alright. Even the standard will-it-scale and stress-ng tests people use >>> to detect regressions usually have glaring problems and are insanely >>> microbenchey. >> >> My theory is that most heavy (high frequency where it would really hit performance) >> mprotect users (like JITs) perform mprotect on very small ranges (e.g., single page), >> where all the other overhead (syscall, TLB flush) dominates. >> >> That's why I was wondering which use cases that behave similar to the reproducer exist. >> >>> >>> >>> Sure, but pte-mapped 2M folios is almost a worst-case (why not a PMD at that >>> point...) >> >> Well, 1M and all the way down will similarly benefit. 2M is just always the extreme case. >> >>> >>> >>> I suspect it's not that huge of a deal. Worst case you can always provide a >>> software PTE_CONT bit that would e.g be set when mapping a large folio. Or >>> perhaps "if this pte has a PFN, and the next pte has PFN + 1, then we're >>> probably in a large folio, thus do the proper batching stuff". I think that >>> could satisfy everyone. There are heuristics we can use, and perhaps >>> pte_batch_hint() does not need to be that simple and useless in the !arm64 >>> case then. I'll try to look into a cromulent solution for everyone. >> >> Software bits are generally -ENOSPC, but maybe we are lucky on some architectures. >> >> We'd run into similar issues like aarch64 when shattering contiguity etc, so >> there is quite some complexity too it that might not be worth it. >> >>> >>> (shower thought: do we always get wins when batching large folios, or do these >>> need to be of a significant order to get wins?) >> >> For mprotect(), I don't know. For fork() and unmap() batching there was always a >> win even with order-2 folios. (never measured order-1, because they don't apply to >> anonymous memory) >> >> I assume for mprotect() it depends whether we really needed the folio before, or >> whether it's just not required like for mremap(). >> >>> >>> But personally I would err on the side of small folios, like we did for mremap() >>> a few months back. >> >> The following (completely untested) might make most people happy by looking up >> the folio only if (a) required or (b) if the architecture indicates that there is a large folio. >> >> I assume for some large folio use cases it might perform worse than before. But for >> the write-upgrade case with large anon folios the performance improvement should remain. >> >> Not sure if some regression would remain for which we'd have to special-case the implementation >> to take a separate path for nr_ptes == 1. >> >> Maybe you had something similar already: >> >> >> diff --git a/mm/mprotect.c b/mm/mprotect.c >> index c0571445bef7..0b3856ad728e 100644 >> --- a/mm/mprotect.c >> +++ b/mm/mprotect.c >> @@ -211,6 +211,25 @@ static void set_write_prot_commit_flush_ptes(struct vm_area_struct *vma, >> commit_anon_folio_batch(vma, folio, page, addr, ptep, oldpte, ptent, nr_ptes, tlb); >> } >> +static bool mprotect_wants_folio_for_pte(unsigned long cp_flags, pte_t *ptep, >> + pte_t pte, unsigned long max_nr_ptes) >> +{ >> + /* NUMA hinting needs decide whether working on the folio is ok. */ >> + if (cp_flags & MM_CP_PROT_NUMA) >> + return true; >> + >> + /* We want the folio for possible write-upgrade. */ >> + if (!pte_write(pte) && (cp_flags & MM_CP_TRY_CHANGE_WRITABLE)) >> + return true; >> + >> + /* There is nothing to batch. */ >> + if (max_nr_ptes == 1) >> + return false; >> + >> + /* For guaranteed large folios it's usually a win. */ >> + return pte_batch_hint(ptep, pte) > 1; >> +} >> + >> static long change_pte_range(struct mmu_gather *tlb, >> struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, >> unsigned long end, pgprot_t newprot, unsigned long cp_flags) >> @@ -241,16 +260,18 @@ static long change_pte_range(struct mmu_gather *tlb, >> const fpb_t flags = FPB_RESPECT_SOFT_DIRTY | FPB_RESPECT_WRITE; >> int max_nr_ptes = (end - addr) >> PAGE_SHIFT; >> struct folio *folio = NULL; >> - struct page *page; >> + struct page *page = NULL; >> pte_t ptent; >> /* Already in the desired state. */ >> if (prot_numa && pte_protnone(oldpte)) >> continue; >> - page = vm_normal_page(vma, addr, oldpte); >> - if (page) >> - folio = page_folio(page); >> + if (mprotect_wants_folio_for_pte(cp_flags, pte, oldpte, max_nr_ptes)) { >> + page = vm_normal_page(vma, addr, oldpte); >> + if (page) >> + folio = page_folio(page); >> + } >> /* >> * Avoid trapping faults against the zero or KSM >> > > Yes, this is a better version than what I had, I'll take this hunk if you don't mind :) Not at all, thanks for working on this. > Note that it still doesn't handle large folios on !contpte architectures, which > is partly the issue. It should when we really need the folio (write-upgrade, NUMA faults). So I guess the benchmark with THP will still show the benefit (as it does the write upgrade). I suspect some sort of PTE lookahead might work well in > practice, aside from the issues where e.g two order-0 folios that are > contiguous in memory are separately mapped. > > Though perhaps inlining vm_normal_folio() might also be interesting and side-step > most of the issue. I'll play around with that. I'd assume that it could also help fork/munmap() etc. For common architectures with vmemmap, vm_normal_page() is extremely short code. -- Cheers, David