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 CE333C021A4 for ; Mon, 24 Feb 2025 11:27:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 224DD6B007B; Mon, 24 Feb 2025 06:27:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D6286B0083; Mon, 24 Feb 2025 06:27:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 075726B0085; Mon, 24 Feb 2025 06:27:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id DDDB16B007B for ; Mon, 24 Feb 2025 06:27:20 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 84E06A3425 for ; Mon, 24 Feb 2025 11:27:20 +0000 (UTC) X-FDA: 83154612240.25.7171BB9 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by imf14.hostedemail.com (Postfix) with ESMTP id 898F3100007 for ; Mon, 24 Feb 2025 11:27:18 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iOzKuYFV; spf=pass (imf14.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.48 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740396438; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mPK2zF8A/QoLIlZ7H4rce8LDxvSgapo6R5EV/XU26lw=; b=4L4Di5pgbR+15mGRNzHezePPxhfSoJwVvIiNumeO1UMyHjBVC64ZgpbwwgxAe6OiIwfIhI C4CuLRNWthw3LzCHpInzcSyIwJjlvRtpacdNUtBj8IAME76N7iwRGTK1fMfwESeOKKUYS/ KX6KkTNw5+I8wyiniEfcH9g/IZJ3j64= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iOzKuYFV; spf=pass (imf14.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.48 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740396438; a=rsa-sha256; cv=none; b=E8fjmiWGVzfvY92LNCkXoKmJd5v3bCyXLsFa41MQhDn45i1Qjzhu13X0HOr4ckB2L3i6CN yhijcH29tP3AkuGpfBz7zhCDoQPnTiyhz0pCbH/MmOPfuupaUhfRTSpSRmcG4G4B5fmMO9 DAe52GbQOE9sc/WRR+4W3AoDpQmELTk= Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-5e0939c6456so6781228a12.3 for ; Mon, 24 Feb 2025 03:27:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740396437; x=1741001237; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mPK2zF8A/QoLIlZ7H4rce8LDxvSgapo6R5EV/XU26lw=; b=iOzKuYFVyeo17LP1Wnxt5PgVpCyvSvH2rMm9SZykQdH2whFurHytf0aWmb/zDEWzWO GITuJ4HsZEiQSrfmhHj/aE4aiFBa3+d8UEJ0wBu5LRHjBF+Sv6nzOF1CYTf891fRcXlv gAquKoiydwgfiaC4H/1k8VpGnUwN/zD1JqQjDHCU5TumFfaN9hCGn07f17NRTevFiWEq jgAlQNsbdZYOv9shY/WYWglWO+FmPC1FLjPff9g9jHfnqxgb7v+/U6vRW3yiLIpnzfl8 azeUoJ00bJSQCLYOXY+FUD8/EdNl65iNk0aHbFMuJpUOPyp5gttk6EeNg8IVNPf+yrm0 DO0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740396437; x=1741001237; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mPK2zF8A/QoLIlZ7H4rce8LDxvSgapo6R5EV/XU26lw=; b=wxrtDsIdm+qVqa/MqyQTu3c7fuohQmIrBSVAYU9yxzT+/JAwyztfR345bdvKu3nTR6 uNm+9rHZxuMiPxD0qCaghj7Mjc23XrHkxBHcKKH6pjSUBfq1iO/COhM17JooEOIuh7ai rq3niTL+mB98XUNSbPKBpaZQBMjCO0imjg06PjZ0VYG/VEkOq3pI+8fmgSitR60YXOvH tBJu947m9Nb/bYrZKkwYDEVqWqd/UBgAKD+BuUqEhSgI9+opkMiHXQkYgxKV1a4tvYRQ d97MvZJN9ESDoFxjiYuPP8XsdHoJzQOxdjGVjAtBIfpm2FuQBFWd3dKIM9mje9Qm2fdb YAww== X-Forwarded-Encrypted: i=1; AJvYcCVsJ8ixHa7FOPEZRKflBtvbOUHaOwwa2/qmB+TmwyyNfuj8RpQYQFTwKhGDshAKnjxa+GFm48Pg9w==@kvack.org X-Gm-Message-State: AOJu0YyvThLhCyI6i4hK4kdY75f7C38wvgtW3+spFbuLJYFaM7tdN/Wd SZAvHtxAKY8KjtyL5c7TWf6pPKmSAtxImfpmJ4E/tn5xWSgqh93Xj20yuzzleK4jcIzzRo5IrfF hdkpqbvDH56LkhNKe8nXXnIjm3yDdAxxZee2Dwg== X-Gm-Gg: ASbGncvt1ulqHnt8bFkA/aaXUDfD5GL4+20fxDbwWMyJRsS5CKzFWnlFWSeEFxt3afl IKWf+p070fdeMxZv6AuysfNDIlC0f766K7aQuYWPlPYCl05RC6tpkAz4kClEUrtxwi4hhnFqDIL yiM5ge4m0= X-Google-Smtp-Source: AGHT+IFzHPiP7HgEi1iLeAsUfPrgcPiwkLzFkdvMUmpitpPx5I5iW5pGV+bPYBd/LufmeWzu4SPrwChI7bwpkBQUkUA= X-Received: by 2002:a05:6402:274b:b0:5dc:db28:6afc with SMTP id 4fb4d7f45d1cf-5e0b6fdf732mr13463545a12.0.1740396436756; Mon, 24 Feb 2025 03:27:16 -0800 (PST) MIME-Version: 1.0 References: <20250217140419.1702389-1-ryan.roberts@arm.com> <20250217140419.1702389-3-ryan.roberts@arm.com> In-Reply-To: <20250217140419.1702389-3-ryan.roberts@arm.com> From: Alexandre Ghiti Date: Mon, 24 Feb 2025 12:27:03 +0100 X-Gm-Features: AWEUYZkGhzPlXZwydhM9MLmyrXrPvhtmhDQdRWEVf0EDYo_eOVstS6UzcJ92dwo Message-ID: Subject: Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes To: Ryan Roberts Cc: Catalin Marinas , Will Deacon , 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 , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 898F3100007 X-Stat-Signature: n6thjqhoc54pqgm53oite6z7cryh594q X-HE-Tag: 1740396438-18753 X-HE-Meta: U2FsdGVkX1/ikIcPC5zslsSokWWoS0uvWEXEBk/Ket9wGT+vlH4epZS1isJGthneE00XhrkDWn+G9u5Ma2toeCda0BY9NckqJMqXvCefEHpqVDbJhGN614CS5xT7pkoyjNeKCIZYsGERrvUnfqmXVj8Qymj9SiwxLFkFYna9keThtn/1PkRBmbZci+uzjxH3ZDuAoyjJCGsASzGt2jm3s78z1/JsyTbB2rNaDPsp8dl1SxkCiyFWHEfsCd1FoigAGKNRdqMVGeVS3zkDafOj1GkUNmoPXbtX5WoIYzn62cxaHPztHUzZy4ltOt2W/Sw4XEO+5bLdCDK2Ya/TKp/pyD7NrqVCqmzjCS7n3pFvrnn8YOoeOPJOlrRnoLW/1GNuVm2nU+QuB6IkI2lugwtq5DkHDZo7RVmXAMdwSKYxd77UaMQYWPgrw7vMUH/AQ5nt3nwXor7d/El4DaVE9vKJ8wSNqzG8mGLdiDTKlPukqhNUwcKOnVFpvKmcVlGV9BI3g9Qtz7af0/z+Em9C66HOYb3h+T1XXT6KLkwPUcxNdQ8ZcUh0PcnXQSu4yZNX+X/NSBFoalS9wEYfVYd+0vmRfaRCt3+YZ4qe+82QP6dPQJlTn6JWFH5IZ5p0AIRgy12Wc9hUvsSdptClSIzrir2cGyvl/zynL0iD4kK6CMEqNPdF6MYglf2mZ45av61ji/EZKd4sA9HyZZRkCCv2qyUk/tiTX9S4z1+OftKFLb45YpBIjBRuI4hZhA9pXG+Ki1guFXh+GoVpwjK3Qq/dPKJ0ytLUmJ9SmGE2WF2DTjzIp4/0l4e5n+fgTe9RobAXfzE+a/p7C20buL9MVABai4eYK3sHjdU/P+iluPxE/H8k+QTavgRYXx2tzNNiCXXj62NdMJqvL0AW6+J5CTaR9ZCtIwOXj6PpUQ89HUKt5Ja/yND/bh8tGhVL1D73AbP9Qon+GAc5X9VfXd4mGTSaXEQ BzOFVR2a s+YG/6ZG0niWZ3wxS7bVYfVxdV31hXHvqFHgukdVUdhemV05izGuwXqUpjIkVji68DxxrijfbQ50557YlYwnCP7nkuLkwQE0syUqaiPKdICfuhZJnh7gCsgmcs/gcIRIhc68JoqQ5o+ijPprOgmaj2uCwvb6oIfkP8aMdHhWVdtfoty2N7A5A/2bK+vuHBYxADRY0ACtLO9XXU0NhVACcebLr3F5++Q/Zyn7pELw8a/egpEb6dI5ogbdsedgQK3bYXiSSNdoABEJvBbp95k6lJi2faLKst3n1dTqt1iDC4BRvE4J4n84OjeQ2XTB94f6Hng+x8C4AX9QRqL15XQAW47W+2JfhPZ6RkD5JjuSdqafR4JarNetV7dx9Lg== 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: Hi Ryan, On Mon, Feb 17, 2025 at 3:04=E2=80=AFPM 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 =3D __ptep_get(ptep); > - unsigned long i; > - > - for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) { > - pte_t pte =3D __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 =3D pte_mkdirty(orig_pte); > - > - if (pte_young(pte)) > - orig_pte =3D pte_mkyoung(orig_pte); > + pte_t pte, tmp_pte; > + bool present; > + > + pte =3D __ptep_get_and_clear(mm, addr, ptep); > + present =3D pte_present(pte); > + while (--ncontig) { > + ptep++; > + addr +=3D pgsize; > + tmp_pte =3D __ptep_get_and_clear(mm, addr, ptep); > + if (present) { > + if (pte_dirty(tmp_pte)) > + pte =3D pte_mkdirty(pte); > + if (pte_young(tmp_pte)) > + pte =3D pte_mkyoung(pte); > + } > } > - return orig_pte; > + return pte; > } > > static pte_t get_clear_contig_flush(struct mm_struct *mm, > @@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, = unsigned long addr, > { > int ncontig; > size_t pgsize; > - pte_t orig_pte =3D __ptep_get(ptep); > - > - if (!pte_cont(orig_pte)) > - return __ptep_get_and_clear(mm, addr, ptep); > - > - ncontig =3D find_num_contig(mm, addr, ptep, &pgsize); > > + ncontig =3D num_contig_ptes(sz, &pgsize); > return get_clear_contig(mm, addr, ptep, pgsize, ncontig); > } > > -- > 2.43.0 > Thanks for ccing me on this fix, we have the same issue (in huge_pte_clear() too) in riscv. I'll come up with a fix and wait for you guys to merge this. That only comforts my idea to merge both riscv and arm64 implementations :) Thanks again, Alex