]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
x86/insn: Simplify for_each_insn_prefix()
authorPeter Zijlstra <peterz@infradead.org>
Thu, 25 Sep 2025 08:19:05 +0000 (10:19 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Thu, 16 Oct 2025 09:13:48 +0000 (11:13 +0200)
Use the new-found freedom of allowing variable declarions inside
for() to simplify the for_each_insn_prefix() iterator to no longer
need an external temporary.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
arch/x86/boot/compressed/sev-handle-vc.c
arch/x86/include/asm/insn.h
arch/x86/kernel/kprobes/core.c
arch/x86/kernel/uprobes.c
arch/x86/lib/insn-eval.c
tools/arch/x86/include/asm/insn.h

index 7530ad8b768b1dec7b3596e34e6b83d4b9723bd7..030001b465541fe484b757707809e7d63c155bc0 100644 (file)
 bool insn_has_rep_prefix(struct insn *insn)
 {
        insn_byte_t p;
-       int i;
 
        insn_get_prefixes(insn);
 
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                if (p == 0xf2 || p == 0xf3)
                        return true;
        }
index 091f88c8254d3a25295805efd08b4e651239ab48..846d21c1a7f88c7c6a9d233ff0abd7412a02b724 100644 (file)
@@ -312,7 +312,6 @@ static inline int insn_offset_immediate(struct insn *insn)
 /**
  * for_each_insn_prefix() -- Iterate prefixes in the instruction
  * @insn: Pointer to struct insn.
- * @idx:  Index storage.
  * @prefix: Prefix byte.
  *
  * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
@@ -321,8 +320,8 @@ static inline int insn_offset_immediate(struct insn *insn)
  * Since prefixes.nbytes can be bigger than 4 if some prefixes
  * are repeated, it cannot be used for looping over the prefixes.
  */
-#define for_each_insn_prefix(insn, idx, prefix)        \
-       for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+#define for_each_insn_prefix(insn, prefix)     \
+       for (int idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
 
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
index 3863d7709386fc4891fdb79acd9c7117f3cef6d2..c1fac3a9fecc20311402d809f0b633afccc3d253 100644 (file)
@@ -141,7 +141,6 @@ bool can_boost(struct insn *insn, void *addr)
 {
        kprobe_opcode_t opcode;
        insn_byte_t prefix;
-       int i;
 
        if (search_exception_tables((unsigned long)addr))
                return false;   /* Page fault may occur on this address. */
@@ -154,7 +153,7 @@ bool can_boost(struct insn *insn, void *addr)
        if (insn->opcode.nbytes != 1)
                return false;
 
-       for_each_insn_prefix(insn, i, prefix) {
+       for_each_insn_prefix(insn, prefix) {
                insn_attr_t attr;
 
                attr = inat_get_opcode_attribute(prefix);
index 6318898eb9d0d9c43158268d3eee826c6b417a6e..a563e90832d7a632090dd4dd2b97efeba6dfe27f 100644 (file)
@@ -259,9 +259,8 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 static bool is_prefix_bad(struct insn *insn)
 {
        insn_byte_t p;
-       int i;
 
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                insn_attr_t attr;
 
                attr = inat_get_opcode_attribute(p);
@@ -1404,7 +1403,6 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
        u8 opc1 = OPCODE1(insn);
        insn_byte_t p;
-       int i;
 
        if (insn_is_nop(insn))
                goto setup;
@@ -1437,7 +1435,7 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
         * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
         * No one uses these insns, reject any branch insns with such prefix.
         */
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                if (p == 0x66)
                        return -ENOTSUPP;
        }
index c991dac1a38e0cee2dadf2c3ac52a61492a616e6..e03eeec55cfeb50d4bb81841a73e40ec63d83d32 100644 (file)
@@ -63,11 +63,10 @@ static bool is_string_insn(struct insn *insn)
 bool insn_has_rep_prefix(struct insn *insn)
 {
        insn_byte_t p;
-       int i;
 
        insn_get_prefixes(insn);
 
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                if (p == 0xf2 || p == 0xf3)
                        return true;
        }
@@ -92,13 +91,13 @@ bool insn_has_rep_prefix(struct insn *insn)
 static int get_seg_reg_override_idx(struct insn *insn)
 {
        int idx = INAT_SEG_REG_DEFAULT;
-       int num_overrides = 0, i;
+       int num_overrides = 0;
        insn_byte_t p;
 
        insn_get_prefixes(insn);
 
        /* Look for any segment override prefixes. */
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                insn_attr_t attr;
 
                attr = inat_get_opcode_attribute(p);
@@ -1701,7 +1700,6 @@ bool insn_is_nop(struct insn *insn)
        u8 sib = 0, sib_scale, sib_index, sib_base;
        u8 nrex, rex;
        u8 p, rep = 0;
-       int i;
 
        if ((nrex = insn->rex_prefix.nbytes)) {
                rex = insn->rex_prefix.bytes[nrex-1];
@@ -1741,7 +1739,7 @@ bool insn_is_nop(struct insn *insn)
                modrm_rm = sib_base;
        }
 
-       for_each_insn_prefix(insn, i, p) {
+       for_each_insn_prefix(insn, p) {
                if (p == 0xf3) /* REPE */
                        rep = 1;
        }
@@ -1789,7 +1787,7 @@ bool insn_is_nop(struct insn *insn)
                if (sib && (sib_scale != 0 || sib_index != 4)) /* (%reg, %eiz, 1) */
                        return false;
 
-               for_each_insn_prefix(insn, i, p) {
+               for_each_insn_prefix(insn, p) {
                        if (p != 0x3e) /* DS */
                                return false;
                }
index c683d609934b796f4d2fca4ef49b4552c5b22af1..8f10f29433703fefc7b3f8e6f2f76cec453ae023 100644 (file)
@@ -312,7 +312,6 @@ static inline int insn_offset_immediate(struct insn *insn)
 /**
  * for_each_insn_prefix() -- Iterate prefixes in the instruction
  * @insn: Pointer to struct insn.
- * @idx:  Index storage.
  * @prefix: Prefix byte.
  *
  * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
@@ -321,8 +320,8 @@ static inline int insn_offset_immediate(struct insn *insn)
  * Since prefixes.nbytes can be bigger than 4 if some prefixes
  * are repeated, it cannot be used for looping over the prefixes.
  */
-#define for_each_insn_prefix(insn, idx, prefix)        \
-       for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+#define for_each_insn_prefix(insn, prefix)     \
+       for (int idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
 
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e