Skip to contentSkip to conversation

arm64/cpufeature: Validate feature bits spacing in arm64_ftr_regs[]

PATCH Superseded Anshuman Khandual 2 participants 2 messages Jul 1, 2020 – Jul 7, 2020 arm64

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Mark Brown <[email protected]>
Cc Mark Rutland <[email protected]>

---
Applies on 5.8-rc3.

Changes in V2:

- Replaced WARN_ON() with WARN() dropping the conditional block per Suzuki

Changes in V1: (https://patchwork.kernel.org/patch/11606285/)

 arch/arm64/kernel/cpufeature.c | 45 +++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)
arch/arm64/kernel/cpufeature.c +42 -3
index 9f63053a63a9..7bd7e6f936a5 100644
@@ -697,11 +697,50 @@ static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 
 static void __init sort_ftr_regs(void)
 {
-	int i;
+	const struct arm64_ftr_reg *ftr_reg;
+	const struct arm64_ftr_bits *ftr_bits;
+	unsigned int i, j, width, shift, prev_shift;
+
+	for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) {
+		/*
+		 * Features here must be sorted in descending order with respect
+		 * to their shift values and should not overlap with each other.
+		 */
+		ftr_reg = arm64_ftr_regs[i].reg;
+		for (ftr_bits = ftr_reg->ftr_bits, j = 0;
+				ftr_bits->width != 0; ftr_bits++, j++) {
+			WARN((ftr_bits->shift  + ftr_bits->width) > 64,
+				"%s has invalid feature at shift %d\n",
+				ftr_reg->name, ftr_bits->shift);
+
+			/*
+			 * Skip the first feature. There is nothing to
+			 * compare against for now.
+			 */
+			if (j == 0)
+				continue;
+
+			prev_shift = ftr_reg->ftr_bits[j - 1].shift;
+			width = ftr_reg->ftr_bits[j].width;
+			shift = ftr_reg->ftr_bits[j].shift;
+			WARN(prev_shift < (shift + width),
+				"%s has feature overlap at shift %d\n",
+				ftr_reg->name, ftr_bits->shift);
+		}
 
-	/* Check that the array is sorted so that we can do the binary search */
-	for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
+		/*
+		 * Skip the first register. There is nothing to
+		 * compare against for now.
+		 */
+		if (i == 0)
+			continue;
+		/*
+		 * Registers here must be sorted in ascending order with respect
+		 * to sys_id for subsequent binary search in get_arm64_ftr_reg()
+		 * to work correctly.
+		 */
 		BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
+	}
 }
 
 /*
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Anshuman,

Quoted 4 lines · Anshuman Khandual

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.

The patch looks fine to me. I have a few minor nits on the coding style, feel free to ignore.

Quoted 27 lines
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Mark Brown <[email protected]>
Cc Mark Rutland <[email protected]>

---
Applies on 5.8-rc3.

Changes in V2:

- Replaced WARN_ON() with WARN() dropping the conditional block per Suzuki

Changes in V1: (https://patchwork.kernel.org/patch/11606285/)

  arch/arm64/kernel/cpufeature.c | 45 +++++++++++++++++++++++++++++++---
  1 file changed, 42 insertions(+), 3 deletions(-)
arch/arm64/kernel/cpufeature.c +0 -0
index 9f63053a63a9..7bd7e6f936a5 100644
@@ -697,11 +697,50 @@ static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 

arch/arm64/kernel/cpufeature.c static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, · line 706

Quoted 10 lines
arch/arm64/kernel/cpufeature.c +4 -1
  static void __init sort_ftr_regs(void)
  {
-	int i;
+	
 
+	unsigned int i;
 
 
+
+	for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) {
	 const struct arm64_ftr_reg *ftr_reg = arm64_ftr_regs[i].reg;
 	 const struct arm64_ftr_bits *ftr_bits = ftr_reg->ftr_bits;
	 unsigned int j = 0;

arch/arm64/kernel/cpufeature.c static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, · line 711

Quoted 7 lines
arch/arm64/kernel/cpufeature.c +7 -0
+		/*
+		 * Features here must be sorted in descending order with respect
+		 * to their shift values and should not overlap with each other.
+		 */
+		ftr_reg = arm64_ftr_regs[i].reg;
+		for (ftr_bits = ftr_reg->ftr_bits,
+				ftr_bits->width != 0; ftr_bits++, j++) {
			unsigned int shift = ftr_bits->shift;
			unsigned int width = ftr_bits->width;
			unsigned int prev_shift;

arch/arm64/kernel/cpufeature.c static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, · line 714

Quoted 3 lines
arch/arm64/kernel/cpufeature.c +3 -0
+			WARN((ftr_bits->shift  + ftr_bits->width) > 64,
+				"%s has invalid feature at shift %d\n",
+				ftr_reg->name, ftr_bits->shift);
			WARN(shit + width > 64, ....);

Either way,