From nobody Sat Jun 20 07:12:36 2026 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 740EE3CAE7D for ; Tue, 5 May 2026 06:21:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777962068; cv=none; b=tnaM63Ar4B2Ly5km3IlSVMmTGwWr2dr1QaGGVkRvyzeHMavrygJk8Z/E8ozzKrKfBU/sQavnyjClRdjKTZfvA5KZ3t44fUJ4rusqN/jeyeY8RUGeTeXFC5HwZtiXq1kj6hX3LFjejKcTeA6zBblm+5IjRDbkwR35pw20+rRDLLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777962068; c=relaxed/simple; bh=6vd2WgAVEeop7ukkZoD7aM1VrCz8dUOAcyeyUNwokY4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FzjtIOhO61Z0wP185MV/kHikR/6oLr4cKg2UdTnZtbwYYwDK2jyHZTzPX+kUNmULBntLKG3o5QEvntBuC+4sdcmTA4pKztJRhTI1NkBxOW1uSkB09rHZwsj3/QZPvuPSH4gO8XibzusbilpaemuaOQhVD8/X5uJmuDLiXtBtHlo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=RJ3tag0/; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="RJ3tag0/" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-8353c9f24d2so1486247b3a.3 for ; Mon, 04 May 2026 23:21:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777962063; x=1778566863; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=GUWhdkh+jSa+kjESo9b/CmN+H/tJl8wa3ioBeRpyc7Q=; b=RJ3tag0//hy9WXcGS/GTzeS9mf3NTmnMRRdvE84zM+cc8CumSeuZew1i/4iF/Owoit 75lyYnvoGRn9bI30wrOBzbS+EmuqGslraahAn/m6J9jxj+pNKRrZeo8Gnhzo985Ah/d7 JRULi9jSY3L3FAPH++Dh671UQs64IJvtsOR1OYVhNvDG0TIDMZNStFfLplYIqWu9QCIF 1PR45chetHwWRU1yGhIZEBvw378E5yLaugtjXpMil16rfLKgX6Ze30iVV7wQxW5nZLSv W48aAxE8XNcinjxVt7sLuWU4cD3vR6MQwlcErZjkArKTDXLrLxTE8GcziUlYMvBvuJ65 hGMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777962063; x=1778566863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=GUWhdkh+jSa+kjESo9b/CmN+H/tJl8wa3ioBeRpyc7Q=; b=OWnFO+BT2C/TB4n8t/S8mzNk02vraKXrcuvU7RHDK7vuGeqqBXvTMgl4xNhvsfT/6Q /MYFmXu7uqEtX2q7yc8LjDtGyAVGTRxKtwd+Bxz/HmntKExfwPyIU0zhDwo0B2YzVuA3 he5x8uZ9ZVSuvee/eqnlDy89NzZ1/RxhzliYuJFGD2StPAFtQkxjRvDH2Pq+YX1qDNbH CZApzNiZrUKsj5bzHaLjJUaYrzPYNDLdRAA/HjXPF0hzRSTcZ9Qvkh1eeRYUf4pfHOPl 3TYw9P11XXZrCqWyamkP55R3GFuHKLHFX+7EcCQudFRgkoeSHq6HAR+00lKXMIBjAswM x5/Q== X-Forwarded-Encrypted: i=1; AFNElJ+xiBNntPj6K5GNQqHPkgCkIN7gt5Pg03KXZ5nQ5tqvPZiTKl5QGEUqr/ORcAZ3n8LGGVQBGryg5zm+Irw=@vger.kernel.org X-Gm-Message-State: AOJu0Ywm+YuK9w2FBQ2eTTFr4KE+vUEqZxxo8tCZ3A70tmsW5nC/NLh3 r5uI6cydZR4DVu/+rh7ragjZDVrdrefjg+t/mGiz0kzVJa6MjnL/ukeyHLIECEO9rss= X-Gm-Gg: AeBDieuLfxac47H2q9GmTMSxLyYCLwibhOhfKV3xcFG+LDY/ynJkXVEjsAapchNmT4C DvnYO3pNJaSfjNeTsxYilqKSStOS9kEmharkPunKbkdayGFtXW2qhn2CKJaYB5mRFuzPWj7Nurr vTJZyd248VtThQRosHGNdErjr8lx0A0CVH9L/BEKPeYx5uomF7Lf/L4S4uypMoKPN1Y/vF55SnG m6aP09Tzv03WVePpQGxxnsrHITIucFvX/OJqBZRW+x1iLmKvkSvyVZWj526WxeH03Lhh5t3DTZi 3CfHh1XWzdj53wVy7/EsFkwUMdnHFtW5RBNsUzPzA8IiARsyi7oH1Bm72xT0/ulF8+zlqbDYiu6 DbrXton1nXlG+KJsl6P9rRPDkqqH7Kutbfug7TuQrBWmtRVWjmu8IL3xOtf6hXDE2+fFkysEvaW SiQmMR8dC5XUP7ZB54sClIWccU/rWaxaTfKIAQ/2JQVH9EWqNXWHhVrDPr5Kwz47wr X-Received: by 2002:a05:6a00:4b55:b0:82c:d9d0:f482 with SMTP id d2e1a72fcca58-83924eb8dd2mr1858261b3a.46.1777962063135; Mon, 04 May 2026 23:21:03 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.12]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8c10sm880738b3a.37.2026.05.04.23.20.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 May 2026 23:21:02 -0700 (PDT) From: Yunhui Cui To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, bjorn@kernel.org, pulehui@huawei.com, puranjay@kernel.org, thuth@redhat.com, ajones@ventanamicro.com, ben.dooks@codethink.co.uk, rkrcmar@ventanamicro.com, cuiyunhui@bytedance.com, samuel.holland@sifive.com, zong.li@sifive.com, conor.dooley@microchip.com, tglx@kernel.org, debug@rivosinc.com, seanwascoding@gmail.com, andybnac@gmail.com, menglong8.dong@gmail.com, cyrilbur@tenstorrent.com, wangruikang@iscas.ac.cn, atishp@rivosinc.com, apatel@ventanamicro.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, arnd@arndb.de, nathan@kernel.org, nick.desaulniers+lkml@gmail.com, morbo@google.com, justinstitt@google.com, qingfang.deng@siflower.com.cn, linux-arch@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 1/3] riscv: io: avoid null-pointer arithmetic in PIO helpers Date: Tue, 5 May 2026 14:20:24 +0800 Message-Id: <20260505062026.91724-2-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260505062026.91724-1-cuiyunhui@bytedance.com> References: <20260505062026.91724-1-cuiyunhui@bytedance.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" The RISC-V PIO helpers derive I/O addresses from PCI_IOBASE in ins*(), outs*(), and ioport_map(). Under configurations where I/O port support is not available, these expressions can still be formed during compilation and trigger -Wnull-pointer-arithmetic warnings from clang. Introduce a helper for the address calculation and guard the PIO-only helpers with CONFIG_HAS_IOPORT so unsupported configurations do not construct these PIO address expressions. Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/io.h | 26 ++++++++++++++++++-------- include/asm-generic/io.h | 4 ++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h index 09bb5f57a9d34..6f5d70313c83e 100644 --- a/arch/riscv/include/asm/io.h +++ b/arch/riscv/include/asm/io.h @@ -56,6 +56,8 @@ #define __io_pbw() RISCV_FENCE(iow, o) #define __io_paw() RISCV_FENCE(o, io) =20 +#define PCI_IO_ADDR(addr) ((void __iomem *)((unsigned long)PCI_IOBASE + (a= ddr))) + /* * Accesses from a single hart to a single I/O address must be ordered. T= his * allows us to use the raw read macros, but we still need to fence before= and @@ -102,12 +104,14 @@ __io_reads_ins(reads, u32, l, __io_br(), __io_ar(addr= )) #define readsw(addr, buffer, count) __readsw(addr, buffer, count) #define readsl(addr, buffer, count) __readsl(addr, buffer, count) =20 +#ifdef CONFIG_HAS_IOPORT __io_reads_ins(ins, u8, b, __io_pbr(), __io_par(addr)) __io_reads_ins(ins, u16, w, __io_pbr(), __io_par(addr)) __io_reads_ins(ins, u32, l, __io_pbr(), __io_par(addr)) -#define insb(addr, buffer, count) __insb(PCI_IOBASE + (addr), buffer, coun= t) -#define insw(addr, buffer, count) __insw(PCI_IOBASE + (addr), buffer, coun= t) -#define insl(addr, buffer, count) __insl(PCI_IOBASE + (addr), buffer, coun= t) +#define insb(addr, buffer, count) __insb(PCI_IO_ADDR(addr), buffer, count) +#define insw(addr, buffer, count) __insw(PCI_IO_ADDR(addr), buffer, count) +#define insl(addr, buffer, count) __insl(PCI_IO_ADDR(addr), buffer, count) +#endif =20 __io_writes_outs(writes, u8, b, __io_bw(), __io_aw()) __io_writes_outs(writes, u16, w, __io_bw(), __io_aw()) @@ -116,25 +120,31 @@ __io_writes_outs(writes, u32, l, __io_bw(), __io_aw()) #define writesw(addr, buffer, count) __writesw(addr, buffer, count) #define writesl(addr, buffer, count) __writesl(addr, buffer, count) =20 +#ifdef CONFIG_HAS_IOPORT __io_writes_outs(outs, u8, b, __io_pbw(), __io_paw()) __io_writes_outs(outs, u16, w, __io_pbw(), __io_paw()) __io_writes_outs(outs, u32, l, __io_pbw(), __io_paw()) -#define outsb(addr, buffer, count) __outsb(PCI_IOBASE + (addr), buffer, co= unt) -#define outsw(addr, buffer, count) __outsw(PCI_IOBASE + (addr), buffer, co= unt) -#define outsl(addr, buffer, count) __outsl(PCI_IOBASE + (addr), buffer, co= unt) +#define outsb(addr, buffer, count) __outsb(PCI_IO_ADDR(addr), buffer, coun= t) +#define outsw(addr, buffer, count) __outsw(PCI_IO_ADDR(addr), buffer, coun= t) +#define outsl(addr, buffer, count) __outsl(PCI_IO_ADDR(addr), buffer, coun= t) +#endif =20 #ifdef CONFIG_64BIT __io_reads_ins(reads, u64, q, __io_br(), __io_ar(addr)) #define readsq(addr, buffer, count) __readsq(addr, buffer, count) =20 +#ifdef CONFIG_HAS_IOPORT __io_reads_ins(ins, u64, q, __io_pbr(), __io_par(addr)) -#define insq(addr, buffer, count) __insq(PCI_IOBASE + (addr), buffer, coun= t) +#define insq(addr, buffer, count) __insq(PCI_IO_ADDR(addr), buffer, count) +#endif =20 __io_writes_outs(writes, u64, q, __io_bw(), __io_aw()) #define writesq(addr, buffer, count) __writesq(addr, buffer, count) =20 +#ifdef CONFIG_HAS_IOPORT __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw()) -#define outsq(addr, buffer, count) __outsq(PCI_IOBASE + (addr), buffer, co= unt) +#define outsq(addr, buffer, count) __outsq(PCI_IO_ADDR(addr), buffer, coun= t) +#endif #endif =20 #include diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h index ca5a1ce6f0f89..d799e5ccc9437 100644 --- a/include/asm-generic/io.h +++ b/include/asm-generic/io.h @@ -1205,8 +1205,12 @@ static inline void __iomem *ioremap_np(phys_addr_t o= ffset, size_t size) #define ioport_map ioport_map static inline void __iomem *ioport_map(unsigned long port, unsigned int nr) { +#ifdef CONFIG_HAS_IOPORT port &=3D IO_SPACE_LIMIT; return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port; +#else + return NULL; +#endif } #define ARCH_HAS_GENERIC_IOPORT_MAP #endif --=20 2.39.5 From nobody Sat Jun 20 07:12:36 2026 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 2D0E03CEBB1 for ; Tue, 5 May 2026 06:21:14 +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=1777962080; cv=none; b=WM+DOwq794E8+94zFrdb+CbPNKv3FI7oO4INBjy4tXQD6QkwSS/74wkxXEaM5khvfXltjBm6bsg0AoNpVg8Um2Fxa9lb2Cbr79QIAD4JWvPXoXVuBXskiYjfLZdKQ/F6+bjgF0RV21QSnyXWB2dE7YdzSsfH9/7q4Pn9gQO9GBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777962080; c=relaxed/simple; bh=ly3RZYd2tyI4Ap2g7r4gHEPzmLlRzXERLmyl3czhuss=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cTMDkyS8Lp6L3/fvgIRBHRdKNQEWU1zoPqgEfG6lb9nZ+GClwWPqKfG7cSJidit43KdSNCuAzU0LoL3KHPZ3WplAdZo91ILF6tSjwGHVlvcynCIo2ck/xcyvetsGPowkVAeOcEi/HlQqZPVYCiUSngjhX4DyRvV30ggfKficU+E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=RnJ3Z+/w; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="RnJ3Z+/w" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-83659d38e38so849258b3a.1 for ; Mon, 04 May 2026 23:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777962074; x=1778566874; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Oo8D+bpHIA0LAgv97mwhWXEIJKH4RloET0FJQLqr/w0=; b=RnJ3Z+/wKgZq4/bQSOV27RXtcBOdQGECGfMTt1/7+lMtQbp7PbivmCzm4KyCGUAKwI Tw3LJdeWUP1s+J8d6cyYhkgN3VQlbVL+jTJEhighfSedFphJtKtauJ6QBGz1MpK0d0Rs smb/nfjCPoIG0DTjomM69bydlAlmWPnvUQLUzqZ6flzCGAp1A4jsgLhm7H8fzBMMeYR9 edeRx2cIMWz8RrsJt1MfgFvr8PVJf2QGAN7ivWHcDtDMszDilii76NI6ueRidFrDjM/H ODogomZTnASH9q71foWHHsDFgdxckmAHiGaPwhUJpwdT2KxHsG326bnLSISvhzX9axZm hkVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777962074; x=1778566874; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Oo8D+bpHIA0LAgv97mwhWXEIJKH4RloET0FJQLqr/w0=; b=CSJAPCPCFdYR3VNGOEjrxIjvS64gwV44KTjJQNFyt/37o/seZogCCoOX5j7Vks4CDD R1BLpV/LCp27XDzQLuznSx63S7g8jdG3mLy0dKdLQBm3mrrp2YCVoiFS3zs1fGZaNylx lgBFiXkdl+KO8fFZBVsdJdiJpDmdZacAYszdv5OjSg/RIdUB/+PJJgm/kwHPJFqKNJBs 9SLLnU7kt0OCFF1ied5eWToWQ7R16iT1vXEVETVL3OtqSqmaoh/62IkUNZPB0ZQhX9dg ijdJjyiVdQQ3Y0UVJ74IMpFKMWNTZBg5qhuXsFa780KLHr3mSnwdYzSYIZlCJVG393BN A+ig== X-Forwarded-Encrypted: i=1; AFNElJ9Atxn5TNOkTgluoXKrP9M9bX+yJVBMrHkY7E8lcLMfQVYOqyLOy483CwaIEyivvgSdsmvKwyPslpHGK5M=@vger.kernel.org X-Gm-Message-State: AOJu0Yxu5GOhsSOBHKdcWGwR9a1pY4AMPIiyTL090jaNg7FbJDIlmSsz 7MrB9P4V1+U047MG9KfFSF89glUg+quiAmERJWD96FSu9iR2bAiJ38tnrv+Tw1en5g8= X-Gm-Gg: AeBDiesD0u0cIwtN9zpiwdsRu24EMkra4m8I2TURZ1Zi5BTJwbXkX2GI3AnFH0EACA5 dBfI5voBCKwHMUZeDpy6A3z4hA5Mvn5qe3p0kiYg9BNSIbULDyEgrwFKek6Cq2phHe8tAoztwpj 6n0WQnSnGxUnX2q+txPvG//ZkPLfGyZkzTBlnzIdh0O6Ko6w5mXawMUKyDt2AjeJfl7nqZKknQ7 HbBtxbKcRLnaMqTP0eUZ0HuGN42WKA06AdoTOK5k/m+gcGWldshZOWxwMC7VZAY2yETI3vRGhhh pnoibIkQh1ErCrUJiYwc+JxAvQfUlQ2Qx0GTYurssQJPJGsqC+aJyPqfOxnZzIFABv1Dw+E1Ce5 +uqTfjttzn0m0zxz4ok/raMg1xn3/r5OOC79ESs82e5W3CJhn11VMo3EULQbyGfr/yccpunXKEn LdEhnvCterH9ZG9coBoqauhtkEbmG0Ym6MNMIsUAejBVMhhLIFif0D+11U7aCfCT4mquSILYCH0 nM= X-Received: by 2002:a05:6a00:b90a:b0:829:7d31:dd99 with SMTP id d2e1a72fcca58-839246bc34fmr1947042b3a.38.1777962073663; Mon, 04 May 2026 23:21:13 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.12]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8c10sm880738b3a.37.2026.05.04.23.21.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 May 2026 23:21:13 -0700 (PDT) From: Yunhui Cui To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, bjorn@kernel.org, pulehui@huawei.com, puranjay@kernel.org, thuth@redhat.com, ajones@ventanamicro.com, ben.dooks@codethink.co.uk, rkrcmar@ventanamicro.com, cuiyunhui@bytedance.com, samuel.holland@sifive.com, zong.li@sifive.com, conor.dooley@microchip.com, tglx@kernel.org, debug@rivosinc.com, seanwascoding@gmail.com, andybnac@gmail.com, menglong8.dong@gmail.com, cyrilbur@tenstorrent.com, wangruikang@iscas.ac.cn, atishp@rivosinc.com, apatel@ventanamicro.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, arnd@arndb.de, nathan@kernel.org, nick.desaulniers+lkml@gmail.com, morbo@google.com, justinstitt@google.com, qingfang.deng@siflower.com.cn, linux-arch@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 2/3] riscv: introduce percpu.h into include/asm Date: Tue, 5 May 2026 14:20:25 +0800 Message-Id: <20260505062026.91724-3-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260505062026.91724-1-cuiyunhui@bytedance.com> References: <20260505062026.91724-1-cuiyunhui@bytedance.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" Introduce a RISC-V percpu header with direct this_cpu helpers for read/write/add/and/or/xchg/cmpxchg operations. Make the 8/16-bit add_return LR/SC fallback return the logical subword result so callers at non-zero subword offsets see the expected value. Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/percpu.h | 280 ++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 arch/riscv/include/asm/percpu.h diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percp= u.h new file mode 100644 index 0000000000000..3b26fe45e70f4 --- /dev/null +++ b/arch/riscv/include/asm/percpu.h @@ -0,0 +1,280 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __ASM_PERCPU_H +#define __ASM_PERCPU_H + +#include + +#include +#include +#include + +#define PERCPU_RW_OPS(sz) \ +static inline unsigned long __percpu_read_##sz(void *ptr) \ +{ \ + return READ_ONCE(*(u##sz *)ptr); \ +} \ + \ +static inline void __percpu_write_##sz(void *ptr, unsigned long val) \ +{ \ + WRITE_ONCE(*(u##sz *)ptr, (u##sz)val); \ +} + +PERCPU_RW_OPS(8) +PERCPU_RW_OPS(16) +PERCPU_RW_OPS(32) + +#ifdef CONFIG_64BIT +PERCPU_RW_OPS(64) +#endif + +#define __PERCPU_AMO_OP_CASE(sfx, name, sz, amo_insn) \ +static inline void \ +__percpu_##name##_amo_case_##sz(void *ptr, unsigned long val) \ +{ \ + asm volatile ( \ + "amo" #amo_insn #sfx " zero, %[val], %[ptr]" \ + : [ptr] "+A" (*(u##sz *)ptr) \ + : [val] "r" ((u##sz)(val)) \ + : "memory"); \ +} + +#ifdef CONFIG_64BIT +#define PERCPU_OP(name, amo_insn) \ + __PERCPU_AMO_OP_CASE(.w, name, 32, amo_insn) \ + __PERCPU_AMO_OP_CASE(.d, name, 64, amo_insn) +#else +#define PERCPU_OP(name, amo_insn) \ + __PERCPU_AMO_OP_CASE(.w, name, 32, amo_insn) +#endif + +PERCPU_OP(add, add) +PERCPU_OP(andnot, and) +PERCPU_OP(or, or) + +/* + * Currently, only this_cpu_add_return_xxx() requires a return value, + * and the PERCPU_RET_OP() does not account for other operations. + */ +#define __PERCPU_AMO_RET_OP_CASE(sfx, name, sz, amo_insn) \ +static inline u##sz \ +__percpu_##name##_return_amo_case_##sz(void *ptr, unsigned long val) \ +{ \ + register u##sz ret; \ + \ + asm volatile ( \ + "amo" #amo_insn #sfx " %[ret], %[val], %[ptr]" \ + : [ptr] "+A" (*(u##sz *)ptr), [ret] "=3Dr" (ret) \ + : [val] "r" ((u##sz)(val)) \ + : "memory"); \ + \ + return ret + val; \ +} + +#ifdef CONFIG_64BIT +#define PERCPU_RET_OP(name, amo_insn) \ + __PERCPU_AMO_RET_OP_CASE(.w, name, 32, amo_insn) \ + __PERCPU_AMO_RET_OP_CASE(.d, name, 64, amo_insn) +#else +#define PERCPU_RET_OP(name, amo_insn) \ + __PERCPU_AMO_RET_OP_CASE(.w, name, 32, amo_insn) +#endif + +PERCPU_RET_OP(add, add) + +#define PERCPU_8_16_GET_SHIFT(ptr) (((unsigned long)(ptr) & 0x3) * BITS_PE= R_BYTE) +#define PERCPU_8_16_GET_MASK(sz) GENMASK((sz) - 1, 0) +#define PERCPU_8_16_GET_PTR32(ptr) ((u32 *)((unsigned long)(ptr) & ~0x3)) + +#define PERCPU_8_16_OP(name, amo_insn, sz, sfx, val_type, new_val_expr, as= m_op) \ +static inline void __percpu_##name##_amo_case_##sz(void *ptr, unsigned lon= g val) \ +{ \ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && \ + riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) { \ + asm volatile ("amo" #amo_insn #sfx " zero, %[val], %[ptr]" \ + : [ptr] "+A"(*(val_type *)ptr) \ + : [val] "r"((val_type)((new_val_expr) & PERCPU_8_16_GET_MASK(sz))) \ + : "memory"); \ + } else { \ + u32 *ptr32 =3D PERCPU_8_16_GET_PTR32(ptr); \ + const unsigned long shift =3D PERCPU_8_16_GET_SHIFT(ptr); \ + const u32 mask =3D PERCPU_8_16_GET_MASK(sz) << shift; \ + const val_type val_trunc =3D (val_type)((new_val_expr) \ + & PERCPU_8_16_GET_MASK(sz)); \ + u32 retx, rc; \ + val_type new_val_type; \ + \ + asm volatile ( \ + "0: lr.w %0, %2\n" \ + "and %3, %0, %4\n" \ + "srl %3, %3, %5\n" \ + #asm_op " %3, %3, %6\n" \ + "sll %3, %3, %5\n" \ + "and %1, %0, %7\n" \ + "or %1, %1, %3\n" \ + "sc.w %1, %1, %2\n" \ + "bnez %1, 0b\n" \ + : "=3D&r"(retx), "=3D&r"(rc), "+A"(*ptr32), "=3D&r"(new_val_type) \ + : "r"(mask), "r"(shift), "r"(val_trunc), "r"(~mask) \ + : "memory"); \ + } \ +} + +#define PERCPU_OP_8_16(op_name, op, expr, final_op) \ + PERCPU_8_16_OP(op_name, op, 8, .b, u8, expr, final_op); \ + PERCPU_8_16_OP(op_name, op, 16, .h, u16, expr, final_op) + +PERCPU_OP_8_16(add, add, val, add) +PERCPU_OP_8_16(andnot, and, ~(val), and) +PERCPU_OP_8_16(or, or, val, or) + +#define PERCPU_8_16_RET_OP(name, amo_insn, sz, sfx, val_type, new_val_expr= ) \ +static inline val_type __percpu_##name##_return_amo_case_##sz(void *ptr, u= nsigned long val) \ +{ \ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && \ + riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) { \ + register val_type ret; \ + asm volatile ("amo" #amo_insn #sfx " %[ret], %[val], %[ptr]" \ + : [ptr] "+A"(*(val_type *)ptr), [ret] "=3Dr"(ret) \ + : [val] "r"((val_type)((new_val_expr) & PERCPU_8_16_GET_MASK(sz))) \ + : "memory"); \ + return ret + (val_type)((new_val_expr) & PERCPU_8_16_GET_MASK(sz)); \ + } else { \ + u32 *ptr32 =3D PERCPU_8_16_GET_PTR32(ptr); \ + const unsigned long shift =3D PERCPU_8_16_GET_SHIFT(ptr); \ + const u32 mask =3D (PERCPU_8_16_GET_MASK(sz) << shift); \ + const u32 inv_mask =3D ~mask; \ + const val_type val_trunc =3D (val_type)((new_val_expr) \ + & PERCPU_8_16_GET_MASK(sz)); \ + u32 old, new, tmp; \ + \ + asm volatile ( \ + "0: lr.w %0, %3\n" \ + "and %1, %0, %4\n" \ + "srl %1, %1, %5\n" \ + "add %1, %1, %6\n" \ + "and %1, %1, %7\n" \ + "sll %1, %1, %5\n" \ + "and %2, %0, %8\n" \ + "or %2, %2, %1\n" \ + "sc.w %2, %2, %3\n" \ + "bnez %2, 0b\n" \ + : "=3Dr"(old), "=3Dr"(tmp), "=3D&r"(new), "+A"(*ptr32) \ + : "r"(mask), "r"(shift), "r"(val_trunc), "r"(PERCPU_8_16_GET_MASK(sz)),= \ + "r"(inv_mask) \ + : "memory"); \ + return (val_type)(tmp >> shift); \ + } \ +} + +PERCPU_8_16_RET_OP(add, add, 8, .b, u8, val) +PERCPU_8_16_RET_OP(add, add, 16, .h, u16, val) + +#define _pcp_protect(op, pcp, ...) \ +({ \ + preempt_disable_notrace(); \ + op(raw_cpu_ptr(&(pcp)), __VA_ARGS__); \ + preempt_enable_notrace(); \ +}) + +#define _pcp_protect_return(op, pcp, args...) \ +({ \ + typeof(pcp) __retval; \ + preempt_disable_notrace(); \ + __retval =3D (typeof(pcp))op(raw_cpu_ptr(&(pcp)), ##args); \ + preempt_enable_notrace(); \ + __retval; \ +}) + +#define this_cpu_read_1(pcp) _pcp_protect_return(__percpu_read_8, pcp) +#define this_cpu_read_2(pcp) _pcp_protect_return(__percpu_read_16, pcp) +#define this_cpu_read_4(pcp) _pcp_protect_return(__percpu_read_32, pcp) + +#ifdef CONFIG_64BIT +#define this_cpu_read_8(pcp) _pcp_protect_return(__percpu_read_64, pcp) +#endif + +#define this_cpu_write_1(pcp, val) _pcp_protect(__percpu_write_8, pcp, (un= signed long)val) +#define this_cpu_write_2(pcp, val) _pcp_protect(__percpu_write_16, pcp, (u= nsigned long)val) +#define this_cpu_write_4(pcp, val) _pcp_protect(__percpu_write_32, pcp, (u= nsigned long)val) + +#ifdef CONFIG_64BIT +#define this_cpu_write_8(pcp, val) _pcp_protect(__percpu_write_64, pcp, (u= nsigned long)val) +#endif + +#define this_cpu_add_1(pcp, val) _pcp_protect(__percpu_add_amo_case_8, pcp= , val) +#define this_cpu_add_2(pcp, val) _pcp_protect(__percpu_add_amo_case_16, pc= p, val) +#define this_cpu_add_4(pcp, val) _pcp_protect(__percpu_add_amo_case_32, pc= p, val) + +#ifdef CONFIG_64BIT +#define this_cpu_add_8(pcp, val) _pcp_protect(__percpu_add_amo_case_64, pc= p, val) +#endif + +#define this_cpu_add_return_1(pcp, val) \ +_pcp_protect_return(__percpu_add_return_amo_case_8, pcp, val) + +#define this_cpu_add_return_2(pcp, val) \ +_pcp_protect_return(__percpu_add_return_amo_case_16, pcp, val) + +#define this_cpu_add_return_4(pcp, val) \ +_pcp_protect_return(__percpu_add_return_amo_case_32, pcp, val) + +#ifdef CONFIG_64BIT +#define this_cpu_add_return_8(pcp, val) \ +_pcp_protect_return(__percpu_add_return_amo_case_64, pcp, val) +#endif + +#define this_cpu_and_1(pcp, val) _pcp_protect(__percpu_andnot_amo_case_8, = pcp, ~(val)) +#define this_cpu_and_2(pcp, val) _pcp_protect(__percpu_andnot_amo_case_16,= pcp, ~(val)) +#define this_cpu_and_4(pcp, val) _pcp_protect(__percpu_andnot_amo_case_32,= pcp, ~(val)) + +#ifdef CONFIG_64BIT +#define this_cpu_and_8(pcp, val) _pcp_protect(__percpu_andnot_amo_case_64,= pcp, ~(val)) +#endif + +#define this_cpu_or_1(pcp, val) _pcp_protect(__percpu_or_amo_case_8, pcp, = val) +#define this_cpu_or_2(pcp, val) _pcp_protect(__percpu_or_amo_case_16, pcp,= val) +#define this_cpu_or_4(pcp, val) _pcp_protect(__percpu_or_amo_case_32, pcp,= val) + +#ifdef CONFIG_64BIT +#define this_cpu_or_8(pcp, val) _pcp_protect(__percpu_or_amo_case_64, pcp,= val) +#endif + +#define this_cpu_xchg_1(pcp, val) _pcp_protect_return(xchg_relaxed, pcp, v= al) +#define this_cpu_xchg_2(pcp, val) _pcp_protect_return(xchg_relaxed, pcp, v= al) +#define this_cpu_xchg_4(pcp, val) _pcp_protect_return(xchg_relaxed, pcp, v= al) + +#ifdef CONFIG_64BIT +#define this_cpu_xchg_8(pcp, val) _pcp_protect_return(xchg_relaxed, pcp, v= al) +#endif + +#define this_cpu_cmpxchg_1(pcp, o, n) _pcp_protect_return(cmpxchg_relaxed,= pcp, o, n) +#define this_cpu_cmpxchg_2(pcp, o, n) _pcp_protect_return(cmpxchg_relaxed,= pcp, o, n) +#define this_cpu_cmpxchg_4(pcp, o, n) _pcp_protect_return(cmpxchg_relaxed,= pcp, o, n) + +#ifdef CONFIG_64BIT +#define this_cpu_cmpxchg_8(pcp, o, n) _pcp_protect_return(cmpxchg_relaxed,= pcp, o, n) + +#define this_cpu_cmpxchg64(pcp, o, n) this_cpu_cmpxchg_8(pcp, o, n) +#endif + +#ifdef system_has_cmpxchg128 +#define this_cpu_cmpxchg128(pcp, o, n) \ +({ \ + u128 ret__; \ + typeof(pcp) *ptr__; \ + \ + preempt_disable_notrace(); \ + ptr__ =3D raw_cpu_ptr(&(pcp)); \ + if (system_has_cmpxchg128()) \ + ret__ =3D cmpxchg128_local(ptr__, (o), (n)); \ + else \ + ret__ =3D this_cpu_generic_cmpxchg(pcp, (o), (n)); \ + preempt_enable_notrace(); \ + ret__; \ +}) +#endif + +#include + +#endif /* __ASM_PERCPU_H */ --=20 2.39.5 From nobody Sat Jun 20 07:12:36 2026 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 9E2463CBE8B for ; Tue, 5 May 2026 06:21:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777962090; cv=none; b=E1rHV8i55+AsRKTlRBYofPmblC2+qqoqF4pE2NvYc+gNrFXxUo3yW+c6CcqQ1JOgyczcYgY8u3/nT4K8WFfaMI4PdlrrDzj+KvW5PjtFwnot5so/fEVcrf+XLhyQ2GiVQX7CEExkPQalan3gYSHHfn6weJq/rdT9beq/0asAuQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777962090; c=relaxed/simple; bh=AK7F7VoU9F9EinHl0J/LunF3c/i6HBW1++hF5htlcaA=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=cuQl7IJYvxUdlez6fpgZFKsotyT/8l9dFZgByutlMt4TdcHT5qFolXBbfm5qSV5l8vCnrX/6KSbxFNUTPRFkp+H0u6WlV5S+Fy9cguNG9KlbpbLLJFIawx4wkWDpaf6OF/ROKwgqKi0NnTvB96tpLs2ESEbGMdMznhjK5qfq2x8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=GjR5QJxL; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="GjR5QJxL" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-83537a80ab6so1415744b3a.1 for ; Mon, 04 May 2026 23:21:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1777962084; x=1778566884; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=4znkv+W5vuAGwYDPrkKlG4HbKRGYXv2X7CusMf1hFi8=; b=GjR5QJxLk2Bx+o4Tp8/+Kd6zribhZ9+M30mTDwY9x6Y00q6QA7ioCKVb5zNSoKZcDw i2QkD/VW+EtmC/S9z6ei3FXxk4avJPt744KIhP5GXncJtIeFsYHSQ7cQnYfEYHEmj8J0 Carbd4fbaHBYijvvXOPCztfAr8U2j+5joT5ZikOq17qkFGPe/JJGeW2AQdYsFEse+F3W KvisFkgw3K9Nl9yDxdiCCIl24EADQk8IoySd+u1eOPpd6t6c6s3WHPxb5ndmHfPzNSa9 8wLYlzyt9g2XEEWaUVW3tzkQJfkNbvvFZP93anzTSr1VmNMf/WvT3ZFGs4cQw6yJoEvW LCUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777962084; x=1778566884; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=4znkv+W5vuAGwYDPrkKlG4HbKRGYXv2X7CusMf1hFi8=; b=iDeiCRr5IMPIrQnoWiy/cQ3xu69ySTlO8ZOM0XqkL84ECbpYAr6Z1ZActcRzjSrE70 /XEjxJr43hrXruGPzIMkz/RJJ0M6QMosERVq1OL23Vg1MGpDO0S8hZ8lw3YZqRz4YQ+B LJO3MLIr+dUBx9dzqJgwNT8B578+Zmeq8blQc2Pw0FaDOxWL4cGbgNu87tCyfF367y1r ekoUDwAec6h5Td5jfZUZMXX1/JmtpERnU3na0bOkxaJzDaNY5PldLhd/uWE95+wusNhm mw5e3oi7vCPSWvf0SBp3XUkTFhrH6YGHMHF7gaEjoRANzYPwKj4TCs9q/xeUa9Za96oE AFPQ== X-Forwarded-Encrypted: i=1; AFNElJ//2boF+UAvQ4x8V4CyTuK5oTiEeDNJeUT5WIxVcJ+KEOT6ezFm/S6JecWkUWbQHyezunzmCottkj0yci0=@vger.kernel.org X-Gm-Message-State: AOJu0YxaRg4ts70+my0lskEML5wIqSVgdDqhLFMD3xk3TsmabauJy0nF o4eHuLzKjpHF8JQFA9giRRkcUSjuAX2hs0JOsScvZw5GPq/Kts8XU94LG/XoFa+L2dw= X-Gm-Gg: AeBDieuKeyDIRPtuohKjZkdrEDwNS7VRWo14iQN3RURFR2/CARHOErSN8qZmU4G6GXA Ed10lc4iosMBW5A2Zec8NEjQ5vYne9J8e+EllUDsL6ur8sHaeQt/kUtct6b8NWQDvDhY7bmOVZX RElHpA8DL1rOhVIIQ9a8aWXeZKOuHscjfuvye/WnrJedUKmeCuHyy9zzzpfpXv3TdoiSNwYQl1V 2kKYq3mX+qe0sGY/A5FexIr1uQ90c03v1g4z1H815FJ8cbbdvCGDQwvRqnEZTmIWezgayGMwXFG mkNBY7Lm7LWgtBBDx3L0TlMRctllebyTMBqDhh5FIePSDqKIJ7sk2/nB7N7/9QaSrFPyXJrlrDx 9coSJNJ0okKjllwivtMWVhh4Ja6IC/SFS9kJWel74NHDVqiYfHL3T7r4QnLILonFynZzLJXT9Mz qc05yi003gppI61ap4uvkl/5xloLFYdhDPgTVwgAtXk+wRkm6sbFvdObSxeIckgpWv X-Received: by 2002:a05:6a00:14ca:b0:82f:721f:10ea with SMTP id d2e1a72fcca58-8352d1bf331mr13120000b3a.18.1777962084168; Mon, 04 May 2026 23:21:24 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.12]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8c10sm880738b3a.37.2026.05.04.23.21.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 May 2026 23:21:23 -0700 (PDT) From: Yunhui Cui To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, bjorn@kernel.org, pulehui@huawei.com, puranjay@kernel.org, thuth@redhat.com, ajones@ventanamicro.com, ben.dooks@codethink.co.uk, rkrcmar@ventanamicro.com, cuiyunhui@bytedance.com, samuel.holland@sifive.com, zong.li@sifive.com, conor.dooley@microchip.com, tglx@kernel.org, debug@rivosinc.com, seanwascoding@gmail.com, andybnac@gmail.com, menglong8.dong@gmail.com, cyrilbur@tenstorrent.com, wangruikang@iscas.ac.cn, atishp@rivosinc.com, apatel@ventanamicro.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, arnd@arndb.de, nathan@kernel.org, nick.desaulniers+lkml@gmail.com, morbo@google.com, justinstitt@google.com, qingfang.deng@siflower.com.cn, linux-arch@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 3/3] riscv: store percpu offset into thread_info Date: Tue, 5 May 2026 14:20:26 +0800 Message-Id: <20260505062026.91724-4-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260505062026.91724-1-cuiyunhui@bytedance.com> References: <20260505062026.91724-1-cuiyunhui@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Originally we planned to add a register for the percpu offset, which would speed up percpu variable R/W and reduce access instructions. After discussion [1], it=E2=80=99s now stored in thread_info. [1] https://lists.riscv.org/g/tech-privileged/topic/risc_v_tech_arch_review= /113437553?page=3D2 Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/asm.h | 6 +----- arch/riscv/include/asm/percpu.h | 4 ++++ arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/include/asm/thread_info.h | 3 ++- arch/riscv/kernel/asm-offsets.c | 1 + arch/riscv/kernel/smpboot.c | 7 +++++++ arch/riscv/net/bpf_jit_comp64.c | 9 +-------- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h index e9e8ba83e632f..137a49488325e 100644 --- a/arch/riscv/include/asm/asm.h +++ b/arch/riscv/include/asm/asm.h @@ -91,11 +91,7 @@ =20 #ifdef CONFIG_SMP .macro asm_per_cpu dst sym tmp - lw \tmp, TASK_TI_CPU_NUM(tp) - slli \tmp, \tmp, RISCV_LGPTR - la \dst, __per_cpu_offset - add \dst, \dst, \tmp - REG_L \tmp, 0(\dst) + REG_L \tmp, TASK_TI_PCPU_OFFSET(tp) la \dst, \sym add \dst, \dst, \tmp .endm diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percp= u.h index 3b26fe45e70f4..84612d672105d 100644 --- a/arch/riscv/include/asm/percpu.h +++ b/arch/riscv/include/asm/percpu.h @@ -7,7 +7,9 @@ =20 #include #include +#include #include +#include =20 #define PERCPU_RW_OPS(sz) \ static inline unsigned long __percpu_read_##sz(void *ptr) \ @@ -275,6 +277,8 @@ _pcp_protect_return(__percpu_add_return_amo_case_64, pc= p, val) }) #endif =20 +#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset) + #include =20 #endif /* __ASM_PERCPU_H */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/sw= itch_to.h index 0e71eb82f920c..733b6cd306e40 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -88,6 +88,13 @@ static inline void __switch_to_envcfg(struct task_struct= *next) :: "r" (next->thread.envcfg) : "memory"); } =20 +static inline void __switch_to_pcpu_offset(struct task_struct *next) +{ +#ifdef CONFIG_SMP + next->thread_info.pcpu_offset =3D __my_cpu_offset; +#endif +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); =20 @@ -122,6 +129,7 @@ do { \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ __switch_to_envcfg(__next); \ + __switch_to_pcpu_offset(__next); \ ((last) =3D __switch_to(__prev, __next)); \ } while (0) =20 diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/= thread_info.h index 55019fdfa9eca..f10ba62b61016 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -53,6 +53,7 @@ struct thread_info { unsigned long flags; /* low level flags */ int preempt_count; /* 0=3D>preemptible, <0=3D>BUG */ + int cpu; /* * These stack pointers are overwritten on every system call or * exception. SP is also saved to the stack it can be recovered when @@ -60,8 +61,8 @@ struct thread_info { */ long kernel_sp; /* Kernel stack pointer */ long user_sp; /* User stack pointer */ - int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ + unsigned long pcpu_offset; #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offset= s.c index af827448a609e..fbf53b66b0e06 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -38,6 +38,7 @@ void asm_offsets(void) OFFSET(TASK_THREAD_SUM, task_struct, thread.sum); =20 OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu); + OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset); OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count); OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp); OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp); diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 8b628580fe118..41463a8400f6c 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -209,6 +209,11 @@ int __cpu_up(unsigned int cpu, struct task_struct *tid= le) } #endif =20 +void __init smp_prepare_boot_cpu(void) +{ + __my_cpu_offset =3D per_cpu_offset(smp_processor_id()); +} + void __init smp_cpus_done(unsigned int max_cpus) { } @@ -234,6 +239,8 @@ asmlinkage __visible void smp_callin(void) mmgrab(mm); current->active_mm =3D mm; =20 + __my_cpu_offset =3D per_cpu_offset(smp_processor_id()); + #ifdef CONFIG_HOTPLUG_PARALLEL cpuhp_ap_sync_alive(); #endif diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp6= 4.c index 2f1109dbf105b..177c19216013e 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -1395,15 +1395,8 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, s= truct rv_jit_context *ctx, if (rd !=3D rs) emit_mv(rd, rs, ctx); #ifdef CONFIG_SMP - /* Load current CPU number in T1 */ - emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu), + emit_ld(RV_REG_T1, offsetof(struct thread_info, pcpu_offset), RV_REG_TP, ctx); - /* Load address of __per_cpu_offset array in T2 */ - emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx); - /* Get address of __per_cpu_offset[cpu] in T1 */ - emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx); - /* Load __per_cpu_offset[cpu] in T1 */ - emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx); /* Add the offset to Rd */ emit_add(rd, rd, RV_REG_T1, ctx); #endif --=20 2.39.5