Skip to content

Commit c6c83d7

Browse files
Anshuman Khandualctmarinas
authored andcommitted
arm64/cpufeature: Validate feature bits spacing in arm64_ftr_regs[]
arm64_feature_bits for a register in arm64_ftr_regs[] are in a descending order as per their shift values. Validate that these features bits are defined correctly and do not overlap with each other. This check protects against any inadvertent erroneous changes to the register definitions. Signed-off-by: Anshuman Khandual <[email protected]> Reviewed-by: Suzuki K Poulose <[email protected]> Cc: Will Deacon <[email protected]> Cc: Suzuki K Poulose <[email protected]> Cc: Mark Brown <[email protected]> Cc: Mark Rutland <[email protected]> Cc: [email protected] Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 8d3154a commit c6c83d7

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

arch/arm64/kernel/cpufeature.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,52 @@ static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
712712

713713
static void __init sort_ftr_regs(void)
714714
{
715-
int i;
715+
unsigned int i;
716+
717+
for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) {
718+
const struct arm64_ftr_reg *ftr_reg = arm64_ftr_regs[i].reg;
719+
const struct arm64_ftr_bits *ftr_bits = ftr_reg->ftr_bits;
720+
unsigned int j = 0;
721+
722+
/*
723+
* Features here must be sorted in descending order with respect
724+
* to their shift values and should not overlap with each other.
725+
*/
726+
for (; ftr_bits->width != 0; ftr_bits++, j++) {
727+
unsigned int width = ftr_reg->ftr_bits[j].width;
728+
unsigned int shift = ftr_reg->ftr_bits[j].shift;
729+
unsigned int prev_shift;
730+
731+
WARN((shift + width) > 64,
732+
"%s has invalid feature at shift %d\n",
733+
ftr_reg->name, shift);
734+
735+
/*
736+
* Skip the first feature. There is nothing to
737+
* compare against for now.
738+
*/
739+
if (j == 0)
740+
continue;
716741

717-
/* Check that the array is sorted so that we can do the binary search */
718-
for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
742+
prev_shift = ftr_reg->ftr_bits[j - 1].shift;
743+
WARN((shift + width) > prev_shift,
744+
"%s has feature overlap at shift %d\n",
745+
ftr_reg->name, shift);
746+
}
747+
748+
/*
749+
* Skip the first register. There is nothing to
750+
* compare against for now.
751+
*/
752+
if (i == 0)
753+
continue;
754+
/*
755+
* Registers here must be sorted in ascending order with respect
756+
* to sys_id for subsequent binary search in get_arm64_ftr_reg()
757+
* to work correctly.
758+
*/
719759
BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
760+
}
720761
}
721762

722763
/*

0 commit comments

Comments
 (0)