From nobody Sun Dec 28 22:52:56 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13203C4167B for ; Mon, 4 Dec 2023 17:27:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231682AbjLDR1G (ORCPT ); Mon, 4 Dec 2023 12:27:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231737AbjLDR1D (ORCPT ); Mon, 4 Dec 2023 12:27:03 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CDBCFA for ; Mon, 4 Dec 2023 09:27:09 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d3f951af5aso51937347b3.0 for ; Mon, 04 Dec 2023 09:27:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701710829; x=1702315629; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WhY7K21fTuleJoRqinTH8FQOk8orwGh5uZOpWymHf1Q=; b=WzGYea8n8gKWhOwUN/QBnPZuZ64gGSovVSoWBoiu4lbkEjmhix1LpnW/02s19SFZrU QMVFdWKaxmPZunHSypbzY+U7yR2JTPyA41Cbhk3LyKYudi8ww5ybiC3hT+8dLtwBDuQq Qg74t1rFE9TL1mv3NkTfGz2vK3NybbXMukI32VwczFC2vBgCRWOjUSHMYJs+emm/e3nO xJFhfR5/0+lQmbwE/Uo0mGt4fp6L7vE/lQzYwMNzQTTLWdUS1e4TAi4krkOISbwofcZg mKe0cT4CAIz81vMLSxpK8K8AUvk5aMUMSlTkMo+6raEgYH+8Z5D8apajVYlCQjqmzIDZ 9+Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701710829; x=1702315629; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WhY7K21fTuleJoRqinTH8FQOk8orwGh5uZOpWymHf1Q=; b=gRPyqFiFL/MxV/JjRMpo7MxeOT5auYFD5roBdq2tOlXx2A2NiTEAzuDD7Yizdg7/1c B4gyLvFJil2qn6PSudch586cMuDk1Ub6F7Z8ppxQMtOds1hEEX4M1dOQwqjoCrRTii9o v26hxz0Zi/CfcH/S2xPJUm1ZhrJdRMAU9rVJ/5kVY6mk+B1KYIblKJt3jJ9qpQ3aI+PH jLmruS9adutV96JJGhRknkXXKpFlO1GYQBw9qOoXo/UX0aXlRVfZxO0V33IEFGozhudm 4RKzOpgFgteEWhwkbzBkliNc1utIX7udem0tqEq9yJny/ndRno7SgaKc0khjd6vQzobo TZ0g== X-Gm-Message-State: AOJu0Yy77gQP8aJvSeM/Rd3sGPPzcQIbYQ/AWZWX40RiJXg9+L9T3n7Z X1b8oQmhju/2uXrgCElTxnF3p0JctX67X3o0 X-Google-Smtp-Source: AGHT+IEhvePiHl3xyV55jaT6gG1N1cwFNsflp2+fj8EBDZAd96hh+3/YkVm099+dmC2eTTOXHiutCQyCzt5QJhnb X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a81:b61d:0:b0:5d3:84d4:eb35 with SMTP id u29-20020a81b61d000000b005d384d4eb35mr358241ywh.3.1701710828830; Mon, 04 Dec 2023 09:27:08 -0800 (PST) Date: Mon, 4 Dec 2023 17:26:45 +0000 In-Reply-To: <20231204172646.2541916-1-jthoughton@google.com> Mime-Version: 1.0 References: <20231204172646.2541916-1-jthoughton@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204172646.2541916-2-jthoughton@google.com> Subject: [PATCH 1/2] arm64: hugetlb: Distinguish between hw and sw dirtiness in __cont_access_flags_changed From: James Houghton To: Steve Capper , Will Deacon , Andrew Morton Cc: Mike Kravetz , Muchun Song , Anshuman Khandual , Catalin Marinas , Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton , stable@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __cont_access_flags_changed was originally introduced to avoid making unnecessary changes to the PTEs. Consider the following case: all the PTEs in the contiguous group have PTE_DIRTY | PTE_RDONLY | PTE_WRITE, and we are running on a system without HAFDBS. When writing via these PTEs, we will get a page fault, and hugetlb_fault will (rightly) attempt to update the PTEs with PTE_DIRTY | PTE_WRITE, but, as both the original PTEs and the new PTEs are pte_dirty(), __cont_access_flags_changed prevents the pgprot update from occurring. To avoid the page fault loop that we get ourselves into, distinguish between hardware-dirty and software-dirty for this check. Non-contiguous PTEs aren't broken in the same way, as we will always write a new PTE unless the new PTE is exactly equal to the old one. Fixes: 031e6e6b4e12 ("arm64: hugetlb: Avoid unnecessary clearing in huge_pt= ep_set_access_flags") Signed-off-by: James Houghton Cc: diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index f5aae342632c..87a9564976fa 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -437,7 +437,10 @@ static int __cont_access_flags_changed(pte_t *ptep, pt= e_t pte, int ncontig) for (i =3D 0; i < ncontig; i++) { pte_t orig_pte =3D ptep_get(ptep + i); =20 - if (pte_dirty(pte) !=3D pte_dirty(orig_pte)) + if (pte_sw_dirty(pte) !=3D pte_sw_dirty(orig_pte)) + return 1; + + if (pte_hw_dirty(pte) !=3D pte_hw_dirty(orig_pte)) return 1; =20 if (pte_young(pte) !=3D pte_young(orig_pte)) --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Sun Dec 28 22:52:56 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19F08C46CA0 for ; Mon, 4 Dec 2023 17:27:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234858AbjLDR1J (ORCPT ); Mon, 4 Dec 2023 12:27:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231655AbjLDR1G (ORCPT ); Mon, 4 Dec 2023 12:27:06 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DA43C0 for ; Mon, 4 Dec 2023 09:27:12 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d0c4ba7081so78366597b3.0 for ; Mon, 04 Dec 2023 09:27:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701710832; x=1702315632; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BIP1VBSrHfriGjh5Ogk9INZtbtmoj9c5sEQfKrkJBSE=; b=m4uH/7hzVu5zigT1huoKtvcvFkWnSAug3GAlEda3+9FUh0K+crF/J5hHQzUHGAt4rl ej3lyThtJQiZunyJaNC8od6dZxLx6rwkT40wY/fwMY+B7TLGEa/fVu4xO6M/KNGnm+sD rBNbNpbOGcoFRpglZ4viyH0DMU1/KrJnD7N2wzTTr5YcHzI+FPQG3SXhraLaBbNdyQNQ 6c2Wxx+98ueDomh7lThK9UM52Ewh0/BMzWkxtC0A/6QZGdUF82kkmgvxSpknTvVf8aUW T1trWAXZclpo1OP63LF7Yaacq9pxr//sppS719lkVKiTquvM2eTH2zy1ZqXzdQBXjlsO I7BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701710832; x=1702315632; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BIP1VBSrHfriGjh5Ogk9INZtbtmoj9c5sEQfKrkJBSE=; b=pQCpQ7NQLtJm+nr9S9imziSyZKEqLU0xcPaptpFgPVdOixN7kcOlg5y4/x+jhSp3aB pcsz//4yXFwmteaeucEqeC/RHmTz6Cq3Bwojf4RH+7Q1wqrrmSLvbblWsgKwL8xRhGCG hfJCJQbLiM/EwHD+iD6dIb8gth87eyzIXznvvpqjMVARLOwtCNp3mj4ARez5uPtk7l46 6YqtpE58zFSHRVEVTUuiATkI86Pl9G958zsl+UYeBzAz4O3uvaDj13CLrQOntfP5Dl7U R9jcBWCl6a2M7ndfWSJaBkKrsM9uLWD6RxK3aWY10N7zJzuvmxM7N1bzxUyCgB9uqPGm V7aQ== X-Gm-Message-State: AOJu0YxJt9qkbLmh7nZCFzH+UrJBwpAB+OnaWWIRw+33mNk1b/BWvFzv iJuEXdCBI8AKSK+Zapt6wzZiylhGLoXUEAY9 X-Google-Smtp-Source: AGHT+IFq88uu4hPlgv5GbGd/ux88m4iUlZ64ATBmq3rePqIk98p8cvyl0w6nJZb3uZTHk5FQwmgEwV0mbM4oM1P0 X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:2710:b0:5d5:5183:ebd7 with SMTP id dy16-20020a05690c271000b005d55183ebd7mr222984ywb.7.1701710831851; Mon, 04 Dec 2023 09:27:11 -0800 (PST) Date: Mon, 4 Dec 2023 17:26:46 +0000 In-Reply-To: <20231204172646.2541916-1-jthoughton@google.com> Mime-Version: 1.0 References: <20231204172646.2541916-1-jthoughton@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204172646.2541916-3-jthoughton@google.com> Subject: [PATCH 2/2] arm64: mm: Always make sw-dirty PTEs hw-dirty in pte_modify From: James Houghton To: Steve Capper , Will Deacon , Andrew Morton Cc: Mike Kravetz , Muchun Song , Anshuman Khandual , Catalin Marinas , Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make it impossible to create a sw-dirty, hw-clean PTE with pte_modify. Such a PTE should be impossible to create, and there may be places that assume that pte_dirty() implies pte_hw_dirty(). Signed-off-by: James Houghton diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgta= ble.h index b19a8aee684c..79ce70fbb751 100644 Reviewed-by: Ryan Roberts --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -834,6 +834,12 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t new= prot) pte =3D set_pte_bit(pte, __pgprot(PTE_DIRTY)); =20 pte_val(pte) =3D (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask); + /* + * If we end up clearing hw dirtiness for a sw-dirty PTE, set hardware + * dirtiness again. + */ + if (pte_sw_dirty(pte)) + pte =3D pte_mkdirty(pte); return pte; } =20 --=20 2.43.0.rc2.451.g8631bc7472-goog