From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 688E53A3E7F for ; Tue, 21 Apr 2026 09:26:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763578; cv=none; b=Jfev3YfxI5ox2JrzFt5kknBfBzeGSUEv1U2IfPNtWuf5U0KQ8jdlghngAoGbflba6P9jv8ypqHxNY6hmNqspkI9NTXpLk3BjW8bAUldbscPGJlwBexWbsGsaBkrmXoMgoOZWNU+UGCGM3cnimjzgns8PWbKcRTaMIIiYBNbFn2U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763578; c=relaxed/simple; bh=NqIa/AVjalXxIFvZZGUvR0dRYemXOYMzQmU4EZu8VMY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ALIRYMRK0hjc4UBBWT56Oe0fU3cfOiJzY5ipz/HUiu3ns68VPKR1x8FOCuN9DsekR8irUQyLYknys2nPkLnHz6fk7YzJmLMQeOpUWCvH649huOlhcF6olqfG5uF7Yzi2EnAoG2SuQXafqfLguDkHzCMYFUu4EPlgp/lvTHG2/0k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=k6STzqCz; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="k6STzqCz" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3567e2b4159so2683211a91.0 for ; Tue, 21 Apr 2026 02:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1776763576; x=1777368376; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=vuusCw6rlfDGFySSxpy3cAwZYM4fJMPSUnMyh4fNp+s=; b=k6STzqCzh4nQJwlUfc7Dq+aZKHTOH3FAx/Yfnx1vUnkIWPz+5dAS95O4t4E1PvQIaw Vp06O/tCOIi55uNnDoKonOa8x2E+ab9kPixsievWw4CiSvHAMTh7ItbB9XEu2nA+FUjB ZsRhSxig1AikZ9TW8VoT/geODGCoegvKd/koJRaAW34cjZUtC4sb4xFQ6Prg3VXBjVfg LPUyMRVLXHUXIRm7uLwLOBCnjTYlfqAtLYd6ngjq4OKtaR/7JH4yS8VAxxHiWxKWpJgp M7SotZF+N+pjrgWRAEF8R4M/W8qqVjHiyzKg7GW/YQMn23UpHHoFIGftvu7+WfmkAx2e erKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776763576; x=1777368376; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=vuusCw6rlfDGFySSxpy3cAwZYM4fJMPSUnMyh4fNp+s=; b=dqCtn0HJwyDxGnG61sTm5c6c2ilzgiyfQSbYINRE+Rl2a19/7rew8kKXD1e5rQlXl4 gYyFr6P1oL5cPCymOvUy4tU+dgRVfoO69XkR0RDcPvB5EVOUUuiEHKWCokDjSgzXZJu1 Ld2l0P0UYg00OXdzX01QQwWwJ9ah36RHFVS+06L5VHpGl2TnLr9Xlz8AEd1y9J5f19kw QLLEPDPmgnHRXlGEj9Fpc5TyBiRI0UvDyjORP6W1yEZ1FlQObNpcrGJA4R9i8HscDu4g zRe1YMU3/8r3DwhSrFaZCujlzXuxUjnYvgsUpS+LZCuCHxt13Q3cFdsLtxOyJJ8unG8p uGAw== X-Forwarded-Encrypted: i=1; AFNElJ9l9vlMuMHxdWeYMiFR3aZ1e+NVI16c8OERXsWOtUOGKwDXADuOkmB1Af7tvKVJEJ1kYX90T7HM/Q0lubk=@vger.kernel.org X-Gm-Message-State: AOJu0YwGI8aLqd/k5BVvxv3xoCqtUNPgIT9Dd0zh2kWpXF9VN8QJCg2Q LDAx6eUGJoBhhxcx2fwabC7s0sXPIzLHsZ6Myx6siBYru/Anv//TWN8fRCZlxhCZ004= X-Gm-Gg: AeBDietLs7GJbEok5/pKOBD7+bNplhRIEAUhFWwWlLJXBcEUaLa9iGBVlbWye18vQpW LgFBBlsyewXaENkPU5B6llKS6vJMyyRdixrA+BQifCWOG8HxbSLLpE3PpiGWh4xKt1yznhR79S7 oEIk1xQF0c0q6AzZ0PxuqnmnKS2AWY0hJH7/lVN1oD6yjuGvDTEwGLFolwK6P4WPQNRX0XCoGww AQjPiGdBKNigRkSKFJ1YZb0rcCsxr0qs9zAcstrwbkvrp9erq286xv/H00IsRuu83Z882fb+5Xv jRApaF6+LVDQSJMQuFG3vM2NY7MYf5YY/6lOs5tvMIC8D98l+Gj0pOrPMcltix3sqPB7AqXGrdJ atsgxZ+r/7r2U6qqTvBEAcnogaqIRTI7Dhugs0RW1z3bcAnEMJXKtoSVZyTE3d7mtK3DwaKDsFx wNN2AG6zgP4JjvZL8LMRRhPKDvWWDW+HRcndmuFsbOucaTvaxkBVr8JrJh5Nz2jlfOAmzy X-Received: by 2002:a17:90b:1809:b0:359:15c8:e8e1 with SMTP id 98e67ed59e1d1-361404a8241mr17256461a91.25.1776763575646; Tue, 21 Apr 2026 02:26:15 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff39csm131965105ad.4.2026.04.21.02.26.06 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 21 Apr 2026 02:26:15 -0700 (PDT) From: Yunhui Cui To: akpm@linux-foundation.org, alex@ghiti.fr, andrew+kernel@donnellan.id.au, andreyknvl@gmail.com, anup@brainfault.org, aou@eecs.berkeley.edu, apopple@nvidia.com, ardb@kernel.org, atish.patra@linux.dev, baolin.wang@linux.alibaba.com, cuiyunhui@bytedance.com, david@kernel.org, debug@rivosinc.com, djordje.todorovic@htecgroup.com, dvyukov@google.com, elver@google.com, glider@google.com, ilias.apalodimas@linaro.org, junhui.liu@pigmoral.tech, kasan-dev@googlegroups.com, kees@kernel.org, kevin.brodsky@arm.com, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, liu.xuemei1@zte.com.cn, ljs@kernel.org, namcao@linutronix.de, osalvador@suse.de, palmer@dabbelt.com, pjw@kernel.org, rmclure@linux.ibm.com, rostedt@goodmis.org, rppt@kernel.org, ryabinin.a.a@gmail.com, surenb@google.com, vincenzo.frascino@arm.com, vishal.moola@gmail.com, wangruikang@iscas.ac.cn, zhangchunyan@iscas.ac.cn Subject: [PATCH 7/7] riscv: add Svnapot-aware pte_batch_hint support Date: Tue, 21 Apr 2026 17:24:57 +0800 Message-Id: <20260421092457.37649-8-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260421092457.37649-1-cuiyunhui@bytedance.com> References: <20260421092457.37649-1-cuiyunhui@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Provide a Svnapot-specific pte_batch_hint() implementation so callers can batch over a contiguous napot range without re-reading each PTE entry. Keep the public wrapper in pgtable.h and leave the CONFIG-disabled case on the existing single-entry fallback. Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/pgtable.h | 19 +++++++++++++++++- arch/riscv/mm/contpte.c | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index db82253efb218..264af77392c6e 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -872,6 +872,13 @@ static inline bool __ptep_clear_flush_young(struct vm_area_struct *vma, #define __ptep_clear_flush_young __ptep_clear_flush_young +static inline unsigned int __pte_batch_hint(pte_t *ptep, pte_t pte) +{ + return 1; +} + +#define __pte_batch_hint __pte_batch_hint + #ifdef CONFIG_RISCV_ISA_SVNAPOT /* @@ -886,6 +893,7 @@ void __napotpte_try_unfold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte); pte_t napotpte_ptep_get(pte_t *ptep, pte_t orig_pte); pte_t napotpte_ptep_get_lockless(pte_t *ptep); +unsigned int napotpte_pte_batch_hint(pte_t *ptep); void napotpte_set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned int nr); void napotpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, @@ -1056,6 +1064,15 @@ static inline bool ptep_clear_flush_young(struct vm_area_struct *vma, return napotpte_ptep_clear_flush_young(vma, address, ptep); } +#define pte_batch_hint pte_batch_hint +static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) +{ + if (!pte_present(pte)) + return 1; + + return napotpte_pte_batch_hint(ptep); +} + #else /* CONFIG_RISCV_ISA_SVNAPOT */ static __always_inline bool riscv_pte_present_napot(pte_t pte) @@ -1100,9 +1117,9 @@ napotpte_ptep_clear_flush_young(struct vm_area_struct *vma, #define ptep_set_wrprotect __ptep_set_wrprotect #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH #define ptep_clear_flush_young __ptep_clear_flush_young +#define pte_batch_hint __pte_batch_hint #endif /* CONFIG_RISCV_ISA_SVNAPOT */ - #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) { diff --git a/arch/riscv/mm/contpte.c b/arch/riscv/mm/contpte.c index 077ffa49e89d9..134b8c401cabc 100644 --- a/arch/riscv/mm/contpte.c +++ b/arch/riscv/mm/contpte.c @@ -187,6 +187,12 @@ static inline bool napotpte_is_consistent(pte_t pte, pte_t orig_pte) pte_val(pte_mask_ad(pte)) == pte_val(pte_mask_ad(orig_pte)); } +static inline bool napotpte_is_batch_consistent(pte_t pte, pte_t orig_pte) +{ + return pte_present_napot(pte) && + pte_val(pte_mkold(pte)) == pte_val(pte_mkold(orig_pte)); +} + void __napotpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) { @@ -391,6 +397,33 @@ pte_t napotpte_ptep_get_lockless(pte_t *orig_ptep) } EXPORT_SYMBOL(napotpte_ptep_get_lockless); +unsigned int napotpte_pte_batch_hint(pte_t *ptep) +{ + pte_t orig_pte, pte; + pte_t *start; + unsigned int i, nr, off; + + if (!napot_hw_supported()) + return 1; + + orig_pte = READ_ONCE(*ptep); + if (!pte_present_napot(orig_pte)) + return 1; + + start = napot_align_ptep(ptep); + nr = napotpte_pte_num(); + off = ptep - start; + + for (i = off; i < nr; i++) { + pte = READ_ONCE(start[i]); + if (!napotpte_is_batch_consistent(pte, orig_pte)) + return 1; + } + + return nr - off; +} +EXPORT_SYMBOL(napotpte_pte_batch_hint); + void napotpte_set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned int nr) { -- 2.39.5