Re: Why is text_mutex used in jump_label_transform for x86_64

From: Peter Zijlstra
Date: Fri Mar 20 2020 - 06:27:20 EST


On Thu, Mar 19, 2020 at 09:49:04PM +0800, chengjian (D) wrote:
> Hiïeveryone
>
> I'm sorry to disturb you. I have a problem about jump_label, and a bit
> confused about the code
>
> I noticed that text_mutex is used in this function under x86_64
> architecture,
> but other architectures do not.
>
> in arch/x86/kernel/jump_label.c
> ÂÂÂÂÂÂÂ static void __ref jump_label_transform(struct jump_entry *entry,
> ÂÂÂÂÂÂÂÂÂÂÂÂ enum jump_label_type type,
> ÂÂÂÂÂÂÂÂÂÂÂÂ int init)
> ÂÂÂÂÂÂÂ {
> ÂÂÂÂÂÂÂÂ mutex_lock(&text_mutex);
> ÂÂÂÂÂÂÂÂ __jump_label_transform(entry, type, init);
> ÂÂÂÂÂÂÂÂ mutex_unlock(&text_mutex);
>
> in arch/arm64/kernel/jump_label.c
>
> ÂÂÂÂÂÂÂ void arch_jump_label_transform(struct jump_entry *entry,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum jump_label_type type)
> ÂÂÂÂÂÂÂ {
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ void *addr = (void *)jump_entry_code(entry);
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u32 insn;
>
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (type == JUMP_LABEL_JMP) {
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ insn =
> aarch64_insn_gen_branch_imm(jump_entry_code(entry),
> jump_entry_target(entry),
> AARCH64_INSN_BRANCH_NOLINK);
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ } else {
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ insn = aarch64_insn_gen_nop();
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ aarch64_insn_patch_text_nosync(addr, insn);
> ÂÂÂÂÂÂÂ }
>
>
> Is there anything wrong with x86
>
> or
>
> is this missing for other architectures?

It depends on the architecture details of how self-modifying code works.
In particular, x86 is a variable instruction length architecture and
needs extreme care -- it's implementation requires there only be a
single text modifier at any one time, hence the use of text_mutex.

ARM64 OTOH is, like most RISC based architectures, a fixed width
instruction architecture. And in particular it can re-write certain
(branch) instructions with impunity (see their
aarch64_insn_patch_text_nosync()). Which is why they don't need
additional serialization.