public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iommupt/amdv1: avoid GCOV builds triggering FIELD_PREP build failure
@ 2026-03-10 16:31 Sherry Yang
  2026-03-10 16:41 ` Jason Gunthorpe
  0 siblings, 1 reply; 6+ messages in thread
From: Sherry Yang @ 2026-03-10 16:31 UTC (permalink / raw)
  To: jgg, joerg.roedel, kevin.tian, vasant.hegde, joro, will,
	robin.murphy, iommu
  Cc: sherry.yang, linux-kernel

After enabling CONFIG_GCOV_KERNEL and CONFIG_GCOV_PROFILE_ALL, following
build failure is observed:

In function 'amdv1pt_install_leaf_entry',
    inlined from '__do_map_single_page' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:650:3,
    inlined from '__map_single_page0' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:661:1,
    inlined from 'pt_descend' at drivers/iommu/generic_pt/fmt/../pt_iter.h:391:9,
    inlined from '__do_map_single_page' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:657:10,
    inlined from '__map_single_page1.constprop' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:661:1:
././include/linux/compiler_types.h:706:45: error: call to '__compiletime_assert_71' declared with attribute error: FIELD_PREP: value too large for the field
  706 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
      |

......

drivers/iommu/generic_pt/fmt/amdv1.h:220:26: note: in expansion of macro 'FIELD_PREP'
  220 |                          FIELD_PREP(AMDV1PT_FMT_OA,
      |                          ^~~~~~~~~~

In the path '__do_map_single_page()', level 0 always invokes
'pt_install_leaf_entry(&pts, map->oa, PAGE_SHIFT, …)'. At runtime that
lands in the 'if (oasz_lg2 == isz_lg2)' arm of 'amdv1pt_install_leaf_entry()';
the contiguous-only 'else' block is unreachable for 4 KiB pages.

With CONFIG_GCOV_KERNEL + CONFIG_GCOV_PROFILE_ALL, the extra
instrumentation changes GCC's inlining so that the "dead" 'else' branch
still gets instantiated. The compiler constant-folds the contiguous OA
expression, runs the 'FIELD_PREP()' compile-time check, and produces:

    FIELD_PREP: value too large for the field

gcov-enabled builds therefore fail even though the code path never executes.

Fix this by keeping the compile-time guard on the NEXT_LEVEL field but
encoding the OA field manually after a runtime 'FIELD_FIT()' check. We
shift-and-mask the value ourselves, so the compiler no longer evaluates
'FIELD_PREP(AMDV1PT_FMT_OA, …)' with constant inputs, while we preserve
the range validation and resulting bit pattern.

Fixes: 879ced2bab1b ("iommupt: Add the AMD IOMMU v1 page table format")
Signed-off-by: Sherry Yang <sherry.yang@oracle.com>
---
 drivers/iommu/generic_pt/fmt/amdv1.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/generic_pt/fmt/amdv1.h b/drivers/iommu/generic_pt/fmt/amdv1.h
index aa8e1a8ec95f..c0a200afbe79 100644
--- a/drivers/iommu/generic_pt/fmt/amdv1.h
+++ b/drivers/iommu/generic_pt/fmt/amdv1.h
@@ -214,13 +214,14 @@ amdv1pt_install_leaf_entry(struct pt_state *pts, pt_oaddr_t oa,
 	} else {
 		unsigned int num_contig_lg2 = oasz_lg2 - isz_lg2;
 		u64 *end = tablep + log2_to_int(num_contig_lg2);
+		pt_oaddr_t contig_oa = oalog2_to_int(oasz_lg2 - PT_GRANULE_LG2SZ - 1) - 1;
+
+		if (PT_WARN_ON(!FIELD_FIT(AMDV1PT_FMT_OA, contig_oa)))
+			return;
 
 		entry |= FIELD_PREP(AMDV1PT_FMT_NEXT_LEVEL,
 				    AMDV1PT_FMT_NL_SIZE) |
-			 FIELD_PREP(AMDV1PT_FMT_OA,
-				    oalog2_to_int(oasz_lg2 - PT_GRANULE_LG2SZ -
-						  1) -
-					    1);
+			 (((u64)contig_oa << __bf_shf(AMDV1PT_FMT_OA)) & AMDV1PT_FMT_OA);
 
 		/* See amdv1pt_clear_entries() */
 		if (num_contig_lg2 <= ilog2(32)) {
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-03-26 12:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10 16:31 [PATCH] iommupt/amdv1: avoid GCOV builds triggering FIELD_PREP build failure Sherry Yang
2026-03-10 16:41 ` Jason Gunthorpe
2026-03-10 17:17   ` Sherry Yang
2026-03-10 17:57     ` Jason Gunthorpe
2026-03-25 23:39       ` Sherry Yang
2026-03-26 12:01         ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox