Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp318523ybt; Wed, 8 Jul 2020 00:20:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3eGBLS6xCszSEcFB2yFQ5OacP98NXLAQOH7T5M3vC+tTclM2G9/9fqeebTHQIv1dkJiMe X-Received: by 2002:a50:ba8b:: with SMTP id x11mr67274008ede.201.1594192831423; Wed, 08 Jul 2020 00:20:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594192831; cv=none; d=google.com; s=arc-20160816; b=oAO0eNxrsaW0sHuRw5/5zp79yFn0irmjMzHtrOgMUR0NFFJJiVgqwh1MUv7D6/tmEX kUFUeCoxBEyQ5KtHQWnIMWAcP8TRLMsfUr+EQvnQ2SxgKyL0+IQzC2VhxEG2X2Q9DtWv /ub4PdKQ8LefnvpTn+sAJNRKlORCaP57iB3W+8xy7bUsAk1Kw/djgnafhKroM6XVbzFT Kdq/opfmoMQ+H8bzreIXNXschoaHm8QUAC5MhP6Ca6h6dITrQgqIX+wW6BMGZwQsahC4 A/mvfEx6Ms+L4hWX44NOQU147NIBQMl1/IvcFzMf+YXNSeHGaAb76NrnfOWQpPAPamyN PKGg== 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 :dkim-signature; bh=+bENu48E3Xibja+rVmQV5HpKpq/BuWjmLHS6VhU2gVA=; b=HTEGNfhK/bQJs9EroJjn9hAs57s7ASPWnp/l1SAG1RBIn5MlXrmSCL2IbQKjFgI+d/ LR0It5AFfULWbx4jp33DULGfjrl5DPjA+pX9ePkSX+TQdeQfkisGqWxqUIISRIrQi6VO ot1LkF6Ahsex2oCf14+rQ9unUFu+2ElZIpw0uk8A1XpilbJJ09jXGVE7zJo+6TPmRtWW a1EPTOALwOiq+El294ePdqhiNPWbmmsAaCEElTPoFWFS67O9rYY/kWJBYFsCA4+bhKVz O+1YRkw4bEfqKBMtC23oOm+wF9Hb6d+6IKYT5OhhuoTAcoWZirlqFmPOPlQEBd80eq8B HAmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DVEtrkM2; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bm13si16131679edb.426.2020.07.08.00.20.08; Wed, 08 Jul 2020 00:20:31 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DVEtrkM2; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730232AbgGHHTj (ORCPT + 99 others); Wed, 8 Jul 2020 03:19:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729972AbgGHHTi (ORCPT ); Wed, 8 Jul 2020 03:19:38 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F7C8C061755; Wed, 8 Jul 2020 00:19:38 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id k6so47732730wrn.3; Wed, 08 Jul 2020 00:19:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=+bENu48E3Xibja+rVmQV5HpKpq/BuWjmLHS6VhU2gVA=; b=DVEtrkM2Z/thFTtE+Z2m5dn42jOXTWGBE6+5jKVGC48OQJROm+i7uz5gg/efEAcq8d Cubh9LWnbap/1BndP7PUlVdMQ0UtNy/t256u9P9oPz9Vg2wnv7jnWVFkhVVe6Thu1oVu zbq1tEfNtqZdsdtTS/bZEZhMQgIjkca8smYcFkSHPbMep9vKYC2f3iKqzNxLu/6pKcNk kjI5IuRlO02tEshvAONg/T3F1PVaqqldDLdGhNmW2BGu60/8Q8rMWvZFqAOtZSVEzg/8 EAr+/KrGY9Fiq9zNeoCPNyi7jwBqT91csMk1DSqdmyakbjmmRL+HzYYVphYOLKe93w9w am4A== 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=+bENu48E3Xibja+rVmQV5HpKpq/BuWjmLHS6VhU2gVA=; b=tWMZjYVamtkCWrPoKwoDUbXPHz08e8GI2liNbEN7mzvMgYwjxo82w3bii+9ZMRi04C 8ieAvrpIAuu8Lr1aZEZ+7ygRA9P0kNW8W0XQk4+U1EDH2cdGOnzZ2ldj+12HmpQpc6a+ RGQ/PZ0Z6iH+MjtV2hnVlcHnNrUcuqrKgwq19gzp7HlBI+n54mb65VpUMDEEz50wk444 jRc44sfNvXuNgS3WF5H2Jdj0IGytvaCkatIpJ3euZBqpLigGLGJMkRzjbaB2jK28q5Cr AsBdtI/YeIzIaSSMyNpQc5EairM4XYF2bcTD6xm80TmQ3ZQriDRDKDe9A9U8GH6oGNo/ 3DOg== X-Gm-Message-State: AOAM532lAPa19StfO9+C3fj/oAA5hIcVe2bT7wQY9qECiJ1Pm5s9hZul IfjSxdRBVcQZLTO6EV2D25jxeYDL5Vls6vWejiE= X-Received: by 2002:adf:82b8:: with SMTP id 53mr61795092wrc.172.1594192777045; Wed, 08 Jul 2020 00:19:37 -0700 (PDT) MIME-Version: 1.0 References: <20200707150748.14651-1-kernel@esmil.dk> In-Reply-To: <20200707150748.14651-1-kernel@esmil.dk> From: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= Date: Wed, 8 Jul 2020 09:19:25 +0200 Message-ID: Subject: Re: [PATCH v1] riscv: Add jump-label implementation To: Emil Renner Berthing 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: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > > 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=C3=B6rn > - 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/D= ocumentation/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 Missed one thing in the last reply; Please update defconfig with CONFIG_JUMP_LABEL=3Dy. Bj=C3=B6rn > 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 *k= ey, > + 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) +=3D cpu-hotplug.o > obj-$(CONFIG_KGDB) +=3D kgdb.o > > +obj-$(CONFIG_JUMP_LABEL) +=3D jump_label.o > + > clean: > diff --git a/arch/riscv/kernel/jump_label.c b/arch/riscv/kernel/jump_labe= l.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 =3D (void *)jump_entry_code(entry); > + u32 insn; > + > + if (type =3D=3D JUMP_LABEL_JMP) { > + u32 offset =3D jump_entry_target(entry) - jump_entry_code= (entry); > + > + insn =3D 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 =3D 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 >