From nobody Sat Oct 4 17:32:56 2025 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6C3D1A9F9E; Thu, 14 Aug 2025 03:27:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755142062; cv=none; b=IdxJZLOf+1XRs6aLgC2ExNnG6qen6X9KwJhJOwcfY9UrTPN+a2KNuWKxLEIEzwC7u2LJPnPaH8qLQe7CtPoP3aVzRRe0JTQxEBNAsJ6qcxS7f8jA9ylcoe53qw34bwGOuyu+nqWN/nWCuM3rD4038ZtoPGQe6PnUOs0xHfuCbWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755142062; c=relaxed/simple; bh=4paha/HlT7sl6TkAQyu3pluDW/iXskSC/4ZDKp2WyFk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i/UqiY6/LkkXsNkfQnjzbie8BGUKra+HVM0hWWTPu5x5wWlL2O20s+ebzqCnia+QMYP/G0rO5X0NGSuxr+InX76Sjx9l1Ywye1cG3uOWHCLbEBtA5AYsuIqTPeMratIzuuK4dvULeXQqQUmEV0w0T3qd0JqsC95Q3oyow/JI9Ts= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CSI4wzHJ; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CSI4wzHJ" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-32326789e06so440053a91.1; Wed, 13 Aug 2025 20:27:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755142060; x=1755746860; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5WrqE+v5L4PZm59cqGTJ6nmR9SkmSoYFPmnwdRNwHng=; b=CSI4wzHJ2SIh5xyFVUKiOydMIaVGFtvUS2E+CEKpMkKRHUtgFa+fNYvuCtc28c/uaN ZHvTNDsCi0hgqBvHI89klUjjCdporvzl/137jNcIw/S2WHBX6v52igzlAIC8Q9ZL9d8k +ui0upedODm3kf6MeESXFl6A2tFpURxSW/BXhfiKUGocCjDlBfIinGghUbVVfGzOZGQS lsTmbQNx+7MDzP+rV7/p6b3iW/MiZovE20Msm36O0C8qJQg32xcipniCYPGoSohU7MgO R4S8QyqheYtvhjMyzYD4yy3PcTdzWn75Di5EXgjIVZCah9YX5M+Kg+PehzPHdGaBEB0V /YCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755142060; x=1755746860; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5WrqE+v5L4PZm59cqGTJ6nmR9SkmSoYFPmnwdRNwHng=; b=j1e2y6NWPscOpkPBP2EGN6/FRSY88wDHrJETRma8lMcVJaZlbxtR1V9JJ1uDemYMpP 5uAKpnGWBqXeetFKnJfZRERdmknIED8uwx6/ILkp7FB0lEwG6fP+jPVvMXLWz2URedTY gis4IYXIrJLPIiEzla+KOmnou3cVhjpfmMUm9nRq5AL25zOik77JyDBL86YxObjZkiLb D+HWfI4czdlWSC3XBX9ls70ziInYfG7gTRjA2Y7E62ZNhtwlpLYh+3tUPtlXvRidqBrE NeNAmRlxaRG8d4dbxWHAqW3wCedVusr543fI2LaGxsefLHzeRlYIsRTq43uBcnY+WKqo 1cEw== X-Forwarded-Encrypted: i=1; AJvYcCXEtlx4MMNijOyWZNXkEiUukKMnpmSsbuT7E2lo7erQX59vsgetOMaiEvEkPrmmhuSLBj0jh9HXeUJZ4TQJ2g==@vger.kernel.org X-Gm-Message-State: AOJu0YzI+unGlvWmeQ5LMy1hZ/eFXw2hEQzPlTdd65m4giUY70MbKRJO RwTcCCc4LHmSx+/7Y16A0J8iLOGXNdKdDRUm6EpNKiV5EMh4u+J5HvcsmvIr9X8Q+GCbHRJG X-Gm-Gg: ASbGncuwSBlOzwcARu0y0jYvcOCDUuoDUyqGPZtY8mEGw8Z7zTjJUIYeT5fmIx+32+C qQBcgtzQ1K6BU2smk3wJYTVFywPw2N3ndBCW3hdcEZ81XZumZIkLnei9U0Ljcm1LcPRN5MKxO3P I2o0M33Lsl9yvD0yE24CS3JWjBL1bqXbspHhdXDqGFiztTyMcMYqwE6S5wyJyvlzboz0qQvI11M 8ZB4iRjo+slrVIRXHtnriqQxlPHceQec+E4oIPAoP+3F7uPDGFVfOvxZAH3mEbqfr4fxDE1/ZDu CR7etbTTZIuDBVtA6IIp444/JT4HC3LPiI6VwhrZRK7WDepUFKBWYVdrq3eBfcganIpqmw6wgHi dg0g4LxhwToQ6XSjRmw== X-Google-Smtp-Source: AGHT+IEMcQEosrugs3FGOX1KUS9BiWtJ6aWaXFFkL8rUt34cDPgPvEakDh8KYD30WvMMDgy4eOy8og== X-Received: by 2002:a17:90b:17cd:b0:321:7528:ab43 with SMTP id 98e67ed59e1d1-3232b3d19d9mr1507662a91.24.1755142059692; Wed, 13 Aug 2025 20:27:39 -0700 (PDT) Received: from nyaos.. ([45.12.134.112]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-32331167ff6sm276009a91.27.2025.08.13.20.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Aug 2025 20:27:39 -0700 (PDT) From: ChenMiao To: Stafford Horne Cc: Linux Kernel , Linux OpenRISC , chenmiao , Jonas Bonn , Stefan Kristiansson , Arnd Bergmann , "Mike Rapoport (Microsoft)" , Andrew Morton , Luis Chamberlain , Geert Uytterhoeven , Sahil Siddiq , Johannes Berg , Nicolas Schier , Masahiro Yamada , Dave Hansen Subject: [PATCH v3 1/2] openrisc: Add text patching API support Date: Thu, 14 Aug 2025 03:27:01 +0000 Message-ID: <20250814032717.785395-2-chenmiao.ku@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250814032717.785395-1-chenmiao.ku@gmail.com> References: <20250814032717.785395-1-chenmiao.ku@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: chenmiao We need a text patching mechanism to ensure that in the subsequent implementation of jump_label, the code can be modified to the correct location. Therefore, FIX_TEXT_POKE0 has been added as a mapping area. Among these changes, we implement patch_map and support the patch_insn_write API for single instruction writing. Link: https://lore.kernel.org/openrisc/aJIC8o1WmVHol9RY@antec/T/#t Signed-off-by: chenmiao --- Changes in V3: - Removed the unimplemented and unsupported is_exit_text, added comments for the set_fixmap modification explaining why __init was removed, and added new comments for patch_insn_write. Changes in V2: - We modify the patch_insn_write(void *addr, const void *insn) API to patch_insn_write(void *addr, u32 insn), derectly support a single u32 instruction write to map memory. - Create a new file named insn-def.h to define the or1k insn macro size and more define in the future. Signed-off-by: chenmiao --- arch/openrisc/include/asm/Kbuild | 1 - arch/openrisc/include/asm/fixmap.h | 1 + arch/openrisc/include/asm/insn-def.h | 12 ++++ arch/openrisc/include/asm/text-patching.h | 13 ++++ arch/openrisc/kernel/Makefile | 1 + arch/openrisc/kernel/patching.c | 78 +++++++++++++++++++++++ arch/openrisc/mm/init.c | 10 ++- 7 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 arch/openrisc/include/asm/insn-def.h create mode 100644 arch/openrisc/include/asm/text-patching.h create mode 100644 arch/openrisc/kernel/patching.c diff --git a/arch/openrisc/include/asm/Kbuild b/arch/openrisc/include/asm/K= build index 2b1a6b00cdac..cef49d60d74c 100644 --- a/arch/openrisc/include/asm/Kbuild +++ b/arch/openrisc/include/asm/Kbuild @@ -9,4 +9,3 @@ generic-y +=3D spinlock.h generic-y +=3D qrwlock_types.h generic-y +=3D qrwlock.h generic-y +=3D user.h -generic-y +=3D text-patching.h diff --git a/arch/openrisc/include/asm/fixmap.h b/arch/openrisc/include/asm= /fixmap.h index aaa6a26a3e92..74000215064d 100644 --- a/arch/openrisc/include/asm/fixmap.h +++ b/arch/openrisc/include/asm/fixmap.h @@ -28,6 +28,7 @@ =20 enum fixed_addresses { FIX_EARLYCON_MEM_BASE, + FIX_TEXT_POKE0, __end_of_fixed_addresses }; =20 diff --git a/arch/openrisc/include/asm/insn-def.h b/arch/openrisc/include/a= sm/insn-def.h new file mode 100644 index 000000000000..dc8d16db1579 --- /dev/null +++ b/arch/openrisc/include/asm/insn-def.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2025 Chen Miao + */ + +#ifndef __ASM_INSN_DEF_H +#define __ASM_INSN_DEF_H + +/* or1k instructions are always 32 bits. */ +#define OPENRISC_INSN_SIZE 4 + +#endif /* __ASM_INSN_DEF_H */ diff --git a/arch/openrisc/include/asm/text-patching.h b/arch/openrisc/incl= ude/asm/text-patching.h new file mode 100644 index 000000000000..bffe828288c3 --- /dev/null +++ b/arch/openrisc/include/asm/text-patching.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2025 Chen Miao + */ + +#ifndef _ASM_PATCHING_H_ +#define _ASM_PATCHING_H_ + +#include + +int patch_insn_write(void *addr, u32 insn); + +#endif /* _ASM_PATCHING_H_ */ diff --git a/arch/openrisc/kernel/Makefile b/arch/openrisc/kernel/Makefile index 58e6a1b525b7..f0957ce16d6b 100644 --- a/arch/openrisc/kernel/Makefile +++ b/arch/openrisc/kernel/Makefile @@ -13,5 +13,6 @@ obj-$(CONFIG_SMP) +=3D smp.o sync-timer.o obj-$(CONFIG_STACKTRACE) +=3D stacktrace.o obj-$(CONFIG_MODULES) +=3D module.o obj-$(CONFIG_OF) +=3D prom.o +obj-y +=3D patching.o =20 clean: diff --git a/arch/openrisc/kernel/patching.c b/arch/openrisc/kernel/patchin= g.c new file mode 100644 index 000000000000..73ae449c6c4e --- /dev/null +++ b/arch/openrisc/kernel/patching.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (C) 2020 SiFive + * Copyright (C) 2025 Chen Miao + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +static DEFINE_RAW_SPINLOCK(patch_lock); + +static __always_inline void *patch_map(void *addr, int fixmap) +{ + uintptr_t uaddr =3D (uintptr_t) addr; + phys_addr_t phys; + + if (core_kernel_text(uaddr)) { + phys =3D __pa_symbol(addr); + } else { + struct page *page =3D vmalloc_to_page(addr); + BUG_ON(!page); + phys =3D page_to_phys(page) + offset_in_page(addr); + } + + return (void *)set_fixmap_offset(fixmap, phys); +} + +static void patch_unmap(int fixmap) +{ + clear_fixmap(fixmap); +} + +static int __patch_insn_write(void *addr, u32 insn) +{ + void *waddr =3D addr; + unsigned long flags =3D 0; + int ret; + + raw_spin_lock_irqsave(&patch_lock, flags); + + waddr =3D patch_map(addr, FIX_TEXT_POKE0); + + ret =3D copy_to_kernel_nofault(waddr, &insn, OPENRISC_INSN_SIZE); + local_icache_range_inv((unsigned long)waddr, + (unsigned long)waddr + OPENRISC_INSN_SIZE); + + patch_unmap(FIX_TEXT_POKE0); + + raw_spin_unlock_irqrestore(&patch_lock, flags); + + return ret; +} + +/* patch_insn_write - Write a single instruction to a specified memory loc= ation + * This API provides a single-instruction patching, primarily used for run= time + * code modification. + * By the way, the insn size must be 4 bytes. + */ +int patch_insn_write(void *addr, u32 insn) +{ + u32 *tp =3D addr; + int ret; + + if ((uintptr_t) tp & 0x3) + return -EINVAL; + + ret =3D __patch_insn_write(tp, insn); + + return ret; +} diff --git a/arch/openrisc/mm/init.c b/arch/openrisc/mm/init.c index e4904ca6f0a0..ac256c3d9c7a 100644 --- a/arch/openrisc/mm/init.c +++ b/arch/openrisc/mm/init.c @@ -226,7 +226,15 @@ static int __init map_page(unsigned long va, phys_addr= _t pa, pgprot_t prot) return 0; } =20 -void __init __set_fixmap(enum fixed_addresses idx, +/* Removing __init is necessary. Before supporting FIX_TEXT_POKE0, + * __init here indicates that it is valid during the initialization phase + * and is used for FIX_EARLYCON_MEM_BASE. However, attempting to support + * FIX_TEXT_POKE0 would introduce a bug. FIX_TEXT_POKE0 is used after the + * initialization phase, so __init would cause the function to become inva= lid. + * At that point, using set_fixmap would lead to accessing dirty data, + * which is invalid. + */ +void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) { unsigned long address =3D __fix_to_virt(idx); --=20 2.45.2 From nobody Sat Oct 4 17:32:56 2025 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C873525A334; Thu, 14 Aug 2025 03:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755142076; cv=none; b=elvkCLaqBBvRp7HklC0p9CR/19em8cJyXiQJp11Hsjoetnrkix3FVO5c4uiNl1qxdUeadvnme3x6nhsFw+G89A+gAAWuU6Eoqzs5XoJxOun8V58mdU0+jTWdP4uDpW5hF6U5OimzY6+DLSgJb02HSlz6psapxDoMVcBZ9TClgKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755142076; c=relaxed/simple; bh=kykKGfIHcuwt47+6LgW+9CplGFJGHW6DCFpYD4TgPCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=puFDd3/DIA1JA+UVDhOUluYF9A+GGFcrVQ/QM6xzR8Nhdrpvud+AqiA7VzcCsVwA+qiHiony3W7fTir85hD0ogHf2NXpMnO55TVd2RWRnSTN6z20ZR4HLl//7xE/iv/8Rx1UUjmwhK1N769iu8bIyoZpbQs8f2spam/0fu5YDE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=McVLsnHG; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="McVLsnHG" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-76e2eb6ce24so500056b3a.3; Wed, 13 Aug 2025 20:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755142074; x=1755746874; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vJyUJQGnwLdteNMwm5dTN2/3BR3DTUlHzhKHSmLgMZc=; b=McVLsnHGJ3UZFxda8X/An/eAL8ODH5mi24EXyBvftWDuJ/4eCjPWrSBWuCVXfhc706 R9la0jpuq51ws0rn5rsoZ6pmyuH4HplGu4GK+quDmMTIqpB/LBce5bzNIaz+gC7oAZQ3 hnWSYoVyHZWohB1+3ETQz5mptD6X84bwSHfN2tOowzQqBgn0Yua8+bCQMziZqqjEmzwZ DRLBJ1Q8hzctQrgPEzA3CCEIL3Z6AueKWzgdFvboOFWVjWIUdRNPRtcSHo2cekETqe+K Fz7TBQ9UMAuHDe0hvoHQHMNdtjJrxoNZ+V7N5ncFgpcg4duCltljOgzkiRPP9B39nKAC GIng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755142074; x=1755746874; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vJyUJQGnwLdteNMwm5dTN2/3BR3DTUlHzhKHSmLgMZc=; b=lS83lJV+MUGXKEGAPMHUFjDhDqoS/51C6u8qVLjcCh+IGxpiVMs8W4VIcMNTLgcyWl sVkzyVcLrUYvhz0fvZGKMPhsPi7kNH5Me4eYtzHwyawZs8QtXIx9vJIWVYf1IkjRkZn4 F3aFiubnrYGh1TaLQiMUtOp/sxUES/hVH/mDb5fNjA4/xgOVhU4iAYH2fNuLq5nnoW4r FBDL8acOeCdKzCBpmLBIlMduYD7CLS8xHt9+/r1r0JCPkBayKNCMmkS/oLINfjS4oDXg swsEp1gA4MWoJ7YMm+OhnGU9gGyFnhBjIHv4k7Qj17KBND3SN56x2ujBTT6MJFaEZW2N +/vw== X-Forwarded-Encrypted: i=1; AJvYcCV+gKRSwGbX6rI9+hKO+26eWIsbdXiAh4wvziMeUnNFY5za1LYjbPFk76Kp+sRN1tUXEC6QDFWkAu2TJXpgAnQ=@vger.kernel.org, AJvYcCV4wzO4YWaUCxBTmyJbr2M2i5nklCBhmqjOQKn5QIbPuToTMFnU2hd59T8VwztjjvE1BjFqLvffHLQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwI/yJp7Eh4m3RAoX5mdWQ6nZT9NuEva/j1qTDKQmQhX9nWFbgJ Jax13GBHORcw5wwavYf0I61gdrsEnYEAd+MCb2yoLTyyjBKnUyJo3Dmz X-Gm-Gg: ASbGncte+/WOlaN3iBVLssbZUYjMYbaPAfQrc7rU/f0jJ38clnHGg57zuFKsl28JSZB pzTSSw84CHHZYwT4C64ZJ+GyOjGBnjvwtcGSb6dT5kgRCaW6moyzuFdYqA9fg5X7QKxVy2rDk6I /6k0ncxS8oZnmvrRP2pqkiInxc2xS7px7y6emLtbmIx+jQZBPUKJAiWxElL47jE7IZ5rmONEdsM /LBRN4KEGCeGecQVv/bZgQ5/VBlTBZBwaNp4l1xE3ifQMbanasHmIDb+ZqT0lkPAA72bVz42XsG uZQBKo+3NTrtCJwOiYKIZHYR1jFYzozrce3zVBpFqDWAo636SJAvX3jQ58+sxhUOhF4bAzGBI/E PyZS1uzytU0wgbcyY9Q== X-Google-Smtp-Source: AGHT+IFvRKA4umLDJYe2dlnUd3qZBZ9OcV78OfWMoQ3YorCC1zPH2Ck8dYtL6XjSb9rNFTw+t6Ypew== X-Received: by 2002:a17:902:ce0f:b0:240:72e9:87bb with SMTP id d9443c01a7336-244586c3844mr22740325ad.42.1755142073836; Wed, 13 Aug 2025 20:27:53 -0700 (PDT) Received: from nyaos.. ([45.12.134.112]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-32331167ff6sm276009a91.27.2025.08.13.20.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Aug 2025 20:27:53 -0700 (PDT) From: ChenMiao To: Stafford Horne Cc: Linux Kernel , Linux OpenRISC , chenmiao , Jonathan Corbet , Jonas Bonn , Stefan Kristiansson , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Steven Rostedt , Ard Biesheuvel , Masahiro Yamada , Johannes Berg , Nicolas Schier , Sahil Siddiq , linux-doc@vger.kernel.org Subject: [PATCH v3 2/2] openrisc: Add jump label support Date: Thu, 14 Aug 2025 03:27:02 +0000 Message-ID: <20250814032717.785395-3-chenmiao.ku@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250814032717.785395-1-chenmiao.ku@gmail.com> References: <20250814032717.785395-1-chenmiao.ku@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: chenmiao Implemented the full functionality of jump_label, of course, with text patching supported by just one API. Link: https://lore.kernel.org/openrisc/aJIC8o1WmVHol9RY@antec/T/#t Signed-off-by: chenmiao --- Changes in V3: - Ensure the two defconfig using the make savedefconfig. - modify the __ASSEMBLY__ to __ASSEMBLER__, modify the __ASM_JUMP_LABEL_H to __ASM_OPENRISC_JUMP_LABEL_H and remove invalid comment. Changes in V2: - using the patch_insn_write(void *addr, u32 insn) not the const void *insn. - add new macro OPENRISC_INSN_NOP in insn-def.h to use. Signed-off-by: chenmiao --- .../core/jump-labels/arch-support.txt | 2 +- arch/openrisc/Kconfig | 2 + arch/openrisc/configs/or1ksim_defconfig | 19 ++---- arch/openrisc/configs/virt_defconfig | 2 +- arch/openrisc/include/asm/insn-def.h | 3 + arch/openrisc/include/asm/jump_label.h | 68 +++++++++++++++++++ arch/openrisc/kernel/Makefile | 1 + arch/openrisc/kernel/jump_label.c | 52 ++++++++++++++ arch/openrisc/kernel/setup.c | 2 + 9 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 arch/openrisc/include/asm/jump_label.h create mode 100644 arch/openrisc/kernel/jump_label.c diff --git a/Documentation/features/core/jump-labels/arch-support.txt b/Doc= umentation/features/core/jump-labels/arch-support.txt index ccada815569f..683de7c15058 100644 --- a/Documentation/features/core/jump-labels/arch-support.txt +++ b/Documentation/features/core/jump-labels/arch-support.txt @@ -17,7 +17,7 @@ | microblaze: | TODO | | mips: | ok | | nios2: | TODO | - | openrisc: | TODO | + | openrisc: | ok | | parisc: | ok | | powerpc: | ok | | riscv: | ok | diff --git a/arch/openrisc/Kconfig b/arch/openrisc/Kconfig index b38fee299bc4..9156635dd264 100644 --- a/arch/openrisc/Kconfig +++ b/arch/openrisc/Kconfig @@ -24,6 +24,8 @@ config OPENRISC select GENERIC_PCI_IOMAP select GENERIC_IOREMAP select GENERIC_CPU_DEVICES + select HAVE_ARCH_JUMP_LABEL + select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_PCI select HAVE_UID16 select HAVE_PAGE_SIZE_8KB diff --git a/arch/openrisc/configs/or1ksim_defconfig b/arch/openrisc/config= s/or1ksim_defconfig index 59fe33cefba2..769705ac24d5 100644 --- a/arch/openrisc/configs/or1ksim_defconfig +++ b/arch/openrisc/configs/or1ksim_defconfig @@ -3,26 +3,23 @@ CONFIG_LOG_BUF_SHIFT=3D14 CONFIG_BLK_DEV_INITRD=3Dy # CONFIG_RD_GZIP is not set CONFIG_EXPERT=3Dy -# CONFIG_KALLSYMS is not set # CONFIG_EPOLL is not set # CONFIG_TIMERFD is not set # CONFIG_EVENTFD is not set # CONFIG_AIO is not set -# CONFIG_VM_EVENT_COUNTERS is not set -# CONFIG_COMPAT_BRK is not set -CONFIG_SLUB=3Dy -CONFIG_SLUB_TINY=3Dy -CONFIG_MODULES=3Dy -# CONFIG_BLOCK is not set +# CONFIG_KALLSYMS is not set CONFIG_BUILTIN_DTB_NAME=3D"or1ksim" CONFIG_HZ_100=3Dy +CONFIG_JUMP_LABEL=3Dy +CONFIG_MODULES=3Dy +# CONFIG_BLOCK is not set +CONFIG_SLUB_TINY=3Dy +# CONFIG_COMPAT_BRK is not set +# CONFIG_VM_EVENT_COUNTERS is not set CONFIG_NET=3Dy CONFIG_PACKET=3Dy CONFIG_UNIX=3Dy CONFIG_INET=3Dy -# CONFIG_INET_XFRM_MODE_TRANSPORT is not set -# CONFIG_INET_XFRM_MODE_TUNNEL is not set -# CONFIG_INET_XFRM_MODE_BEET is not set # CONFIG_INET_DIAG is not set CONFIG_TCP_CONG_ADVANCED=3Dy # CONFIG_TCP_CONG_BIC is not set @@ -35,7 +32,6 @@ CONFIG_DEVTMPFS=3Dy CONFIG_DEVTMPFS_MOUNT=3Dy # CONFIG_PREVENT_FIRMWARE_BUILD is not set # CONFIG_FW_LOADER is not set -CONFIG_PROC_DEVICETREE=3Dy CONFIG_NETDEVICES=3Dy CONFIG_ETHOC=3Dy CONFIG_MICREL_PHY=3Dy @@ -53,4 +49,3 @@ CONFIG_SERIAL_OF_PLATFORM=3Dy # CONFIG_DNOTIFY is not set CONFIG_TMPFS=3Dy CONFIG_NFS_FS=3Dy -# CONFIG_ENABLE_MUST_CHECK is not set diff --git a/arch/openrisc/configs/virt_defconfig b/arch/openrisc/configs/v= irt_defconfig index c1b69166c500..a93a3e1e4f87 100644 --- a/arch/openrisc/configs/virt_defconfig +++ b/arch/openrisc/configs/virt_defconfig @@ -12,6 +12,7 @@ CONFIG_NR_CPUS=3D8 CONFIG_SMP=3Dy CONFIG_HZ_100=3Dy # CONFIG_OPENRISC_NO_SPR_SR_DSX is not set +CONFIG_JUMP_LABEL=3Dy # CONFIG_COMPAT_BRK is not set CONFIG_NET=3Dy CONFIG_PACKET=3Dy @@ -55,7 +56,6 @@ CONFIG_DRM=3Dy # CONFIG_DRM_FBDEV_EMULATION is not set CONFIG_DRM_VIRTIO_GPU=3Dy CONFIG_FB=3Dy -CONFIG_FIRMWARE_EDID=3Dy CONFIG_FRAMEBUFFER_CONSOLE=3Dy CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY=3Dy CONFIG_LOGO=3Dy diff --git a/arch/openrisc/include/asm/insn-def.h b/arch/openrisc/include/a= sm/insn-def.h index dc8d16db1579..2ccdbb37c27c 100644 --- a/arch/openrisc/include/asm/insn-def.h +++ b/arch/openrisc/include/asm/insn-def.h @@ -9,4 +9,7 @@ /* or1k instructions are always 32 bits. */ #define OPENRISC_INSN_SIZE 4 =20 +/* or1k nop instruction code */ +#define OPENRISC_INSN_NOP 0x15000000U + #endif /* __ASM_INSN_DEF_H */ diff --git a/arch/openrisc/include/asm/jump_label.h b/arch/openrisc/include= /asm/jump_label.h new file mode 100644 index 000000000000..28dd6c78f8ce --- /dev/null +++ b/arch/openrisc/include/asm/jump_label.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2025 Chen Miao + * + * Based on arch/arm/include/asm/jump_label.h + */ +#ifndef __ASM_OPENRISC_JUMP_LABEL_H +#define __ASM_OPENRISC_JUMP_LABEL_H + +#ifndef __ASSEMBLER__ + +#include +#include + +#define HAVE_JUMP_LABEL_BATCH + +#define JUMP_LABEL_NOP_SIZE OPENRISC_INSN_SIZE + +/* + * should aligned 4 + * for jump_label relative + * entry.code =3D nop.addr - . -> return false + * entry.target =3D l_yes - . -> return true + * entry.key =3D key - . + */ +#define JUMP_TABLE_ENTRY(key, label) \ + ".pushsection __jump_table, \"aw\" \n\t" \ + ".align 4 \n\t" \ + ".long 1b - ., " label " - . \n\t" \ + ".long " key " - . \n\t" \ + ".popsection \n\t" + +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + ".align 4 \n\t" \ + "1: l.nop \n\t" \ + " l.nop \n\t" \ + JUMP_TABLE_ENTRY(key, label) + +static __always_inline bool arch_static_branch(struct static_key *const ke= y, + const bool branch) +{ + asm goto (ARCH_STATIC_BRANCH_ASM("%0", "%l[l_yes]") + ::"i"(&((char *)key)[branch])::l_yes); + + return false; +l_yes: + return true; +} + +#define ARCH_STATIC_BRANCH_JUMP_ASM(key, label) \ + ".align 4 \n\t" \ + "1: l.j " label " \n\t" \ + " l.nop \n\t" \ + JUMP_TABLE_ENTRY(key, label) + +static __always_inline bool +arch_static_branch_jump(struct static_key *const key, const bool branch) +{ + asm goto (ARCH_STATIC_BRANCH_JUMP_ASM("%0", "%l[l_yes]") + ::"i"(&((char *)key)[branch])::l_yes); + + return false; +l_yes: + return true; +} + +#endif /* __ASSEMBLER__ */ +#endif /* __ASM_OPENRISC_JUMP_LABEL_H */ diff --git a/arch/openrisc/kernel/Makefile b/arch/openrisc/kernel/Makefile index f0957ce16d6b..19e0eb94f2eb 100644 --- a/arch/openrisc/kernel/Makefile +++ b/arch/openrisc/kernel/Makefile @@ -9,6 +9,7 @@ obj-y :=3D head.o setup.o or32_ksyms.o process.o dma.o \ traps.o time.o irq.o entry.o ptrace.o signal.o \ sys_call_table.o unwinder.o cacheinfo.o =20 +obj-$(CONFIG_JUMP_LABEL) +=3D jump_label.o obj-$(CONFIG_SMP) +=3D smp.o sync-timer.o obj-$(CONFIG_STACKTRACE) +=3D stacktrace.o obj-$(CONFIG_MODULES) +=3D module.o diff --git a/arch/openrisc/kernel/jump_label.c b/arch/openrisc/kernel/jump_= label.c new file mode 100644 index 000000000000..071dacad885c --- /dev/null +++ b/arch/openrisc/kernel/jump_label.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Chen Miao + * + * Based on arch/arm/kernel/jump_label.c + */ +#include +#include +#include +#include +#include +#include + +bool arch_jump_label_transform_queue(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) { + long offset; + + offset =3D jump_entry_target(entry) - jump_entry_code(entry); + /* + * The actual maximum range of the l.j instruction's offset is -134,217,= 728 + * ~ 134,217,724 (sign 26-bit imm). + * For the original jump range, we need to right-shift N by 2 to obtain = the + * instruction's offset. + */ + if (unlikely(offset < -134217728 || offset > 134217724)) { + WARN_ON_ONCE(true); + } + /* 26bit imm mask */ + offset =3D (offset >> 2) & 0x03ffffff; + + insn =3D offset; + } else { + insn =3D OPENRISC_INSN_NOP; + } + + if (early_boot_irqs_disabled) { + copy_to_kernel_nofault(addr, &insn, sizeof(insn)); + } else { + patch_insn_write(addr, insn); + } + return true; +} + +void arch_jump_label_transform_apply(void) +{ + kick_all_cpus_sync(); +} diff --git a/arch/openrisc/kernel/setup.c b/arch/openrisc/kernel/setup.c index a9fb9cc6779e..000a9cc10e6f 100644 --- a/arch/openrisc/kernel/setup.c +++ b/arch/openrisc/kernel/setup.c @@ -249,6 +249,8 @@ void __init setup_arch(char **cmdline_p) initrd_below_start_ok =3D 1; } #endif + /* perform jump_table sorting before paging_init locks down read only mem= ory */ + jump_label_init(); =20 /* paging_init() sets up the MMU and marks all pages as reserved */ paging_init(); --=20 2.45.2