From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9338136A374 for ; Wed, 8 Jul 2026 03:11:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783480308; cv=none; b=som8srVIC9kw4uYgqK4uCtN8yPt2MP9gYsQnLohp6qJs9QsjJMJ21yOwYroKKAvzxxV1886prNUX66O/EWNNqzEs8MqRp1YdxuQgcgy3ndSmj1i9zM2IHOGKQb0OT1VIV2vA6E9RZASbi5vyEpF+iQDq+oyJ40cX2KT/ARRYGBk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783480308; c=relaxed/simple; bh=R5hbbhmNhCKCNzl6tMem/1Sp04mUvK1H9tZZCj22VQU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FWhu36iX+BHdr28xKUaDD5fYYv0+hWd8sxsXHmW40YYfPsQyVuKmOzjuNpitVEzLGWC2TNtHmSUR++HnnkgLK6MY6YRzSJr9WAahuY83Opi5kzh6cgJsFDhs4hPU+M5/3Z8mO+wO9Ld6IJgybeILzSWHQGG2tuNwa0xToShaqZo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lI2c3o3k; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lI2c3o3k" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2cc8bde6318so5479465ad.3 for ; Tue, 07 Jul 2026 20:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783480304; x=1784085104; 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=LL4AMr/kt9xgKI4EE6n4T/6niteeK43+z1sjA/ZS5rw=; b=lI2c3o3ku7IRABomH/V0vvQASWgMqkSYu9kP3aHteyHHo88wjqqUHl0E8lrBA5SDXG tG/NrGZV02SAA+IyuJDvifcltu4NVJalCKDHMqcN2Lk3FXlnSyUiY8gde2qe4HTXzLjY VTO6x5iH5ou4m27G/P8qyGZpJTsa39XHsDNqb4BcGsxTBU4i4YnpdbVTQtb6YStkwyUH 5yc/9V9RaVGG9BwT5L5XlxGipeFyEj/INNIpmx9aj1ktTRHgxFOhXRqMBA+9UM5e6QLq TdeA1hmCKIAkhl4pjxqSo1NhDXI1/GQ2AAykBo+2s/ZQrRKXDJ/pNPlG/aq4i5xn2J/S wbWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783480304; x=1784085104; 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=LL4AMr/kt9xgKI4EE6n4T/6niteeK43+z1sjA/ZS5rw=; b=kNm2BmuKgq/VSJ47ubGXnuiuXULzKbVY7TRcmRiTLtQsyuEnRnSq1nDbRrxSK3hdnm +P+E32WbrMQqYRuNo2VB0Cltz9u+SCV7jaPI2AkjmH1ZuUTAPPay8MeONyLROKMvhgjg Dh0gC0ZTwZoPKlWeJq8x31eldYc/knIrbDZwqMSTt3dvOdgWj1MjwMdrZ+gC8GDnMtKE SrSEGttbc5FnL54qRuStmPs+ebm2BeiBTMQXzgYPzyBmZ5seVgLb2v7ftovxyXBdYsMc aU2W9VVm6lOBuBI5qVbTJBDTqLpQRDFm7pl+9dANjGkBWAMYSzHk5aYWbCOaM2S8KaZG ytyw== X-Forwarded-Encrypted: i=1; AHgh+Rp5N6Two25kJKLU7ElP187RQ8f9WF2n5vNQR4fxOnl1cHFOWRloSccza7n8iojuHSabnW+8gyTjQh0jcA4=@vger.kernel.org X-Gm-Message-State: AOJu0YzKL7NrKjb4Oj6WZJe+fFg2OijIhvtKj4VAOh6lfRKZcEaxHoxZ 5sNAGIiQ9Uw/BcWE0DWGdexDzNsQgpgYZ0yNq8S/o8KleSws51oaIMMjtYHfiUVxtkbqk5fwKqH KO9fq2j/F/scehnEdtMB/NQ== X-Received: from plad7.prod.google.com ([2002:a17:902:e147:b0:2cc:e88e:4251]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e54e:b0:2c9:e86e:aa0a with SMTP id d9443c01a7336-2ccea274c6amr6735195ad.0.1783480303733; Tue, 07 Jul 2026 20:11:43 -0700 (PDT) Date: Wed, 8 Jul 2026 03:11:22 +0000 In-Reply-To: <20260708031129.3503195-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260708031129.3503195-1-jthoughton@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260708031129.3503195-13-jthoughton@google.com> Subject: [PATCH 12/18] arm64: Implement try_populate_vmemmap_pmd using AF trick From: James Houghton To: Will Deacon , Catalin Marinas , Muchun Song , Oscar Salvador Cc: Nikos Nikoleris , Linu Cherian , Mark Rutland , David Hildenbrand , Andrew Morton , Ryan Roberts , Nanyong Sun , Yu Zhao , Frank van der Linden , David Rientjes , James Houghton , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" This routine is to be used for updating in-use, leaf-level PMDs in the vmemmap without introducing a window where vmemmap accesses might fault. Implementing this on arm64 requires some care: use the same access flag trick that is used for vmemmap PTE updates. HAFT is not needed and the TLB flushing routine remains identical, as we are not overwriting a non-leaf PMD. For systems that support BBML2_NOABORT, there is no need to use the AF trick. Signed-off-by: James Houghton --- arch/arm64/include/asm/pgalloc.h | 51 ++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index c8946250d431..26b05f8b70cd 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -125,9 +125,54 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) static inline int try_populate_vmemmap_pmd(pmd_t *pmdp, pte_t *pgtable, unsigned long addr) { - /* BBML2_NOABORT is required. Its presence has been checked. */ - pmd_populate_kernel(&init_mm, pmdp, pgtable); - return 0; + const int max_attempts = 16; + int attempts = 0; + pmd_t old_pmd, new_pmd; + + if (!system_supports_hvo()) + return -EOPNOTSUPP; + + if (system_supports_bbml2_noabort()) { + /* + * BBML2_NOABORT allows block->table transitions if the PTEs + * underneath do not conflict with existing, potentially cached + * translations. + */ + pmd_populate_kernel(&init_mm, pmdp, pgtable); + return 0; + } + + new_pmd = __pmd(__phys_to_pmd_val(__pa(pgtable)) | + PMD_TYPE_TABLE | PMD_TABLE_AF | PMD_TABLE_UXN); + + old_pmd = pmdp_get(pmdp); + + do { + if (WARN_ON_ONCE(!pmd_valid(old_pmd))) + return -EINVAL; + + if (WARN_ON_ONCE(!pmd_leaf(old_pmd))) + return -EINVAL; + + /* We should never get a contiguous PMD here. */ + if (WARN_ON_ONCE(pmd_cont(old_pmd))) + return -EINVAL; + + if (pmd_young(old_pmd)) { + /* __ptep_clear_young() returns the overwritten PTE */ + old_pmd = pte_pmd(pte_mkold(__ptep_clear_young((pte_t *)pmdp))); + + flush_tlb_kernel_range(addr, addr + PMD_SIZE); + } + /* + * Translations without AF cannot be cached, so we can replace + * them without BBM. + */ + } while (!try_cmpxchg_relaxed(&pmd_val(*pmdp), &pmd_val(old_pmd), + pmd_val(new_pmd)) && + ++attempts < max_attempts); + + return attempts == max_attempts ? -EAGAIN : 0; } #endif -- 2.55.0.795.g602f6c329a-goog