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 6B52A266B55 for ; Fri, 2 May 2025 07:37:49 +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=1746171473; cv=none; b=a6dYVZGyf7CTBT+aVp+ZHJKacXMILhubuA1r7tMHinmndfFDNgoMAyIvQ5v274SM13f2Nqyl7LA8Gj+96tPgb/0xOQx6IZVTrHoJZ6G3OCfGjFuZ4Ih/V/QkcHqnkC+/gFNMH2QhdYonqtjYFuFx4EuOyuKCORtE79P5bKyZnAk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746171473; c=relaxed/simple; bh=Un8AN6+W7hdXCGs5FakehGp8gQb8LyaMepwlot1T11k=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=elAFR9Y/5IvZ6uZAvzd5ObiNKTBYPSoodGjJywyQ+fN32wntPFtXdEH0qq6xdNNwiHT5hP2Md8CqGCgBa0RO+uOttL5ewWybt9cqaciR2GxeY1ELLqfHH1KS96Qs4stz/kTmo9ksPsr5rFSv68zHGeoUl85Pl217T1r5rEiGjYs= 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=YztEvjP8; 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="YztEvjP8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746171468; 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=q87iNuAOCQ7MPJb31XTFDqu25SSlsYHKaJtmZwAFnHA=; b=YztEvjP8iF6hSiNEVLqq/WR8QhPRXkav2esRMvv37iFXC+5ZSznA4rjFzvVlchOf2otsVQ IvQMEBEWPWTfbMM7XQp2cCaRq9UTIjDzJAXjpC3Qp7CGWHNA+Q+zOPc3EGcEikOH23Wfmt nUzaAhXX9ZghEmeJf+DZwtrr6OHO5rI= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595--GmbhYcMNnaUpKPyKetoQA-1; Fri, 02 May 2025 03:37:47 -0400 X-MC-Unique: -GmbhYcMNnaUpKPyKetoQA-1 X-Mimecast-MFC-AGG-ID: -GmbhYcMNnaUpKPyKetoQA_1746171466 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3912a0439afso462035f8f.3 for ; Fri, 02 May 2025 00:37:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746171466; x=1746776266; 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=q87iNuAOCQ7MPJb31XTFDqu25SSlsYHKaJtmZwAFnHA=; b=o5GErN2KZokCIqt1hjP3JP6PzQM50RwWWlO5GANZIeUwYhDpSWcn8Oy6TCX1Q1iYwn KWQ7KT2ddrJzuQ9xzHWCOagTaT51zEWX2nYKVJKcFmdHNtDfpw9wZtNcIKfAJLFXluEi vG5Fo022uCRwMAEZx0zC0OS5++rSFQWTcfn+IvrqGVEJCNNnarPY2yJTGaiB3tHCV/Z6 W4DXC2cxkjCeBGn1P5hurVmDuBFsdZHopWRbrZLYgyUqNlgVUfjpIUY0uROT+h4ZFta4 i37q2aq4JORBiq8GZuaKSa7Dfo9sJSSB67/0IQe6Ey8hEDn07Jr95D9AZFCUwNsE1bmw BM3w== X-Gm-Message-State: AOJu0YzInfp486vM+SYEzFXAbce6opph9h40n+q9SFT1eeelKa9rhkUf ESZ6LYU9zVuHSq7P/lSga7abbQnMvKaT+LleyQhDF2xDyUuxMzXowO6QlwGdDPKM0gRJULSTj5g RjwRJgb8EcR2G3NtaABbFnIGxh9k53xmCIS/APgGkO6KN6nSo83l7FTXNiafuMtKsLNJbGyn0 X-Gm-Gg: ASbGnct29+/+i7c/Ldb5lOiQYbG/xJuyeyH14I/D5TH5PcT8Thb/vpb7AKRXF5Twl6m HwiPP3f/UtborM8TOI09LbgG5g/KyhJnRTPtyJdhERgwAEHpEBbL/0Ex+siBPqwoHPkDVk0+W3V u4ZAN7bNVLmJzcg88KvKpiSakxJIy5TvrEtMHr4iFKbd/wnB+wZuXHfMNX8WU5HqGizgYTZC/Ol qvZbHTfK+Px2e7sum4kZCpaW4kFrNEnIIpjwWzKF1kSzfUiNY8pZXxeH3PV3xOQFaWjeqYwySNE dbmNvrUmjLtOJjOLjqQ1gdY9h9iq+h7MhdBSjjpcIlunWMvjjg+0bKuHyOcwYWblt8+YZwnGlx+ +WnO6pLNwmXzvxYXKitkDXHpPZSHac7/zD1zBhqI= X-Received: by 2002:a05:6000:18ac:b0:39c:3107:c503 with SMTP id ffacd0b85a97d-3a099ade134mr1118195f8f.31.1746171465781; Fri, 02 May 2025 00:37:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6LK4gg7MWrcfnIs9ZPGWqEpStvztLzi4/OGUyN+TyGu/HXImDYYR7x2Rv5Mj4MnOzp9ctyw== X-Received: by 2002:a05:6000:18ac:b0:39c:3107:c503 with SMTP id ffacd0b85a97d-3a099ade134mr1118171f8f.31.1746171465370; Fri, 02 May 2025 00:37:45 -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-3a099ae0bb5sm1349695f8f.7.2025.05.02.00.37.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 May 2025 00:37:44 -0700 (PDT) Message-ID: <0e029877-4ca9-40f0-933f-6d0779c95d72@redhat.com> Date: Fri, 2 May 2025 09:37:44 +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: <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> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0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9XQssfrf4Gusdqx8nPEpfOqCtbbwJMATbHyqLt7/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: <20255174537-aBMmobhpYTFtoONI-arkamar@atlas.cz> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. > > I am still considering if it would make sense to send both patches, I am > not sure if reseting _PAGE_PRESENT flag is enough, because of swapping > or other areas which I am not aware of. The problem I'm having with pte_mfn_to_pfn() updates is that I don't understand what the expected output is even supposed to be. You should probably ask XEN folks (I realized that they are not CCed). -- Cheers, David / dhildenb