BPF List
 help / color / mirror / Atom feed
* [PATCH v11 00/10] riscv: optimize percpu access
@ 2026-12-03 13:53 Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
                   ` (9 more replies)
  0 siblings, 10 replies; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

RISC-V derives the current per-CPU offset from the CPU number and
__per_cpu_offset[], and uses generic this_cpu fallbacks that disable
local interrupts for read-modify-write operations.

Cache the per-CPU offset in thread_info and implement preemptible this_cpu
operations using recoverable GPR critical sections. Record the registers
used to form the per-CPU address so exception return can repair them if
the task resumes on a different CPU. Use native-width AMOs, Zabha for
subword operations when available, and masked LR/SC sequences otherwise.
Select HAVE_CMPXCHG_LOCAL for common per-CPU counter users.

The recoverable-GPR scheme follows the arm64 design. Only the protocol is
common, however: RISC-V uses a cached offset in thread_info, its own GPR
encoding and exception entry code, and AMO/LR/SC operation sequences.
Factoring these pieces into generic code would share little beyond the
field layout while coupling architecture entry paths, so keep the
implementation under arch/riscv.

As with arm64's SDEI path, independent NMI-like entry paths must
participate in the entry-state protocol directly. Keep the save, clear and
restore operations in common RISC-V assembly macros so SBI Supervisor
Software Events (SSE) can preserve an interrupted per-CPU operation. SBI
SSE support is developed separately and is not a dependency of this
series.

Previous version (v10):
https://lore.kernel.org/r/20261112104149.38589-1-cuiyunhui@bytedance.com

Changes since v10:

- Rebase onto next-20261202.
- Share always-inline GPR helpers with exception-table handling and keep
  x0 distinct from the epc slot in pt_regs, as reported by Sashiko.
- Pass GPR protocol operand names explicitly, as suggested by David Laight.
- Share the RMW implementation between void and add-return operations;
  use the common AMO primitive for exchange as well. Preserve typed
  outputs and the original pointer-to-integer input conversions.
- Split common primitives into patch 3, followed by the operation classes.
- Update the cmpxchg-local support table in the same patch as Kconfig.

Additional benchmark results (v9 vs. v10):

Compared with v9, which disables preemption around per-CPU RMW
operations, the preemptible v10 implementation was better in 125 of
174 full-suite results, worse in 41, and unchanged in 8.

Full-suite results:

+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| Benchmark               | Result Class                                             | Better | v9             | v10            | Value change     |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| lmbench/lat-mem-rd      | size:4MB P:1 stride:128B rnd_read_lat (ns)               | Lower  |        26.6190 |        24.6890 |    -7.25% better |
|                         | size:4MB P:1 stride:128B seq_read_lat (ns)               | Lower  |        14.4920 |        14.3700 |    -0.84% better |
|                         | size:16MB P:1 stride:128B rnd_read_lat (ns)              | Lower  |        32.8840 |        33.3580 |     +1.44% worse |
|                         | size:16MB P:1 stride:128B seq_read_lat (ns)              | Lower  |        14.1460 |        13.5610 |    -4.14% better |
|                         | size:64MB P:1 stride:128B rnd_read_lat (ns)              | Lower  |        60.6420 |        60.4600 |    -0.30% better |
|                         | size:64MB P:1 stride:128B seq_read_lat (ns)              | Lower  |        22.6720 |        23.6330 |     +4.24% worse |
|                         | size:256MB P:1 stride:128B rnd_read_lat (ns)             | Lower  |       112.5330 |       111.3480 |    -1.05% better |
|                         | size:256MB P:1 stride:128B seq_read_lat (ns)             | Lower  |        33.7700 |        35.6260 |     +5.50% worse |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| micromm/fork            | fork: p:1, d:10 (seconds)                                | Lower  |       0.009629 |       0.008324 |   -13.55% better |
|                         | fork: p:512, d:10 (seconds)                              | Lower  |       0.005628 |       0.005571 |    -1.01% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| micromm/munmap          | munmap: p:1, d:10 (seconds)                              | Lower  |       0.049843 |       0.044534 |   -10.65% better |
|                         | munmap: p:512, d:10 (seconds)                            | Lower  |       0.016619 |       0.014086 |   -15.24% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| micromm/vmalloc         | fix_align_alloc_test: p:1, h:0, l:500000 (usec)          | Lower  |      730413.00 |      728697.00 |    -0.23% better |
|                         | fix_size_alloc_test: p:1, h:0, l:500000 (usec)           | Lower  |      474259.00 |      465960.00 |    -1.75% better |
|                         | fix_size_alloc_test: p:4, h:0, l:500000 (usec)           | Lower  |      563186.00 |      550898.00 |    -2.18% better |
|                         | fix_size_alloc_test: p:16, h:0, l:500000 (usec)          | Lower  |     1155390.00 |     1101303.00 |    -4.68% better |
|                         | fix_size_alloc_test: p:16, h:1, l:500000 (usec)          | Lower  |     1155940.00 |     1104025.00 |    -4.49% better |
|                         | fix_size_alloc_test: p:64, h:0, l:100000 (usec)          | Lower  |      663881.00 |      616397.00 |    -7.15% better |
|                         | fix_size_alloc_test: p:64, h:1, l:100000 (usec)          | Lower  |      665416.00 |      617008.00 |    -7.27% better |
|                         | fix_size_alloc_test: p:256, h:0, l:100000 (usec)         | Lower  |     2916658.00 |     2643424.00 |    -9.37% better |
|                         | fix_size_alloc_test: p:256, h:1, l:100000 (usec)         | Lower  |     3041097.00 |     2802723.00 |    -7.84% better |
|                         | fix_size_alloc_test: p:512, h:0, l:100000 (usec)         | Lower  |     6653643.00 |     6193891.00 |    -6.91% better |
|                         | fix_size_alloc_test: p:512, h:1, l:100000 (usec)         | Lower  |     5768634.00 |     5254692.00 |    -8.91% better |
|                         | full_fit_alloc_test: p:1, h:0, l:500000 (usec)           | Lower  |      704128.00 |      687529.00 |    -2.36% better |
|                         | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | Lower  |      645524.00 |      619581.00 |    -4.02% better |
|                         | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | Lower  |      649019.00 |      629164.00 |    -3.06% better |
|                         | long_busy_list_alloc_test: p:1, h:0, l:500000 (usec)     | Lower  |     5795539.00 |     5465925.00 |    -5.69% better |
|                         | pcpu_alloc_test: p:1, h:0, l:500000 (usec)               | Lower  |      112804.00 |      106390.00 |    -5.69% better |
|                         | random_size_align_alloc_test: p:1, h:0, l:500000 (usec)  | Lower  |      874236.00 |      838639.00 |    -4.07% better |
|                         | random_size_alloc_test: p:1, h:0, l:500000 (usec)        | Lower  |     3025540.00 |     2821929.00 |    -6.73% better |
|                         | vm_map_ram_test: p:1, h:0, l:500000 (usec)               | Lower  |      152701.00 |      128499.00 |   -15.85% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| mmtests/hackbench       | hackbench-process-pipes-1 (seconds)                      | Lower  |       0.254000 |       0.263000 |     +3.54% worse |
|                         | hackbench-process-pipes-4 (seconds)                      | Lower  |       0.781000 |       0.809000 |     +3.59% worse |
|                         | hackbench-process-pipes-7 (seconds)                      | Lower  |         1.1930 |         1.1500 |    -3.60% better |
|                         | hackbench-process-pipes-12 (seconds)                     | Lower  |         1.8080 |         1.8340 |     +1.44% worse |
|                         | hackbench-process-pipes-21 (seconds)                     | Lower  |         2.7880 |         2.7690 |    -0.68% better |
|                         | hackbench-process-pipes-30 (seconds)                     | Lower  |         3.7400 |         3.7250 |    -0.40% better |
|                         | hackbench-process-pipes-48 (seconds)                     | Lower  |         5.6080 |         5.5810 |    -0.48% better |
|                         | hackbench-process-pipes-79 (seconds)                     | Lower  |         8.7140 |         8.7000 |    -0.16% better |
|                         | hackbench-process-pipes-110 (seconds)                    | Lower  |        11.9490 |        11.8520 |    -0.81% better |
|                         | hackbench-process-pipes-141 (seconds)                    | Lower  |        15.0550 |        14.9920 |    -0.42% better |
|                         | hackbench-process-pipes-172 (seconds)                    | Lower  |        18.2490 |        18.0760 |    -0.95% better |
|                         | hackbench-process-pipes-203 (seconds)                    | Lower  |        21.4070 |        21.2500 |    -0.73% better |
|                         | hackbench-process-pipes-234 (seconds)                    | Lower  |        24.5750 |        24.3840 |    -0.78% better |
|                         | hackbench-process-pipes-256 (seconds)                    | Lower  |        26.7710 |        26.6390 |    -0.49% better |
|                         | hackbench-process-sockets-1 (seconds)                    | Lower  |       0.511000 |       0.520000 |     +1.76% worse |
|                         | hackbench-process-sockets-4 (seconds)                    | Lower  |         1.1670 |         1.1760 |     +0.77% worse |
|                         | hackbench-process-sockets-7 (seconds)                    | Lower  |         1.8470 |         1.8320 |    -0.81% better |
|                         | hackbench-process-sockets-12 (seconds)                   | Lower  |         3.0650 |         3.0500 |    -0.49% better |
|                         | hackbench-process-sockets-21 (seconds)                   | Lower  |         5.4300 |         5.4170 |    -0.24% better |
|                         | hackbench-process-sockets-30 (seconds)                   | Lower  |         7.9250 |         7.9030 |    -0.28% better |
|                         | hackbench-process-sockets-48 (seconds)                   | Lower  |        13.0520 |        12.9270 |    -0.96% better |
|                         | hackbench-process-sockets-79 (seconds)                   | Lower  |        21.9220 |        21.8600 |    -0.28% better |
|                         | hackbench-process-sockets-110 (seconds)                  | Lower  |        31.1200 |        30.9530 |    -0.54% better |
|                         | hackbench-process-sockets-141 (seconds)                  | Lower  |        40.4780 |        40.3250 |    -0.38% better |
|                         | hackbench-process-sockets-172 (seconds)                  | Lower  |        50.1010 |        49.9360 |    -0.33% better |
|                         | hackbench-process-sockets-203 (seconds)                  | Lower  |        59.8770 |        59.6810 |    -0.33% better |
|                         | hackbench-process-sockets-234 (seconds)                  | Lower  |        69.7730 |        69.4610 |    -0.45% better |
|                         | hackbench-process-sockets-256 (seconds)                  | Lower  |        76.7300 |        76.3810 |    -0.45% better |
|                         | hackbench-thread-pipes-1 (seconds)                       | Lower  |       0.283000 |       0.260000 |    -8.13% better |
|                         | hackbench-thread-pipes-4 (seconds)                       | Lower  |       0.856000 |       0.870000 |     +1.64% worse |
|                         | hackbench-thread-pipes-7 (seconds)                       | Lower  |         1.3550 |         1.3250 |    -2.21% better |
|                         | hackbench-thread-pipes-12 (seconds)                      | Lower  |         1.9980 |         2.0130 |     +0.75% worse |
|                         | hackbench-thread-pipes-21 (seconds)                      | Lower  |         3.0800 |         3.0970 |     +0.55% worse |
|                         | hackbench-thread-pipes-30 (seconds)                      | Lower  |         4.1150 |         4.1310 |     +0.39% worse |
|                         | hackbench-thread-pipes-48 (seconds)                      | Lower  |         6.0770 |         6.0240 |    -0.87% better |
|                         | hackbench-thread-pipes-79 (seconds)                      | Lower  |         9.3780 |         9.2730 |    -1.12% better |
|                         | hackbench-thread-pipes-110 (seconds)                     | Lower  |        12.7690 |        12.6950 |    -0.58% better |
|                         | hackbench-thread-pipes-141 (seconds)                     | Lower  |        16.1010 |        15.9940 |    -0.66% better |
|                         | hackbench-thread-pipes-172 (seconds)                     | Lower  |        19.3980 |        19.0990 |    -1.54% better |
|                         | hackbench-thread-pipes-203 (seconds)                     | Lower  |        22.7110 |        22.3700 |    -1.50% better |
|                         | hackbench-thread-pipes-234 (seconds)                     | Lower  |        25.9580 |        25.9340 |    -0.09% better |
|                         | hackbench-thread-pipes-256 (seconds)                     | Lower  |        28.3330 |        28.0820 |    -0.89% better |
|                         | hackbench-thread-sockets-1 (seconds)                     | Lower  |       0.496000 |       0.468000 |    -5.65% better |
|                         | hackbench-thread-sockets-4 (seconds)                     | Lower  |         1.2080 |         1.2010 |    -0.58% better |
|                         | hackbench-thread-sockets-7 (seconds)                     | Lower  |         1.9020 |         1.9130 |     +0.58% worse |
|                         | hackbench-thread-sockets-12 (seconds)                    | Lower  |         3.0880 |         3.0740 |    -0.45% better |
|                         | hackbench-thread-sockets-21 (seconds)                    | Lower  |         5.4020 |         5.3330 |    -1.28% better |
|                         | hackbench-thread-sockets-30 (seconds)                    | Lower  |         7.8620 |         7.7810 |    -1.03% better |
|                         | hackbench-thread-sockets-48 (seconds)                    | Lower  |        12.9010 |        12.8100 |    -0.71% better |
|                         | hackbench-thread-sockets-79 (seconds)                    | Lower  |        21.7640 |        21.6060 |    -0.73% better |
|                         | hackbench-thread-sockets-110 (seconds)                   | Lower  |        30.7970 |        30.5640 |    -0.76% better |
|                         | hackbench-thread-sockets-141 (seconds)                   | Lower  |        40.1350 |        39.8600 |    -0.69% better |
|                         | hackbench-thread-sockets-172 (seconds)                   | Lower  |        49.4890 |        49.2010 |    -0.58% better |
|                         | hackbench-thread-sockets-203 (seconds)                   | Lower  |        59.1620 |        58.6650 |    -0.84% better |
|                         | hackbench-thread-sockets-234 (seconds)                   | Lower  |        68.7690 |        68.3610 |    -0.59% better |
|                         | hackbench-thread-sockets-256 (seconds)                   | Lower  |        75.6150 |        75.0600 |    -0.73% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| mmtests/kernbench       | kernbench-elsp-32 (seconds)                              | Lower  |       866.6114 |       865.3174 |    -0.15% better |
|                         | kernbench-syst-32 (seconds)                              | Lower  |       994.1059 |       967.4799 |    -2.68% better |
|                         | kernbench-user-32 (seconds)                              | Lower  |       24849.41 |       24837.62 |    -0.05% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| mmtests/sysbench-cpu    | sysbenchcpu-1 (ops/second)                               | Higher |       631.7800 |       631.5800 |     -0.03% worse |
|                         | sysbenchcpu-4 (ops/second)                               | Higher |        2526.93 |        2526.96 |  +0.0012% better |
|                         | sysbenchcpu-7 (ops/second)                               | Higher |        4422.28 |        4422.45 |  +0.0038% better |
|                         | sysbenchcpu-12 (ops/second)                              | Higher |        7581.49 |        7581.41 |   -0.0011% worse |
|                         | sysbenchcpu-21 (ops/second)                              | Higher |       13267.27 |       13267.25 |   -0.0002% worse |
|                         | sysbenchcpu-30 (ops/second)                              | Higher |       18948.00 |       18952.26 |    +0.02% better |
|                         | sysbenchcpu-48 (ops/second)                              | Higher |       20198.14 |       20181.18 |     -0.08% worse |
|                         | sysbenchcpu-79 (ops/second)                              | Higher |       20191.20 |       20189.42 |     -0.01% worse |
|                         | sysbenchcpu-110 (ops/second)                             | Higher |       20190.79 |       20197.18 |    +0.03% better |
|                         | sysbenchcpu-128 (ops/second)                             | Higher |       20193.14 |       20193.02 |   -0.0006% worse |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| mmtests/sysbench-mutex  | sysbenchmutex-1 (ms)                                     | Lower  |       169.4300 |       169.4100 |    -0.01% better |
|                         | sysbenchmutex-4 (ms)                                     | Lower  |       180.0300 |       179.9900 |    -0.02% better |
|                         | sysbenchmutex-7 (ms)                                     | Lower  |       180.6400 |       181.1000 |     +0.25% worse |
|                         | sysbenchmutex-12 (ms)                                    | Lower  |       181.7300 |       181.8200 |     +0.05% worse |
|                         | sysbenchmutex-21 (ms)                                    | Lower  |       182.3100 |       182.2200 |    -0.05% better |
|                         | sysbenchmutex-30 (ms)                                    | Lower  |       180.6600 |       180.5600 |    -0.06% better |
|                         | sysbenchmutex-48 (ms)                                    | Lower  |       248.5800 |       249.6300 |     +0.42% worse |
|                         | sysbenchmutex-79 (ms)                                    | Lower  |       417.2400 |       416.1900 |    -0.25% better |
|                         | sysbenchmutex-110 (ms)                                   | Lower  |       581.3000 |       581.4300 |     +0.02% worse |
|                         | sysbenchmutex-128 (ms)                                   | Lower  |       685.5700 |       654.6700 |    -4.51% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| mmtests/sysbench-thread | sysbenchthread-1 (ms)                                    | Lower  |       0.340000 |       0.350000 |     +2.94% worse |
|                         | sysbenchthread-4 (ms)                                    | Lower  |       0.460000 |       0.430000 |    -6.52% better |
|                         | sysbenchthread-7 (ms)                                    | Lower  |         2.8600 |         2.6600 |    -6.99% better |
|                         | sysbenchthread-12 (ms)                                   | Lower  |         4.6500 |         3.6800 |   -20.86% better |
|                         | sysbenchthread-21 (ms)                                   | Lower  |        15.2700 |        15.5500 |     +1.83% worse |
|                         | sysbenchthread-30 (ms)                                   | Lower  |        23.9500 |        23.9500 | +0.00% unchanged |
|                         | sysbenchthread-48 (ms)                                   | Lower  |        84.4700 |        81.4800 |    -3.54% better |
|                         | sysbenchthread-79 (ms)                                   | Lower  |       186.5400 |       158.6300 |   -14.96% better |
|                         | sysbenchthread-110 (ms)                                  | Lower  |       297.9200 |       235.7400 |   -20.87% better |
|                         | sysbenchthread-128 (ms)                                  | Lower  |       397.3900 |       356.7000 |   -10.24% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| perf/futex              | hash (ops/sec)                                           | Higher |     2067692.00 |      304671.00 |    -85.27% worse |
|                         | lock-pi (ops/sec)                                        | Higher |       541.0000 |       541.0000 | +0.00% unchanged |
|                         | requeue (ms)                                             | Lower  |       0.017600 |       0.016900 |    -3.98% better |
|                         | wake (ms)                                                | Lower  |       0.037800 |       0.047000 |    +24.34% worse |
|                         | wake-parallel (ms)                                       | Lower  |       0.002500 |       0.002300 |    -8.00% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| perf/mem                | memset -k 1GB -f default -s 16GB (bytes/sec)             | Higher | 36656487161.92 | 36609462638.74 |     -0.13% worse |
|                         | mmap -p 1GB -f demand -s 32GB -l 5 (bytes/sec)           | Higher | 47179232199.05 | 51227254981.66 |    +8.58% better |
|                         | mmap -p 4KB -f demand -s 32GB -l 5 (bytes/sec)           | Higher |  1743560227.42 |  1559118225.60 |    -10.58% worse |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| perf/sched              | messaging -l 10000 (sec)                                 | Lower  |         2.5860 |         2.5500 |    -1.39% better |
|                         | pipe (ops/sec)                                           | Higher |      409742.00 |      482570.00 |   +17.77% better |
|                         | seccomp-notify (ops/sec)                                 | Higher |      173488.00 |      175578.00 |    +1.20% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| perf/syscall            | basic (ops/sec)                                          | Higher |     8613932.00 |     8534993.00 |     -0.92% worse |
|                         | execve (ops/sec)                                         | Higher |        3177.00 |        3197.00 |    +0.63% better |
|                         | fork (ops/sec)                                           | Higher |        8837.00 |        9098.00 |    +2.95% better |
|                         | getpgid (ops/sec)                                        | Higher |     8627107.00 |     8622726.00 |     -0.05% worse |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| sockperf/echo-lat-tcp   | ping-pong m:64B t:10s, avg_latency (usec)                | Lower  |         6.2880 |         6.1970 |    -1.45% better |
|                         | ping-pong m:64B t:10s, latency_p99 (usec)                | Lower  |         6.4940 |         6.4050 |    -1.37% better |
|                         | ping-pong m:256B t:10s, avg_latency (usec)               | Lower  |         6.3740 |         6.1720 |    -3.17% better |
|                         | ping-pong m:256B t:10s, latency_p99 (usec)               | Lower  |         6.7850 |         6.5700 |    -3.17% better |
|                         | ping-pong m:1024B t:10s, avg_latency (usec)              | Lower  |         6.6480 |         6.3580 |    -4.36% better |
|                         | ping-pong m:1024B t:10s, latency_p99 (usec)              | Lower  |         7.0250 |         6.7740 |    -3.57% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| sockperf/echo-lat-udp   | ping-pong m:64B t:10s, avg_latency (usec)                | Lower  |         4.8730 |         4.8540 |    -0.39% better |
|                         | ping-pong m:64B t:10s, latency_p99 (usec)                | Lower  |         4.9690 |         4.9340 |    -0.70% better |
|                         | ping-pong m:256B t:10s, avg_latency (usec)               | Lower  |         4.8530 |         4.7940 |    -1.22% better |
|                         | ping-pong m:256B t:10s, latency_p99 (usec)               | Lower  |         4.9250 |         4.8750 |    -1.02% better |
|                         | ping-pong m:1024B t:10s, avg_latency (usec)              | Lower  |         5.1370 |         4.9760 |    -3.13% better |
|                         | ping-pong m:1024B t:10s, latency_p99 (usec)              | Lower  |         5.2200 |         5.0500 |    -3.26% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| sockperf/packet-tp-tcp  | throughput m:64B t:10s, msg_rate (msg/sec)               | Higher |     1148912.00 |     1009929.00 |    -12.10% worse |
|                         | throughput m:64B t:10s, throughput_mbps (Mbps)           | Higher |       560.9920 |       493.1290 |    -12.10% worse |
|                         | throughput m:256B t:10s, msg_rate (msg/sec)              | Higher |     2008960.00 |     1983810.00 |     -1.25% worse |
|                         | throughput m:256B t:10s, throughput_mbps (Mbps)          | Higher |        3923.75 |        3874.63 |     -1.25% worse |
|                         | throughput m:1024B t:10s, msg_rate (msg/sec)             | Higher |     1491569.00 |     1488793.00 |     -0.19% worse |
|                         | throughput m:1024B t:10s, throughput_mbps (Mbps)         | Higher |       11652.88 |       11631.19 |     -0.19% worse |
|                         | throughput m:1472B t:10s, msg_rate (msg/sec)             | Higher |     1262055.00 |     1278946.00 |    +1.34% better |
|                         | throughput m:1472B t:10s, throughput_mbps (Mbps)         | Higher |       14173.47 |       14363.16 |    +1.34% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| sockperf/packet-tp-udp  | throughput m:64B t:10s, msg_rate (msg/sec)               | Higher |      485145.00 |      490886.00 |    +1.18% better |
|                         | throughput m:64B t:10s, throughput_mbps (Mbps)           | Higher |       236.8870 |       239.6900 |    +1.18% better |
|                         | throughput m:256B t:10s, msg_rate (msg/sec)              | Higher |      482665.00 |      487222.00 |    +0.94% better |
|                         | throughput m:256B t:10s, throughput_mbps (Mbps)          | Higher |       942.7050 |       951.6050 |    +0.94% better |
|                         | throughput m:1024B t:10s, msg_rate (msg/sec)             | Higher |      438805.00 |      455347.00 |    +3.77% better |
|                         | throughput m:1024B t:10s, throughput_mbps (Mbps)         | Higher |        3428.16 |        3557.40 |    +3.77% better |
|                         | throughput m:1472B t:10s, msg_rate (msg/sec)             | Higher |      431885.00 |      448922.00 |    +3.94% better |
|                         | throughput m:1472B t:10s, throughput_mbps (Mbps)         | Higher |        4850.27 |        5041.60 |    +3.94% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| speedometer/v2.0        | score (runs/min)                                         | Higher |       104.0000 |       103.0000 |     -0.96% worse |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| speedometer/v2.1        | score (runs/min)                                         | Higher |       103.0000 |       104.0000 |    +0.97% better |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| syscall/getpid          | mean (ns)                                                | Lower  |       132.0000 |       133.0000 |     +0.76% worse |
|                         | p99 (ns)                                                 | Lower  |       140.0000 |       140.0000 | +0.00% unchanged |
|                         | p99.9 (ns)                                               | Lower  |       140.0000 |       140.0000 | +0.00% unchanged |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| syscall/getppid         | mean (ns)                                                | Lower  |       136.0000 |       137.0000 |     +0.74% worse |
|                         | p99 (ns)                                                 | Lower  |       140.0000 |       140.0000 | +0.00% unchanged |
|                         | p99.9 (ns)                                               | Lower  |       140.0000 |       140.0000 | +0.00% unchanged |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+
| syscall/invalid         | mean (ns)                                                | Lower  |       125.0000 |       128.0000 |     +2.40% worse |
|                         | p99 (ns)                                                 | Lower  |       130.0000 |       130.0000 | +0.00% unchanged |
|                         | p99.9 (ns)                                               | Lower  |       130.0000 |       130.0000 | +0.00% unchanged |
+-------------------------+----------------------------------------------------------+--------+----------------+----------------+------------------+

Focused follow-up results:

+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| Benchmark              | Result Class                                             | Better | v9 median     | v10 median    | Value change     |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| lmbench/lat-mem-rd     | size:16MB P:1 stride:128B seq_read_lat (ns)              | Lower  |        5.5695 |        3.8965 |   -30.04% better |
|                        | size:256MB P:1 stride:128B seq_read_lat (ns)             | Lower  |       32.5340 |       32.5300 |    -0.01% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| micromm/munmap         | munmap: p:1, d:10 (seconds)                              | Lower  |      0.047575 |      0.044289 |    -6.91% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| micromm/vmalloc        | fix_align_alloc_test: p:1, h:0, l:500000 (usec)          | Lower  |     754962.50 |     736697.00 |    -2.42% better |
|                        | fix_size_alloc_test: p:1, h:0, l:500000 (usec)           | Lower  |     483472.50 |     477528.50 |    -1.23% better |
|                        | fix_size_alloc_test: p:4, h:0, l:500000 (usec)           | Lower  |     571667.50 |     559921.50 |    -2.05% better |
|                        | fix_size_alloc_test: p:16, h:0, l:500000 (usec)          | Lower  |    1160889.50 |    1106664.50 |    -4.67% better |
|                        | fix_size_alloc_test: p:16, h:1, l:500000 (usec)          | Lower  |    1159897.00 |    1107734.00 |    -4.50% better |
|                        | fix_size_alloc_test: p:64, h:0, l:100000 (usec)          | Lower  |     665350.50 |     618978.00 |    -6.97% better |
|                        | fix_size_alloc_test: p:64, h:1, l:100000 (usec)          | Lower  |     667655.00 |     621521.50 |    -6.91% better |
|                        | fix_size_alloc_test: p:256, h:0, l:100000 (usec)         | Lower  |    2901193.00 |    2657711.50 |    -8.39% better |
|                        | fix_size_alloc_test: p:256, h:1, l:100000 (usec)         | Lower  |    3031306.50 |    2770959.00 |    -8.59% better |
|                        | fix_size_alloc_test: p:512, h:0, l:100000 (usec)         | Lower  |    6608133.50 |    6233210.00 |    -5.67% better |
|                        | fix_size_alloc_test: p:512, h:1, l:100000 (usec)         | Lower  |    5731815.50 |    5373078.00 |    -6.26% better |
|                        | full_fit_alloc_test: p:1, h:0, l:500000 (usec)           | Lower  |     763408.50 |     745992.50 |    -2.28% better |
|                        | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | Lower  |     632521.50 |     640865.50 |     +1.32% worse |
|                        | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | Lower  |     624654.50 |     629378.00 |     +0.76% worse |
|                        | long_busy_list_alloc_test: p:1, h:0, l:500000 (usec)     | Lower  |    5936540.50 |    5615995.50 |    -5.40% better |
|                        | pcpu_alloc_test: p:1, h:0, l:500000 (usec)               | Lower  |     113027.00 |     106170.50 |    -6.07% better |
|                        | random_size_align_alloc_test: p:1, h:0, l:500000 (usec)  | Lower  |     866112.00 |     851073.00 |    -1.74% better |
|                        | random_size_alloc_test: p:1, h:0, l:500000 (usec)        | Lower  |    3036474.00 |    2842874.50 |    -6.38% better |
|                        | vm_map_ram_test: p:1, h:0, l:500000 (usec)               | Lower  |     148239.50 |     133500.50 |    -9.94% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| mmtests/sysbench-cpu   | sysbenchcpu-30 (ops/second)                              | Higher |      18950.26 |      18949.75 |   -0.0027% worse |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| perf/futex             | hash (ops/sec)                                           | Higher |     569218.50 |    2437354.00 |  +328.19% better |
|                        | wake (ms)                                                | Lower  |      0.041650 |      0.038600 |    -7.32% better |
|                        | wake-parallel (ms)                                       | Lower  |      0.002100 |      0.001950 |    -7.14% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| perf/mem               | mmap -p 4KB -f demand -s 32GB -l 5 (bytes/sec)           | Higher | 3403421742.79 | 3510746533.07 |    +3.15% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| perf/sched             | pipe (ops/sec)                                           | Higher |     485044.00 |     507518.00 |    +4.63% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| perf/syscall           | basic (ops/sec)                                          | Higher |    8615089.50 |    8535332.50 |     -0.93% worse |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| sockperf/packet-tp-tcp | throughput m:64B t:10s, msg_rate (msg/sec)               | Higher |    1068231.00 |    1070331.50 |    +0.20% better |
|                        | throughput m:64B t:10s, throughput_mbps (Mbps)           | Higher |      521.5970 |      522.6230 |    +0.20% better |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| syscall/getpid         | mean (ns)                                                | Lower  |      132.0000 |      133.0000 |     +0.76% worse |
|                        | p99 (ns)                                                 | Lower  |      140.0000 |      140.0000 | +0.00% unchanged |
|                        | p99.9 (ns)                                               | Lower  |      140.0000 |      140.0000 | +0.00% unchanged |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| syscall/getppid        | mean (ns)                                                | Lower  |      135.5000 |      137.0000 |     +1.11% worse |
|                        | p99 (ns)                                                 | Lower  |      140.0000 |      140.0000 | +0.00% unchanged |
|                        | p99.9 (ns)                                               | Lower  |      140.0000 |      140.0000 | +0.00% unchanged |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+
| syscall/invalid        | mean (ns)                                                | Lower  |      125.0000 |      127.0000 |     +1.60% worse |
|                        | p99 (ns)                                                 | Lower  |      130.0000 |      130.0000 | +0.00% unchanged |
|                        | p99.9 (ns)                                               | Lower  |      130.0000 |      130.0000 | +0.00% unchanged |
+------------------------+----------------------------------------------------------+--------+---------------+---------------+------------------+

Changes since v9 (v10):

- Rework the this_cpu operations to remain preemptible using recoverable
  GPR critical sections, as suggested by David Laight and Jisheng Zhang.
- Put the cached per-CPU offset first, then split the exception/fixup
  infrastructure and operation classes into independently reviewable
  patches.
- Keep GPR-state save and restore in reusable assembly macros for NMI-like
  entry paths such as SBI SSE.
- Keep masked LR/SC cmpxchg operands XLEN-wide so RV64 high-byte and
  high-halfword comparisons are not affected by LR.W sign extension.
- Force both halves of the AMOCAS.Q new value into fixed registers so the
  implicit register pair remains valid when either half is zero.
- Use TYPEOF_UNQUAL() in wrappers and fully parenthesize this_cpu_write()
  values.

Changes since v8 (v9):

- Drop the PIO helper patch, which has already been merged.
- Rename the first patch and update its changelog as suggested by
  Davidlohr Bueso.
- Use value-returning AMOs instead of rd=x0 for CPU-local operations.
- Remove unnecessary memory clobbers from the new relaxed AMO and LR/SC
  code.
- Use .option arch,+zabha for the Zabha inline asm, matching the
  existing RISC-V cmpxchg implementation.
- Isolate arch_cmpxchg_relaxed() behind typed always-inline helpers to
  avoid macro variable capture while preserving atomic instrumentation.
- Make the cmpxchg128 fallback use the already resolved per-CPU pointer,
  avoiding the same macro variable capture.
- Copy the cached per-CPU offset from prev to next during context switch,
  as suggested by Thomas Gleixner.
- Initialize secondary idle tasks in the common startup path, covering
  parallel bringup and CPU hotplug, and remove the duplicate initialization
  from smp_callin(), as reported by Sashiko.
- Select HAVE_CMPXCHG_LOCAL in a separate patch.

Yunhui Cui (10):
  riscv: store percpu offset into thread_info
  riscv: add infrastructure for preemptible this_cpu operations
  riscv: add common primitives for preemptible this_cpu operations
  riscv: implement preemptible this_cpu read and write operations
  riscv: implement preemptible this_cpu void RMW operations
  riscv: implement preemptible this_cpu add_return operations
  riscv: implement preemptible this_cpu xchg operations
  riscv: implement preemptible this_cpu cmpxchg operations
  riscv: implement preemptible this_cpu cmpxchg128
  riscv: select HAVE_CMPXCHG_LOCAL

 .../locking/cmpxchg-local/arch-support.txt    |   2 +-
 arch/riscv/Kconfig                            |   1 +
 arch/riscv/include/asm/asm.h                  |  25 +-
 arch/riscv/include/asm/percpu.h               | 575 ++++++++++++++++++
 arch/riscv/include/asm/ptrace.h               |  22 +
 arch/riscv/include/asm/switch_to.h            |   9 +
 arch/riscv/include/asm/thread_info.h          |   4 +-
 arch/riscv/kernel/Makefile                    |   1 +
 arch/riscv/kernel/asm-offsets.c               |   4 +-
 arch/riscv/kernel/entry-common.c              |  30 +
 arch/riscv/kernel/entry.S                     |  10 +
 arch/riscv/kernel/smpboot.c                   |  10 +-
 arch/riscv/mm/extable.c                       |  18 -
 arch/riscv/net/bpf_jit_comp64.c               |   9 +-
 14 files changed, 685 insertions(+), 35 deletions(-)
 create mode 100644 arch/riscv/include/asm/percpu.h
 create mode 100644 arch/riscv/kernel/entry-common.c


base-commit: 799dd936553182df5770dde7f38d94c8337df75a

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v11 01/10] riscv: store percpu offset into thread_info
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:20   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

RISC-V currently derives the current per-CPU offset from the CPU number
and __per_cpu_offset[]. Cache it in thread_info so per-CPU accesses can
load the offset directly.

Initialize the cached offset for the boot and secondary CPUs, and
propagate it across context switches. Set it before each secondary CPU
startup, including parallel bringup and CPU hotplug.

Link: https://lists.riscv.org/g/tech-privileged/topic/risc_v_tech_arch_review/113437553?page=2
Link: https://lore.kernel.org/r/20260826012457.ig3rfmji7vywf6kc@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/asm.h         |  6 +-----
 arch/riscv/include/asm/percpu.h      | 14 ++++++++++++++
 arch/riscv/include/asm/switch_to.h   |  9 +++++++++
 arch/riscv/include/asm/thread_info.h |  3 ++-
 arch/riscv/kernel/asm-offsets.c      |  2 +-
 arch/riscv/kernel/smpboot.c          | 10 +++++++++-
 arch/riscv/net/bpf_jit_comp64.c      |  9 +--------
 7 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100644 arch/riscv/include/asm/percpu.h

diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
index b8bf842d4c136..06ac38f733b0a 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -92,11 +92,7 @@
 
 #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/percpu.h
new file mode 100644
index 0000000000000..faf9a75ec66d7
--- /dev/null
+++ b/arch/riscv/include/asm/percpu.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ASM_PERCPU_H
+#define __ASM_PERCPU_H
+
+#include <linux/thread_info.h>
+
+#include <asm/current.h>
+
+#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
+
+#include <asm-generic/percpu.h>
+
+#endif /* __ASM_PERCPU_H */
diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
index 970f89935f0ee..1b11c79847610 100644
--- a/arch/riscv/include/asm/switch_to.h
+++ b/arch/riscv/include/asm/switch_to.h
@@ -107,6 +107,14 @@ static inline void __switch_to_envcfg(struct task_struct *next)
 			:: "r" (next->thread.envcfg) : "memory");
 }
 
+static inline void __switch_to_pcpu_offset(struct task_struct *prev,
+					   struct task_struct *next)
+{
+#ifdef CONFIG_SMP
+	next->thread_info.pcpu_offset = prev->thread_info.pcpu_offset;
+#endif
+}
+
 extern struct task_struct *__switch_to(struct task_struct *,
 				       struct task_struct *);
 
@@ -143,6 +151,7 @@ do {							\
 	if (switch_to_should_flush_icache(__next))	\
 		local_flush_icache_all();		\
 	__switch_to_envcfg(__next);			\
+	__switch_to_pcpu_offset(__prev, __next);		\
 	((last) = __switch_to(__prev, __next));		\
 } while (0)
 
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=>preemptible, <0=>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-offsets.c
index a75f0cfea1e9f..f4f7ee976cd94 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);
 
 	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);
@@ -50,7 +51,6 @@ void asm_offsets(void)
 	OFFSET(TASK_TI_A2, task_struct, thread_info.a2);
 #endif
 
-	OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu);
 #ifdef CONFIG_RISCV_USER_CFI
 	OFFSET(TASK_TI_CFI_STATE, task_struct, thread_info.user_cfi_state);
 	OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk);
diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c
index 6436cf1fac067..a40f0c47a238d 100644
--- a/arch/riscv/kernel/smpboot.c
+++ b/arch/riscv/kernel/smpboot.c
@@ -177,6 +177,10 @@ void __init setup_smp(void)
 
 static int start_secondary_cpu(int cpu, struct task_struct *tidle)
 {
+	/* The idle task is reused across CPU hotplug. */
+	tidle->thread_info.cpu = cpu;
+	tidle->thread_info.pcpu_offset = per_cpu_offset(cpu);
+
 	if (cpu_ops->cpu_start)
 		return cpu_ops->cpu_start(cpu, tidle);
 
@@ -192,7 +196,6 @@ int arch_cpuhp_kick_ap_alive(unsigned int cpu, struct task_struct *tidle)
 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 {
 	int ret;
-	tidle->thread_info.cpu = cpu;
 
 	ret = start_secondary_cpu(cpu, tidle);
 	if (!ret) {
@@ -210,6 +213,11 @@ int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 }
 #endif
 
+void __init smp_prepare_boot_cpu(void)
+{
+	__my_cpu_offset = per_cpu_offset(smp_processor_id());
+}
+
 void __init smp_cpus_done(unsigned int max_cpus)
 {
 }
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index a7a7d2f9da85a..23380d12f6163 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -1466,15 +1466,8 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 			if (rd != 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

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:33   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 03/10] riscv: add common primitives " Yunhui Cui
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Generic this_cpu operations disable preemption or local interrupts to
keep per-CPU accesses on the current CPU. Provide infrastructure for
RISC-V operations to remain preemptible.

Track the registers used for per-CPU address generation so exception
return can repair an interrupted operation after CPU migration.

Preserve this state across exception handlers so handlers can use
per-CPU operations independently. Make the entry/exit protocol available
to NMI-like paths, including SBI Supervisor Software Events.

Suggested-by: David Laight <david.laight.linux@gmail.com>
Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
Suggested-by: Jisheng Zhang <jszhang@kernel.org>
Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/asm.h         | 19 ++++++++++++
 arch/riscv/include/asm/percpu.h      | 46 ++++++++++++++++++++++++++++
 arch/riscv/include/asm/ptrace.h      | 22 +++++++++++++
 arch/riscv/include/asm/thread_info.h |  1 +
 arch/riscv/kernel/Makefile           |  1 +
 arch/riscv/kernel/asm-offsets.c      |  2 ++
 arch/riscv/kernel/entry-common.c     | 30 ++++++++++++++++++
 arch/riscv/kernel/entry.S            | 10 ++++++
 arch/riscv/mm/extable.c              | 18 -----------
 9 files changed, 131 insertions(+), 18 deletions(-)
 create mode 100644 arch/riscv/kernel/entry-common.c

diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h
index 06ac38f733b0a..57af10a936d9b 100644
--- a/arch/riscv/include/asm/asm.h
+++ b/arch/riscv/include/asm/asm.h
@@ -81,6 +81,25 @@
 
 /* Common assembly source macros */
 
+/*
+ * Save and clear the active per-CPU GPR state on exception entry. Entry
+ * paths which bypass handle_exception, such as SBI Supervisor Software
+ * Events (SSE), must pair this with pcpu_gprs_exit around their handlers.
+ */
+.macro pcpu_gprs_entry tsk regs tmp
+	lhu \tmp, TASK_TI_PCPU_GPRS(\tsk)
+	REG_S \tmp, PT_PCPU_GPRS(\regs)
+	sh zero, TASK_TI_PCPU_GPRS(\tsk)
+.endm
+
+/*
+ * Restore the saved state before resuming the interrupted context. tmp must
+ * contain PT_PCPU_GPRS from the interrupted frame.
+ */
+.macro pcpu_gprs_exit tsk tmp
+	sh \tmp, TASK_TI_PCPU_GPRS(\tsk)
+.endm
+
 /*
  * NOP sequence
  */
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index faf9a75ec66d7..3cd60ef18815e 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -3,12 +3,58 @@
 #ifndef __ASM_PERCPU_H
 #define __ASM_PERCPU_H
 
+#include <linux/bits.h>
+#include <linux/stringify.h>
 #include <linux/thread_info.h>
 
+#include <asm/asm.h>
 #include <asm/current.h>
+#include <asm/gpr-num.h>
 
 #define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
 
+#define PCPU_GPR_PCP_SHIFT	0
+#define PCPU_GPR_PCP		GENMASK(4, 0)
+#define PCPU_GPR_OFF_SHIFT	5
+#define PCPU_GPR_OFF		GENMASK(9, 5)
+#define PCPU_GPR_ADDR_SHIFT	10
+#define PCPU_GPR_ADDR		GENMASK(14, 10)
+
+#define __PCPU_GPR_NUM(gpr)	".L__gpr_num_%[" __stringify(gpr) "]"
+
+#define __PCPU_GPRS_VALUE						\
+	"("								\
+		"(.L__pcpu_gpr_pcp << " __stringify(PCPU_GPR_PCP_SHIFT) ") | " \
+		"(.L__pcpu_gpr_off << " __stringify(PCPU_GPR_OFF_SHIFT) ") | " \
+		"(.L__pcpu_gpr_addr << " __stringify(PCPU_GPR_ADDR_SHIFT) ")" \
+	")"
+
+#define __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr)			\
+	".set .L__pcpu_gpr_pcp, " __PCPU_GPR_NUM(pcp) "\n" \
+	".set .L__pcpu_gpr_off, " __PCPU_GPR_NUM(off) "\n" \
+	".set .L__pcpu_gpr_addr, " __PCPU_GPR_NUM(addr) "\n" \
+	".if ((.L__pcpu_gpr_pcp == .L__pcpu_gpr_off) || " \
+	      "(.L__pcpu_gpr_pcp == .L__pcpu_gpr_addr) || " \
+	      "(.L__pcpu_gpr_off == .L__pcpu_gpr_addr))\n" \
+	".error \"PCPU GPRs overlap\"\n" \
+	".endif\n"
+
+/*
+ * Mark a region where the per-CPU offset and address registers may need to
+ * be rewritten by exception return after migration.  The three registers
+ * must remain distinct for the whole region.
+ */
+#define __PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)		\
+	__DEFINE_ASM_GPR_NUMS						\
+	__PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr)			\
+	"li %[" #off "], " __PCPU_GPRS_VALUE "\n"\
+	"sh %[" #off "], %[" #gprs "]\n"		\
+	REG_L " %[" #off "], %[" #pcpu_offset "]\n"\
+	"add %[" #addr "], %[" #pcp "], %[" #off "]\n"
+
+#define __PCPU_GPRS_END(gprs)						\
+	"sh zero, " gprs "\n"
+
 #include <asm-generic/percpu.h>
 
 #endif /* __ASM_PERCPU_H */
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f7..355818c709b36 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -53,6 +53,8 @@ struct pt_regs {
 	unsigned long cause;
 	/* a0 value before the syscall */
 	unsigned long orig_a0;
+	/* Active preemptible per-CPU operation, if any. */
+	unsigned long pcpu_gprs;
 };
 
 #define PTRACE_SYSEMU			0x1f
@@ -145,6 +147,26 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
 	return *(unsigned long *)((unsigned long)regs + offset);
 }
 
+static __always_inline unsigned long
+regs_get_gpr(struct pt_regs *regs, unsigned int offset)
+{
+	if (unlikely(!offset || offset > MAX_REG_OFFSET))
+		return 0;
+
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+static __always_inline void regs_set_gpr(struct pt_regs *regs,
+					 unsigned int offset, unsigned long val)
+{
+	if (unlikely(!offset || offset > MAX_REG_OFFSET))
+		return;
+
+	*(unsigned long *)((unsigned long)regs + offset) = val;
+}
+
+void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs);
+
 /**
  * regs_get_kernel_argument() - get Nth function argument in kernel
  * @regs:       pt_regs of that context
diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
index f10ba62b61016..51c4076763c1b 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -63,6 +63,7 @@ struct thread_info {
 	long			user_sp;	/* User stack pointer */
 	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
 	unsigned long		pcpu_offset;
+	unsigned short		pcpu_gprs;
 #ifdef CONFIG_SHADOW_CALL_STACK
 	void			*scs_base;
 	void			*scs_sp;
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index f54c336bf547c..bf786d5dd4de3 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -52,6 +52,7 @@ obj-y	+= alternative.o
 obj-y	+= cpu.o
 obj-y	+= cpufeature.o
 obj-y	+= entry.o
+obj-y	+= entry-common.o
 obj-y	+= irq.o
 obj-y	+= process.o
 obj-y	+= ptrace.o
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index f4f7ee976cd94..38f246a691c60 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -39,6 +39,7 @@ void asm_offsets(void)
 
 	OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
 	OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset);
+	OFFSET(TASK_TI_PCPU_GPRS, task_struct, thread_info.pcpu_gprs);
 	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);
@@ -130,6 +131,7 @@ void asm_offsets(void)
 	OFFSET(PT_STATUS, pt_regs, status);
 	OFFSET(PT_BADADDR, pt_regs, badaddr);
 	OFFSET(PT_CAUSE, pt_regs, cause);
+	OFFSET(PT_PCPU_GPRS, pt_regs, pcpu_gprs);
 
 	OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs);
 
diff --git a/arch/riscv/kernel/entry-common.c b/arch/riscv/kernel/entry-common.c
new file mode 100644
index 0000000000000..164505962d131
--- /dev/null
+++ b/arch/riscv/kernel/entry-common.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/bitfield.h>
+#include <linux/compiler.h>
+#include <linux/percpu.h>
+
+#include <asm/current.h>
+#include <asm/ptrace.h>
+
+/*
+ * If the interrupted context had an active per-CPU operation, rewrite its
+ * saved offset and address registers for the CPU on which it will resume.
+ */
+void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs)
+{
+	unsigned long pcpu_offset = __my_cpu_offset;
+	unsigned long gprs = regs->pcpu_gprs;
+	unsigned long pcp;
+	unsigned int reg_pcp;
+	unsigned int reg_off;
+	unsigned int reg_addr;
+
+	reg_pcp = FIELD_GET(PCPU_GPR_PCP, gprs);
+	reg_off = FIELD_GET(PCPU_GPR_OFF, gprs);
+	reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
+
+	pcp = regs_get_gpr(regs, reg_pcp * sizeof(unsigned long));
+	regs_set_gpr(regs, reg_off * sizeof(unsigned long), pcpu_offset);
+	regs_set_gpr(regs, reg_addr * sizeof(unsigned long), pcp + pcpu_offset);
+}
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 628b85e3915a7..65298ac828959 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -198,6 +198,8 @@ SYM_CODE_START(handle_exception)
 	REG_S s4, PT_CAUSE(sp)
 	REG_S s5, PT_TP(sp)
 
+	pcpu_gprs_entry tp sp s0
+
 	/*
 	 * Set the scratch register to 0, so that if a recursive exception
 	 * occurs, the exception vector knows it came from the kernel
@@ -281,6 +283,14 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
 	move a0, sp
 	call riscv_v_context_nesting_end
 #endif
+	REG_L t0, PT_PCPU_GPRS(sp)
+	beqz t0, .Lpcpu_regs_restored
+	move a0, sp
+	call riscv_pcpu_regs_fixup
+	REG_L t0, PT_PCPU_GPRS(sp)
+.Lpcpu_regs_restored:
+	pcpu_gprs_exit tp t0
+
 	REG_L a0, PT_STATUS(sp)
 	restore_userssp s3, a0
 	/*
diff --git a/arch/riscv/mm/extable.c b/arch/riscv/mm/extable.c
index e252eba55edec..cb64c58148a83 100644
--- a/arch/riscv/mm/extable.c
+++ b/arch/riscv/mm/extable.c
@@ -27,24 +27,6 @@ static bool ex_handler_fixup(const struct exception_table_entry *ex,
 	return true;
 }
 
-static inline unsigned long regs_get_gpr(struct pt_regs *regs, unsigned int offset)
-{
-	if (unlikely(!offset || offset > MAX_REG_OFFSET))
-		return 0;
-
-	return *(unsigned long *)((unsigned long)regs + offset);
-}
-
-static inline void regs_set_gpr(struct pt_regs *regs, unsigned int offset,
-				unsigned long val)
-{
-	if (unlikely(offset > MAX_REG_OFFSET))
-		return;
-
-	if (offset)
-		*(unsigned long *)((unsigned long)regs + offset) = val;
-}
-
 static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
 					struct pt_regs *regs)
 {

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 03/10] riscv: add common primitives for preemptible this_cpu operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:19   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Void RMW and add-return operations need the same atomic updates, while
exchange operations can share the AMO execution. Provide common
primitives for these paths to avoid separate implementations of the
same address handling and atomic instructions.

Link: https://lore.kernel.org/r/20261112120928.24145d44@pumpkin
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 131 ++++++++++++++++++++++++++++++++
 1 file changed, 131 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 3cd60ef18815e..a5ceedc934b54 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -4,12 +4,15 @@
 #define __ASM_PERCPU_H
 
 #include <linux/bits.h>
+#include <linux/compiler.h>
 #include <linux/stringify.h>
 #include <linux/thread_info.h>
 
 #include <asm/asm.h>
+#include <asm/cpufeature-macros.h>
 #include <asm/current.h>
 #include <asm/gpr-num.h>
+#include <asm/hwcap.h>
 
 #define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
 
@@ -55,6 +58,134 @@
 #define __PCPU_GPRS_END(gprs)						\
 	"sh zero, " gprs "\n"
 
+#define __PCPU_OP_CONTEXT()						\
+	struct thread_info *__ti = (struct thread_info *)current;	\
+	unsigned long __off, __addr
+
+#define PERCPU_8_16_GET_SHIFT(pcp)					\
+	(((__force unsigned long)(pcp) & 0x3) * BITS_PER_BYTE)
+#define PERCPU_8_16_GET_MASK(sz)	GENMASK((sz) - 1, 0)
+#define PERCPU_8_16_GET_PCP32(pcp)					\
+	((void __percpu *)((__force unsigned long)(pcp) & ~0x3UL))
+
+#ifdef CONFIG_AS_HAS_OPTION_ARCH
+#define __PCPU_AMO_OPTION(isa)					\
+	".option push\n"					\
+	".option arch, +" isa "\n"
+#define __PCPU_AMO_OPTION_END					\
+	".option pop\n"
+#else
+#define __PCPU_AMO_OPTION(isa)
+#define __PCPU_AMO_OPTION_END
+#endif
+
+/*
+ * Keep address generation and the AMO in one recoverable region. A real
+ * output register is required even when the caller discards the old value,
+ * to favor execution near the CPU for CPU-local data.
+ */
+#define __PERCPU_AMO(insn, isa, ret_arg, pcp_arg, val_arg)		\
+do {									\
+	__PCPU_OP_CONTEXT();						\
+									\
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)		\
+		__PCPU_AMO_OPTION(isa)					\
+		insn " %[ret], %[val], 0(%[addr])\n"			\
+		__PCPU_AMO_OPTION_END				\
+		__PCPU_GPRS_END("%[gprs]")				\
+		: [gprs] "=m" (__ti->pcpu_gprs),			\
+		  [off] "=&r" (__off), [addr] "=&r" (__addr),		\
+		  [ret] "=&r" (ret_arg)					\
+		: [pcpu_offset] "m" (__ti->pcpu_offset),		\
+		  [pcp] "r" (pcp_arg), [val] "r" (val_arg)		\
+		: "memory");						\
+									\
+} while (0)
+
+/* Generate RMW helpers returning the new value; void interfaces discard it. */
+#define __PERCPU_AMO_OP_CASE(name, sz, sfx, op)				\
+static __always_inline unsigned long					\
+__percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val)	\
+{									\
+	u##sz tmp;							\
+									\
+	__PERCPU_AMO("amo" #name #sfx, "a", tmp, pcp, (u##sz)val);	\
+									\
+	tmp = tmp op val;						\
+	return tmp;							\
+}
+
+#define __PERCPU_8_16_OP_CASE(name, sz, sfx, op)			\
+static __always_inline unsigned long					\
+__percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val)	\
+{									\
+	__PCPU_OP_CONTEXT();						\
+	const u32 field_mask = PERCPU_8_16_GET_MASK(sz);		\
+	const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp);		\
+	const u32 mask = field_mask << shift;				\
+	const u32 val_trunc = val & field_mask;				\
+	void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp);		\
+	u32 old, new, field;						\
+									\
+	if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&			\
+	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) {	\
+		u##sz tmp;						\
+									\
+		__PERCPU_AMO("amo" #name #sfx, "zabha",			\
+			     tmp, pcp, (u##sz)val);			\
+									\
+		tmp = tmp op val;					\
+		return tmp;						\
+	}								\
+									\
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)		\
+		"0: lr.w %[old], 0(%[addr])\n"				\
+		"and %[field], %[old], %[mask]\n"			\
+		"srl %[field], %[field], %[shift]\n"			\
+		#name " %[field], %[field], %[val]\n"			\
+		"and %[field], %[field], %[field_mask]\n"		\
+		"sll %[field], %[field], %[shift]\n"			\
+		"and %[new], %[old], %[inv_mask]\n"			\
+		"or %[new], %[new], %[field]\n"				\
+		"sc.w %[new], %[new], 0(%[addr])\n"			\
+		"bnez %[new], 0b\n"					\
+		__PCPU_GPRS_END("%[gprs]")				\
+		: [gprs] "=m" (__ti->pcpu_gprs),			\
+		  [off] "=&r" (__off), [addr] "=&r" (__addr),		\
+		  [old] "=&r" (old), [new] "=&r" (new),			\
+		  [field] "=&r" (field)					\
+		: [pcpu_offset] "m" (__ti->pcpu_offset),		\
+		  [pcp] "r" (pcp32), [mask] "r" (mask),			\
+		  [shift] "r" (shift), [val] "r" (val_trunc),		\
+		  [field_mask] "r" (field_mask),			\
+		  [inv_mask] "r" (~mask)				\
+		: "memory");						\
+									\
+	return (u##sz)(field >> shift);					\
+}
+
+#define _pcp_wrap(op, pcp, ...)						\
+({									\
+	(void)op(&(pcp), __VA_ARGS__);					\
+})
+
+#define _pcp_wrap_return(op, pcp, args...)				\
+({									\
+	__auto_type __pcp_ptr = &(pcp);					\
+									\
+	(TYPEOF_UNQUAL(*__pcp_ptr))op(__pcp_ptr, ##args);		\
+})
+
+#undef __PERCPU_AMO
+#undef __PERCPU_AMO_OP_CASE
+#undef __PCPU_AMO_OPTION
+#undef __PCPU_AMO_OPTION_END
+#undef __PERCPU_8_16_OP_CASE
+#undef PERCPU_8_16_GET_SHIFT
+#undef PERCPU_8_16_GET_MASK
+#undef PERCPU_8_16_GET_PCP32
+#undef __PCPU_OP_CONTEXT
+
 #include <asm-generic/percpu.h>
 
 #endif /* __ASM_PERCPU_H */

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (2 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 03/10] riscv: add common primitives " Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:23   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to read and write
operations so they can remain preemptible across CPU migration.
This avoids the preemption or interrupt masking required by the
generic fallbacks.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 66 +++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index a5ceedc934b54..cd88618889494 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -176,7 +176,73 @@ __percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val)	\
 	(TYPEOF_UNQUAL(*__pcp_ptr))op(__pcp_ptr, ##args);		\
 })
 
+#define PERCPU_RW_OPS(load, store, sz)					\
+static __always_inline unsigned long					\
+__percpu_read_##sz(const void __percpu *pcp)				\
+{									\
+	__PCPU_OP_CONTEXT();						\
+	unsigned long val;						\
+									\
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)	\
+		load " %[val], 0(%[addr])\n"				\
+		__PCPU_GPRS_END("%[gprs]")				\
+		: [gprs] "=m" (__ti->pcpu_gprs),			\
+		  [off] "=&r" (__off), [addr] "=&r" (__addr),		\
+		  [val] "=&r" (val)					\
+		: [pcpu_offset] "m" (__ti->pcpu_offset),		\
+		  [pcp] "r" (pcp)					\
+		: "memory");						\
+									\
+	return (u##sz)val;						\
+}									\
+									\
+static __always_inline void __percpu_write_##sz(void __percpu *pcp,	\
+				       unsigned long val)		\
+{									\
+	__PCPU_OP_CONTEXT();						\
+									\
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)	\
+		store " %[val], 0(%[addr])\n"				\
+		__PCPU_GPRS_END("%[gprs]")				\
+		: [gprs] "=m" (__ti->pcpu_gprs),			\
+		  [off] "=&r" (__off), [addr] "=&r" (__addr)		\
+		: [pcpu_offset] "m" (__ti->pcpu_offset),		\
+		  [pcp] "r" (pcp), [val] "r" ((u##sz)val)		\
+		: "memory");						\
+}
+
+PERCPU_RW_OPS("lbu", "sb", 8)
+PERCPU_RW_OPS("lhu", "sh", 16)
+
+#ifdef CONFIG_64BIT
+PERCPU_RW_OPS("lwu", "sw", 32)
+PERCPU_RW_OPS("ld", "sd", 64)
+#else
+PERCPU_RW_OPS("lw", "sw", 32)
+#endif
+
+#define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
+#define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
+#define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
+
+#ifdef CONFIG_64BIT
+#define this_cpu_read_8(pcp)	_pcp_wrap_return(__percpu_read_64, pcp)
+#endif
+
+#define this_cpu_write_1(pcp, val)					\
+	_pcp_wrap(__percpu_write_8, pcp, (unsigned long)(val))
+#define this_cpu_write_2(pcp, val)					\
+	_pcp_wrap(__percpu_write_16, pcp, (unsigned long)(val))
+#define this_cpu_write_4(pcp, val)					\
+	_pcp_wrap(__percpu_write_32, pcp, (unsigned long)(val))
+
+#ifdef CONFIG_64BIT
+#define this_cpu_write_8(pcp, val)					\
+	_pcp_wrap(__percpu_write_64, pcp, (unsigned long)(val))
+#endif
+
 #undef __PERCPU_AMO
+#undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE
 #undef __PCPU_AMO_OPTION
 #undef __PCPU_AMO_OPTION_END

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (3 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:18   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to addition and bitwise
AND/OR operations. Use the common RMW helpers through the void wrapper
to make the updates atomic against interrupt handlers without masking
local interrupts or disabling preemption.

Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 38 +++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index cd88618889494..e39210ddd5026 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -221,6 +221,23 @@ PERCPU_RW_OPS("ld", "sd", 64)
 PERCPU_RW_OPS("lw", "sw", 32)
 #endif
 
+__PERCPU_AMO_OP_CASE(add, 32, .w, +)
+__PERCPU_AMO_OP_CASE(and, 32, .w, &)
+__PERCPU_AMO_OP_CASE(or, 32, .w, |)
+
+#ifdef CONFIG_64BIT
+__PERCPU_AMO_OP_CASE(add, 64, .d, +)
+__PERCPU_AMO_OP_CASE(and, 64, .d, &)
+__PERCPU_AMO_OP_CASE(or, 64, .d, |)
+#endif
+
+__PERCPU_8_16_OP_CASE(add, 8, .b, +)
+__PERCPU_8_16_OP_CASE(add, 16, .h, +)
+__PERCPU_8_16_OP_CASE(and, 8, .b, &)
+__PERCPU_8_16_OP_CASE(and, 16, .h, &)
+__PERCPU_8_16_OP_CASE(or, 8, .b, |)
+__PERCPU_8_16_OP_CASE(or, 16, .h, |)
+
 #define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
 #define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
 #define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
@@ -241,6 +258,27 @@ PERCPU_RW_OPS("lw", "sw", 32)
 	_pcp_wrap(__percpu_write_64, pcp, (unsigned long)(val))
 #endif
 
+#define this_cpu_add_1(pcp, val)	_pcp_wrap(__percpu_add_amo_case_8, pcp, val)
+#define this_cpu_add_2(pcp, val)	_pcp_wrap(__percpu_add_amo_case_16, pcp, val)
+#define this_cpu_add_4(pcp, val)	_pcp_wrap(__percpu_add_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_add_8(pcp, val)	_pcp_wrap(__percpu_add_amo_case_64, pcp, val)
+#endif
+
+#define this_cpu_and_1(pcp, val)	_pcp_wrap(__percpu_and_amo_case_8, pcp, val)
+#define this_cpu_and_2(pcp, val)	_pcp_wrap(__percpu_and_amo_case_16, pcp, val)
+#define this_cpu_and_4(pcp, val)	_pcp_wrap(__percpu_and_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_and_8(pcp, val)	_pcp_wrap(__percpu_and_amo_case_64, pcp, val)
+#endif
+
+#define this_cpu_or_1(pcp, val)		_pcp_wrap(__percpu_or_amo_case_8, pcp, val)
+#define this_cpu_or_2(pcp, val)		_pcp_wrap(__percpu_or_amo_case_16, pcp, val)
+#define this_cpu_or_4(pcp, val)		_pcp_wrap(__percpu_or_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_or_8(pcp, val)		_pcp_wrap(__percpu_or_amo_case_64, pcp, val)
+#endif
+
 #undef __PERCPU_AMO
 #undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 06/10] riscv: implement preemptible this_cpu add_return operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (4 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to add-return operations.
Use the common add helpers and value-returning wrapper so void and
add-return operations share the same atomic implementation.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index e39210ddd5026..939a0fef7246c 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -279,6 +279,17 @@ __PERCPU_8_16_OP_CASE(or, 16, .h, |)
 #define this_cpu_or_8(pcp, val)		_pcp_wrap(__percpu_or_amo_case_64, pcp, val)
 #endif
 
+#define this_cpu_add_return_1(pcp, val)					\
+	_pcp_wrap_return(__percpu_add_amo_case_8, pcp, val)
+#define this_cpu_add_return_2(pcp, val)					\
+	_pcp_wrap_return(__percpu_add_amo_case_16, pcp, val)
+#define this_cpu_add_return_4(pcp, val)					\
+	_pcp_wrap_return(__percpu_add_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_add_return_8(pcp, val)					\
+	_pcp_wrap_return(__percpu_add_amo_case_64, pcp, val)
+#endif
+
 #undef __PERCPU_AMO
 #undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 07/10] riscv: implement preemptible this_cpu xchg operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (5 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to exchange operations.
Use the shared AMO implementation for native-width and Zabha exchanges
to avoid generic interrupt masking. Provide an LR/SC fallback for subword
exchanges when Zabha is unavailable.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 69 +++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 939a0fef7246c..ca0b78b3e3397 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -238,6 +238,62 @@ __PERCPU_8_16_OP_CASE(and, 16, .h, &)
 __PERCPU_8_16_OP_CASE(or, 8, .b, |)
 __PERCPU_8_16_OP_CASE(or, 16, .h, |)
 
+#define __PERCPU_XCHG_AMO_CASE(sfx, sz)					\
+static __always_inline unsigned long					\
+__percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val)		\
+{									\
+	u##sz ret;							\
+									\
+	__PERCPU_AMO("amoswap" #sfx, "a", ret, pcp, (u##sz)val);	\
+	return ret;							\
+}
+
+__PERCPU_XCHG_AMO_CASE(.w, 32)
+#ifdef CONFIG_64BIT
+__PERCPU_XCHG_AMO_CASE(.d, 64)
+#endif
+
+#define __PERCPU_8_16_XCHG_CASE(sz, sfx)				\
+static __always_inline unsigned long					\
+__percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val)		\
+{									\
+	__PCPU_OP_CONTEXT();						\
+	const u32 field_mask = PERCPU_8_16_GET_MASK(sz);		\
+	const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp);		\
+	const u32 mask = field_mask << shift;				\
+	const u32 new_field = (val & field_mask) << shift;		\
+	void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp);		\
+	u32 old, new, rc;						\
+	u##sz ret;							\
+									\
+	if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&			\
+	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) {	\
+		__PERCPU_AMO("amoswap" #sfx, "zabha", ret, pcp,		\
+			     (u##sz)val);				\
+		return ret;						\
+	}								\
+									\
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)	\
+		"0: lr.w %[old], 0(%[addr])\n"				\
+		"and %[new], %[old], %[inv_mask]\n"			\
+		"or %[new], %[new], %[new_field]\n"			\
+		"sc.w %[rc], %[new], 0(%[addr])\n"			\
+		"bnez %[rc], 0b\n"					\
+		__PCPU_GPRS_END("%[gprs]")				\
+		: [gprs] "=m" (__ti->pcpu_gprs),			\
+		  [off] "=&r" (__off), [addr] "=&r" (__addr),		\
+		  [old] "=&r" (old), [new] "=&r" (new), [rc] "=&r" (rc)	\
+		: [pcpu_offset] "m" (__ti->pcpu_offset),		\
+		  [pcp] "r" (pcp32), [inv_mask] "r" (~mask),		\
+		  [new_field] "r" (new_field)				\
+		: "memory");						\
+									\
+	return (old & mask) >> shift;					\
+}
+
+__PERCPU_8_16_XCHG_CASE(8, .b)
+__PERCPU_8_16_XCHG_CASE(16, .h)
+
 #define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
 #define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
 #define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
@@ -290,12 +346,25 @@ __PERCPU_8_16_OP_CASE(or, 16, .h, |)
 	_pcp_wrap_return(__percpu_add_amo_case_64, pcp, val)
 #endif
 
+#define this_cpu_xchg_1(pcp, val)					\
+	_pcp_wrap_return(__percpu_xchg_case_8, pcp, (unsigned long)(val))
+#define this_cpu_xchg_2(pcp, val)					\
+	_pcp_wrap_return(__percpu_xchg_case_16, pcp, (unsigned long)(val))
+#define this_cpu_xchg_4(pcp, val)					\
+	_pcp_wrap_return(__percpu_xchg_case_32, pcp, (unsigned long)(val))
+#ifdef CONFIG_64BIT
+#define this_cpu_xchg_8(pcp, val)					\
+	_pcp_wrap_return(__percpu_xchg_case_64, pcp, (unsigned long)(val))
+#endif
+
 #undef __PERCPU_AMO
 #undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE
 #undef __PCPU_AMO_OPTION
 #undef __PCPU_AMO_OPTION_END
 #undef __PERCPU_8_16_OP_CASE
+#undef __PERCPU_XCHG_AMO_CASE
+#undef __PERCPU_8_16_XCHG_CASE
 #undef PERCPU_8_16_GET_SHIFT
 #undef PERCPU_8_16_GET_MASK
 #undef PERCPU_8_16_GET_PCP32

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 08/10] riscv: implement preemptible this_cpu cmpxchg operations
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (6 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
  2026-12-03 13:53 ` [PATCH v11 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
  9 siblings, 0 replies; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to compare-and-exchange
operations so they do not require local interrupt masking. Use AMOCAS
where available and LR/SC otherwise, keeping the API usable without
optional Zacas support.

Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 136 ++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index ca0b78b3e3397..5e7365044eae5 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -9,6 +9,7 @@
 #include <linux/thread_info.h>
 
 #include <asm/asm.h>
+#include <asm/cmpxchg.h>
 #include <asm/cpufeature-macros.h>
 #include <asm/current.h>
 #include <asm/gpr-num.h>
@@ -294,6 +295,123 @@ __percpu_xchg_case_##sz(void __percpu *pcp, unsigned long val)		\
 __PERCPU_8_16_XCHG_CASE(8, .b)
 __PERCPU_8_16_XCHG_CASE(16, .h)
 
+#define __PERCPU_CMPXCHG_CASE(sz, lr_sfx, sc_sfx, cas_sfx)		\
+static __always_inline unsigned long					\
+__percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old,	\
+			   unsigned long new)				\
+{									\
+	__PCPU_OP_CONTEXT();						\
+	const long cmp = (long)(s##sz)(u##sz)old;			\
+	unsigned long rc;						\
+	u##sz ret = (u##sz)old;						\
+									\
+	if (IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) &&			\
+	    IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZACAS) &&			\
+	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) {	\
+		asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+			".option push\n"				\
+			".option arch, +zacas\n"			\
+			"amocas" #cas_sfx " %[ret], %[new], 0(%[addr])\n"	\
+			".option pop\n"					\
+			__PCPU_GPRS_END("%[gprs]")			\
+			: [gprs] "=m" (__ti->pcpu_gprs),		\
+			  [off] "=&r" (__off), [addr] "=&r" (__addr),	\
+			  [ret] "+&r" (ret)				\
+			: [pcpu_offset] "m" (__ti->pcpu_offset),	\
+			  [pcp] "r" (pcp), [new] "r" ((u##sz)new)	\
+			: "memory");					\
+	} else {							\
+		asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+			"0: lr" #lr_sfx " %[ret], 0(%[addr])\n"		\
+			"bne %[ret], %[old], 1f\n"			\
+			"sc" #sc_sfx " %[rc], %[new], 0(%[addr])\n"	\
+			"bnez %[rc], 0b\n"				\
+			"1:\n"						\
+			__PCPU_GPRS_END("%[gprs]")			\
+			: [gprs] "=m" (__ti->pcpu_gprs),		\
+			  [off] "=&r" (__off), [addr] "=&r" (__addr),	\
+			  [ret] "=&r" (ret), [rc] "=&r" (rc)		\
+			: [pcpu_offset] "m" (__ti->pcpu_offset),	\
+			  [pcp] "r" (pcp), [old] "r" (cmp),		\
+			  [new] "r" ((u##sz)new)			\
+			: "memory");					\
+	}								\
+									\
+	return ret;							\
+}
+
+__PERCPU_CMPXCHG_CASE(32, .w, .w, .w)
+#ifdef CONFIG_64BIT
+__PERCPU_CMPXCHG_CASE(64, .d, .d, .d)
+#endif
+
+/*
+ * Use XLEN-wide masks to clear the upper bits of an RV64 sign-extended
+ * LR.W result before comparing a high byte or halfword.
+ */
+#define __PERCPU_8_16_CMPXCHG_CASE(sz, sfx)				\
+static __always_inline unsigned long					\
+__percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old,	\
+			   unsigned long new)				\
+{									\
+	__PCPU_OP_CONTEXT();						\
+	const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp);		\
+	const unsigned long field_mask = PERCPU_8_16_GET_MASK(sz);	\
+	const unsigned long mask = field_mask << shift;			\
+	const unsigned long old_field = (old & field_mask) << shift;	\
+	const unsigned long new_field = (new & field_mask) << shift;	\
+	void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp);		\
+	unsigned long ret32, tmp, rc;					\
+	u##sz ret = (u##sz)old;						\
+									\
+	if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&			\
+	    IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) &&			\
+	    IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZACAS) &&			\
+	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA) &&	\
+	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) {	\
+		asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+			".option push\n"				\
+			".option arch, +zacas, +zabha\n"		\
+			"amocas" #sfx " %[ret], %[new], 0(%[addr])\n"	\
+			".option pop\n"					\
+			__PCPU_GPRS_END("%[gprs]")			\
+			: [gprs] "=m" (__ti->pcpu_gprs),		\
+			  [off] "=&r" (__off), [addr] "=&r" (__addr),	\
+			  [ret] "+&r" (ret)				\
+			: [pcpu_offset] "m" (__ti->pcpu_offset),	\
+			  [pcp] "r" (pcp), [new] "r" ((u##sz)new)	\
+			: "memory");					\
+	} else {							\
+		asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)\
+			"0: lr.w %[ret], 0(%[addr])\n"			\
+			"and %[tmp], %[ret], %[mask]\n"			\
+			"bne %[tmp], %[old], 1f\n"			\
+			"and %[tmp], %[ret], %[inv_mask]\n"		\
+			"or %[tmp], %[tmp], %[new]\n"			\
+			"sc.w %[rc], %[tmp], 0(%[addr])\n"		\
+			"bnez %[rc], 0b\n"				\
+			"1:\n"						\
+			__PCPU_GPRS_END("%[gprs]")			\
+			: [gprs] "=m" (__ti->pcpu_gprs),		\
+			  [off] "=&r" (__off), [addr] "=&r" (__addr),	\
+			  [ret] "=&r" (ret32), [tmp] "=&r" (tmp),	\
+			  [rc] "=&r" (rc)				\
+			: [pcpu_offset] "m" (__ti->pcpu_offset),	\
+			  [pcp] "r" (pcp32), [mask] "r" (mask),		\
+			  [old] "r" (old_field),			\
+			  [new] "r" (new_field),			\
+			  [inv_mask] "r" (~mask)			\
+			: "memory");					\
+									\
+		ret = (ret32 & mask) >> shift;				\
+	}								\
+									\
+	return ret;							\
+}
+
+__PERCPU_8_16_CMPXCHG_CASE(8, .b)
+__PERCPU_8_16_CMPXCHG_CASE(16, .h)
+
 #define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
 #define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
 #define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
