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.133.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 B1CF41E47A8 for ; Fri, 2 May 2025 09:42:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746178948; cv=none; b=cSQsCe9SPNYo59ykiM9AOGbb1wh3Mxs4vMaKs9qHT5tJHQutpzVvjWf/g92QmbvxJWTxoc4V+ZbYGGKT+fC2kStsBrqUosb9fM7dWadUJmxulMmlX0s/we1vrZvOphM6szBvcg/EG1esZAF93OmGx4RveRomxxPiHQD9Q7TW/NE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746178948; c=relaxed/simple; bh=1XFPRM5Am7t3ns0WFWl9l1pKCVZXWCkOlbPnqbj2ITk=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=nAsHIRhiykYcuqnVk/teoOkxpu8SQC49Wk4fgls3TsOPI9nqofRf7QFqr//T3MMirrvoZIwpvOjFqJuLAiksyj9UPCRCLg9io1eWEOTNrRs5Gol9Df0sFoIRhrrbWlpI+Mb9zXkZcrbadq9U2tYBxFUUrEIHiIXqtkFWgp5hm0E= 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=BtV5FZVm; arc=none smtp.client-ip=170.10.133.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="BtV5FZVm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746178945; 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=miNJgwsqELXveVAn/lTZelbfXx6FusXbd6KjeEVrBDU=; b=BtV5FZVmmklh5x6zuDQ8VbXceE6dogJix3luYmDqHVXbrCKr4bxOlp8QqjvOsMIuP2YY/n bfwiQXBKGviclSaycAeGG5Z5zZ0nwY3bDoOn50zqdMTpo/XQ9dLMs1Fi+ksgmDMGBJtl6d OCAhe8ens+5E4WMLeRNnSTdt67AsTW8= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-418-yYYKip_bM068kgrELINOUQ-1; Fri, 02 May 2025 05:42:24 -0400 X-MC-Unique: yYYKip_bM068kgrELINOUQ-1 X-Mimecast-MFC-AGG-ID: yYYKip_bM068kgrELINOUQ_1746178943 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-39130f02631so493027f8f.2 for ; Fri, 02 May 2025 02:42:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746178942; x=1746783742; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=miNJgwsqELXveVAn/lTZelbfXx6FusXbd6KjeEVrBDU=; b=jic9P3Nx8nnmhhJ7630NJOstrG9qV7hr/jUzQHnOCu19XTMJgIhJF6LB2ORpRHGq0S UYSMh+8xSi3D3WpMONE+5lr6eZrZW+FuwctStXe01ZnZJqc48INYMcRTHUbelVjDKVSo MSlaSSikua6r5F/xxS+UvS/kO+QGlmMxqNyOGJVxWeTdvjQyvDvb0dFYz2XKV3XFgawS EnlBRPCd0iWq0xnjIInEE/vLMeyaCFbFjijDDsVGBR57LMM87QuGXTqIccb3aQdCQeDG zN/S+lAfjF6EF+nRZK8YjZfS84YjMnGqkrFBYnFiXSrIqpaXGPZ1ZnncxSVz9UteTGV/ SQLQ== X-Gm-Message-State: AOJu0YyTyzxMUEddfV5nmvs29IS3KvjgCXqdYyGrYTlBjp95vFR5IMNC y7U76PYMY1cYmQAM1LrhZUYxpdCW5r42DxXiEVaTdeF6ZxwrKvIyFx87Eb6lXn+tlks0scUMsDj ZE/0retcd22Y3IzBq0hHqkK54RXFJz0Ng+afLdBBHVskaLIh1Izo7KST7ZFraHhUXX3p37F8w X-Gm-Gg: ASbGncviSGc+Si5abA/6ySzxG9EqNlaexczGwJURyj2mZOAnY/p8acg4IP0fqX2gwQE Fp9vhg4OzjI/p0iAc3g5dHRhDshHIOm6qr2CpktvG5C2x96IwIK6pjsq92KJt2R/c7zXfsAkkY8 spEcJrPWM2w3FiI3c6MWZzG5+uoqO7KFmNy3tpTKl5gc+RUt6nKHqf3AbVqDkiiBWpfBAjCZ+pg U6arANw1G0XHcUdRicJK7ImR1HuZECESJVitakTND4Q6kCq5DF8uLq1yJgY/PyX6Ep+7ncIVCIR lYYV7IHcqQVJ3G4XIKkbIJRKjHMX51fjzNP/3To+9HYd6oVa/gfj40y9eQ2XrImUN3XlpLuIYFX XGfm2LUgm7ubezu5SUi/9gvZiaWJoUp+IOMeardg= X-Received: by 2002:a05:6000:1843:b0:3a0:8c3d:d7ed with SMTP id ffacd0b85a97d-3a099adcf68mr1373591f8f.30.1746178942617; Fri, 02 May 2025 02:42:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqI4s9svPYgaPtQzJzmo7HGast8DY8uxTkT2z8vO9wXKoJLd2K9V5WFPktwNwa5jTxVbpjaQ== X-Received: by 2002:a05:6000:1843:b0:3a0:8c3d:d7ed with SMTP id ffacd0b85a97d-3a099adcf68mr1373569f8f.30.1746178942173; Fri, 02 May 2025 02:42:22 -0700 (PDT) Received: from ?IPV6:2003:cb:c713:d600:afc5:4312:176f:3fb0? (p200300cbc713d600afc54312176f3fb0.dip0.t-ipconnect.de. [2003:cb:c713:d600:afc5:4312:176f:3fb0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a099ae0ca4sm1671063f8f.14.2025.05.02.02.42.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 May 2025 02:42:21 -0700 (PDT) Message-ID: Date: Fri, 2 May 2025 11:42:20 +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 From: David Hildenbrand 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: <2025429144547-aBDmGzJBQc9RMBj--arkamar@atlas.cz> <2025429183321-aBEbcQQY3WX6dsNI-arkamar@atlas.cz> <1df577bb-eaba-4e34-9050-309ee1c7dc57@redhat.com> <202543011526-aBIO5nq6Olsmq2E--arkamar@atlas.cz> <9c412f4f-3bdf-43c0-a3cd-7ce52233f4e5@redhat.com> <202543016037-aBJJJdupFVd_6FTX-arkamar@atlas.cz> <91c4e7e6-e4c2-4ff0-8b13-7b3ff138e98e@redhat.com> <20255174537-aBMmobhpYTFtoONI-arkamar@atlas.cz> <0e029877-4ca9-40f0-933f-6d0779c95d72@redhat.com> Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7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qCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB 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: <0e029877-4ca9-40f0-933f-6d0779c95d72@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 02.05.25 09:37, David Hildenbrand wrote: > On 01.05.25 09:45, Petr Vaněk wrote: >> On Wed, Apr 30, 2025 at 11:25:56PM +0200, David Hildenbrand wrote: >>> On 30.04.25 18:00, Petr Vaněk wrote: >>>> On Wed, Apr 30, 2025 at 04:37:21PM +0200, David Hildenbrand wrote: >>>>> 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? >>>> >>>> Yes, it solves the issue for me. >>> >>> Cool! >>> >>>> >>>> However, maybe it would be better to solve it with the following patch. >>>> The pte_pfn_to_mfn() actually returns the same value for non-present >>>> PTEs. I suggest to return original PTE if the mfn is INVALID_P2M_ENTRY, >>>> rather than empty non-present PTE, but the _PAGE_PRESENT bit will be set >>>> to zero. Thus, we will not loose information about original pfn but it >>>> will be clear that the page is not present. >>>> >>>> From e84781f9ec4fb7275d5e7629cf7e222466caf759 Mon Sep 17 00:00:00 2001 >>>> From: =?UTF-8?q?Petr=20Van=C4=9Bk?= >>>> Date: Wed, 30 Apr 2025 17:08:41 +0200 >>>> Subject: [PATCH] x86/mm: Reset pte _PAGE_PRESENT bit for invalid mft >>>> MIME-Version: 1.0 >>>> Content-Type: text/plain; charset=UTF-8 >>>> Content-Transfer-Encoding: 8bit >>>> >>>> Signed-off-by: Petr Vaněk >>>> --- >>>> arch/x86/xen/mmu_pv.c | 9 +++------ >>>> 1 file changed, 3 insertions(+), 6 deletions(-) >>>> >>>> diff --git a/arch/x86/xen/mmu_pv.c b/arch/x86/xen/mmu_pv.c >>>> index 38971c6dcd4b..92a6a9af0c65 100644 >>>> --- a/arch/x86/xen/mmu_pv.c >>>> +++ b/arch/x86/xen/mmu_pv.c >>>> @@ -392,28 +392,25 @@ static pteval_t pte_mfn_to_pfn(pteval_t val) >>>> static pteval_t pte_pfn_to_mfn(pteval_t val) >>>> { >>>> if (val & _PAGE_PRESENT) { >>>> unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT; >>>> pteval_t flags = val & PTE_FLAGS_MASK; >>>> unsigned long mfn; >>>> >>>> mfn = __pfn_to_mfn(pfn); >>>> >>>> /* >>>> - * If there's no mfn for the pfn, then just create an >>>> - * empty non-present pte. Unfortunately this loses >>>> - * information about the original pfn, so >>>> - * pte_mfn_to_pfn is asymmetric. >>>> + * If there's no mfn for the pfn, then just reset present pte bit. >>>> */ >>>> if (unlikely(mfn == INVALID_P2M_ENTRY)) { >>>> - mfn = 0; >>>> - flags = 0; >>>> + mfn = pfn; >>>> + flags &= ~_PAGE_PRESENT; >>>> } else >>>> mfn &= ~(FOREIGN_FRAME_BIT | IDENTITY_FRAME_BIT); >>>> val = ((pteval_t)mfn << PAGE_SHIFT) | flags; >>>> } >>>> >>>> return val; >>>> } >>>> >>>> __visible pteval_t xen_pte_val(pte_t pte) >>>> { >>> >>> That might do as well. >>> >>> >>> I assume the following would also work? (removing the early ==1 check) >> >> Yes, it also works in my case and the removal makes sense to me. >> >>> It has the general benefit of removing the pte_pfn() call from the >>> loop body, which is why I like that fix. (almost looks like a cleanup) >> >> Indeed, it looks like a cleanup to me as well :) > > Okay, let me polish the patch up and send it out later. Actually, can you polish it up and send it out? I think we need a better description on how exactly that problems happens, in particular involving pte_none(). So stuff from the cover letter should probably be living in here. Feel free to add my Co-developed-by: David Hildenbrand Signed-off-by: David Hildenbrand and stay first author. Thanks! -- Cheers, David / dhildenb