Skip to content

Commit 51cf71d

Browse files
rnavgregkh
authored andcommitted
powerpc/lib: Add helper to check if offset is within conditional branch range
upstream commit 4549c3e Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper. Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> Acked-by: Song Liu <songliubraving@fb.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/442b69a34ced32ca346a0d9a855f3f6cfdbbbd41.1633464148.git.naveen.n.rao@linux.vnet.ibm.com Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7429322 commit 51cf71d

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎arch/powerpc/include/asm/code-patching.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define BRANCH_ABSOLUTE 0x2
2424

2525
bool is_offset_in_branch_range(long offset);
26+
bool is_offset_in_cond_branch_range(long offset);
2627
int create_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
2728
unsigned long target, int flags);
2829
int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,

‎arch/powerpc/lib/code-patching.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ bool is_offset_in_branch_range(long offset)
230230
return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
231231
}
232232

233+
bool is_offset_in_cond_branch_range(long offset)
234+
{
235+
return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
236+
}
237+
233238
/*
234239
* Helper to check if a given instruction is a conditional branch
235240
* Derived from the conditional checks in analyse_instr()
@@ -283,7 +288,7 @@ int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
283288
offset = offset - (unsigned long)addr;
284289

285290
/* Check we can represent the target in the instruction format */
286-
if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
291+
if (!is_offset_in_cond_branch_range(offset))
287292
return 1;
288293

289294
/* Mask out the flags and target, so they don't step on each other. */

‎arch/powerpc/net/bpf_jit.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@
7171
#define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0)
7272
#endif
7373

74-
static inline bool is_nearbranch(int offset)
75-
{
76-
return (offset < 32768) && (offset >= -32768);
77-
}
78-
7974
/*
8075
* The fly in the ointment of code size changing from pass to pass is
8176
* avoided by padding the short branch case with a NOP. If code size differs
@@ -84,7 +79,7 @@ static inline bool is_nearbranch(int offset)
8479
* state.
8580
*/
8681
#define PPC_BCC(cond, dest) do { \
87-
if (is_nearbranch((dest) - (ctx->idx * 4))) { \
82+
if (is_offset_in_cond_branch_range((long)(dest) - (ctx->idx * 4))) { \
8883
PPC_BCC_SHORT(cond, dest); \
8984
EMIT(PPC_RAW_NOP()); \
9085
} else { \

0 commit comments

Comments
 (0)