@@ -357,6 +475,22 @@ __PERCPU_8_16_XCHG_CASE(16, .h)
 	_pcp_wrap_return(__percpu_xchg_case_64, pcp, (unsigned long)(val))
 #endif
 
+#define this_cpu_cmpxchg_1(pcp, o, n)					\
+	_pcp_wrap_return(__percpu_cmpxchg_case_8, pcp,			\
+			 (unsigned long)(o), (unsigned long)(n))
+#define this_cpu_cmpxchg_2(pcp, o, n)					\
+	_pcp_wrap_return(__percpu_cmpxchg_case_16, pcp,			\
+			 (unsigned long)(o), (unsigned long)(n))
+#define this_cpu_cmpxchg_4(pcp, o, n)					\
+	_pcp_wrap_return(__percpu_cmpxchg_case_32, pcp,			\
+			 (unsigned long)(o), (unsigned long)(n))
+#ifdef CONFIG_64BIT
+#define this_cpu_cmpxchg_8(pcp, o, n)					\
+	_pcp_wrap_return(__percpu_cmpxchg_case_64, pcp,			\
+			 (unsigned long)(o), (unsigned long)(n))
+#define this_cpu_cmpxchg64(pcp, o, n)	this_cpu_cmpxchg_8(pcp, o, n)
+#endif
+
 #undef __PERCPU_AMO
 #undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE
