From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137701; b=WkvzteHYd48CVqyeBBP/M+CSAnLDUAuu2nw5sFf/IPBhhAYKS724lH19X9h4p/KqNCUk rN/Ns5UwXJkzYTfHwU8bzQKNZFQgHneWvNTAWcsNfSu+6FD0dJzl8Mzx98INjQ9qIMhai CDbxhn1VlNjCAPPvIdR6cX4seGf0OW7esjWsItsbf1ry/0ZHTDwfhaw+9WS4PegUeqVwL a8L57SsaabAVBvI8hJz2j//B9cZFjxbpwM2kZNOqavJ4q+mXnYiOevUjwLuo9eUEBH7UK oWOkcJuEaUP8WP+USKoBhJAsI6zFGrd5tzPglgHXrJtxVusWm8Qs7/ygK2MQVSAt+6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137701; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=WPvu0kvQ9eCUukFWJx/UsQQb/OKZ7ywzJ8SBRnKMWzsUEpavol0MbuogTVIuq/lpNUGL +xrvFiRvh+J6lm5DBMxNr2IDLr8wqG/wzUe0Q8Zp6zAyTDoEK3BoQYfKQl2RAOsroeyeJ A9ayXPkPYAoeSOQm1rmWr4CIM3Q02FM8QYCmNWcNqAEvq6/M3ZsLSAvISP4c2Y22CBorI rE5Tr8ZQVzYFjO7oTxTkWrbj/etYNXzB5uvLIhQUpxxyvOUUpMZw8VV4v5v8TE0KivVzj qTVX+FHX8A+ee+JEoMoq+yrQaEnSxbuf3Au9mOXh57z8v8JGFkMTzalybNoJhKMGyPg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Q0XLNLoY; arc=none smtp.remote-ip=209.85.214.197 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E893606A6B2 for ; Tue, 1 Dec 2026 15:08:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E893606A6B2 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.a=rsa-sha256 header.s=20260707 header.b=Q0XLNLoY Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2f1426c0ee8so32308365ad.2 for ; Tue, 01 Dec 2026 07:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137701; x=1796742501; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=Q0XLNLoYi7sbEzgEryJLZjy0mnrqmu4BZfjRhkV2Sa3tO5mwXZClMDiJK5iKpT9oV1 RKn7g23UquJZ3VZUz4OdhUnKNZ53XXAI9Xj0zXbVXjzk1X2DCDUFRYiGQKiM+UZEowDz fyJTu/qIeOCiqlIeativfOzIau5cEnLnDpXAdjua8MqaJrrtFgHveAScWQLkxN0Wy+1K RnRlkaX0J3xXaeVrhYl5v7A3ysavP6IImTqI2yDZ6xBDBTLfkRcmC61RVgmzqD9MC7er pAbAngWd8CCc6uDaQatpQ0ei2aFS7g2AxflUuBYjxI5TuO8A2sEhJKSSu9+OotcEIm72 vfaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137701; x=1796742501; h=content-type: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:content-type; bh=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=ybXB1bGfl6WVXA4hiV2zt3FzVIAfBM9BfwvuW3yAyOQ+eJmUTm7D9N5Ezcd5cIOaNK 8nM+0Q3W4VaEg+MOPpHogtuvoB4jtI4djbI4y7N4p+qwm2Tw+ipOKn2By7JpyJRyh3yw +ftLajH1a50c7Ztsa2XlVnr8A2d7iQwFzRikzg0bq2G8xRHWe625bmwAA1XCMrCZGCzs njnT1McjHaunIKcbzLlFyBdHlpqNjJKYTM0Rl+MBl40ZcJk7rxAp0btpPgXRvMjTvDR8 wge5RPPQ0A/+ccQvYzcpL8EMoiDLrXZOoou9QvZ5B04yYoeb3+A4Mm17dqKpzvth0YWE cyew== X-Forwarded-Encrypted: i=1; AL+hErGgdrviUNhzRGs77rIZWEdf1N4Xoh9c4vi+orNhyllyXnXPY2MgdC6MuL00fhBLNSUhlOkuqgq1eExneUw=@vger.kernel.org X-Gm-Message-State: APuU4Z/eHShTfQFXzfLofdudeUAHXIEe66nuP7tGNvO6Lhu93Ff+odBQ 76I1hr9uXlnZm1XXxLn7eozxtlZ70INfl7IcHFnggKIvbQlUucqy9RexNM3DIq5p9QRlNaPLBsz L8sjQnBriQat6SjYNv/B8Jg== X-Received: from plbkc12.prod.google.com ([2002:a17:903:33cc:b0:2ee:2d4e:e83b]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ec8b:b0:2e9:66f6:7894 with SMTP id d9443c01a7336-2f03f15c27amr364425315ad.40.1796137700312; Tue, 01 Dec 2026 07:08:20 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:57 +0000 In-Reply-To: <20261201150801.2239894-1-jthoughton@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201150801.2239894-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201150801.2239894-16-jthoughton@google.com> Subject: [RFC PATCH v4 15/18] arm64: Implement vmemmap_update_pmd using AF trick From: James Houghton To: Will Deacon , Catalin Marinas , Muchun Song , Oscar Salvador , Andrew Morton Cc: Kefeng Wang , Nikos Nikoleris , Linu Cherian , Mark Rutland , David Hildenbrand , 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 BBML3, there is no need to use the AF trick. Signed-off-by: James Houghton --- arch/arm64/include/asm/pgalloc.h | 58 ++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index 421e07545cfd..54a68f7bdb53 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -125,9 +125,61 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) static inline int vmemmap_update_pmd(unsigned long addr, pmd_t *pmdp, pte_t *pgtable) { - /* BBLM3 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_bbm_through_af()) + return -EOPNOTSUPP; + + if (system_supports_bbml3()) { + /* + * BBML3 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))); + + /* + * A non-contiguous block entry is cached as a single + * TLB entry, and invalidating any address it maps + * removes it (see unmap_hotplug_pmd_range()). Flushing + * the whole PMD_SIZE range is unnecessary and may + * escalate to flush_tlb_all(). + */ + flush_tlb_kernel_range(addr, addr + PAGE_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.56.1.839.g22a8290db9-goog