Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp403156ybt; Wed, 8 Jul 2020 02:46:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwo5ug+ZcsdCzUBA+57ZB5UUlbarbEyfzTtfoJdSFGYlULTHC6hZjGYENHA/jFQB9/qwy8s X-Received: by 2002:a17:906:2786:: with SMTP id j6mr50260445ejc.216.1594201603188; Wed, 08 Jul 2020 02:46:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594201603; cv=none; d=google.com; s=arc-20160816; b=k8GUkFnWePTv6xu2KI60awov9UeHth0lWPi4uvs3/IsZbDEEy3dN0nadeQQdpGH32Y pmf5Cjrn8fwJIzrRobyrGJg1zWAQTwuFiuT9bvlt8OupLj516vpwMsDPcyEjk2QO7h1H VUNNcmZHP7dSe7RryQB747UAVCbKVozpnRX0G1sc4yvrZpWEolJEysj2YWSgjW7XwmXt C4rUQ1Q3yEusFTBIsTb6HYnM9XlRTZZH+3QcYu2mxg9PN4TRaI7Kq2wkyG/8hseQFmnN a1q3QvONphTi3kkURCzqg7oEXXyJBP6I9ilUxv7CgbvjancLjUWcixfZg+zMcMG2Qt1/ htsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=VAWGG1zqxRqRuGb2A3UgYeJMqrMvB1aw8Y5Gt1LdmlQ=; b=l21PXwACvr9nfe8xObT99JuYIZffoDK4Y4l8S1JY6XS8fT1lUzjtHdN6prP9bLcWTU CzSGbAvxoadZ/4CIY/oYl2S2N5NJnn9qEkUdIzSeNI1CSvtuptncE3+53CxWhK544rua 7WVr8NVDSIzRhZd49EyK9NP2mhrWl4oQllHu+6TekeIMLDJ2r8Kka7N8wRyzLVO8kRKl CHAzDRAFgUMJLOwm/pF4JtCanLGR73CCFzZzO/jXF59judl2hG9NZdalHSoCE1haICD8 nOOUi7eBqs0Iqfj1c3kTb4gPC6okaYV3lMCmvlR84KPIjHwFD8wyvf5DfhBKVxpmAC5D 4ROg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bs20si17141449edb.444.2020.07.08.02.46.20; Wed, 08 Jul 2020 02:46:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728246AbgGHJpS convert rfc822-to-8bit (ORCPT + 99 others); Wed, 8 Jul 2020 05:45:18 -0400 Received: from mail-yb1-f194.google.com ([209.85.219.194]:41400 "EHLO mail-yb1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726302AbgGHJpS (ORCPT ); Wed, 8 Jul 2020 05:45:18 -0400 Received: by mail-yb1-f194.google.com with SMTP id a15so7145332ybs.8; Wed, 08 Jul 2020 02:45:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Z+Hv0F2P4n1XVQheQtChcgxf0BXLLqGWuYKeMgWau8Q=; b=VsL9TocYv82BCKJIesnEwlQYhc8mop/AjwUKGxG3rlMcj/TKP5+NmB/9ZC1garq3IN N8jwUmjW2WRaXFt+9YSErT7PZYHgjhlN9t03DYRRgS3bI+IXVIij24TUvNqgZepC2tl8 iWnDV07uBOh3pGI5gDPxhciXi3zWoezvrcvUOXbTwKwrBsIRBep1knHOynUHqYIaOFXn X2Q2Kt6rUHZgXhRaTyFVhNmvRW6g7VVz4gvwktl9RFpYMMZKm95wmxyAaCVdZ1oJglfC bo058XQ3jLVy+ZY6dyyuapzh5rfMCDHNQ5bU6MRLvG63skIpuUn/V60mxVqoz7Tb1Kw/ mN2g== X-Gm-Message-State: AOAM532HV6x1dt6V7WFRsaLJdkHAsTCS/A1i6ezEet4fLLp/FILp20dV Ck5F1RNgEJJXH2jbV1n8BfBUFaCixpQ5b4J5BMc= X-Received: by 2002:a25:4603:: with SMTP id t3mr85348810yba.471.1594201516575; Wed, 08 Jul 2020 02:45:16 -0700 (PDT) MIME-Version: 1.0 References: <20200707150748.14651-1-kernel@esmil.dk> In-Reply-To: From: Emil Renner Berthing Date: Wed, 8 Jul 2020 11:45:05 +0200 Message-ID: Subject: Re: [PATCH v1] riscv: Add jump-label implementation To: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= Cc: linux-riscv , Palmer Dabbelt , Paul Walmsley , linux-doc@vger.kernel.org, Jonathan Corbet , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 8 Jul 2020 at 09:42, Björn Töpel wrote: > > On Tue, 7 Jul 2020 at 17:08, Emil Renner Berthing wrote: > > > > Add jump-label implementation based on the ARM64 version. > > > > Tested on the HiFive Unleashed board. > > > > I took your patch for a spin on qemu. The boot selftest > (CONFIG_STATIC_KEYS_SELFTEST=y) passes, but the module test > (CONFIG_TEST_STATIC_KEYS=m) does not. > > When I run the in "test tools/testing/selftests/static_keys" (this > simply loads the test_static_key_base.ko and test_static_keys.ko > modules) I get: > > [ 134.090464] test_static_keys: Unknown relocation type 36 > > 36 is the relocation type R_RISCV_ADD64, which is not handled in > arch/riscv/kernel/module.c. > > If you dump the relocation entries for test_static_keys.ko > (riscv64-linux-gnu-objdump -r test_static_keys.ko), you'll see that: > > RELOCATION RECORDS FOR [__jump_table]: > OFFSET TYPE VALUE > 0000000000000000 R_RISCV_ADD32 .L1^B1 > 0000000000000000 R_RISCV_SUB32 .L0 > 0000000000000004 R_RISCV_ADD32 .L3 > 0000000000000004 R_RISCV_SUB32 .L0 > 0000000000000008 R_RISCV_ADD64 old_true_key+0x0000000000000001 > 0000000000000008 R_RISCV_SUB64 .L0 > ... > > It would be great if you could add a patch for that as well (separate, > same series). R_RISCV_ADD64 *and* R_RISCV_SUB64 are currently > unhandled by module.c. On it! I'll also add the CONFIG_JUMP_LABEL to defconfig to the next version. > > Cheers, > Björn > > > Signed-off-by: Emil Renner Berthing > > --- > > > > Changes since RFC: > > - Use RISCV_PTR and RISCV_LGPTR macros to match struct jump_table > > also in 32bit kernels. > > - Remove unneeded branch ? 1 : 0, thanks Björn > > - Fix \n\n instead of \n\t mistake > > > > .../core/jump-labels/arch-support.txt | 2 +- > > arch/riscv/Kconfig | 2 + > > arch/riscv/include/asm/jump_label.h | 59 +++++++++++++++++++ > > arch/riscv/kernel/Makefile | 2 + > > arch/riscv/kernel/jump_label.c | 44 ++++++++++++++ > > 5 files changed, 108 insertions(+), 1 deletion(-) > > create mode 100644 arch/riscv/include/asm/jump_label.h > > create mode 100644 arch/riscv/kernel/jump_label.c > > > > diff --git a/Documentation/features/core/jump-labels/arch-support.txt b/Documentation/features/core/jump-labels/arch-support.txt > > index 632a1c7aefa2..760243d18ed7 100644 > > --- a/Documentation/features/core/jump-labels/arch-support.txt > > +++ b/Documentation/features/core/jump-labels/arch-support.txt > > @@ -23,7 +23,7 @@ > > | openrisc: | TODO | > > | parisc: | ok | > > | powerpc: | ok | > > - | riscv: | TODO | > > + | riscv: | ok | > > | s390: | ok | > > | sh: | TODO | > > | sparc: | ok | > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > > index fd639937e251..d2f5c53fdc19 100644 > > --- a/arch/riscv/Kconfig > > +++ b/arch/riscv/Kconfig > > @@ -46,6 +46,8 @@ config RISCV > > select GENERIC_TIME_VSYSCALL if MMU && 64BIT > > select HANDLE_DOMAIN_IRQ > > select HAVE_ARCH_AUDITSYSCALL > > + select HAVE_ARCH_JUMP_LABEL > > + select HAVE_ARCH_JUMP_LABEL_RELATIVE > > select HAVE_ARCH_KASAN if MMU && 64BIT > > select HAVE_ARCH_KGDB > > select HAVE_ARCH_KGDB_QXFER_PKT > > diff --git a/arch/riscv/include/asm/jump_label.h b/arch/riscv/include/asm/jump_label.h > > new file mode 100644 > > index 000000000000..d5fb342bfccf > > --- /dev/null > > +++ b/arch/riscv/include/asm/jump_label.h > > @@ -0,0 +1,59 @@ > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > +/* > > + * Copyright (C) 2020 Emil Renner Berthing > > + * > > + * Based on arch/arm64/include/asm/jump_label.h > > + */ > > +#ifndef __ASM_JUMP_LABEL_H > > +#define __ASM_JUMP_LABEL_H > > + > > +#ifndef __ASSEMBLY__ > > + > > +#include > > + > > +#define JUMP_LABEL_NOP_SIZE 4 > > + > > +static __always_inline bool arch_static_branch(struct static_key *key, > > + bool branch) > > +{ > > + asm_volatile_goto( > > + " .option push \n\t" > > + " .option norelax \n\t" > > + " .option norvc \n\t" > > + "1: nop \n\t" > > + " .option pop \n\t" > > + " .pushsection __jump_table, \"aw\" \n\t" > > + " .align " RISCV_LGPTR " \n\t" > > + " .long 1b - ., %l[label] - . \n\t" > > + " " RISCV_PTR " %0 - . \n\t" > > + " .popsection \n\t" > > + : : "i"(&((char *)key)[branch]) : : label); > > + > > + return false; > > +label: > > + return true; > > +} > > + > > +static __always_inline bool arch_static_branch_jump(struct static_key *key, > > + bool branch) > > +{ > > + asm_volatile_goto( > > + " .option push \n\t" > > + " .option norelax \n\t" > > + " .option norvc \n\t" > > + "1: jal zero, %l[label] \n\t" > > + " .option pop \n\t" > > + " .pushsection __jump_table, \"aw\" \n\t" > > + " .align " RISCV_LGPTR " \n\t" > > + " .long 1b - ., %l[label] - . \n\t" > > + " " RISCV_PTR " %0 - . \n\t" > > + " .popsection \n\t" > > + : : "i"(&((char *)key)[branch]) : : label); > > + > > + return false; > > +label: > > + return true; > > +} > > + > > +#endif /* __ASSEMBLY__ */ > > +#endif /* __ASM_JUMP_LABEL_H */ > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > index b355cf485671..a5287ab9f7f2 100644 > > --- a/arch/riscv/kernel/Makefile > > +++ b/arch/riscv/kernel/Makefile > > @@ -53,4 +53,6 @@ endif > > obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > > obj-$(CONFIG_KGDB) += kgdb.o > > > > +obj-$(CONFIG_JUMP_LABEL) += jump_label.o > > + > > clean: > > diff --git a/arch/riscv/kernel/jump_label.c b/arch/riscv/kernel/jump_label.c > > new file mode 100644 > > index 000000000000..55b2d742efe1 > > --- /dev/null > > +++ b/arch/riscv/kernel/jump_label.c > > @@ -0,0 +1,44 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * Copyright (C) 2020 Emil Renner Berthing > > + * > > + * Based on arch/arm64/kernel/jump_label.c > > + */ > > +#include > > +#include > > +#include > > + > > +#define RISCV_INSN_NOP 0x00000013 > > +#define RISCV_INSN_JAL 0x0000006f > > + > > +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) { > > + u32 offset = jump_entry_target(entry) - jump_entry_code(entry); > > + > > + insn = RISCV_INSN_JAL | > > + ((offset & GENMASK(19, 12)) << (12 - 12)) | > > + ((offset & GENMASK(11, 11)) << (20 - 11)) | > > + ((offset & GENMASK(10, 1)) << (21 - 1)) | > > + ((offset & GENMASK(20, 20)) << (31 - 20)); > > + } else > > + insn = RISCV_INSN_NOP; > > + > > + patch_text_nosync(addr, &insn, sizeof(insn)); > > +} > > + > > +void arch_jump_label_transform_static(struct jump_entry *entry, > > + enum jump_label_type type) > > +{ > > + /* > > + * We use the same instructions in the arch_static_branch and > > + * arch_static_branch_jump inline functions, so there's no > > + * need to patch them up here. > > + * The core will call arch_jump_label_transform when those > > + * instructions need to be replaced. > > + */ > > +} > > -- > > 2.27.0 > >