@@ -365,6 +499,8 @@ __PERCPU_8_16_XCHG_CASE(16, .h)
 #undef __PERCPU_8_16_OP_CASE
 #undef __PERCPU_XCHG_AMO_CASE
 #undef __PERCPU_8_16_XCHG_CASE
+#undef __PERCPU_CMPXCHG_CASE
+#undef __PERCPU_8_16_CMPXCHG_CASE
 #undef PERCPU_8_16_GET_SHIFT
 #undef PERCPU_8_16_GET_MASK
 #undef PERCPU_8_16_GET_PCP32

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (7 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  2026-12-03 14:39   ` sashiko-bot
  2026-12-03 13:53 ` [PATCH v11 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
  9 siblings, 1 reply; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Apply the recoverable per-CPU address handling to native 128-bit
compare-and-exchange. Use AMOCAS.Q to provide an atomic 128-bit update,
with a local-IRQ-protected fallback where native support is unavailable.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/include/asm/percpu.h | 64 +++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 5e7365044eae5..a9d8620417e1b 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -14,6 +14,7 @@
 #include <asm/current.h>
 #include <asm/gpr-num.h>
 #include <asm/hwcap.h>
+#include <asm/irqflags.h>
 
 #define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset)
 
@@ -412,6 +413,64 @@ __percpu_cmpxchg_case_##sz(void __percpu *pcp, unsigned long old,	\
 __PERCPU_8_16_CMPXCHG_CASE(8, .b)
 __PERCPU_8_16_CMPXCHG_CASE(16, .h)
 
+#ifdef system_has_cmpxchg128
+static __always_inline u128
+__percpu_cmpxchg128_case(void __percpu *pcp, u128 old, u128 new)
+{
+	__PCPU_OP_CONTEXT();
+	union __u128_halves old_halves = { .full = old };
+	union __u128_halves new_halves = { .full = new };
+
+	register unsigned long new_lo asm("t1") = new_halves.low;
+	register unsigned long new_hi asm("t2") = new_halves.high;
+	register unsigned long old_lo asm("t3") = old_halves.low;
+	register unsigned long old_hi asm("t4") = old_halves.high;
+
+	asm volatile (__PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)
+		".option push\n"
+		".option arch, +zacas\n"
+		"amocas.q %[old_lo], %[new_lo], 0(%[addr])\n"
+		".option pop\n"
+		__PCPU_GPRS_END("%[gprs]")
+		: [gprs] "=m" (__ti->pcpu_gprs),
+		  [off] "=&r" (__off), [addr] "=&r" (__addr),
+		  [old_lo] "+&r" (old_lo), [old_hi] "+&r" (old_hi)
+		: [pcpu_offset] "m" (__ti->pcpu_offset),
+		  [pcp] "r" (pcp), [new_lo] "r" (new_lo),
+		  [new_hi] "r" (new_hi)
+		: "memory");
+
+	return ((u128)old_hi << 64) | old_lo;
+}
+
+static __always_inline u128
+__percpu_generic_cmpxchg128(void __percpu *pcp, u128 old, u128 new)
+{
+	unsigned long flags;
+	u128 *ptr;
+	u128 ret;
+
+	flags = arch_local_irq_save();
+	ptr = (u128 *)((__force unsigned long)pcp +
+			 __my_cpu_offset);
+	ret = *ptr;
+	if (ret == old)
+		*ptr = new;
+	arch_local_irq_restore(flags);
+
+	return ret;
+}
+
+static __always_inline u128
+__percpu_cmpxchg128(void __percpu *pcp, u128 old, u128 new)
+{
+	if (system_has_cmpxchg128())
+		return __percpu_cmpxchg128_case(pcp, old, new);
+
+	return __percpu_generic_cmpxchg128(pcp, old, new);
+}
+#endif
+
 #define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
 #define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
 #define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
@@ -491,6 +550,11 @@ __PERCPU_8_16_CMPXCHG_CASE(16, .h)
 #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)					\
+	_pcp_wrap_return(__percpu_cmpxchg128, pcp, (u128)(o), (u128)(n))
+#endif
+
 #undef __PERCPU_AMO
 #undef PERCPU_RW_OPS
 #undef __PERCPU_AMO_OP_CASE

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v11 10/10] riscv: select HAVE_CMPXCHG_LOCAL
  2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
                   ` (8 preceding siblings ...)
  2026-12-03 13:53 ` [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
@ 2026-12-03 13:53 ` Yunhui Cui
  9 siblings, 0 replies; 17+ messages in thread
From: Yunhui Cui @ 2026-12-03 13:53 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, bjorn, pulehui, puranjay, broonie, cuiyunhui, dave,
	david.laight.linux, jszhang, tglx, conor.dooley, thuth, ajones,
	ben.dooks, rkrcmar, samuel.holland, zong.li, debug, seanwascoding,
	andybnac, menglong8.dong, cyrilbur, wangruikang, atishp, apatel,
	arnd, nathan, nick.desaulniers+lkml, morbo, justinstitt,
	qingfang.deng, linux-riscv, linux-kernel, linux-mm, bpf,
	linux-arch, llvm

Select HAVE_CMPXCHG_LOCAL so mm/vmstat.c and lib/percpu_counter.c can use
this_cpu_try_cmpxchg() for local counter updates instead of masking
local interrupts.

Suggested-by: Davidlohr Bueso <dave@stgolabs.net>
Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 Documentation/features/locking/cmpxchg-local/arch-support.txt | 2 +-
 arch/riscv/Kconfig                                            | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/Documentation/features/locking/cmpxchg-local/arch-support.txt b/Documentation/features/locking/cmpxchg-local/arch-support.txt
index 127e8159adaa6..c9811fb5ddfd4 100644
--- a/Documentation/features/locking/cmpxchg-local/arch-support.txt
+++ b/Documentation/features/locking/cmpxchg-local/arch-support.txt
@@ -20,7 +20,7 @@
     |    openrisc: | TODO |
     |      parisc: | TODO |
     |     powerpc: | TODO |
-    |       riscv: | TODO |
+    |       riscv: |  ok  |
     |        s390: |  ok  |
     |          sh: | TODO |
     |       sparc: | TODO |
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 5d1bab5ff3316..11cbc682bd628 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -163,6 +163,7 @@ config RISCV
 	select HAVE_ARCH_VMAP_STACK if MMU && 64BIT
 	select HAVE_ASM_MODVERSIONS
 	select HAVE_BUILDTIME_MCOUNT_SORT
+	select HAVE_CMPXCHG_LOCAL
 	select HAVE_CONTEXT_TRACKING_USER
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS if MMU

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations
  2026-12-03 13:53 ` [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
@ 2026-12-03 14:18   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:18 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv: shadowed local variables in __PERCPU_8_16_OP_CASE() macro
--

commit b17c7b8c85ff68f07d099aefa48ef38f45469f69
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: implement preemptible this_cpu void RMW operations

This commit instantiates the 8-, 16-, 32-, and 64-bit preemptible per-CPU
RMW helpers for addition and bitwise AND/OR, and wires up the void
this_cpu_add/and/or_*() macros to use them.

Link: https://lore.kernel.org/r/20260826010748.zphu3wh2hlm4t4o2@offworld

> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index cd88618889494..e39210ddd5026 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h
> @@ -221,6 +221,23 @@ PERCPU_RW_OPS("ld", "sd", 64)
>  PERCPU_RW_OPS("lw", "sw", 32)
>  #endif
>  
> +__PERCPU_AMO_OP_CASE(add, 32, .w, +)
> +__PERCPU_AMO_OP_CASE(and, 32, .w, &)
> +__PERCPU_AMO_OP_CASE(or, 32, .w, |)
> +
> +#ifdef CONFIG_64BIT
> +__PERCPU_AMO_OP_CASE(add, 64, .d, +)
> +__PERCPU_AMO_OP_CASE(and, 64, .d, &)
> +__PERCPU_AMO_OP_CASE(or, 64, .d, |)
> +#endif
> +
> +__PERCPU_8_16_OP_CASE(add, 8, .b, +)
> +__PERCPU_8_16_OP_CASE(add, 16, .h, +)
> +__PERCPU_8_16_OP_CASE(and, 8, .b, &)
> +__PERCPU_8_16_OP_CASE(and, 16, .h, &)
> +__PERCPU_8_16_OP_CASE(or, 8, .b, |)
> +__PERCPU_8_16_OP_CASE(or, 16, .h, |)

[Severity: Low]
Do these __PERCPU_8_16_OP_CASE() instantiations trigger -Wshadow warnings
on __ti, __off, and __addr under W=2 builds?

__PERCPU_8_16_OP_CASE() expands __PCPU_OP_CONTEXT() at function scope and
then calls __PERCPU_AMO() in the Zabha branch, which expands
__PCPU_OP_CONTEXT() again in a nested block.

Because asm/percpu.h is included widely, these six instantiations emit 18
-Wshadow warnings per translation unit regardless of
CONFIG_RISCV_ISA_ZABHA.  Moving __PCPU_OP_CONTEXT() in
__PERCPU_8_16_OP_CASE() after the Zabha early-return block avoids this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=5

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 03/10] riscv: add common primitives for preemptible this_cpu operations
  2026-12-03 13:53 ` [PATCH v11 03/10] riscv: add common primitives " Yunhui Cui
@ 2026-12-03 14:19   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:19 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv: shadowed __ti, __off, and __addr in
  __PERCPU_8_16_OP_CASE()
--

commit cdee8dcf95e6f5bcb812df1069a67587cc2904b5
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: add common primitives for preemptible this_cpu operations

This commit adds shared macro primitives in asm/percpu.h for preemptible
per-CPU AMO and 8/16-bit LR/SC read-modify-write operations.

Link: https://lore.kernel.org/r/20261112120928.24145d44@pumpkin

> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index 3cd60ef18815e..a5ceedc934b54 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h

[ ... ]

> @@ -55,6 +58,134 @@
>  #define __PCPU_GPRS_END(gprs)						\
>  	"sh zero, " gprs "\n"
>  
> +#define __PCPU_OP_CONTEXT()						\
> +	struct thread_info *__ti = (struct thread_info *)current;	\
> +	unsigned long __off, __addr

[ ... ]

> +#define __PERCPU_AMO(insn, isa, ret_arg, pcp_arg, val_arg)		\
> +do {									\
> +	__PCPU_OP_CONTEXT();						\

[ ... ]

> +#define __PERCPU_8_16_OP_CASE(name, sz, sfx, op)			\
> +static __always_inline unsigned long					\
> +__percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val)	\
> +{									\
> +	__PCPU_OP_CONTEXT();						\
> +	const u32 field_mask = PERCPU_8_16_GET_MASK(sz);		\
> +	const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp);		\
> +	const u32 mask = field_mask << shift;				\
> +	const u32 val_trunc = val & field_mask;				\
> +	void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp);		\
> +	u32 old, new, field;						\
> +									\
> +	if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&			\
> +	    riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) {	\
> +		u##sz tmp;						\
> +									\
> +		__PERCPU_AMO("amo" #name #sfx, "zabha",			\
> +			     tmp, pcp, (u##sz)val);			\

[Severity: Low]
Does calling __PERCPU_AMO() inside __PERCPU_8_16_OP_CASE() shadow __ti,
__off, and __addr?

Both __PERCPU_8_16_OP_CASE() and __PERCPU_AMO() expand
__PCPU_OP_CONTEXT(), redeclaring those locals in the Zabha branch.  Once
instantiated, this triggers -Wshadow on W=2 builds (failing with
CONFIG_WERROR=y) in every file including asm/percpu.h.

Moving __PCPU_OP_CONTEXT() and the LR/SC-only locals after the Zabha
early-return branch would avoid the shadowing.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=3

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 01/10] riscv: store percpu offset into thread_info
  2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
@ 2026-12-03 14:20   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:20 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv, bpf: emit_priv_stack_frame() still uses
  __per_cpu_offset[]
--

commit 1b5c188eca72654acc3662013da85bb0d5d13b4f
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: store percpu offset into thread_info

This commit caches the current CPU's per-CPU offset in thread_info,
initializes it on boot and secondary CPUs, and propagates it across
context switches so per-CPU accesses and the RV64 BPF JIT can load it
directly.

Link: https://lists.riscv.org/g/tech-privileged/topic/risc_v_tech_arch_review/113437553?page=2
Link: https://lore.kernel.org/r/20260826012457.ig3rfmji7vywf6kc@offworld

> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> index a7a7d2f9da85a..23380d12f6163 100644
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
> @@ -1466,15 +1466,8 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
>  			if (rd != 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

[Severity: Low]
Should emit_priv_stack_frame() in the same file also be updated to load
thread_info.pcpu_offset directly?

arch/riscv/net/bpf_jit_comp64.c:emit_priv_stack_frame() {
    ...
#ifdef CONFIG_SMP
	emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu), RV_REG_TP, ctx);
	emit_imm(RV_REG_T2, (u64)__per_cpu_offset, ctx);
	emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
	emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
	emit_add(RV_REG_S5, RV_REG_S5, RV_REG_T1, ctx);
#endif
}

As written, every RV64 BPF program or subprogram using a private stack
still emits the multi-instruction __per_cpu_offset[] lookup in its
prologue instead of a single load from thread_info.pcpu_offset.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=1

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations
  2026-12-03 13:53 ` [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
@ 2026-12-03 14:23   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:23 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv: missing __force casts in percpu read/write cause sparse
  warnings
--

commit 2b76c184196d497191f80e585d202ed8e0dcab37
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: implement preemptible this_cpu read and write operations

This commit implements preemptible RISC-V this_cpu_read_{1,2,4,8} and
this_cpu_write_{1,2,4,8} operations using the recoverable per-CPU
address handling instead of the generic fallbacks.

> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index a5ceedc934b54..cd88618889494 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h
> @@ -176,7 +176,73 @@ __percpu_##name##_amo_case_##sz(void __percpu *pcp, unsigned long val)	\
>  	(TYPEOF_UNQUAL(*__pcp_ptr))op(__pcp_ptr, ##args);		\
>  })
>  
[ ... ]
> +#define this_cpu_read_1(pcp)	_pcp_wrap_return(__percpu_read_8, pcp)
> +#define this_cpu_read_2(pcp)	_pcp_wrap_return(__percpu_read_16, pcp)
> +#define this_cpu_read_4(pcp)	_pcp_wrap_return(__percpu_read_32, pcp)
> +
> +#ifdef CONFIG_64BIT
> +#define this_cpu_read_8(pcp)	_pcp_wrap_return(__percpu_read_64, pcp)
> +#endif
> +
> +#define this_cpu_write_1(pcp, val)					\
> +	_pcp_wrap(__percpu_write_8, pcp, (unsigned long)(val))
> +#define this_cpu_write_2(pcp, val)					\
> +	_pcp_wrap(__percpu_write_16, pcp, (unsigned long)(val))
> +#define this_cpu_write_4(pcp, val)					\
> +	_pcp_wrap(__percpu_write_32, pcp, (unsigned long)(val))
> +
> +#ifdef CONFIG_64BIT
> +#define this_cpu_write_8(pcp, val)					\
> +	_pcp_wrap(__percpu_write_64, pcp, (unsigned long)(val))
> +#endif

[Severity: Low]
Do the casts in _pcp_wrap_return() and this_cpu_write_{1,2,4,8}() need
__force to avoid sparse warnings?

_pcp_wrap_return() casts the unsigned long from __percpu_read_*() to
TYPEOF_UNQUAL(*__pcp_ptr) without __force.  Under sparse, this warns on
address-space or __bitwise per-CPU variables, such as this_cpu_read()
on void __iomem * sswi_cpu_regs in aclint_sswi_ipi_clear().

Similarly, this_cpu_write_{1,2,4,8}() cast val to unsigned long without
__force, stripping address-space or __bitwise qualifiers under sparse.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=4

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations
  2026-12-03 13:53 ` [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
@ 2026-12-03 14:33   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:33 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] riscv: noinstr violation in riscv_pcpu_regs_fixup() and
  entry-common.o
- [Medium] riscv: thread_info.pcpu_gprs padding pushes scs_sp across
  cacheline
- [Medium] riscv: unconditional pcpu_gprs migration fixup overhead on
  CONFIG_SMP=n
- [Low] riscv: asymmetric gprs operand syntax in __PCPU_GPRS_BEGIN/END
  macros
- [Low] riscv: redundant pcpu_gprs_exit store in ret_from_exception when
  zero
- [Low] riscv: handle_kernel_stack_overflow() leaves stale
  thread_info.pcpu_gprs
--

commit 072f5f618ab3d6a5823cb33debcecd13e832daaa
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: add infrastructure for preemptible this_cpu operations

This commit adds per-CPU register tracking in thread_info and pt_regs,
inline assembly helpers to delimit preemptible per-CPU regions, and an
exception-return fixup routine to recompute per-CPU offset and address
registers after CPU migration.

Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker

[ ... ]

> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index faf9a75ec66d7..3cd60ef18815e 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h
> @@ -3,12 +3,58 @@

[ ... ]

> +/*
> + * Mark a region where the per-CPU offset and address registers may need to
> + * be rewritten by exception return after migration.  The three registers
> + * must remain distinct for the whole region.
> + */
> +#define __PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset)		\
> +	__DEFINE_ASM_GPR_NUMS						\
> +	__PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr)			\
> +	"li %[" #off "], " __PCPU_GPRS_VALUE "\n"\
> +	"sh %[" #off "], %[" #gprs "]\n"		\
> +	REG_L " %[" #off "], %[" #pcpu_offset "]\n"\
> +	"add %[" #addr "], %[" #pcp "], %[" #off "]\n"
> +
> +#define __PCPU_GPRS_END(gprs)						\
> +	"sh zero, " gprs "\n"

[Severity: Low]
Should __PCPU_GPRS_END(gprs) format its operand the same way as
__PCPU_GPRS_BEGIN()?

__PCPU_GPRS_BEGIN() takes an unquoted operand name and wraps it in %[#gprs],
while __PCPU_GPRS_END() concatenates gprs as a string literal, forcing
callers to pass gprs to one and "%[gprs]" to the other.  __PCPU_GPR_NUM()
also uses __stringify() while the asm lines use raw #.

Using "%[" __stringify(gprs) "]" in __PCPU_GPRS_END() and __stringify()
throughout __PCPU_GPRS_BEGIN() keeps both macros symmetric.  It would also
help to note in the comment that pcp must stay unmodified and off/addr
cannot hold live outputs before __PCPU_GPRS_END().

> diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> index addc8188152f7..355818c709b36 100644
> --- a/arch/riscv/include/asm/ptrace.h
> +++ b/arch/riscv/include/asm/ptrace.h
> @@ -53,6 +53,8 @@ struct pt_regs {
>  	unsigned long cause;
>  	/* a0 value before the syscall */
>  	unsigned long orig_a0;
> +	/* Active preemptible per-CPU operation, if any. */
> +	unsigned long pcpu_gprs;
>  };

[Severity: Medium]
Should this migration-fixup infrastructure be gated on CONFIG_SMP?

On CONFIG_SMP=n kernels tasks cannot migrate and per-CPU offsets are
always zero, yet pcpu_gprs is unconditionally added to struct pt_regs and
struct thread_info, entry-common.o is built unconditionally, and
handle_exception()/ret_from_exception() save and restore it on every trap.

Adding pcpu_gprs pushes struct pt_regs past a 16-byte boundary, growing
PT_SIZE_ON_STACK by 16 bytes on every trap frame (to 304 bytes on RV64 and
160 on RV32).  __PCPU_GPRS_BEGIN()/__PCPU_GPRS_END() also add extra loads,
stores, and fixup calls on every this_cpu operation to recompute pcp + 0.

[ ... ]

> diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
> index f10ba62b61016..51c4076763c1b 100644
> --- a/arch/riscv/include/asm/thread_info.h
> +++ b/arch/riscv/include/asm/thread_info.h
> @@ -63,6 +63,7 @@ struct thread_info {
>  	long			user_sp;	/* User stack pointer */
>  	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
>  	unsigned long		pcpu_offset;
> +	unsigned short		pcpu_gprs;
>  #ifdef CONFIG_SHADOW_CALL_STACK
>  	void			*scs_base;
>  	void			*scs_sp;

[Severity: Medium]
Does placing 2-byte pcpu_gprs before scs_base push scs_sp into a second
cache line on RV64?

struct task_struct (with thread_info at offset 0) is aligned to
L1_CACHE_BYTES (64 bytes).  Before this patch, flags through scs_sp fit
within the first 64 bytes; inserting unsigned short pcpu_gprs at offset 48
adds 6 bytes of alignment padding and shifts hot scs_sp to offset 64.

Because handle_exception(), ret_from_exception(), and __switch_to() access
TASK_TI_SCS_SP(tp), every syscall, IRQ, and context switch under
CONFIG_SHADOW_CALL_STACK will touch two cache lines instead of one.

Putting scs_sp before cold scs_base and placing pcpu_gprs after scs_sp
keeps all hot entry fields in the first cache line.

[ ... ]

> diff --git a/arch/riscv/kernel/entry-common.c b/arch/riscv/kernel/entry-common.c
> new file mode 100644
> index 0000000000000..164505962d131
> --- /dev/null
> +++ b/arch/riscv/kernel/entry-common.c
> @@ -0,0 +1,30 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <linux/bitfield.h>
> +#include <linux/compiler.h>
> +#include <linux/percpu.h>
> +
> +#include <asm/current.h>
> +#include <asm/ptrace.h>
> +
> +/*
> + * If the interrupted context had an active per-CPU operation, rewrite its
> + * saved offset and address registers for the CPU on which it will resume.
> + */
> +void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs)
> +{
> +	unsigned long pcpu_offset = __my_cpu_offset;
> +	unsigned long gprs = regs->pcpu_gprs;
> +	unsigned long pcp;
> +	unsigned int reg_pcp;
> +	unsigned int reg_off;
> +	unsigned int reg_addr;
> +
> +	reg_pcp = FIELD_GET(PCPU_GPR_PCP, gprs);
> +	reg_off = FIELD_GET(PCPU_GPR_OFF, gprs);
> +	reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
> +
> +	pcp = regs_get_gpr(regs, reg_pcp * sizeof(unsigned long));
> +	regs_set_gpr(regs, reg_off * sizeof(unsigned long), pcpu_offset);
> +	regs_set_gpr(regs, reg_addr * sizeof(unsigned long), pcp + pcpu_offset);
> +}

[Severity: Medium]
Can riscv_pcpu_regs_fixup() call non-noinstr instrumentation from
ret_from_exception?

ret_from_exception invokes this helper after irqentry_exit(), outside RCU
watching.  regs_get_gpr() and regs_set_gpr() use unlikely() instead of
unlikely_notrace(), so CONFIG_TRACE_BRANCH_PROFILING emits calls to
ftrace_likely_update(), which can tamper with SR_SUM or enter the tracer.

arch/riscv/kernel/Makefile also builds entry-common.o without
-DDISABLE_BRANCH_PROFILING or KCOV, UBSAN, and KASAN opt-outs, leaving
riscv_pcpu_regs_fixup() open to UBSAN and KCOV (on GCC < 12) calls.

Using unlikely_notrace() in regs_get_gpr()/regs_set_gpr() and disabling
branch profiling, KCOV, UBSAN, and KASAN for entry-common.o avoids this.

> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 628b85e3915a7..65298ac828959 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -198,6 +198,8 @@ SYM_CODE_START(handle_exception)
>  	REG_S s4, PT_CAUSE(sp)
>  	REG_S s5, PT_TP(sp)
>  
> +	pcpu_gprs_entry tp sp s0
> +
>  	/*
>  	 * Set the scratch register to 0, so that if a recursive exception
>  	 * occurs, the exception vector knows it came from the kernel

[Severity: Low]
Does handle_kernel_stack_overflow() leave a stale thread_info.pcpu_gprs
value active?

Under CONFIG_VMAP_STACK, handle_exception branches to
handle_kernel_stack_overflow before reaching pcpu_gprs_entry, and
handle_kernel_stack_overflow tail-calls handle_bad_stack without calling
pcpu_gprs_entry or clearing TASK_TI_PCPU_GPRS(tp).

If a stack overflow hits inside a __PCPU_GPRS_BEGIN/__PCPU_GPRS_END region
and a nested trap occurs during handle_bad_stack() or panic(),
ret_from_exception will run riscv_pcpu_regs_fixup() on the stale descriptor
and clobber two unrelated GPRs in the panic path.

> @@ -281,6 +283,14 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
>  	move a0, sp
>  	call riscv_v_context_nesting_end
>  #endif
> +	REG_L t0, PT_PCPU_GPRS(sp)
> +	beqz t0, .Lpcpu_regs_restored
> +	move a0, sp
> +	call riscv_pcpu_regs_fixup
> +	REG_L t0, PT_PCPU_GPRS(sp)
> +.Lpcpu_regs_restored:
> +	pcpu_gprs_exit tp t0
> +
>  	REG_L a0, PT_STATUS(sp)
>  	restore_userssp s3, a0
>  	/*

[Severity: Low]
Is the pcpu_gprs_exit store redundant when PT_PCPU_GPRS(sp) is zero?

Every path into ret_from_exception already has TASK_TI_PCPU_GPRS(tp)
cleared, so jumping above pcpu_gprs_exit executes an unnecessary sh zero
store on every syscall and exception return.  Moving pcpu_gprs_exit before
.Lpcpu_regs_restored avoids that store on the fast path.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=2

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128
  2026-12-03 13:53 ` [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
@ 2026-12-03 14:39   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-12-03 14:39 UTC (permalink / raw)
  To: Yunhui Cui, pjw, palmer, aou, alex, dennis, tj, cl, oleg, ast,
	daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bjorn, pulehui, puranjay, broonie,
	dave, david.laight.linux, jszhang, tglx, conor.dooley, thuth,
	ajones, ben.dooks, rkrcmar, samuel.holland, zong.li, debug,
	seanwascoding, andybnac, menglong8.dong, cyrilbur, wangruikang,
	atishp, apatel, arnd, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, qingfang.deng, linux-mm, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] riscv: __percpu_generic_cmpxchg128() omits RELOC_HIDE(),
  duplicates generic
--

commit b38800d13faff5874a8e355c1c5f3c4b3deddc6f
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv: implement preemptible this_cpu cmpxchg128

This commit adds preemptible 128-bit this_cpu_cmpxchg128() support on
RISC-V using amocas.q when Zacas is available, with a local-IRQ-disabled
fallback otherwise.

> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index 5e7365044eae5..a9d8620417e1b 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h

[ ... ]

> +static __always_inline u128
> +__percpu_generic_cmpxchg128(void __percpu *pcp, u128 old, u128 new)
> +{
> +	unsigned long flags;
> +	u128 *ptr;
> +	u128 ret;
> +
> +	flags = arch_local_irq_save();
> +	ptr = (u128 *)((__force unsigned long)pcp +
> +			 __my_cpu_offset);
> +	ret = *ptr;
> +	if (ret == old)
> +		*ptr = new;
> +	arch_local_irq_restore(flags);
> +
> +	return ret;
> +}

[Severity: Medium]
Can computing ptr directly from pcp + __my_cpu_offset without RELOC_HIDE()
be miscompiled on CPUs without Zacas?

When this_cpu_cmpxchg128() is called on a static per-CPU variable in
.data..percpu, GCC assumes arithmetic derived from that symbol stays
within the object's bounds and does not wrap, risking miscompilation and
per-CPU data corruption.  SHIFT_PERCPU_PTR() uses RELOC_HIDE() for this.

This helper also duplicates this_cpu_generic_cmpxchg() from
include/asm-generic/percpu.h, skips CONFIG_DEBUG_IRQFLAGS checks by using
arch_local_irq_save()/restore() instead of raw_local_irq_save()/restore(),
and loads __my_cpu_offset unconditionally even when !CONFIG_SMP.

Branching on system_has_cmpxchg128() in this_cpu_cmpxchg128() and falling
back to this_cpu_generic_cmpxchg() would avoid the pointer provenance
issue and drop the duplicate helper.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=9

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-12-03 14:39 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
2026-12-03 14:20   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
2026-12-03 14:33   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 03/10] riscv: add common primitives " Yunhui Cui
2026-12-03 14:19   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
2026-12-03 14:23   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
2026-12-03 14:18   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
2026-12-03 14:39   ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox