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 2E0C2C021AA for ; Wed, 19 Feb 2025 08:46:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE8C328020F; Wed, 19 Feb 2025 03:46:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A71A628020C; Wed, 19 Feb 2025 03:46:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 912F928020F; Wed, 19 Feb 2025 03:46:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7168328020C for ; Wed, 19 Feb 2025 03:46:03 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2573FB364C for ; Wed, 19 Feb 2025 08:46:03 +0000 (UTC) X-FDA: 83136061806.24.4EAA8D8 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf12.hostedemail.com (Postfix) with ESMTP id 30F0540014 for ; Wed, 19 Feb 2025 08:46:00 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739954761; 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; bh=EJgtVuggDol4bUUoBoHdZtCxw6dhDYTxpOE1SY33YZQ=; b=GxRQExweDlhh2YGGHLCb72Oi6PFf1TvN3w1ODR6/eKyp2Pppgqb5Zlsaz0Z6wmkgkIJjuY Sj6iyQzVwbxzIenq5X/fNvL/GsLY8xFTSfVDcOK0fKZlbEsk1TkjICFl8I4AzlvSEtTt7N N/e4WXq1BV06p4g8YcK1eFJCu6G/90w= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739954761; a=rsa-sha256; cv=none; b=TcTNda9ujafBS4OTrw7gKOYPPvgZRSpO6py0FwMIfwmhA29BRVn5hMT9KSXvP2ym5AsOwX igUfKO7jeu0/xsrFOvc0FVIkEfsheZH0G3ZfKwAco910fTLnAB+FO1suwl9UeNeVabv9mj PgAdZWcwuTwIriXs5HY9o/5KN85Xv4A= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BAC461682; Wed, 19 Feb 2025 00:46:18 -0800 (PST) Received: from [10.162.42.6] (unknown [10.162.42.6]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E695D3F5A1; Wed, 19 Feb 2025 00:45:47 -0800 (PST) Message-ID: Date: Wed, 19 Feb 2025 14:15:44 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes To: Ryan Roberts , 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 , Dev Jain , Kevin Brodsky , Alexandre Ghiti Cc: linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20250217140419.1702389-1-ryan.roberts@arm.com> <20250217140419.1702389-3-ryan.roberts@arm.com> Content-Language: en-US From: Anshuman Khandual In-Reply-To: <20250217140419.1702389-3-ryan.roberts@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 30F0540014 X-Stat-Signature: puqg5y1npnfpszajjrpco9m3f9wyux1p X-HE-Tag: 1739954760-99062 X-HE-Meta: U2FsdGVkX19VilC4ZkDwUndqnxUTwQRzOfI/Q/g/mhAZ33sVA3ck2RivWeXtsLwS0B1LU84JYu9XzQrdE7+L8NM6ZXtt/gNwBTJDR8xycj1dCrptJxlf8QZVq9pM19t5fYO/9mWfeviSABhqHecMpyk3/N+maVTOuanLm333JEWpzm8jI89SqORQO9vrzRfBjGrRe/MHothhSue1Ndw6aeWfWWPK5XSIfTqy+E/TFzZWpehjdKNapxpz2BVDl9ZfgfTrQY7DYSp8JnkzJoz8NU8qjgT3KUqGRwiVNA0vwc2mmSOa/OlAiop6fPpuJFKi2etoHR0HXJdc2kLTCCMyVfcNQ9a5YTsVCIMxA0mSpRSK0gC59DrMgwanCTs/itqiH6hfacdNg77xBHoZL1lbDbG/MOZj8fbZcrroDwGQzF/56gSddocA/IKsOKApUQA+0m6Edpy+XRSRBBSxRCESjoDo6vgK5s/DbSsjWGeuBfVzbw6YUVDKJ4xMg7qBeQ6y7Vr0396bGO+s0Ex+qmb6+cbtE4BJniIx0W7k8lLEa2cdP6OZY+LqJdBgk8T+FGzt7FANXXXjZ4K2V+pdkGq80O9NrFfUjh/EnRccTM5P3uDQJOHjqr9elvHbVJx99CfxXvvy8V055vf7oK612nmYF3we7r0aOjpuCqeQ5ukmSPdmbbtHivynm0ajVkQuwAMcof+PcLuqaZIZ887cHWSAvepPuC+YcUkIxeyjgyCFxzIrFLL25SFopbfADwMVcOuC8q3LZrsi18k9seZ/IKKjtWLURzv2bJsIXLiYYUwS6xw+8Vty7WSU5qqk9Kv0UrSHHRazYmhvmyaQbhPqW/UNlWgKXvtKo3ME74p7Kn0voaH4G1dfGSMzqoN/Uj1pzo7i8KJdaYYsrFIJIZE3VHaV7GZmvUJJQeBSPlQ3EulNyFI7suiVt7hrGMEgs0pb9h1gx0A6tq+5AysNk2vSh4h fDbDOo5x fHNTGPsKvJzqvckSQQ1vTwZCabhqDSFe394we49DXKgqTM2LnKzmD3ac3bEOmphRVF+B+CyktICwsyJTb7nDLJ8xgnIJMtz7evHNWDDv7jTQb+yAtKcfNlMYB8NxVeeMEo2yopbSx8/DcUFWzSlgOw4I+LFahTjjbmxFXOKCn/HGMHmRcky+ihWXOTptobCOvwHsUlId0d7+6YRm3ZkcIwwAB0lnvNxuRMtBJ4KtYE6+iMVVXEZckpRb2sdOFK8G40FW9p7UcI1sv+vjvdEZo0eDqqw== 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 2/17/25 19:34, 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); pte_present() may not be evaluated for standard huge pages at [PMD|PUD]_SIZE e.g when ncontig = 1 in the argument. > + while (--ncontig) { Should this be converted into a for loop instead just to be in sync with other similar iterators in this file. for (i = 1; i < ncontig; i++, addr += pgsize, ptep++) { 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); } } > + 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); > + } > } > - 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 = __ptep_get(ptep); > - > - if (!pte_cont(orig_pte)) > - return __ptep_get_and_clear(mm, addr, ptep); > - > - ncontig = find_num_contig(mm, addr, ptep, &pgsize); > > + ncontig = num_contig_ptes(sz, &pgsize); > return get_clear_contig(mm, addr, ptep, pgsize, ncontig); > } >