iommupt/amdv1: avoid GCOV builds triggering FIELD_PREP build failure
PATCH 1/1 iommupt/amdv1: avoid GCOV builds triggering FIELD_PREP build failure
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.
--- drivers/iommu/generic_pt/fmt/amdv1.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/iommu/generic_pt/fmt/amdv1.h +5 -4
@@ -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 )) {
Signature · 1 line
2.50.1
Quoted 15 lines · Sherry Yang
After enabling
CONFIG_GCOV_KERNELandCONFIG_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__) |......
Quoted 8 lines
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.
I think this is fixed already by
commit 98d5110f90ae0dbc5f2f13f033e06f6d57009e0d
Author: Jason Gunthorpe <[email protected]> Date: Mon Jan 19 20:19:49 2026 -0400
iommupt: Make it clearer to the compiler that pts.level == 0 for single page
Older versions of gcc and clang sometimes get tripped up by the build time assertion in FIELD_PREP because they can see that the argument to FIELD_PREP is constant but can't see that the if condition protecting it is also a constant false.
In file included from <command-line>:
In function 'amdv1pt_install_leaf_entry',
inlined from '__do_map_single_page' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:651:3, inlined from '__map_single_page0' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:662: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:658:10, inlined from '__map_single_page1.constprop' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:662:1:
??
Jason
Quoted 51 lines · Jason Gunthorpe
Quoted 15 lines · Sherry Yang
After enabling
CONFIG_GCOV_KERNELandCONFIG_GCOV_PROFILE_ALL, following build failure is observed:In function '
amdv1pt_install_leaf_entry',
inlined from '__do_map_single_page' atdrivers/iommu/generic_pt/fmt/../iommu_pt.h:650:3,
inlined from '__map_single_page0' atdrivers/iommu/generic_pt/fmt/../iommu_pt.h:661:1,
inlined from 'pt_descend' atdrivers/iommu/generic_pt/fmt/../pt_iter.h:391:9,
inlined from '__do_map_single_page' atdrivers/iommu/generic_pt/fmt/../iommu_pt.h:657:10,
inlined from '__map_single_page1.constprop' atdrivers/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__)|......
Quoted 8 lines
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.I think this is fixed already by
commit
98d5110f90ae0dbc5f2f13f033e06f6d57009e0dAuthor: Jason Gunthorpe <[email protected]> Date: Mon Jan 19 20:19:49 2026 -0400iommupt: Make it clearer to the compiler that pts.level == 0 for single page
Older versions of gcc and clang sometimes get tripped up by the build time assertion in
FIELD_PREPbecause they can see that the argument toFIELD_PREPis constant but can't see that the if condition protecting it is also a constant false.In file included from <command-line>: In function 'amdv1pt_install_leaf_entry', inlined from '__do_map_single_page' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:651:3, inlined from '__map_single_page0' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:662: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:658:10, inlined from '__map_single_page1.constprop' at drivers/iommu/generic_pt/fmt/../iommu_pt.h:662:1:
Quoted 3 lines
??
Jason
Hi Jason,
Unfortunately, I ran into the FIELD_PREP build failure with 98d5110f90ae (“iommupt: Make it clearer to the compiler that pts.level == 0 for single page”) already merged, which means it doesn’t work for me. I noticed you mentioned clang 18 hit the same issue and fixed by the patch. We’re using GCC 14.2.1, looks like GCC still constant-folds the contiguous branch despite the change to pts.level.
Thanks,
Sherry
Quoted 7 lines · Sherry Yang
Unfortunately, I ran into the
FIELD_PREPbuild failure with98d5110f90ae(“iommupt: Make it clearer to the compiler that pts.level == 0 for single page”) already merged, which means it doesn’t work for me. I noticed you mentioned clang 18 hit the same issue and fixed by the patch. We’re using GCC 14.2.1, looks like GCC still constant-folds the contiguous branch despite the change to pts.level.
This is all a performance path, I'm reluctant to add more code for everyone just to hide things from GCC bugs.
Maybe you can rework this to be only when gcov is on, or maybe you can fix it by futher enhancing what the other commit does. Perhaps an always inline annotation is all that is missing?
Or maybe we should remove the use of FIELD_PREP?
Jason
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Sherry Yang
Unfortunately, I ran into the
FIELD_PREPbuild failure with98d5110f90ae(“iommupt: Make it clearer to the compiler that pts.level == 0 for single page”) already merged, which means it doesn’t work for me. I noticed you mentioned clang 18 hit the same issue and fixed by the patch. We’re using GCC 14.2.1, looks like GCC still constant-folds the contiguous branch despite the change to pts.level.
Quoted 6 lines
This is all a performance path, I'm reluctant to add more code for everyone just to hide things from GCC bugs.
Maybe you can rework this to be only when gcov is on, or maybe you can fix it by futher enhancing what the other commit does. Perhaps an always inline annotation is all that is missing?
Thanks for the pointer — marking amdv1pt_install_leaf_entry() as __always_inline fixes the gcc/gcov build failure for me while keeping the original FIELD_PREP() path intact.
If you’re happy with this approach, I'll send the patch out.
Thanks,
Sherry
Quoted 21 lines · Sherry Yang
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Sherry Yang
Unfortunately, I ran into the
FIELD_PREPbuild failure with98d5110f90ae(“iommupt: Make it clearer to the compiler that pts.level == 0 for single page”) already merged, which means it doesn’t work for me. I noticed you mentioned clang 18 hit the same issue and fixed by the patch. We’re using GCC 14.2.1, looks like GCC still constant-folds the contiguous branch despite the change to pts.level.Quoted 6 lines
This is all a performance path, I'm reluctant to add more code for everyone just to hide things from GCC bugs.
Maybe you can rework this to be only when gcov is on, or maybe you can fix it by futher enhancing what the other commit does. Perhaps an always inline annotation is all that is missing?
Quoted 5 lines
Thanks for the pointer — marking
amdv1pt_install_leaf_entry()as__always_inlinefixes the gcc/gcov build failure for me while keeping the originalFIELD_PREP()path intact.If you’re happy with this approach, I'll send the patch out.
That sounds good thanks
Jason