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 7DB7BC021B3 for ; Fri, 21 Feb 2025 15:32:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC133280004; Fri, 21 Feb 2025 10:32:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6F58280001; Fri, 21 Feb 2025 10:32:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE894280004; Fri, 21 Feb 2025 10:32:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id AE98B280001 for ; Fri, 21 Feb 2025 10:32:09 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 73E85B69B8 for ; Fri, 21 Feb 2025 15:32:09 +0000 (UTC) X-FDA: 83144342778.01.5D00853 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf04.hostedemail.com (Postfix) with ESMTP id BA1A040016 for ; Fri, 21 Feb 2025 15:32:07 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=syu33c5F; spf=pass (imf04.hostedemail.com: domain of will@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=will@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740151927; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NWKt11fIsoIIrCyQG6SzXtGXYwdoo6CSAzMZiNAYnp0=; b=FxPjmADYAHeImBTqyEUnoMcXNYBwfYJ2KPTAFy377wRuga85JstApZYuuI/1RojHpY1mOR 6mcaDCl2he16ymlj7tdHW+YzRyIoCmpqNA5Z6KltWUY8GdWQJx0SHL5G7zrZsoVvWJhOMD 18MtnhTNeFD+RB6K3r5kTvQxfc9BhI0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740151927; a=rsa-sha256; cv=none; b=gsxLqR45NPqb5KFjk9UG89OsZt8UR4a+72ouTzgUQd7ufMF0HS5H+UG6KcYUeqyVgzseSk I0ZXwhD67lFR78pjDiybBCJl3m+somlVVLh8J2HDlvsiBmO/jGEVIxVr9wvpouAG/Yp2wI jWysCm18PcO/7gbD4oSWfpmzBku9g8A= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=syu33c5F; spf=pass (imf04.hostedemail.com: domain of will@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=will@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 8527D613E9; Fri, 21 Feb 2025 15:32:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51861C4CED6; Fri, 21 Feb 2025 15:32:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740151926; bh=2XaCu9je9IA765r/EB4HdTVXelllIRMjNPyGsqdE/Ic=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=syu33c5FDQHVtMcEcWdynEHBHHQzJPgLhyfB++zvelhIsmLJ2XmDlU7oMDTLeMVwc RV3Z30p9d0YCTA15XIqmfev+ds6Gzy0+nboSXX1xyuUQDxIqnlxxNAGbVbYCpTfG6/ nyskhslJCl5e9kDaO9HoUtZAIcdJJhBdE1hMld+VMc3YNW5pZcqQEpGd9LraPAKYIm GggZVSa1UN4kMkRbcNlO7jkTys4qzvFW3RSZdWaOpnC70Pw9uiJbR6HnQKS2wIfLtK CiEwckQny+xGxD9LpTWSz6M7XtTTJFkuX34BXhlP04kE28YPDHGtiBgP2fV6SWXo4H UJa/7XqpMI+Xg== Date: Fri, 21 Feb 2025 15:31:56 +0000 From: Will Deacon To: Ryan Roberts Cc: Catalin Marinas , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Gerald Schaefer , "David S. Miller" , Andreas Larsson , Arnd Bergmann , Muchun Song , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , David Hildenbrand , "Matthew Wilcox (Oracle)" , Mark Rutland , Anshuman Khandual , Dev Jain , Kevin Brodsky , Alexandre Ghiti , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes Message-ID: <20250221153156.GC20567@willie-the-truck> References: <20250217140419.1702389-1-ryan.roberts@arm.com> <20250217140419.1702389-3-ryan.roberts@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250217140419.1702389-3-ryan.roberts@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Rspam-User: X-Rspamd-Queue-Id: BA1A040016 X-Rspamd-Server: rspam07 X-Stat-Signature: h8wcyyysp5e8os6o7r1u5abs8738wuoc X-HE-Tag: 1740151927-939055 X-HE-Meta: U2FsdGVkX18rMjNGd6ZEonDSTEySwFM6hudl1wwAWeQ+SDH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PNVwaxiqbqlNrnNwYft6iPAXCoeFBleUQma+cXELBOQ3yM1/x0ENMnFGLQJv5szCWbSXEVFuftV+rnM0LZ7GTfjjevv2VWoNpE9xMtEZiUKBIQ2ivwfeGCoY3kBaBJiGuY47iMllPRJ2uptSTdR5w0Ms6O9Owd8X+df82KCnA8CLpOPVzSEc+YcDBO457zrX8uQTHHMd/Vp9MNVI5mzEAJ6+8FoLXhO0Ma5sSSer/LaKQU 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 Mon, Feb 17, 2025 at 02:04:15PM +0000, Ryan Roberts wrote: > arm64 supports multiple huge_pte sizes. Some of the sizes are covered by > a single pte entry at a particular level (PMD_SIZE, PUD_SIZE), and some > are covered by multiple ptes at a particular level (CONT_PTE_SIZE, > CONT_PMD_SIZE). So the function has to figure out the size from the > huge_pte pointer. This was previously done by walking the pgtable to > determine the level and by using the PTE_CONT bit to determine the > number of ptes at the level. > > But the PTE_CONT bit is only valid when the pte is present. For > non-present pte values (e.g. markers, migration entries), the previous > implementation was therefore erroniously determining the size. There is > at least one known caller in core-mm, move_huge_pte(), which may call > huge_ptep_get_and_clear() for a non-present pte. So we must be robust to > this case. Additionally the "regular" ptep_get_and_clear() is robust to > being called for non-present ptes so it makes sense to follow the > behaviour. > > Fix this by using the new sz parameter which is now provided to the > function. Additionally when clearing each pte in a contig range, don't > gather the access and dirty bits if the pte is not present. > > An alternative approach that would not require API changes would be to > store the PTE_CONT bit in a spare bit in the swap entry pte for the > non-present case. But it felt cleaner to follow other APIs' lead and > just pass in the size. > > As an aside, PTE_CONT is bit 52, which corresponds to bit 40 in the swap > entry offset field (layout of non-present pte). Since hugetlb is never > swapped to disk, this field will only be populated for markers, which > always set this bit to 0 and hwpoison swap entries, which set the offset > field to a PFN; So it would only ever be 1 for a 52-bit PVA system where > memory in that high half was poisoned (I think!). So in practice, this > bit would almost always be zero for non-present ptes and we would only > clear the first entry if it was actually a contiguous block. That's > probably a less severe symptom than if it was always interpretted as 1 > and cleared out potentially-present neighboring PTEs. > > Cc: stable@vger.kernel.org > Fixes: 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit") > Signed-off-by: Ryan Roberts > --- > arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- > 1 file changed, 17 insertions(+), 23 deletions(-) > > diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c > index 06db4649af91..614b2feddba2 100644 > --- a/arch/arm64/mm/hugetlbpage.c > +++ b/arch/arm64/mm/hugetlbpage.c > @@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm, > unsigned long pgsize, > unsigned long ncontig) > { > - pte_t orig_pte = __ptep_get(ptep); > - unsigned long i; > - > - for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) { > - pte_t pte = __ptep_get_and_clear(mm, addr, ptep); > - > - /* > - * If HW_AFDBM is enabled, then the HW could turn on > - * the dirty or accessed bit for any page in the set, > - * so check them all. > - */ > - if (pte_dirty(pte)) > - orig_pte = pte_mkdirty(orig_pte); > - > - if (pte_young(pte)) > - orig_pte = pte_mkyoung(orig_pte); > + pte_t pte, tmp_pte; > + bool present; > + > + pte = __ptep_get_and_clear(mm, addr, ptep); > + present = pte_present(pte); > + while (--ncontig) { > + ptep++; > + addr += pgsize; > + tmp_pte = __ptep_get_and_clear(mm, addr, ptep); > + if (present) { > + if (pte_dirty(tmp_pte)) > + pte = pte_mkdirty(pte); > + if (pte_young(tmp_pte)) > + pte = pte_mkyoung(pte); > + } > } nit: With the loop now structured like this, we really can't handle num_contig_ptes() returning 0 if it gets an unknown size. Granted, that really shouldn't happen, but perhaps it would be better to add a 'default' case with a WARN() to num_contig_ptes() and then add an early return here? Will