From nobody Fri Dec 19 07:24:15 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97BBD34C9AB for ; Tue, 16 Dec 2025 14:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765896383; cv=none; b=JxEqGVR+CGlzM5gEuTaP2A8wFY3kkK/iO4on+0qpt8f4Am6jHElXLxwCfweKSdwmVkSs3Od9m+H+fh40eK+x7tCbjDZdP4R9UZlziG4FrNPBmp7DlEPefXIjApFF2Zw+Bwqp+42POJk60UZouM3DAmw8XqR2siKzdl2e2/UwzNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765896383; c=relaxed/simple; bh=hbZvsdl75oi0GW1FQDlf1P8U3yeh/ivdZiAqt9xkZE4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z1Flbluw47Kr+8AgOT1zxfI5WYqkaDnRyiyVvt1VBOFGwj8eMY0X9nMiqjE700zKfDYeJqtSeQ9aVtKc+x482lQQQFja0OCx+6YjNchDIPNgkC1j4Javk5x4jQXK90iCwM5xebCqBktXss8XvfkL5F1rQCfH6WPDdgDC9Egea5I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 3CD72168F; Tue, 16 Dec 2025 06:46:14 -0800 (PST) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E43D93F73F; Tue, 16 Dec 2025 06:46:19 -0800 (PST) From: Ryan Roberts To: Will Deacon , Ard Biesheuvel , Catalin Marinas , Mark Rutland , Linus Torvalds , Oliver Upton , Marc Zyngier , Dev Jain , Linu Cherian Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 06/13] arm64: mm: Re-implement the __flush_tlb_range_op macro in C Date: Tue, 16 Dec 2025 14:45:51 +0000 Message-ID: <20251216144601.2106412-7-ryan.roberts@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251216144601.2106412-1-ryan.roberts@arm.com> References: <20251216144601.2106412-1-ryan.roberts@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The __flush_tlb_range_op() macro is horrible and has been a previous source of bugs thanks to multiple expansions of its arguments (see commit f7edb07ad7c6 ("Fix mmu notifiers for range-based invalidates")). Rewrite the thing in C. Suggested-by: Linus Torvalds Co-developed-by: Will Deacon Signed-off-by: Will Deacon Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/tlbflush.h | 84 +++++++++++++++++-------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlb= flush.h index 887dd1f05a89..d2a144a09a8f 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -419,12 +419,13 @@ static inline void arch_tlbbatch_flush(struct arch_tl= bflush_unmap_batch *batch) /* * __flush_tlb_range_op - Perform TLBI operation upon a range * - * @op: TLBI instruction that operates on a range (has 'r' prefix) + * @lop: TLBI level operation to perform + * @rop: TLBI range operation to perform * @start: The start address of the range * @pages: Range as the number of pages from 'start' * @stride: Flush granularity * @asid: The ASID of the task (0 for IPA instructions) - * @tlb_level: Translation Table level hint, if known + * @level: Translation Table level hint, if known * @lpa2: If 'true', the lpa2 scheme is used as set out below * * When the CPU does not support TLB range operations, flush the TLB @@ -491,36 +492,44 @@ static __always_inline void __tlbi_range(tlbi_op op, = u64 addr, op(arg); } =20 -#define __flush_tlb_range_op(op, start, pages, stride, \ - asid, tlb_level, lpa2) \ -do { \ - typeof(start) __flush_start =3D start; \ - typeof(pages) __flush_pages =3D pages; \ - int num =3D 0; \ - int scale =3D 3; \ - \ - while (__flush_pages > 0) { \ - if (!system_supports_tlb_range() || \ - __flush_pages =3D=3D 1 || \ - (lpa2 && __flush_start !=3D ALIGN(__flush_start, SZ_64K))) { \ - __tlbi_level_asid(op, __flush_start, tlb_level, asid); \ - __flush_start +=3D stride; \ - __flush_pages -=3D stride >> PAGE_SHIFT; \ - continue; \ - } \ - \ - num =3D __TLBI_RANGE_NUM(__flush_pages, scale); \ - if (num >=3D 0) { \ - __tlbi_range(r##op, __flush_start, asid, scale, num, tlb_level, lpa2); \ - __flush_start +=3D __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \ - __flush_pages -=3D __TLBI_RANGE_PAGES(num, scale);\ - } \ - scale--; \ - } \ -} while (0) +static __always_inline void __flush_tlb_range_op(tlbi_op lop, tlbi_op rop, + u64 start, size_t pages, + u64 stride, u16 asid, + u32 level, bool lpa2) +{ + u64 addr =3D start, end =3D start + pages * PAGE_SIZE; + int scale =3D 3; + + while (addr !=3D end) { + int num; + + pages =3D (end - addr) >> PAGE_SHIFT; + + if (!system_supports_tlb_range() || pages =3D=3D 1) + goto invalidate_one; + + if (lpa2 && !IS_ALIGNED(addr, SZ_64K)) + goto invalidate_one; + + num =3D __TLBI_RANGE_NUM(pages, scale); + if (num >=3D 0) { + __tlbi_range(rop, addr, asid, scale, num, level, lpa2); + addr +=3D __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; + } + + scale--; + continue; +invalidate_one: + __tlbi_level_asid(lop, addr, level, asid); + addr +=3D stride; + } +} + +#define __flush_s1_tlb_range_op(op, start, pages, stride, asid, tlb_level)= \ + __flush_tlb_range_op(op, r##op, start, pages, stride, asid, tlb_level, lp= a2_is_enabled()) =20 #define __flush_s2_tlb_range_op(op, start, pages, stride, tlb_level) \ - __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_= enabled()); + __flush_tlb_range_op(op, r##op, start, pages, stride, 0, tlb_level, kvm_l= pa2_is_enabled()) =20 static inline bool __flush_tlb_range_limit_excess(unsigned long start, unsigned long end, unsigned long pages, unsigned long stride) @@ -559,11 +568,11 @@ static inline void __flush_tlb_range_nosync(struct mm= _struct *mm, asid =3D ASID(mm); =20 if (last_level) - __flush_tlb_range_op(vale1is, start, pages, stride, asid, - tlb_level, lpa2_is_enabled()); + __flush_s1_tlb_range_op(vale1is, start, pages, stride, + asid, tlb_level); else - __flush_tlb_range_op(vae1is, start, pages, stride, asid, - tlb_level, lpa2_is_enabled()); + __flush_s1_tlb_range_op(vae1is, start, pages, stride, + asid, tlb_level); =20 mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } @@ -587,8 +596,7 @@ static inline void local_flush_tlb_contpte(struct vm_ar= ea_struct *vma, =20 dsb(nshst); asid =3D ASID(vma->vm_mm); - __flush_tlb_range_op(vale1, addr, CONT_PTES, PAGE_SIZE, asid, - 3, true, lpa2_is_enabled()); + __flush_s1_tlb_range_op(vale1, addr, CONT_PTES, PAGE_SIZE, asid, 3); mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, addr, addr + CONT_PTE_SIZE); dsb(nsh); @@ -621,8 +629,8 @@ static inline void flush_tlb_kernel_range(unsigned long= start, unsigned long end } =20 dsb(ishst); - __flush_tlb_range_op(vaale1is, start, pages, stride, 0, - TLBI_TTL_UNKNOWN, lpa2_is_enabled()); + __flush_s1_tlb_range_op(vaale1is, start, pages, stride, 0, + TLBI_TTL_UNKNOWN); dsb(ish); isb(); } --=20 2.43.0