From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 A8ED025E459 for ; Wed, 30 Apr 2025 14:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746023851; cv=none; b=u6BG9+1E3KJbMYp3DQBRrXglIMhCEMwvKxCuXlwpw3Jbe4m1vHdf5WT2NAnmlFwMFrFNtwYmqvhM88gVftCAQw8kGCj5M38GSF+oYZfTgYgdTZdh/Ufm7QEsYt0vnp9/a5Vc0X5P5+wY67r2neoCawl5H7i+7xsFUZpiLQ/I0wo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746023851; c=relaxed/simple; bh=n1AgU7Esz7OmbJ0mkpQ7ynDk3NIgF7onvADsHHENBB8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=E6D1HJcKmAfppjX8MDWrrD1/9jAHDtaatXI9iri+N1Yt8UuEqYQhKYalFq3Rer3jO2A61Wuda/XAvFd+9/om69qZ7zqLpa3UbL0nKB47obJWcW1IUM9BWdp80iXknoLqrCzIAz77VBhAkU7z7r63bhjVRZsmE37g4NjiJf1QbjE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JEL4mfxE; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JEL4mfxE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746023847; 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:autocrypt:autocrypt; bh=MnkXjFY3TEcL0dXqwM2Thq4YlP1rbhLrXBXsYqEQ+Ro=; b=JEL4mfxEsQdoXlE8khJhFPDrtfd+e347c13cj97fsVKLLalgMWSX7xjhRXVzjJvF1fgtcp 7LwWcwyS9Oku98iJM55G14EWAguwYgcljNbmtHYRuteWy9ngm2N6Nl62NsfVev8s3jodjd QKc/FYApOaJeJ2p8Rx7O7FPHEF8RV7g= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-671-a8TgBznVMXuvQ65I3zS20w-1; Wed, 30 Apr 2025 10:37:24 -0400 X-MC-Unique: a8TgBznVMXuvQ65I3zS20w-1 X-Mimecast-MFC-AGG-ID: a8TgBznVMXuvQ65I3zS20w_1746023844 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-39c2da64df9so3290323f8f.0 for ; Wed, 30 Apr 2025 07:37:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746023843; x=1746628643; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=MnkXjFY3TEcL0dXqwM2Thq4YlP1rbhLrXBXsYqEQ+Ro=; b=BLHEG0Y48a0fOsYZWUHXVJC0K4Q5/dVLkWBAKwNf6+yBDgvQbbQmeQaKM1WGreh+cP 5ef0M4SMxw02e9sW8jxx6CpEThEPGL09WaXyDH5oBmj4MaKa2vs1Ztoy7NH59MeecyWK IPCDifiUmMIXYTrFe2txWiV7wt2/wMk9A21j2BzD1pcBHNA+07P2XYGs0cRUcyxUvXAa lnKBU4TEeI/GxdtLGrQj0WTDDXYi+rHnhMa9Mz1Ez/ynLbsmMqC8Zsi5PNgvXPa1y8Um bM1X7z0DbNYiswevC/dmDw9z4d4EWTkJzU+lcnc2lr56LVkirYrp0PgbnkqFjTWvlhzv AVwA== X-Gm-Message-State: AOJu0YzIwA1AQDyrLlyqeaN3xANtrESJiybUbRuDrGz2okAY36we4q2R fGxvjMmYKMs20vbVX2RkFl8upCrlGkH8s2t4qxZjAnV/63AowBT781UcxOP/2HrqMwhYBVLjoxL VHzweeSJ9cTHM7Q7MxoFCb+ZNjjSPgfK01RHoUrVokTY6lvqbGOijzG4Ev8uamgirKK56ctXbeU 4= X-Gm-Gg: ASbGncuDIFJ+bScNcRqpDNMAlc5Ja5wqXUDJkcGJC9lXY5G4LNd4JCblCCEa6sMLQMx j42DmbwFwE+Fr5+pBrtDzc/yBolnPF7GHkMe5tX49/04s5Ucwwq+EQ4eiyxMN1hUfNNeHqEfTtr MmcDBYwkUzwSC04lkuMgW4cANXgKVwBqxWRzQPEcK7VSoX+eW99uw3zeho/P0LtuLmgc0/kr7Ek Y96Mt6ZYotPbdh4YQOIly1kVWuMQcf1NPRYVbU73w6ZReEsW4/p9hQOa4T3fEwJNO3JOCA1JH3b hqcPbMd/DBfnYicCmh+62GSRxBJ7xgSW7xsMky4to0m4Jm1y X-Received: by 2002:a05:6000:2481:b0:3a0:80bf:b4e3 with SMTP id ffacd0b85a97d-3a08ff50967mr2212403f8f.58.1746023843583; Wed, 30 Apr 2025 07:37:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwrkb+QAgrfKTfZCLUpqNq2im1MtwJnU5K+x0x8Nke1bFEBVj3+6QE5/TBxK5sBPrLyYpM9g== X-Received: by 2002:a05:6000:2481:b0:3a0:80bf:b4e3 with SMTP id ffacd0b85a97d-3a08ff50967mr2212365f8f.58.1746023843092; Wed, 30 Apr 2025 07:37:23 -0700 (PDT) Received: from [192.168.178.21] (tmo-081-40.customers.d1-online.com. [80.187.81.40]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a073e5d52bsm17672837f8f.90.2025.04.30.07.37.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Apr 2025 07:37:22 -0700 (PDT) Message-ID: <9c412f4f-3bdf-43c0-a3cd-7ce52233f4e5@redhat.com> Date: Wed, 30 Apr 2025 16:37:21 +0200 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: [PATCH 1/1] mm: Fix folio_pte_batch() overcount with zero PTEs To: =?UTF-8?Q?Petr_Van=C4=9Bk?= Cc: linux-kernel@vger.kernel.org, Andrew Morton , Ryan Roberts , linux-mm@kvack.org, stable@vger.kernel.org References: <20250429142237.22138-1-arkamar@atlas.cz> <20250429142237.22138-2-arkamar@atlas.cz> <2025429144547-aBDmGzJBQc9RMBj--arkamar@atlas.cz> <2025429183321-aBEbcQQY3WX6dsNI-arkamar@atlas.cz> <1df577bb-eaba-4e34-9050-309ee1c7dc57@redhat.com> <202543011526-aBIO5nq6Olsmq2E--arkamar@atlas.cz> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtB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1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <202543011526-aBIO5nq6Olsmq2E--arkamar@atlas.cz> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 30.04.25 13:52, Petr Vaněk wrote: > On Tue, Apr 29, 2025 at 08:56:03PM +0200, David Hildenbrand wrote: >> On 29.04.25 20:33, Petr Vaněk wrote: >>> On Tue, Apr 29, 2025 at 05:45:53PM +0200, David Hildenbrand wrote: >>>> On 29.04.25 16:52, David Hildenbrand wrote: >>>>> On 29.04.25 16:45, Petr Vaněk wrote: >>>>>> On Tue, Apr 29, 2025 at 04:29:30PM +0200, David Hildenbrand wrote: >>>>>>> On 29.04.25 16:22, Petr Vaněk wrote: >>>>>>>> folio_pte_batch() could overcount the number of contiguous PTEs when >>>>>>>> pte_advance_pfn() returns a zero-valued PTE and the following PTE in >>>>>>>> memory also happens to be zero. The loop doesn't break in such a case >>>>>>>> because pte_same() returns true, and the batch size is advanced by one >>>>>>>> more than it should be. >>>>>>>> >>>>>>>> To fix this, bail out early if a non-present PTE is encountered, >>>>>>>> preventing the invalid comparison. >>>>>>>> >>>>>>>> This issue started to appear after commit 10ebac4f95e7 ("mm/memory: >>>>>>>> optimize unmap/zap with PTE-mapped THP") and was discovered via git >>>>>>>> bisect. >>>>>>>> >>>>>>>> Fixes: 10ebac4f95e7 ("mm/memory: optimize unmap/zap with PTE-mapped THP") >>>>>>>> Cc: stable@vger.kernel.org >>>>>>>> Signed-off-by: Petr Vaněk >>>>>>>> --- >>>>>>>> mm/internal.h | 2 ++ >>>>>>>> 1 file changed, 2 insertions(+) >>>>>>>> >>>>>>>> diff --git a/mm/internal.h b/mm/internal.h >>>>>>>> index e9695baa5922..c181fe2bac9d 100644 >>>>>>>> --- a/mm/internal.h >>>>>>>> +++ b/mm/internal.h >>>>>>>> @@ -279,6 +279,8 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, >>>>>>>> dirty = !!pte_dirty(pte); >>>>>>>> pte = __pte_batch_clear_ignored(pte, flags); >>>>>>>> >>>>>>>> + if (!pte_present(pte)) >>>>>>>> + break; >>>>>>>> if (!pte_same(pte, expected_pte)) >>>>>>>> break; >>>>>>> >>>>>>> How could pte_same() suddenly match on a present and non-present PTE. >>>>>> >>>>>> In the problematic case pte.pte == 0 and expected_pte.pte == 0 as well. >>>>>> pte_same() returns a.pte == b.pte -> 0 == 0. Both are non-present PTEs. >>>>> >>>>> Observe that folio_pte_batch() was called *with a present pte*. >>>>> >>>>> do_zap_pte_range() >>>>> if (pte_present(ptent)) >>>>> zap_present_ptes() >>>>> folio_pte_batch() >>>>> >>>>> How can we end up with an expected_pte that is !present, if it is based >>>>> on the provided pte that *is present* and we only used pte_advance_pfn() >>>>> to advance the pfn? >>>> >>>> I've been staring at the code for too long and don't see the issue. >>>> >>>> We even have >>>> >>>> VM_WARN_ON_FOLIO(!pte_present(pte), folio); >>>> >>>> So the initial pteval we got is present. >>>> >>>> I don't see how >>>> >>>> nr = pte_batch_hint(start_ptep, pte); >>>> expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); >>>> >>>> would suddenly result in !pte_present(expected_pte). >>> >>> The issue is not happening in __pte_batch_clear_ignored but later in >>> following line: >>> >>> expected_pte = pte_advance_pfn(expected_pte, nr); >>> >>> The issue seems to be in __pte function which converts PTE value to >>> pte_t in pte_advance_pfn, because warnings disappears when I change the >>> line to >>> >>> expected_pte = (pte_t){ .pte = pte_val(expected_pte) + (nr << PFN_PTE_SHIFT) }; >>> >>> The kernel probably uses __pte function from >>> arch/x86/include/asm/paravirt.h because it is configured with >>> CONFIG_PARAVIRT=y: >>> >>> static inline pte_t __pte(pteval_t val) >>> { >>> return (pte_t) { PVOP_ALT_CALLEE1(pteval_t, mmu.make_pte, val, >>> "mov %%rdi, %%rax", ALT_NOT_XEN) }; >>> } >>> >>> I guess it might cause this weird magic, but I need more time to >>> understand what it does :) > > I understand it slightly more. __pte() uses xen_make_pte(), which calls > pte_pfn_to_mfn(), however, mfn for this pfn contains INVALID_P2M_ENTRY > value, therefore the pte_pfn_to_mfn() returns 0, see [1]. > > I guess that the mfn was invalidated by xen-balloon driver? > > [1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/xen/mmu_pv.c?h=v6.15-rc4#n408 > >> What XEN does with basic primitives that convert between pteval and >> pte_t is beyond horrible. >> >> How come set_ptes() that uses pte_next_pfn()->pte_advance_pfn() does not >> run into this? > > I don't know, but I guess it is somehow related to pfn->mfn translation. > >> Is it only a problem if we exceed a certain pfn? > > No, it is a problem if the corresponding mft to given pfn is invalid. > > I am not sure if my original patch is a good fix. No :) Maybe it would be > better to have some sort of native_pte_advance_pfn() which will use > native_make_pte() rather than __pte(). Or do you think the issue is in > Xen part? I think what's happening is that -- under XEN only -- we might get garbage when calling pte_advance_pfn() and the next PFN would no longer fall into the folio. And the current code cannot deal with that XEN garbage. But still not 100% sure. The following is completely untested, could you give that a try? I might find some time this evening to test myself and try to further improve it. From 7d4149a5ea18cba6a694946e59efa9f51d793a4e Mon Sep 17 00:00:00 2001 From: David Hildenbrand Date: Wed, 30 Apr 2025 16:35:12 +0200 Subject: [PATCH] tmp Signed-off-by: David Hildenbrand --- mm/internal.h | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index e9695baa59226..a9ea7f62486ec 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -248,11 +248,9 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, bool *any_writable, bool *any_young, bool *any_dirty) { - unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); - const pte_t *end_ptep = start_ptep + max_nr; pte_t expected_pte, *ptep; bool writable, young, dirty; - int nr; + int nr, cur_nr; if (any_writable) *any_writable = false; @@ -265,11 +263,17 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio); + /* Limit max_nr to the actual remaining PFNs in the folio. */ + max_nr = min_t(unsigned long, max_nr, + folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte)); + if (unlikely(max_nr == 1)) + return 1; + nr = pte_batch_hint(start_ptep, pte); expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); ptep = start_ptep + nr; - while (ptep < end_ptep) { + while (nr < max_nr) { pte = ptep_get(ptep); if (any_writable) writable = !!pte_write(pte); @@ -282,14 +286,6 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, if (!pte_same(pte, expected_pte)) break; - /* - * Stop immediately once we reached the end of the folio. In - * corner cases the next PFN might fall into a different - * folio. - */ - if (pte_pfn(pte) >= folio_end_pfn) - break; - if (any_writable) *any_writable |= writable; if (any_young) @@ -297,12 +293,13 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, if (any_dirty) *any_dirty |= dirty; - nr = pte_batch_hint(ptep, pte); - expected_pte = pte_advance_pfn(expected_pte, nr); - ptep += nr; + cur_nr = pte_batch_hint(ptep, pte); + expected_pte = pte_advance_pfn(expected_pte, cur_nr); + ptep += cur_nr; + nr += cur_nr; } - return min(ptep - start_ptep, max_nr); + return min(nr, max_nr); } /** -- 2.49.0 -- Cheers, David / dhildenb