Introduce Per-CPU Work helpers (was QPW)
Subsystems (8)
v4 cover Introduce Per-CPU Work helpers (was QPW)
The problem:
Some places in the kernel implement a parallel programming strategy consisting on local_locks() for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
On the other hand, for RT workloads this can represent a problem: getting an important workload scheduled out to deal with remote requests is sure to introduce unexpected deadline misses.
The idea:
Currently with PREEMPT_RT=y, local_locks() become per-cpu spinlocks. In this case, instead of scheduling work on a remote cpu, it should be safe to grab that remote cpu's per-cpu spinlock and run the required work locally. That major cost, which is un/locking in every local function, already happens in PREEMPT_RT.
Also, there is no need to worry about extra cache bouncing:
The cacheline invalidation already happens due to schedule_work_on().
This will avoid schedule_work_on(), and thus avoid scheduling-out an RT workload.
Proposed solution:
A new interface called PerCPU Work (PW), which should replace Work Queue in the above mentioned use case.
If CONFIG_PWLOCKS=n this interfaces just wraps the current local_locks + WorkQueue behavior, so no expected change in runtime.
If CONFIG_PWLOCKS=y, and kernel boot option pwlocks=1, pw_queue_on(cpu,...) will lock that cpu's per-cpu structure and perform work on it locally.
v3->v4:
- Mechanism name changed from QPW to PW/PWLOCKS. Helper funcions / API, file names and config options renamed accordingly.
- All members of the Per-CPU Work API now start with the same prefix (Frederic Weisbecker)
- Improved style a bit, reviewed documentation
v2->v3:
- Use preempt_disable/preempt_enable on !CONFIG_PREEMPT_RT (Vlastimil Babka).
- Improve documentation to include local_qpw_lock on operations table (Leonardo Bras).
- Enable qpw=1 automatically if CPU isolation is enabled (Vlastimil Babka).
v1->v2:
- Introduce local_qpw_lock and unlock functions, move preempt_disable/ preempt_enable to it (Leonardo Bras). This reduces performance overhead of the patch.
- Documentation and changelog typo fixes (Leonardo Bras).
- Fix places where preempt_disable/preempt_enable was not being correctly performed.
- Add performance measurements.
RFC->v1:
- Introduce CONFIG_QPW and qpw= kernel boot option to enable remote spinlocking and execution even on !CONFIG_PREEMPT_RT kernels (Leonardo Bras).
- Move buffer_head draining to separate workqueue (Marcelo Tosatti).
- Convert mlock per-CPU page lists to QPW (Marcelo Tosatti).
- Drop memcontrol convertion (as isolated CPUs are not targets of queue_work_on anymore).
- Rebase SLUB against Vlastimil's slab/next.
- Add basic document for QPW (Waiman Long).
The performance numbers, as measured by the following test program, are as follows (v3, mechanics not changed since then):
CONFIG_PREEMPT_DYNAMIC=y
Unpatched kernel: 60 cycles Patched kernel, CONFIG_QPW=n: 62 cycles Patched kernel, CONFIG_QPW=y, qpw=0: 62 cycles Patched kernel, CONFIG_QPW=y, qpw=1: 75 cycles
CONFIG_PREEMPT_RT:
Unpatched kernel: 95 cycles Patched kernel, CONFIG_QPW=y, qpw=0: 99 cycles Patched kernel, CONFIG_QPW=y, qpw=1: 97 cycles
kmalloc_bench.c:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/timex.h>
#include <linux/preempt.h>
#include <linux/irqflags.h>
#include <linux/vmalloc.h>
MODULE_LICENSE("GPL");MODULE_AUTHOR("Gemini AI");MODULE_DESCRIPTION("A simple kmalloc performance benchmark");
static int size = 64; // Default allocation size in bytes module_param(size, int, 0644);
static int iterations = 9000000; // Default number of iterations module_param(iterations, int, 0644);
static int __init kmalloc_bench_init(void) {
void **ptrs;
cycles_t start, end;
uint64_t total_cycles;
int i;
pr_info("kmalloc_bench: Starting test (size=%d, iterations=%d)\n", size, iterations);
// Allocate an array to store pointers to avoid immediate kfree-reuse optimization
ptrs = vmalloc(sizeof(void *) * iterations);
if (!ptrs) {
pr_err("kmalloc_bench: Failed to allocate pointer array\n");
return -ENOMEM;
}
preempt_disable();
start = get_cycles();
for (i = 0; i < iterations; i++) {
ptrs[i] = kmalloc(size, GFP_ATOMIC);
}
end = get_cycles();
total_cycles = end - start;
preempt_enable();
pr_info("kmalloc_bench: Total cycles for %d allocs: %llu\n", iterations, total_cycles);
pr_info("kmalloc_bench: Avg cycles per kmalloc: %llu\n", total_cycles / iterations);
// Cleanup
for (i = 0; i < iterations; i++) {
kfree(ptrs[i]);
}
vfree(ptrs);
return 0;
}static void __exit kmalloc_bench_exit(void) {
pr_info("kmalloc_bench: Module unloaded\n");
}module_init(kmalloc_bench_init);module_exit(kmalloc_bench_exit);
The following testcase triggers lru_add_drain_all on an isolated CPU (that does sys_write to a file before entering its realtime loop).
/*
* Simulates a low latency loop program that is interrupted
* due to lru_add_drain_all. To trigger lru_add_drain_all, run:
*
* blockdev --flushbufs /dev/sdX
*
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
int cpu;
static void *run(void *arg)
{
pthread_t current_thread;
cpu_set_t cpuset;
int ret, nrloops;
struct sched_param sched_p;
pid_t pid;
int fd;
char buf[] = "xxxxxxxxxxx";
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
current_thread = pthread_self();
ret = pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
if (ret) {
perror("pthread_setaffinity_np failed\n");
exit(0);
}
memset(&sched_p, 0, sizeof(struct sched_param));
sched_p.sched_priority = 1;
pid = gettid();
ret = sched_setscheduler(pid, SCHED_FIFO, &sched_p);
if (ret) {
perror("sched_setscheduler");
exit(0);
}
fd = open("/tmp/tmpfile", O_RDWR|O_CREAT|O_TRUNC);
if (fd == -1) {
perror("open");
exit(0);
}
ret = write(fd, buf, sizeof(buf));
if (ret == -1) {
perror("write");
exit(0);
}
do {
nrloops = nrloops+2;
nrloops--;
} while (1);
}int main(int argc, char *argv[])
{
int fd, ret;
pthread_t thread;
long val;
char *endptr, *str;
struct sched_param sched_p;
pid_t pid;
if (argc != 2) {
printf("usage: %s cpu-nr\n", argv[0]);
printf("where CPU number is the CPU to pin thread to\n");
exit(0);
}
str = argv[1];
cpu = strtol(str, &endptr, 10);
if (cpu < 0) {
printf("strtol returns %d\n", cpu);
exit(0);
}
printf("cpunr=%d\n", cpu);
memset(&sched_p, 0, sizeof(struct sched_param));
sched_p.sched_priority = 1;
pid = getpid();
ret = sched_setscheduler(pid, SCHED_FIFO, &sched_p);
if (ret) {
perror("sched_setscheduler");
exit(0);
}
pthread_create(&thread, NULL, run, NULL);
sleep(5000);
pthread_join(thread, NULL);
}Leonardo Bras (3):
Introducing pw_lock() and per-cpu queue & flush work
swap: apply new pw_queue_on() interface
slub: apply new pw_queue_on() interface
Marcelo Tosatti (1):
mm/swap: move bh draining into a separate workqueue
MAINTAINERS | 7 + .../admin-guide/kernel-parameters.txt | 10 + Documentation/locking/pwlocks.rst | 76 +++++ init/Kconfig | 35 +++ kernel/Makefile | 2 + include/linux/pwlocks.h | 265 ++++++++++++++++++ mm/internal.h | 4 +- kernel/pwlocks.c | 47 ++++ mm/mlock.c | 51 +++- mm/page_alloc.c | 2 +- mm/slub.c | 142 +++++----- mm/swap.c | 109 ++++--- 12 files changed, 624 insertions(+), 126 deletions(-) create mode 100644 Documentation/locking/pwlocks.rst create mode 100644 include/linux/pwlocks.h create mode 100644 kernel/pwlocks.c
base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8
Signature · 1 line
2.54.0
PATCH v4 1/4 Introducing pw_lock() and per-cpu queue & flush work
Some places in the kernel implement a parallel programming strategy consisting on local_locks() for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
On the other hand, for RT workloads this can represent a problem:
scheduling work on remote cpu that are executing low latency tasks is undesired and can introduce unexpected deadline misses.
It's interesting, though, that local_lock()s in RT kernels become spinlock(). We can make use of those to avoid scheduling work on a remote cpu by directly updating another cpu's per_cpu structure, while holding it's spinlock().
In order to do that, it's necessary to introduce a new set of functions to make it possible to get another cpu's per-cpu "local" lock (pw_{un,}lock*) and also do the corresponding queueing (pw_queue_on()) and flushing (pw_flush()) helpers to run the remote work.
Users of non-RT kernels but with low latency requirements can select similar functionality by using the CONFIG_PWLOCKS compile time option.
On CONFIG_PWLOCKS disabled kernels, no changes are expected, as every one of the introduced helpers work the exactly same as the current implementation:
pw_{un,}lock*() -> local_{un,}lock*() (ignores cpu parameter)pw_queue_on() -> queue_work_on()pw_flush() -> flush_work()
For PWLOCKS enabled kernels, though, pw_{un,}lock*() will use the extra cpu parameter to select the correct per-cpu structure to work on, and acquire the spinlock for that cpu.
pw_queue_on() will just call the requested function in the current cpu, which will operate in another cpu's per-cpu object. Since the local_locks() become spinlock()s in PWLOCKS enabled kernels, we are safe doing that.
pw_flush() then becomes a no-op since no work is actually scheduled on a remote cpu.
Some minimal code rework is needed in order to make this mechanism work:
The calls for local_{un,}lock*() on the functions that are currently scheduled on remote cpus need to be replaced by either pw_{un,}lock_*(), PWLOCKS enabled kernels they can reference a different cpu. It's also necessary to use a pw_struct instead of a work_struct, but it just contains a work struct and, in CONFIG_PWLOCKS, the target cpu.
This should have almost no impact on non-CONFIG_PWLOCKS kernels: few this_cpu_ptr() will become per_cpu_ptr(,smp_processor_id()) on non-hotpath functions.
On CONFIG_PWLOCKS kernels, this should avoid deadlines misses by removing scheduling noise.
--- MAINTAINERS | 7 + .../admin-guide/kernel-parameters.txt | 10 + Documentation/locking/pwlocks.rst | 76 +++++ init/Kconfig | 35 +++ kernel/Makefile | 2 + include/linux/pwlocks.h | 265 ++++++++++++++++++ kernel/pwlocks.c | 47 ++++ 7 files changed, 442 insertions(+) create mode 100644 Documentation/locking/pwlocks.rst create mode 100644 include/linux/pwlocks.h create mode 100644 kernel/pwlocks.c
MAINTAINERS +7 -0
@@ -21775,20 +21775,27 @@ QORIQ DPAA2 FSL-MC BUS DRIVER M: Ioana Ciornei <[email protected]> L: [email protected] L: [email protected] S: Maintained F: Documentation/ABI/stable/sysfs-bus-fsl-mc F: Documentation/devicetree/bindings/misc/fsl,qoriq-mc.yaml F: Documentation/networking/device_drivers/ethernet/freescale/dpaa2/overview.rst F: drivers/bus/fsl-mc/ F: include/uapi/linux/fsl_mc.h +PW Locks +M: Leonardo Bras <[email protected]> +S: Supported +F: Documentation/locking/pwlocks.rst +F: include/linux/pwlocks.h +F: kernel/pwlocks.c + QT1010 MEDIA DRIVER L: [email protected] S: Orphan W: https://linuxtv.org Q: http://patchwork.linuxtv.org/project/linux-media/list/ F: drivers/media/tuners/qt1010* QUALCOMM ATH12K WIRELESS DRIVER M: Jeff Johnson <[email protected]> L: [email protected]
Documentation/admin-guide/kernel-parameters.txt +10 -0
@@ -2810,20 +2810,30 @@ Kernel parameters If a queue's affinity mask contains only isolated CPUs then this parameter has no effect on the interrupt routing decision, though interrupts are only delivered when tasks running on those isolated CPUs submit IO. IO submitted on housekeeping CPUs has no influence on those queues. The format of <cpu-list> is described above. + pwlocks= [KNL,SMP] Select a behavior on per-CPU resource sharing + and remote interference mechanism on a kernel built with + CONFIG_PWLOCKS. + Format: { "0" | "1" } + 0 - local_lock() + queue_work_on(remote_cpu) + 1 - spin_lock() for both local and remote operations + + Selecting 1 may be interesting for systems that want + to avoid interruption & context switches from IPIs. + iucv= [HW,NET] ivrs_ioapic [HW,X86-64] Provide an override to the IOAPIC-ID<->DEVICE-ID mapping provided in the IVRS ACPI table. By default, PCI segment is 0, and can be omitted. For example, to map IOAPIC-ID decimal 10 to PCI segment 0x1 and PCI device 00:14.0, write the parameter as:
Documentation/locking/pwlocks.rst new file +76 -0
76 lines not on this page · raw message
init/Kconfig +35 -0
@@ -764,20 +764,55 @@ config CPU_ISOLATION depends on SMP default y help Make sure that CPUs running critical tasks are not disturbed by any source of "noise" such as unbound workqueues, timers, kthreads... Unbound jobs get offloaded to housekeeping CPUs. This is driven by the "isolcpus=" boot parameter. Say Y if unsure. +config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST + default n + help + Allow changing the behavior on per-CPU resource sharing with cache, + from the regular local_locks() + queue_work_on(remote_cpu) to using + per-CPU spinlocks on both local and remote operations. + + This is useful to give user the option on reducing IPIs to CPUs, and + thus reduce interruptions and context switches. On the other hand, it + increases generated code and will use atomic operations if spinlocks + are selected. + + If set, will use the default behavior set in PWLOCKS_DEFAULT unless boot + parameter pwlocks is passed with a different behavior. + + If unset, will use the local_lock() + queue_work_on() strategy, + regardless of the boot parameter or PWLOCKS_DEFAULT. + + Say N if unsure. + +config PWLOCKS_DEFAULT + bool "Use per-CPU spinlocks by default on PWLOCKS" + depends on PWLOCKS + default n + help + If set, will use per-CPU spinlocks as default behavior for per-CPU + remote operations. + + If unset, will use local_lock() + queue_work_on(cpu) as default + behavior for remote operations. + + Say N if unsure + source "kernel/rcu/Kconfig" config IKCONFIG tristate "Kernel .config support" help This option enables the complete Linux kernel ".config" file contents to be saved in the kernel. It provides documentation of which kernel options are used in a running kernel or in an on-disk kernel. This information can be extracted from the kernel image file with the script scripts/extract-ikconfig and used as
kernel/Makefile +2 -0
@@ -135,20 +135,22 @@ obj-$(CONFIG_JUMP_LABEL) += jump_label.o obj-$(CONFIG_CONTEXT_TRACKING) += context_tracking.o obj-$(CONFIG_TORTURE_TEST) += torture.o obj-$(CONFIG_HAS_IOMEM) += iomem.o obj-$(CONFIG_RSEQ) += rseq.o obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o +obj-$(CONFIG_PWLOCKS) += pwlocks.o + CFLAGS_kstack_erase.o += $(DISABLE_KSTACK_ERASE) CFLAGS_kstack_erase.o += $(call cc-option,-mgeneral-regs-only) obj-$(CONFIG_KSTACK_ERASE) += kstack_erase.o KASAN_SANITIZE_kstack_erase.o := n KCSAN_SANITIZE_kstack_erase.o := n KCOV_INSTRUMENT_kstack_erase.o := n obj-$(CONFIG_SCF_TORTURE_TEST) += scftorture.o $(obj)/configs.o: $(obj)/config_data.gz
include/linux/pwlocks.h new file +265 -0
265 lines not on this page · raw message
kernel/pwlocks.c new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 2/4 mm/swap: move bh draining into a separate workqueue
In reply to Leonardo BrasFrom: Marcelo Tosatti <[email protected]>
Separate the bh draining into a separate workqueue (from the mm lru draining), so that its possible to switch the mm lru draining to QPW.
To switch bh draining to QPW, it would be necessary to add a spinlock to addition of bhs to percpu cache, and that is a very hot path.
--- mm/swap.c | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-)
mm/swap.c +37 -15
@@ -744,60 +744,70 @@ void lru_add_drain(void)local_unlock (&cpu_fbatches.lock);mlock_drain_local (); }/* * It's called from per-cpu workqueue context in SMP case so * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on * the same cpu. It shouldn't be a problem in !SMP case since * the core is only one and the locks will disable preemption. */ -static void lru_add_and_bh_lrus_drain (void ) +static void lru_add_mm_drain (void ) {local_lock (&cpu_fbatches.lock);lru_add_drain_cpu (smp_processor_id ());local_unlock (&cpu_fbatches.lock); -invalidate_bh_lrus_cpu ();mlock_drain_local (); }void lru_add_drain_cpu_zone (struct zone *zone) {local_lock (&cpu_fbatches.lock);lru_add_drain_cpu (smp_processor_id ());drain_local_pages (zone);local_unlock (&cpu_fbatches.lock);mlock_drain_local (); }#ifdef CONFIG_SMP static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);static void lru_add_drain_per_cpu (struct work_struct *dummy) { -lru_add_and_bh_lrus_drain (); +lru_add_mm_drain (); } -static bool cpu_needs_drain (unsigned int cpu) +static DEFINE_PER_CPU(struct work_struct, bh_add_drain_work); + +static void bh_add_drain_per_cpu (struct work_struct *dummy) +{ +invalidate_bh_lrus_cpu (); +} + +static bool cpu_needs_mm_drain (unsigned int cpu) {struct cpu_fbatches *fbatches = &per_cpu (cpu_fbatches, cpu);/* Check these in order of likelihood that they're not zero */ return folio_batch_count (&fbatches->lru_add) ||folio_batch_count (&fbatches->lru_move_tail) ||folio_batch_count (&fbatches->lru_deactivate_file) ||folio_batch_count (&fbatches->lru_deactivate) ||folio_batch_count (&fbatches->lru_lazyfree) ||folio_batch_count (&fbatches->lru_activate) || -need_mlock_drain (cpu) || -has_bh_in_lru (cpu,NULL ); +need_mlock_drain (cpu); +} + +static bool cpu_needs_bh_drain (unsigned int cpu) +{ +return has_bh_in_lru (cpu,NULL ); }/* * Doesn't need any cpu hotplug locking because we do rely on per-cpu * kworkers being shut down before our page_alloc_cpu_dead callback is * executed on the offlined cpu. * Calling this function with cpu hotplug locks held can actually lead * to obscure indirect dependencies via WQ context. */ static inline void __lru_add_drain_all (bool force_all_cpus)
@@ -806,21 +816,21 @@ static inline void __lru_add_drain_all(bool force_all_cpus)* lru_drain_gen - Global pages generation number * * (A) Definition: global lru_drain_gen = x implies that all generations * 0 < n <= x are already *scheduled* for draining. * * This is an optimization for the highly-contended use case where a * user space workload keeps constantly generating a flow of pages for * each CPU. */ static unsigned int lru_drain_gen; -static struct cpumask has_work; +static struct cpumask has_mm_work, has_bh_work;static DEFINE_MUTEX (lock);unsigned cpu, this_gen;/* * Make sure nobody triggers this path before mm_percpu_wq is fully * initialized. */ if (WARN_ON (!mm_percpu_wq))return ;
@@ -869,34 +879,45 @@ static inline void __lru_add_drain_all(bool force_all_cpus)* along, adds some pages to its per-cpu vectors, then calls * lru_add_drain_all(). * * If the paired barrier is done at any later step, e.g. after the * loop, CPU #x will just exit at (C) and miss flushing out all of its * added pages. */ WRITE_ONCE (lru_drain_gen, lru_drain_gen +1 );smp_mb (); -cpumask_clear (&has_work); +cpumask_clear (&has_mm_work); +cpumask_clear (&has_bh_work);for_each_online_cpu (cpu) { -struct work_struct *work = &per_cpu (lru_add_drain_work, cpu); +struct work_struct *mm_work = &per_cpu (lru_add_drain_work, cpu); +struct work_struct *bh_work = &per_cpu (bh_add_drain_work, cpu); -if (cpu_needs_drain (cpu)) { -INIT_WORK (work, lru_add_drain_per_cpu); -queue_work_on (cpu, mm_percpu_wq, work); -__cpumask_set_cpu (cpu, &has_work); +if (cpu_needs_mm_drain (cpu)) { +INIT_WORK (mm_work, lru_add_drain_per_cpu); +queue_work_on (cpu, mm_percpu_wq, mm_work); +__cpumask_set_cpu (cpu, &has_mm_work); + } + +if (cpu_needs_bh_drain (cpu)) { +INIT_WORK (bh_work, bh_add_drain_per_cpu); +queue_work_on (cpu, mm_percpu_wq, bh_work); +__cpumask_set_cpu (cpu, &has_bh_work); } } -for_each_cpu (cpu, &has_work) +for_each_cpu (cpu, &has_mm_work)flush_work (&per_cpu (lru_add_drain_work, cpu)); +for_each_cpu (cpu, &has_bh_work) +flush_work (&per_cpu (bh_add_drain_work, cpu)); + done: mutex_unlock(&lock); }void lru_add_drain_all (void ) {__lru_add_drain_all (false); }#else void lru_add_drain_all (void )
@@ -928,21 +949,22 @@ void lru_cache_disable(void)* * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on * preempt_disable() regions of code. So any CPU which sees * lru_disable_count = 0 will have exited the critical * section when synchronize_rcu() returns. */ synchronize_rcu_expedited ();#ifdef CONFIG_SMP __lru_add_drain_all (true);#else -lru_add_and_bh_lrus_drain (); +lru_add_mm_drain (); +invalidate_bh_lrus_cpu ();#endif }/** * folios_put_refs - Reduce the reference count on a batch of folios. * @folios: The folios. * @refs: The number of refs to subtract from each folio. * * Like folio_put(), but for a batch of folios. This is more efficient * than writing the loop yourself as it will optimise the locks which need
Signature · 1 line
2.54.0
PATCH v4 3/4 swap: apply new pw_queue_on() interface
In reply to Leonardo BrasMake use of the new pw_{un,}lock*() and pw_queue_on() interface to improve performance & latency.
For functions that may be scheduled in a different cpu, replace local_{un,}lock*() by pw_{un,}lock*(), and replace schedule_work_on() by pw_queue_on(). The same happens for flush_work() and pw_flush().
The change requires allocation of pw_structs instead of a work_structs, and changing parameters of a few functions to include the cpu parameter.
This should bring no relevant performance impact on non-PWLOCKS kernels:
For functions that may be scheduled in a different cpu, the local_*lock's this_cpu_ptr() becomes a per_cpu_ptr(smp_processor_id()).
--- mm/internal.h | 4 ++- mm/mlock.c | 51 ++++++++++++++++++++++++++---------- mm/page_alloc.c | 2 +- mm/swap.c | 69 ++++++++++++++++++++++++++----------------------- 4 files changed, 79 insertions(+), 47 deletions(-)
mm/internal.h +3 -1
@@ -1209,24 +1209,26 @@ static inline void munlock_vma_folio(struct folio *folio,* cause folio not fully mapped to VMA. * * But it's not easy to confirm that's the situation. So we * always munlock the folio and page reclaim will correct it * if it's wrong. */ if (unlikely (vma->vm_flags &VM_LOCKED ))munlock_folio (folio); } +int __init mlock_init (void );void mlock_new_folio (struct folio *folio);bool need_mlock_drain (int cpu);void mlock_drain_local (void ); -void mlock_drain_remote (int cpu); +void mlock_drain_cpu (int cpu); +void mlock_drain_offline (int cpu);extern pmd_t maybe_pmd_mkwrite (pmd_t pmd,struct vm_area_struct *vma);/** * vma_address - Find the virtual address a page range is mapped at * @vma: The vma which maps this object. * @pgoff: The page offset within its object. * @nr_pages: The number of pages to consider. * * If any page in this range is mapped by this VMA, return the first address
mm/mlock.c +38 -13
190 lines not on this page · raw message
mm/page_alloc.c +1 -1
@@ -6217,21 +6217,21 @@ void free_reserved_page(struct page *page)__free_page (page);adjust_managed_page_count (page,1 ); }EXPORT_SYMBOL (free_reserved_page);static int page_alloc_cpu_dead (unsigned int cpu) {struct zone *zone;lru_add_drain_cpu (cpu); -mlock_drain_remote (cpu); +mlock_drain_offline (cpu);drain_pages (cpu);/* * Spill the event counters of the dead processor * into the current processors event counters. * This artificially elevates the count of the current * processor. */ vm_events_fold_cpu (cpu);
mm/swap.c +37 -32
326 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 4/4 slub: apply new pw_queue_on() interface
In reply to Leonardo BrasMake use of the new pw_{un,}lock*() and pw_queue_on() interface to improve performance & latency.
For functions that may be scheduled in a different cpu, replace local_{un,}lock*() by pw_{un,}lock*(), and replace schedule_work_on() by pw_queue_on(). The same happens for flush_work() and pw_flush().
This change requires allocation of pw_structs instead of a work_structs, and changing parameters of a few functions to include the cpu parameter.
This should bring no relevant performance impact on non-PWLOCKS kernels:
For functions that may be scheduled in a different cpu, the local_*lock's this_cpu_ptr() becomes a per_cpu_ptr(smp_processor_id()).
--- mm/slub.c | 142 +++++++++++++++++++++++++++--------------------------- 1 file changed, 72 insertions(+), 70 deletions(-)
mm/slub.c +72 -70
@@ -43,20 +43,21 @@#include <linux/prefetch.h> #include <linux/memcontrol.h> #include <linux/random.h> #include <linux/prandom.h> #include <kunit/test.h> #include <kunit/test-bug.h> #include <linux/sort.h> #include <linux/irq_work.h> #include <linux/kprobes.h> #include <linux/debugfs.h> +#include <linux/pwlocks.h> #include <trace/events/kmem.h> #include "internal.h" /* * Lock order: * 0. cpu_hotplug_lock * 1. slab_mutex (Global Mutex) * 2a. kmem_cache->cpu_sheaves->lock (Local trylock) * 2b. barn->lock (Spinlock)
@@ -122,21 +123,21 @@* (Note that the total number of slabs is an atomic value that may be * modified without taking the list lock). * * The list_lock is a centralized lock and thus we avoid taking it as * much as possible. As long as SLUB does not have to handle partial * slabs, operations can continue without any centralized lock. * * For debug caches, all allocations are forced to go through a list_lock * protected region to serialize against concurrent validation. * -* cpu_sheaves->lock (local_trylock) +* cpu_sheaves->lock (pw_trylock) * * This lock protects fastpath operations on the percpu sheaves. On !RT it * only disables preemption and does no atomic operations. As long as the main * or spare sheaf can handle the allocation or free, there is no other * overhead. * * barn->lock (spinlock) * * This lock protects the operations on per-NUMA-node barn. It can quickly * serve an empty or full sheaf if available, and avoid more expensive refill
@@ -150,21 +151,21 @@* cmpxchg_double this is done by a lockless update of slab's freelist and * counters, otherwise slab_lock is taken. This only needs to take the * list_lock if it's a first free to a full slab, or when a slab becomes empty * after the free. * * irq, preemption, migration considerations * * Interrupts are disabled as part of list_lock or barn lock operations, or * around the slab_lock operation, in order to make the slab allocator safe * to use in the context of an irq. -* Preemption is disabled as part of local_trylock operations. +* Preemption is disabled as part of pw_trylock operations. * kmalloc_nolock() and kfree_nolock() are safe in NMI context but see * their limitations. * * SLUB assigns two object arrays called sheaves for caching allocations and * frees on each cpu, with a NUMA node shared barn for balancing between cpus. * Allocations and frees are primarily served from these sheaves. * * Slabs with free elements are kept on a partial list and during regular * operations no list for full slabs is used. If an object in a full slab is * freed then the slab will show up again on the partial lists.
@@ -411,21 +412,21 @@ struct slab_sheaf {bool pfmemalloc; }; };struct kmem_cache *cache;unsigned int size;int node;/* only used for rcu_sheaf */ void *objects[]; };struct slub_percpu_sheaves { -local_trylock_t lock; +pw_trylock_t lock;struct slab_sheaf *main;/* never NULL when unlocked */ struct slab_sheaf *spare;/* empty or full, may be NULL */ struct slab_sheaf *rcu_free;/* for batching kfree_rcu() */ };/* * The slab lists for all objects. */ struct kmem_cache_node {spinlock_t list_lock;
@@ -477,21 +478,21 @@ static nodemask_t slab_nodes;* Corresponds to N_ONLINE nodes. */ static nodemask_t slab_barn_nodes;/* * Workqueue used for flushing cpu and kfree_rcu sheaves. */ static struct workqueue_struct *flushwq;struct slub_flush_work { -struct work_struct work; +struct pw_struct pw;struct kmem_cache *s;bool skip; };static DEFINE_MUTEX (flush_lock); static DEFINE_PER_CPU(struct slub_flush_work, slub_flush);/******************************************************************** * Core slab cache functions *******************************************************************/
@@ -2838,74 +2839,74 @@ static void __kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);* Free all objects from the main sheaf. In order to perform * __kmem_cache_free_bulk() outside of cpu_sheaves->lock, work in batches where * object pointers are moved to a on-stack array under the lock. To bound the * stack usage, limit each batch to PCS_BATCH_MAX. * * Must be called with s->cpu_sheaves->lock locked, returns with the lock * unlocked. * * Returns how many objects are remaining to be flushed */ -static unsigned int __sheaf_flush_main_batch (struct kmem_cache *s) +static unsigned int __sheaf_flush_main_batch (struct kmem_cache *s,int cpu) {struct slub_percpu_sheaves *pcs;unsigned int batch, remaining;void *objects[PCS_BATCH_MAX ];struct slab_sheaf *sheaf; -lockdep_assert_held (this_cpu_ptr (&s->cpu_sheaves->lock)); - - pcs =this_cpu_ptr (s->cpu_sheaves); + pcs =per_cpu_ptr (s->cpu_sheaves, cpu); sheaf = pcs->main; batch =min (PCS_BATCH_MAX , sheaf->size); sheaf->size -= batch;memcpy (objects, sheaf->objects + sheaf->size, batch *sizeof (void *)); remaining = sheaf->size; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock (&s->cpu_sheaves->lock, cpu);__kmem_cache_free_bulk (s, batch, &objects[0 ]);stat_add (s,SHEAF_FLUSH , batch);return remaining; } -static void sheaf_flush_main (struct kmem_cache *s) +static void sheaf_flush_main (struct kmem_cache *s,int cpu) {unsigned int remaining;do { -local_lock (&s->cpu_sheaves->lock); +pw_lock (&s->cpu_sheaves->lock, cpu); - remaining =__sheaf_flush_main_batch (s); + remaining =__sheaf_flush_main_batch (s, cpu); }while (remaining); }/* * Returns true if the main sheaf was at least partially flushed. */ static bool sheaf_try_flush_main (struct kmem_cache *s) {unsigned int remaining;bool ret = false;do { -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return ret; ret = true; - remaining =__sheaf_flush_main_batch (s); + +pw_lockdep_assert_held (&s->cpu_sheaves->lock); + remaining =__sheaf_flush_main_batch (s,smp_processor_id ()); }while (remaining);return ret; }/* * Free all objects from a sheaf that's unused, i.e. not linked to any * cpu_sheaves, so we need no locking and batching. The locking is also not * necessary when flushing cpu's sheaves (both spare and main) during cpu
@@ -2968,45 +2969,45 @@ static void rcu_free_sheaf_nobarn(struct rcu_head *head)/* * Caller needs to make sure migration is disabled in order to fully flush * single cpu's sheaves * * must not be called from an irq * * flushing operations are rare so let's keep it simple and flush to slabs * directly, skipping the barn */ -static void pcs_flush_all (struct kmem_cache *s) +static void pcs_flush_all (struct kmem_cache *s,int cpu) {struct slub_percpu_sheaves *pcs;struct slab_sheaf *spare, *rcu_free; -local_lock (&s->cpu_sheaves->lock); - pcs =this_cpu_ptr (s->cpu_sheaves); +pw_lock (&s->cpu_sheaves->lock, cpu); + pcs =per_cpu_ptr (s->cpu_sheaves, cpu); spare = pcs->spare; pcs->spare =NULL ; rcu_free = pcs->rcu_free; pcs->rcu_free =NULL ; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock (&s->cpu_sheaves->lock, cpu);if (spare) {sheaf_flush_unused (s, spare);free_empty_sheaf (s, spare); }if (rcu_free)call_rcu (&rcu_free->rcu_head, rcu_free_sheaf_nobarn); -sheaf_flush_main (s); +sheaf_flush_main (s, cpu); }static void __pcs_flush_all_cpu (struct kmem_cache *s,unsigned int cpu) {struct slub_percpu_sheaves *pcs; pcs =per_cpu_ptr (s->cpu_sheaves, cpu);/* The cpu is not executing anymore so we don't need pcs->lock */ sheaf_flush_unused (s, pcs->main);
@@ -3942,83 +3943,84 @@ static bool has_pcs_used(int cpu, struct kmem_cache *s)/* * Flush percpu sheaves * * Called from CPU work handler with migration disabled. */ static void flush_cpu_sheaves (struct work_struct *w) {struct kmem_cache *s;struct slub_flush_work *sfw; +int cpu =pw_get_cpu (w); - sfw = container_of(w, struct slub_flush_work, work); - + sfw = &per_cpu (slub_flush, cpu); s = sfw->s;if (cache_has_sheaves (s)) -pcs_flush_all (s); +pcs_flush_all (s, cpu); }static void flush_all_cpus_locked (struct kmem_cache *s) {struct slub_flush_work *sfw;unsigned int cpu;lockdep_assert_cpus_held ();mutex_lock (&flush_lock);for_each_online_cpu (cpu) { sfw = &per_cpu (slub_flush, cpu);if (!has_pcs_used (cpu, s)) { sfw->skip = true;continue ; } -INIT_WORK (&sfw->work, flush_cpu_sheaves); +INIT_PW (&sfw->pw, flush_cpu_sheaves, cpu); sfw->skip = false; sfw->s = s; -queue_work_on (cpu, flushwq, &sfw->work); +pw_queue_on (cpu, flushwq, &sfw->pw); }for_each_online_cpu (cpu) { sfw = &per_cpu (slub_flush, cpu);if (sfw->skip)continue ; -flush_work (&sfw->work); +pw_flush (&sfw->pw); }mutex_unlock (&flush_lock); }static void flush_all (struct kmem_cache *s) {cpus_read_lock ();flush_all_cpus_locked (s);cpus_read_unlock (); }static void flush_rcu_sheaf (struct work_struct *w) {struct slub_percpu_sheaves *pcs;struct slab_sheaf *rcu_free;struct slub_flush_work *sfw;struct kmem_cache *s; +int cpu =pw_get_cpu (w); - sfw = container_of(w, struct slub_flush_work, work); + sfw = &per_cpu (slub_flush, cpu); s = sfw->s; -local_lock (&s->cpu_sheaves->lock); - pcs =this_cpu_ptr (s->cpu_sheaves); +pw_lock (&s->cpu_sheaves->lock, cpu); + pcs =per_cpu_ptr (s->cpu_sheaves, cpu); rcu_free = pcs->rcu_free; pcs->rcu_free =NULL ; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock (&s->cpu_sheaves->lock, cpu);if (rcu_free)call_rcu (&rcu_free->rcu_head, rcu_free_sheaf_nobarn); }/* needed for kvfree_rcu_barrier() */ void flush_rcu_sheaves_on_cache (struct kmem_cache *s) {struct slub_flush_work *sfw;
@@ -4029,28 +4031,28 @@ void flush_rcu_sheaves_on_cache(struct kmem_cache *s)for_each_online_cpu (cpu) { sfw = &per_cpu (slub_flush, cpu);/* * we don't check if rcu_free sheaf exists - racing * __kfree_rcu_sheaf() might have just removed it. * by executing flush_rcu_sheaf() on the cpu we make * sure the __kfree_rcu_sheaf() finished its call_rcu() */ -INIT_WORK (&sfw->work, flush_rcu_sheaf); +INIT_PW (&sfw->pw, flush_rcu_sheaf, cpu); sfw->s = s; -queue_work_on (cpu, flushwq, &sfw->work); +pw_queue_on (cpu, flushwq, &sfw->pw); }for_each_online_cpu (cpu) { sfw = &per_cpu (slub_flush, cpu); -flush_work (&sfw->work); +pw_flush (&sfw->pw); }mutex_unlock (&flush_lock); }void flush_all_rcu_sheaves (void ) {struct kmem_cache *s;cpus_read_lock ();
@@ -4589,36 +4591,36 @@ bool slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,* unlocked. */ static struct slub_percpu_sheaves *__pcs_replace_empty_main (struct kmem_cache *s,struct slub_percpu_sheaves *pcs,gfp_t gfp) {struct slab_sheaf *empty =NULL ;struct slab_sheaf *full;struct node_barn *barn;bool allow_spin; -lockdep_assert_held (this_cpu_ptr (&s->cpu_sheaves->lock)); +pw_lockdep_assert_held (&s->cpu_sheaves->lock);/* Bootstrap or debug cache, back off */ if (unlikely (!cache_has_sheaves (s))) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return NULL ; }if (pcs->spare && pcs->spare->size >0 ) {swap (pcs->main, pcs->spare);return pcs; } barn =get_barn (s);if (!barn) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return NULL ; } allow_spin =gfpflags_allow_spinning (gfp); full =barn_replace_empty_sheaf (barn, pcs->main, allow_spin);if (full) {stat (s,BARN_GET ); pcs->main = full;
@@ -4629,21 +4631,21 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,if (allow_spin) {if (pcs->spare) { empty = pcs->spare; pcs->spare =NULL ; }else { empty =barn_get_empty_sheaf (barn, true); } } -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock); pcs =NULL ;if (!allow_spin)return NULL ;if (!empty) { empty =alloc_empty_sheaf (s, gfp);if (!empty)return NULL ; }
@@ -4655,21 +4657,21 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,*/ sheaf_flush_unused (s, empty);free_empty_sheaf (s, empty);return NULL ; } full = empty; empty =NULL ; -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock)) goto barn_put; pcs =this_cpu_ptr (s->cpu_sheaves);/* * If we put any empty or full sheaf to the barn below, it's due to * racing or being migrated to a different cpu. Breaching the barn's * sheaf limits should be thus rare enough so just ignore them to * simplify the recovery. */
@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's * not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; } -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ; pcs =this_cpu_ptr (s->cpu_sheaves);if (unlikely (pcs->main->size ==0 )) { pcs =__pcs_replace_empty_main (s, pcs, gfp);if (unlikely (!pcs))return NULL ; } object = pcs->main->objects[pcs->main->size -1 ];if (unlikely (node_requested)) {/* * Verify that the object was from the node we want. This could * be false because of cpu migration during an unlocked part of * the current allocation or previous freeing process. */ if (page_to_nid (virt_to_page (object)) != node) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat (s,ALLOC_NODE_MISMATCH );return NULL ; } } pcs->main->size--; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat (s,ALLOC_FASTPATH );return object; }static __fastpath_inline unsigned int alloc_from_pcs_bulk(struct kmem_cache *s, gfp_t gfp, size_t size,void **p) {struct slub_percpu_sheaves *pcs;struct slab_sheaf *main;unsigned int allocated =0 ;unsigned int batch; next_batch: -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return allocated; pcs =this_cpu_ptr (s->cpu_sheaves);if (unlikely (pcs->main->size ==0 )) {struct slab_sheaf *full;struct node_barn *barn;if (unlikely (!cache_has_sheaves (s))) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return allocated; }if (pcs->spare && pcs->spare->size >0 ) {swap (pcs->main, pcs->spare); goto do_alloc; } barn =get_barn (s);if (!barn) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return allocated; } full =barn_replace_empty_sheaf (barn, pcs->main,gfpflags_allow_spinning (gfp));if (full) {stat (s,BARN_GET ); pcs->main = full; goto do_alloc; }stat (s,BARN_GET_FAIL ); -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);/* * Once full sheaves in barn are depleted, let the bulk * allocation continue from slab pages, otherwise we would just * be copying arrays of pointers twice. */ return allocated; } do_alloc: main = pcs->main; batch =min (size, main->size); main->size -= batch;memcpy (p, main->objects + main->size, batch *sizeof (void *)); -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat_add (s,ALLOC_FASTPATH , batch); allocated += batch;if (batch < size) { p += batch; size -= batch; goto next_batch; }
@@ -5017,40 +5019,40 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size) &sheaf->objects[0])) {kfree (sheaf);return NULL ; } sheaf->size = size;return sheaf; } -local_lock (&s->cpu_sheaves->lock); +pw_lock_local (&s->cpu_sheaves->lock); pcs =this_cpu_ptr (s->cpu_sheaves);if (pcs->spare) { sheaf = pcs->spare; pcs->spare =NULL ;stat (s,SHEAF_PREFILL_FAST ); }else { barn =get_barn (s);stat (s,SHEAF_PREFILL_SLOW );if (barn) sheaf =barn_get_full_or_empty_sheaf (barn);if (sheaf && sheaf->size)stat (s,BARN_GET );else stat (s,BARN_GET_FAIL ); } -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);if (!sheaf) sheaf =alloc_empty_sheaf (s, gfp);if (sheaf) { sheaf->capacity = s->sheaf_capacity; sheaf->pfmemalloc = false; if (sheaf->size < size &&
@@ -5080,31 +5082,31 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,struct slub_percpu_sheaves *pcs;struct node_barn *barn;if (unlikely ((sheaf->capacity != s->sheaf_capacity) || sheaf->pfmemalloc)) {sheaf_flush_unused (s, sheaf);kfree (sheaf);return ; } -local_lock (&s->cpu_sheaves->lock); +pw_lock_local (&s->cpu_sheaves->lock); pcs =this_cpu_ptr (s->cpu_sheaves); barn =get_barn (s);if (!pcs->spare) { pcs->spare = sheaf; sheaf =NULL ;stat (s,SHEAF_RETURN_FAST ); } -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);if (!sheaf)return ;stat (s,SHEAF_RETURN_SLOW );/* * If the barn has too many full sheaves or we fail to refill the sheaf, * simply flush and free it. */
@@ -5627,21 +5629,21 @@ static void __slab_free(struct kmem_cache *s, struct slab *slab,* An alternative scenario that gets us here is when we fail * barn_replace_full_sheaf(), because there's no empty sheaf available in the * barn, so we had to allocate it by alloc_empty_sheaf(). But because we saw the * limit on full sheaves was not exceeded, we assume it didn't change and just * put the full sheaf there. */ static void __pcs_install_empty_sheaf (struct kmem_cache *s,struct slub_percpu_sheaves *pcs,struct slab_sheaf *empty,struct node_barn *barn) { -lockdep_assert_held (this_cpu_ptr (&s->cpu_sheaves->lock)); +pw_lockdep_assert_held (&s->cpu_sheaves->lock);/* This is what we expect to find if nobody interrupted us. */ if (likely (!pcs->spare)) { pcs->spare = pcs->main; pcs->main = empty;return ; }/* * Unlikely because if the main sheaf had space, we would have just
@@ -5678,31 +5680,31 @@ static void __pcs_install_empty_sheaf(struct kmem_cache *s,*/ static struct slub_percpu_sheaves *__pcs_replace_full_main (struct kmem_cache *s,struct slub_percpu_sheaves *pcs,bool allow_spin) {struct slab_sheaf *empty;struct node_barn *barn;bool put_fail; restart: -lockdep_assert_held (this_cpu_ptr (&s->cpu_sheaves->lock)); +pw_lockdep_assert_held (&s->cpu_sheaves->lock);/* Bootstrap or debug cache, back off */ if (unlikely (!cache_has_sheaves (s))) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return NULL ; } barn =get_barn (s);if (!barn) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);return NULL ; } put_fail = false;if (!pcs->spare) { empty =barn_get_empty_sheaf (barn, allow_spin);if (empty) { pcs->spare = pcs->main; pcs->main = empty;
@@ -5725,107 +5727,107 @@ __pcs_replace_full_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs, }/* sheaf_flush_unused() doesn't support !allow_spin */ if (PTR_ERR (empty) == -E2BIG && allow_spin) {/* Since we got here, spare exists and is full */ struct slab_sheaf *to_flush = pcs->spare;stat (s,BARN_PUT_FAIL ); pcs->spare =NULL ; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);sheaf_flush_unused (s, to_flush); empty = to_flush; goto got_empty; }/* * We could not replace full sheaf because barn had no empty * sheaves. We can still allocate it and put the full sheaf in * __pcs_install_empty_sheaf(), but if we fail to allocate it, * make sure to count the fail. */ put_fail = true; alloc_empty: -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);/* * alloc_empty_sheaf() doesn't support !allow_spin and it's * easier to fall back to freeing directly without sheaves * than add the support (and to sheaf_flush_unused() above) */ if (!allow_spin)return NULL ; empty =alloc_empty_sheaf (s,GFP_NOWAIT );if (empty) goto got_empty;if (put_fail)stat (s,BARN_PUT_FAIL );if (!sheaf_try_flush_main (s))return NULL ; -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ; pcs =this_cpu_ptr (s->cpu_sheaves);/* * we flushed the main sheaf so it should be empty now, * but in case we got preempted or migrated, we need to * check again */ if (pcs->main->size == s->sheaf_capacity) goto restart;return pcs; got_empty: -if (!local_trylock (&s->cpu_sheaves->lock)) { +if (!pw_trylock_local (&s->cpu_sheaves->lock)) {barn_put_empty_sheaf (barn, empty);return NULL ; } pcs =this_cpu_ptr (s->cpu_sheaves);__pcs_install_empty_sheaf (s, pcs, empty, barn);return pcs; }/* * Free an object to the percpu sheaves. * The object is expected to have passed slab_free_hook() already. */ static __fastpath_inline bool free_to_pcs(struct kmem_cache *s, void *object, bool allow_spin) {struct slub_percpu_sheaves *pcs; -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return false; pcs =this_cpu_ptr (s->cpu_sheaves);if (unlikely (pcs->main->size == s->sheaf_capacity)) { pcs =__pcs_replace_full_main (s, pcs, allow_spin);if (unlikely (!pcs))return false; } pcs->main->objects[pcs->main->size++] = object; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat (s,FREE_FASTPATH );return true; }static void rcu_free_sheaf (struct rcu_head *head) {struct slab_sheaf *sheaf;struct node_barn *barn =NULL ;
@@ -5898,63 +5900,63 @@ static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);bool __kfree_rcu_sheaf (struct kmem_cache *s,void *obj) {struct slub_percpu_sheaves *pcs;struct slab_sheaf *rcu_sheaf;if (WARN_ON_ONCE (IS_ENABLED (CONFIG_PREEMPT_RT )))return false;lock_map_acquire_try (&kfree_rcu_sheaf_map); -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock)) goto fail; pcs =this_cpu_ptr (s->cpu_sheaves);if (unlikely (!pcs->rcu_free)) {struct slab_sheaf *empty;struct node_barn *barn;/* Bootstrap or debug cache, fall back */ if (unlikely (!cache_has_sheaves (s))) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock); goto fail; }if (pcs->spare && pcs->spare->size ==0 ) { pcs->rcu_free = pcs->spare; pcs->spare =NULL ; goto do_free; } barn =get_barn (s);if (!barn) { -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock); goto fail; } empty =barn_get_empty_sheaf (barn, true);if (empty) { pcs->rcu_free = empty; goto do_free; } -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock); empty =alloc_empty_sheaf (s,GFP_NOWAIT );if (!empty) goto fail; -if (!local_trylock (&s->cpu_sheaves->lock)) { +if (!pw_trylock_local (&s->cpu_sheaves->lock)) {barn_put_empty_sheaf (barn, empty); goto fail; } pcs =this_cpu_ptr (s->cpu_sheaves);if (unlikely (pcs->rcu_free))barn_put_empty_sheaf (barn, empty);else pcs->rcu_free = empty;
@@ -5971,27 +5973,27 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) rcu_sheaf->objects[rcu_sheaf->size++] = obj;if (likely (rcu_sheaf->size < s->sheaf_capacity)) { rcu_sheaf =NULL ; }else { pcs->rcu_free =NULL ; rcu_sheaf->node =numa_node_id (); }/* -* we flush before local_unlock to make sure a racing +* we flush before pw_unlock_local to make sure a racing * flush_all_rcu_sheaves() doesn't miss this sheaf */ if (rcu_sheaf)call_rcu (&rcu_sheaf->rcu_head, rcu_free_sheaf); -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat (s,FREE_RCU_SHEAF );lock_map_release (&kfree_rcu_sheaf_map);return true; fail:stat (s,FREE_RCU_SHEAF_FAIL );lock_map_release (&kfree_rcu_sheaf_map);return false; }
@@ -6082,21 +6084,21 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)continue ; } i++; }if (!size) goto flush_remote; next_batch: -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock)) goto fallback; pcs =this_cpu_ptr (s->cpu_sheaves);if (likely (pcs->main->size < s->sheaf_capacity)) goto do_free; barn =get_barn (s);if (!barn) goto no_empty;
@@ -6125,37 +6127,37 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)stat (s,BARN_PUT ); pcs->main = empty; do_free: main = pcs->main; batch =min (size, s->sheaf_capacity - main->size);memcpy (main->objects + main->size, p, batch *sizeof (void *)); main->size += batch; -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);stat_add (s,FREE_FASTPATH , batch);if (batch < size) { p += batch; size -= batch; goto next_batch; }if (remote_nr) goto flush_remote;return ; no_empty: -local_unlock (&s->cpu_sheaves->lock); +pw_unlock_local (&s->cpu_sheaves->lock);/* * if we depleted all empty sheaves in the barn or there are too * many full sheaves, free the rest to slab pages */ fallback:__kmem_cache_free_bulk (s, size, p);stat_add (s,FREE_SLOWPATH , size); flush_remote:
@@ -7554,21 +7556,21 @@ static inline int alloc_kmem_cache_stats(struct kmem_cache *s)static int init_percpu_sheaves (struct kmem_cache *s) {static struct slab_sheaf bootstrap_sheaf = {};int cpu;for_each_possible_cpu (cpu) {struct slub_percpu_sheaves *pcs; pcs =per_cpu_ptr (s->cpu_sheaves, cpu); -local_trylock_init (&pcs->lock); +pw_trylock_init (&pcs->lock);/* * Bootstrap sheaf has zero size so fast-path allocation fails. * It has also size == s->sheaf_capacity, so fast-path free * fails. In the slow paths we recognize the situation by * checking s->sheaf_capacity. This allows fast paths to assume * s->cpu_sheaves and pcs->main always exists and are valid. * It's also safe to share the single static bootstrap_sheaf * with zero-sized objects array as it's never modified. *
Signature · 1 line
2.54.0
syzbot ci has tested the following series
syzbot ci has tested the following series
[v4] Introduce Per-CPU Work helpers (was QPW)
https://lore.kernel.org/all/[email protected]
* [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
* [PATCH v4 2/4] mm/swap: move bh draining into a separate workqueue
* [PATCH v4 3/4] swap: apply new pw_queue_on() interface
* [PATCH v4 4/4] slub: apply new pw_queue_on() interface
and found the following issue:
WARNING in __pcs_replace_empty_main
Full report is available here:
https://ci.syzbot.org/series/804f81bd-77b4-490e-bd57-6345ad2aa923
***
WARNING in __pcs_replace_empty_main
tree: drm-next URL: https://gitlab.freedesktop.org/drm/kernel.git base: 5200f5f493f79f14bbdc349e402a40dfb32f23c8 arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/3ea80958-13bd-49da-9c64-6deb788113f8/config
clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns Zone ranges:
DMA [mem 0x0000000000001000-0x0000000000ffffff] DMA32 [mem 0x0000000001000000-0x00000000ffffffff] Normal [mem 0x0000000100000000-0x000000023fffffff] Device empty Movable zone start for each node Early memory node ranges node 0: [mem 0x0000000000001000-0x000000000009efff] node 0: [mem 0x0000000000100000-0x000000007ffdefff] node 0: [mem 0x0000000100000000-0x0000000160000fff] node 1: [mem 0x0000000160001000-0x000000023fffffff]
Initmem setup node 0 [mem 0x0000000000001000-0x0000000160000fff]
Initmem setup node 1 [mem 0x0000000160001000-0x000000023fffffff]
On node 0, zone DMA: 1 pages in unavailable ranges
On node 0, zone DMA: 97 pages in unavailable ranges
On node 0, zone Normal: 33 pages in unavailable rangessetup_percpu: NR_CPUS:8 nr_cpumask_bits:2 nr_cpu_ids:2 nr_node_ids:2
percpu: Embedded 71 pages/cpu s250632 r8192 d31992 u2097152
kvm-guest: PV spinlocks disabled, no host support
Kernel command line: earlyprintk=serial net.ifnames=0 sysctl.kernel.hung_task_all_cpu_backtrace=1 ima_policy=tcb nf-conntrack-ftp.ports=20000 nf-conntrack-tftp.ports=20000 nf-conntrack-sip.ports=20000 nf-conntrack-irc.ports=20000 nf-conntrack-sane.ports=20000 binder.debug_mask=0 rcupdate.rcu_expedited=1 rcupdate.rcu_cpu_stall_cputime=1 no_hash_pointers page_owner=on sysctl.vm.nr_hugepages=4 sysctl.vm.nr_overcommit_hugepages=4 secretmem.enable=1 sysctl.max_rcu_stall_to_panic=1 msr.allow_writes=off coredump_filter=0xffff root=/dev/sda console=ttyS0 vsyscall=native numa=fake=2 kvm-intel.nested=1 spec_store_bypass_disable=prctl nopcid vivid.n_devs=64 vivid.multiplanar=1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2 netrom.nr_ndevs=32 rose.rose_ndevs=32 smp.csd_lock_timeout=100000 watchdog_thresh=55 workqueue.watchdog_thresh=140 sysctl.net.core.netdev_unregister_timeout_secs=140 dummy_hcd.num=32 max_loop=32 nbds_max=32 \
Kernel command line: comedi.comedi_num_legacy_minors=4 panic_on_warn=1 root=/dev/sda console=ttyS0 root=/dev/sda1
Unknown kernel command line parameters "nbds_max=32", will be passed to user space.
printk: log buffer data + meta data: 262144 + 917504 = 1179648 bytes
software IO TLB: area num 2.
Fallback order for Node 0: 0 1
Fallback order for Node 1: 1 0
Built 2 zonelists, mobility grouping on. Total pages: 1834877
Policy zone: Normal
mem auto-init: stack:all(zero), heap alloc:on, heap free:off
stackdepot: allocating hash table via alloc_large_system_hash
stackdepot hash table entries: 1048576 (order: 12, 16777216 bytes, linear)
stackdepot: allocating space for 8192 stack pools via memblock
**********************************************************
** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE ** ** ** ** This system shows unhashed kernel memory addresses ** ** via the console, logs, and other interfaces. This ** ** might reduce the security of your system. ** ** ** ** If you see this message and you are not debugging ** ** the kernel, report this immediately to your system ** ** administrator! ** ** ** ** Use hash_pointers=always to force this mode off ** ** ** ** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE ** ********************************************************** ------------[ cut here ]------------ debug_locks && !(lock_is_held(&(&s->cpu_sheaves->lock)->dep_map) != 0) WARNING: mm/slub.c:4601 at __pcs_replace_empty_main+0x51b/0x6e0, CPU#0: swapper/0 Modules linked in: CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted syzkaller #0 PREEMPT(undef) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:__pcs_replace_empty_main+0x51b/0x6e0 Code: 48 85 f6 74 15 4c 89 ff 48 89 c6 e8 af 5e ff ff 4d 89 74 24 38 e9 36 fc ff ff 49 89 44 24 40 4d 89 74 24 38 e9 27 fc ff ff 90 <0f> 0b 90 83 7b 2c 00 0f 85 23 fb ff ff 48 8b 1b e8 20 cd 82 09 41 RSP: 0000:ffffffff8e607d58 EFLAGS: 00010046 RAX: 0000000000000000 RBX: ffffffff91bb8398 RCX: 0000000000000002 RDX: 0000000000000cc0 RSI: ffffffff8e21ec94 RDI: ffffffff8c28b160 RBP: 0000000000000cc0 R08: 0000000000005e00 R09: 00000000477ac845 R10: 0000000047d13f7f R11: 000000002fa01ecd R12: ffff88812103f308 R13: 0000000000000000 R14: ffffffff91bb8398 R15: 0000000000000000 FS: 0000000000000000(0000) GS:ffff88818dc8a000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff88823ffff000 CR3: 000000000e74a000 CR4: 00000000000000b0 Call Trace: <TASK> kmem_cache_alloc_node_noprof+0x441/0x690 do_kmem_cache_create+0x172/0x620 create_boot_cache+0xbf/0x120 kmem_cache_init+0x11a/0x1e0 mm_core_init+0x7e/0xb0 start_kernel+0x15a/0x3e0 x86_64_start_reservations+0x24/0x30 x86_64_start_kernel+0x143/0x1c0 common_startup_64+0x13e/0x147 </TASK>
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
To test a patch for this bug, please reply with #syz test (should be on a separate line).
The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.
kernel test robot
build botRe: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Leonardo Braskernel test robot noticed the following build warnings:
Hi Leonardo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 5200f5f493f79f14bbdc349e402a40dfb32f23c8]
url: https://github.com/intel-lab-lkp/linux/commits/Leonardo-Bras/Introducing-pw_lock-and-per-cpu-queue-flush-work/20260519-112741 base: 5200f5f493f79f14bbdc349e402a40dfb32f23c8 patch link: https://lore.kernel.org/r/20260519012754.240804-5-leobras.c%40gmail.com patch subject: [PATCH v4 4/4] slub: apply new pw_queue_on() interface compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) docutils: docutils (Docutils 0.21.2, Python 3.13.5, on linux) reproduce: (https://download.01.org/0day-ci/archive/20260519/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
Runtime Survivability
===================== [docutils]
>> Documentation/locking/pwlocks.rst:3: WARNING: Title overline too short.
--
Documentation/userspace-api/landlock:550: ./include/uapi/linux/landlock.h:45: ERROR: Unknown target name: "network flags". [docutils] Documentation/userspace-api/landlock:550: ./include/uapi/linux/landlock.h:50: ERROR: Unknown target name: "scope flags". [docutils] Documentation/userspace-api/landlock:550: ./include/uapi/linux/landlock.h:24: ERROR: Unknown target name: "filesystem flags". [docutils] Documentation/userspace-api/landlock:559: ./include/uapi/linux/landlock.h:168: ERROR: Unknown target name: "filesystem flags". [docutils] Documentation/userspace-api/landlock:559: ./include/uapi/linux/landlock.h:191: ERROR: Unknown target name: "network flags". [docutils]
>> Documentation/locking/pwlocks.rst: WARNING: document isn't included in any toctree [toc.not_included]
Documentation/networking/skbuff:36: ./include/linux/skbuff.h:181: WARNING: Failed to create a cross reference. A title or caption not found: 'crc' [ref.ref]
vim +3 Documentation/locking/pwlocks.rst
2
> 3 =========
4 PW (Per-CPU Work) locks
5 =========
6 --
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Leonardo BrasQuoted 54 lines · Leonardo Bras
Some places in the kernel implement a parallel programming strategy consisting on
local_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.On the other hand, for RT workloads this can represent a problem:
scheduling work on remote cpu that are executing low latency tasks is undesired and can introduce unexpected deadline misses.It's interesting, though, that local_lock()s in RT kernels become
spinlock(). We can make use of those to avoid scheduling work on a remote cpu by directly updating another cpu'sper_cpustructure, while holding it'sspinlock().In order to do that, it's necessary to introduce a new set of functions to make it possible to get another cpu's per-cpu "local" lock (pw_{un,}lock*) and also do the corresponding queueing (
pw_queue_on()) and flushing (pw_flush()) helpers to run the remote work.Users of non-RT kernels but with low latency requirements can select similar functionality by using the
CONFIG_PWLOCKScompile time option.On
CONFIG_PWLOCKSdisabled kernels, no changes are expected, as every one of the introduced helpers work the exactly same as the current implementation:
pw_{un,}lock*() -> local_{un,}lock*() (ignores cpu parameter)pw_queue_on()->queue_work_on()pw_flush()->flush_work()For PWLOCKS enabled kernels, though, pw_{un,}lock*() will use the extra cpu parameter to select the correct per-cpu structure to work on, and acquire the spinlock for that cpu.
pw_queue_on()will just call the requested function in the current cpu, which will operate in another cpu's per-cpu object. Since thelocal_locks()become spinlock()s in PWLOCKS enabled kernels, we are safe doing that.
pw_flush()then becomes a no-op since no work is actually scheduled on a remote cpu.Some minimal code rework is needed in order to make this mechanism work:
The calls for local_{un,}lock*() on the functions that are currently scheduled on remote cpus need to be replaced by either pw_{un,}lock_*(), PWLOCKS enabled kernels they can reference a different cpu. It's also necessary to use apw_structinstead of awork_struct, but it just contains a work struct and, inCONFIG_PWLOCKS, the target cpu.This should have almost no impact on non-CONFIG_PWLOCKS kernels: few
this_cpu_ptr()will become per_cpu_ptr(,smp_processor_id()) on non-hotpath functions.
Quoted 5 lines
On
CONFIG_PWLOCKSkernels, this should avoid deadlines misses by removing scheduling noise.Signed-off-by Leonardo Bras <[email protected]>Signed-off-by Marcelo Tosatti <[email protected]>
I like it! Just a few observations:
Quoted 7 lines
include/linux/pwlocks.h new file +7
-0+#ifndef CONFIG_PWLOCKS + +typedef local_lock_t pw_lock_t; +typedef local_trylock_t pw_trylock_t; + +struct pw_struct { + struct work_struct work;
include/linux/pwlocks.h line 25
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+}; + +#define pw_lock_init (lock) \ + local_lock_init(lock) + +#define pw_trylock_init (lock) \ + local_trylock_init(lock) + +#define pw_lock (lock, cpu) \ + local_lock(lock)
For debugging purpose, it would be nice to ensure that in those off-case, cpu is indeed the local one. Basically all the non-local functions, those that take a cpu, should verify:
lockdep_assert(cpu == smp_processor_id())
Quoted 62 lines
include/linux/pwlocks.h new file +62
-0+ +#define pw_lock_local (lock) \ + local_lock(lock) + +#define pw_lock_irqsave (lock, flags, cpu) \ + local_lock_irqsave(lock, flags) + +#define pw_lock_local_irqsave (lock, flags) \ + local_lock_irqsave(lock, flags) + +#define pw_trylock (lock, cpu) \ + local_trylock(lock) + +#define pw_trylock_local (lock) \ + local_trylock(lock) + +#define pw_trylock_irqsave (lock, flags, cpu) \ + local_trylock_irqsave(lock, flags) + +#define pw_unlock (lock, cpu) \ + local_unlock(lock) + +#define pw_unlock_local (lock) \ + local_unlock(lock) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ + local_unlock_irqrestore(lock, flags) + +#define pw_unlock_local_irqrestore (lock, flags) \ + local_unlock_irqrestore(lock, flags) + +#define pw_lockdep_assert_held (lock) \ + lockdep_assert_held(lock) + +#define pw_queue_on (c, wq, pw) \ + queue_work_on(c, wq, &(pw)->work) + +#define pw_flush (pw) \ + flush_work(&(pw)->work) + +#define pw_get_cpu (pw) smp_processor_id() + +#define pw_is_cpu_remote (cpu) (false) + +#define INIT_PW (pw, func, c) \ + INIT_WORK(&(pw)->work, (func)) + +#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); + +typedef union { +spinlock_t sl; +local_lock_t ll; +}pw_lock_t ; + +typedef union { +spinlock_t sl; +local_trylock_t ll; +}pw_trylock_t ; + +struct pw_struct {
include/linux/pwlocks.h line 97
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+struct work_struct work; +int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT +#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disable +#define preempt_or_migrate_enable preempt_enable
This can be no-op in !CONFIG_PREEMPT_RT because non-rt spinlocks disable preemption already.
include/linux/pwlocks.h line 106
Quoted 9 lines
include/linux/pwlocks.h new file +9
-0+#endif + +#define pw_lock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0)
It looks like all these macros could be inline functions.
Quoted 11 lines
include/linux/pwlocks.h new file +10
-0+ +#define pw_trylock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_trylock_init(lock.ll); \ +} while (0) + +#define pw_lock (lock, cpu)\
And those could have the same local CPU debug check.
Quoted 53 lines
include/linux/pwlocks.h new file +53
-0+do { \ +if (static_branch_maybe (CONFIG_PWLOCKS_DEFAULT , &pw_sl)) \ +spin_lock (per_cpu_ptr (lock.sl, cpu)); \ +else \ +local_lock (lock.ll); \ +} while (0) + +#define pw_lock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock(this_cpu_ptr(lock.sl)); \ + } else { \ + local_lock(lock.ll); \ + } \ +} while (0) + +#define pw_lock_irqsave (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_lock_irqsave(lock.ll, flags); \ +} while (0) + +#define pw_lock_local_irqsave (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock_irqsave(this_cpu_ptr(lock.sl), flags); \ + } else { \ + local_lock_irqsave(lock.ll, flags); \ + } \ +} while (0) + +#define pw_trylock (lock, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + t = local_trylock(lock.ll); \ + t; \ +}) + +#define pw_trylock_local (lock) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + t = spin_trylock(this_cpu_ptr(lock.sl)); \ + if (!t) \ + preempt_or_migrate_enable();
Quoted 1 line
\
This is duplicating the RT logic in local_lock_internal.h and it would be tempting to propose spin_local_lock_t that both pw and RT local_lock could rely upon. But I'm afraid that would create a less readable result:
- we would need to check the CONFIG_PREEMPT_RT there before doing the migrate_disable/enable
- RT local lock don't take the lock on IRQ/NMI, which is fine as pw is not expected to be used on the non-threaded parts of IRQs not NMIs. Still that's one more conditional to add there.
- we'll need to differenciate local/remote operations.
Well let's stick to what you did for now (Peter might have a different opinion though).
Quoted 139 lines
include/linux/pwlocks.h new file +96
-0+ } else { \ + t =local_trylock (lock.ll); \ + } \ + t; \ +}) + +#define pw_trylock_irqsave (lock, flags, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + t = local_trylock_irqsave(lock.ll, flags); \ + t; \ +}) + +#define pw_unlock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_unlock(lock.ll); \ +} while (0) + +#define pw_unlock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock(this_cpu_ptr(lock.sl)); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock(lock.ll); \ + } \ +} while (0) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock_irqrestore(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_unlock_irqrestore(lock.ll, flags); \ +} while (0) + +#define pw_unlock_local_irqrestore (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock_irqrestore(this_cpu_ptr(lock.sl), flags); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock_irqrestore(lock.ll, flags); \ + } \ +} while (0) + +#define pw_lockdep_assert_held (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + lockdep_assert_held(this_cpu_ptr(lock.sl)); \ + else \ + lockdep_assert_held(this_cpu_ptr(lock.ll)); \ +} while (0) + +#define pw_queue_on (c, wq, pw) \ +do { \ + int __c = c; \ + struct pw_struct *__pw = (pw); \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + WARN_ON((__c) != __pw->cpu); \ + __pw->work.func(&__pw->work); \ + } else { \ + queue_work_on(__c, wq, &(__pw)->work); \ + } \ +} while (0) + +/* +* Does nothing if PWLOCKS is set to use spinlock, as the task is already done at the +* time pw_queue_on() returns. +*/ +#define pw_flush (pw) \ +do { \ + struct pw_struct *__pw = (pw); \ + if (!static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + flush_work(&__pw->work); \ +} while (0) + +#define pw_get_cpu (w) container_of((w), struct pw_struct, work)->cpu + +#define pw_is_cpu_remote (cpu) ((cpu) != smp_processor_id()) + +#define INIT_PW (pw, func, c) \ +do { \ + struct pw_struct *__pw = (pw); \ + INIT_WORK(&__pw->work, (func)); \ + __pw->cpu = (c); \ +} while (0) + +#endif /* CONFIG_PWLOCKS */ +#endif /* LINUX_PWLOCKS_H */ kernel/pwlocks.c new file +37
-0@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "linux/export.h" +#include <linux/sched.h> +#include <linux/pwlocks.h> +#include <linux/string.h> +#include <linux/sched/isolation.h> + +DEFINE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); +EXPORT_SYMBOL (pw_sl); + +static bool pwlocks_param_specified; + +static int __init pwlocks_setup (char *str) +{ +int opt; + +if (!get_option (&str, &opt)) { +pr_warn ("PWLOCKS: invalid pwlocks parameter: %s, ignoring.\n" , str); +return 0 ; + } + +if (opt) +static_branch_enable (&pw_sl); +else +static_branch_disable (&pw_sl); + + pwlocks_param_specified = true; + +return 1 ; +} +__setup ("pwlocks=" , pwlocks_setup); + +/* +* Enable PWLOCKS if CPUs want to avoid kernel noise. +*/ +static int __init pwlocks_init (void ) +{
kernel/pwlocks.c line 47
Quoted 10 lines
kernel/pwlocks.c new file +10
-0+if (pwlocks_param_specified) +return 0 ; + +if (housekeeping_enabled (HK_TYPE_KERNEL_NOISE )) +static_branch_enable (&pw_sl); + +return 0 ; +} + +late_initcall (pwlocks_init);
That should be a pre-SMP initcall. Otherwise you risk some asymetric calls.
Thanks.
Signature · 2 lines
Frederic Weisbecker SUSE Labs
Sebastian Andrzej Siewior
maintainerQuoted 8 lines · Leonardo Bras
The problem:
Some places in the kernel implement a parallel programming strategy consisting onlocal_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
Quoted 6 lines
On the other hand, for RT workloads this can represent a problem: getting an important workload scheduled out to deal with remote requests is sure to introduce unexpected deadline misses.
The idea:
Currently with PREEMPT_RT=y,local_locks()become per-cpu spinlocks.
It does not become a _spin_lock because it does not spin. It sleeps.
Quoted 4 lines
In this case, instead of scheduling work on a remote cpu, it should be safe to grab that remote cpu's per-cpu spinlock and run the required work locally. That major cost, which is un/locking in every local function, already happens in
PREEMPT_RT.
We did have this before but only in the RT tree. It was a bit messy from the naming because it started with local_ but then it was a remote CPU. The main issue was the different code path which led to a few deadlocks back then.
By the time local_lock_t went upstream, the cross-CPU locking was removed. As far as I remember, the cross-CPU user which did schedule work on a remote CPU and annoyed NOHZ folks were replaced.
Quoted 10 lines
Also, there is no need to worry about extra cache bouncing:
The cacheline invalidation already happens due toschedule_work_on().This will avoid
schedule_work_on(), and thus avoid scheduling-out an RT workload.Proposed solution:
A new interface called PerCPU Work (PW), which should replace Work Queue in the above mentioned use case.
Quoted 7 lines
If
CONFIG_PWLOCKS=nthis interfaces just wraps the currentlocal_locks+ WorkQueue behavior, so no expected change in runtime.If
CONFIG_PWLOCKS=y, and kernel boot option pwlocks=1,pw_queue_on(cpu,...)will lock that cpu's per-cpu structure and perform work on it locally.
Sebastian
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Leonardo BrasQuoted 15 lines · Leonardo Bras
Documentation/admin-guide/kernel-parameters.txt +0
-0@@ -2810,20 +2810,30 @@ Kernel parameters If a queue's affinity mask contains only isolated CPUs then this parameter has no effect on the interrupt routing decision, though interrupts are only delivered when tasks running on those isolated CPUs submit IO. IO submitted on housekeeping CPUs has no influence on those queues. The format of <cpu-list> is described above.
Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 2829
Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10
-0+ pwlocks= [KNL,SMP] Select a behavior on per-CPU resource sharing + and remote interference mechanism on a kernel built with + CONFIG_PWLOCKS. + Format: { "0" | "1" } + 0 - local_lock() + queue_work_on(remote_cpu) + 1 - spin_lock() for both local and remote operations + + Selecting 1 may be interesting for systems that want + to avoid interruption & context switches from IPIs. +
This documentation is supposed to be for an administrator/ user of the system. Exposing him to underlying kernel technique shouldn't happen. It does not explain the users/ outcome so it sounds like best hope.
Quoted 16 lines
Documentation/admin-guide/kernel-parameters.txt +0
-0iucv= [HW,NET] ivrs_ioapic [HW,X86-64] Provide an override to the IOAPIC-ID<->DEVICE-ID mapping provided in the IVRS ACPI table. By default, PCI segment is 0, and can be omitted. For example, to map IOAPIC-ID decimal 10 to PCI segment 0x1 and PCI device 00:14.0, write the parameter as:Documentation/locking/pwlocks.rst new file +0
-0
Documentation/locking/pwlocks.rst line 10
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +========= + +Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low since +cacheline tends to be mostly local, and avoids the cost of locks in non-RT
PREEMPT_RT can be spelled out if you mean it so it is not confused with other meanings of the two letters.
Documentation/locking/pwlocks.rst line 20
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem: +scheduling work on remote cpu that are executing low latency tasks +is undesired and can introduce unexpected deadline misses. + +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks.
not spinlocks.
Documentation/locking/pwlocks.rst line 25
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0+ +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter).
If it is a function, it should end with ()
Documentation/locking/pwlocks.rst line 28
Quoted 2 lines
Documentation/locking/pwlocks.rst new file +2
-0+The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter.
CPU.
Quoted 34 lines
Documentation/locking/pwlocks.rst new file +34
-0+For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter): + - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock + - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_work + +* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock + - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock + - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock + - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty
Documentation/locking/pwlocks.rst line 73
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ +pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu. + +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must only +be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead.
Why do you need to either the one or the other? My only guess is that migrate_disable() is sufficient but you prefer preempt_disable() on !PREEMPT_RT because it is cheaper.
Quoted 20 lines
Documentation/locking/pwlocks.rst new file +2
-0+These should only be used when accessing per-CPU data of the local CPU. +init/Kconfig +3
-0@@ -764,20 +764,55 @@ config CPU_ISOLATION depends on SMP default y help Make sure that CPUs running critical tasks are not disturbed by any source of "noise" such as unbound workqueues, timers, kthreads... Unbound jobs get offloaded to housekeeping CPUs. This is driven by the "isolcpus=" boot parameter. Say Y if unsure. +config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST
init/Kconfig config CPU_ISOLATION · line 786
Quoted 10 lines
init/Kconfig +10
-0+ default n + help + Allow changing the behavior on per-CPU resource sharing with cache, + from the regular local_locks() + queue_work_on(remote_cpu) to using + per-CPU spinlocks on both local and remote operations. + + This is useful to give user the option on reducing IPIs to CPUs, and + thus reduce interruptions and context switches. On the other hand, it + increases generated code and will use atomic operations if spinlocks + are selected.
I think the goal is to avoid scheduling a task on a remote CPU to get something done.
init/Kconfig config CPU_ISOLATION · line 792
Quoted 6 lines
init/Kconfig +6
-0+ + If set, will use the default behavior set in PWLOCKS_DEFAULT unless boot + parameter pwlocks is passed with a different behavior. + + If unset, will use the local_lock() + queue_work_on() strategy, + regardless of the boot parameter or PWLOCKS_DEFAULT.
This sounds like it affects the greater kernel.
init/Kconfig config CPU_ISOLATION · line 799
Quoted 6 lines
init/Kconfig +6
-0+ Say N if unsure. + +config PWLOCKS_DEFAULT + bool "Use per-CPU spinlocks by default on PWLOCKS" + depends on PWLOCKS + default n
n is default.
Quoted 46 lines
init/Kconfig +9
-0+ help + If set, will use per-CPU spinlocks as default behavior for per-CPU + remote operations. + + If unset, will use local_lock() + queue_work_on(cpu) as default + behavior for remote operations. + + Say N if unsure + source "kernel/rcu/Kconfig" config IKCONFIG tristate "Kernel .config support" help This option enables the complete Linux kernel ".config" file contents to be saved in the kernel. It provides documentation of which kernel options are used in a running kernel or in an on-disk kernel. This information can be extracted from the kernel image file with the script scripts/extract-ikconfig and used asinclude/linux/pwlocks.h new file +21
-0@@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PWLOCKS_H +#define _LINUX_PWLOCKS_H + +#include "linux/spinlock.h" +#include "linux/local_lock.h" +#include "linux/workqueue.h" + +#ifndef CONFIG_PWLOCKS + +typedef local_lock_t pw_lock_t ; +typedef local_trylock_t pw_trylock_t ; + +struct pw_struct { +struct work_struct work; +}; + +#define pw_lock_init (lock) \ + local_lock_init(lock) + +#define pw_trylock_init (lock) \
include/linux/pwlocks.h line 31
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+ local_trylock_init(lock) + +#define pw_lock (lock, cpu) \ + local_lock(lock) + +#define pw_lock_local (lock) \ + local_lock(lock) + +#define pw_lock_irqsave (lock, flags, cpu) \ + local_lock_irqsave(lock, flags)
The part where you have a `cpu' argument which is not used is entirely confusing.
Quoted 44 lines
include/linux/pwlocks.h new file +44
-0+ +#define pw_lock_local_irqsave (lock, flags) \ + local_lock_irqsave(lock, flags) + +#define pw_trylock (lock, cpu) \ + local_trylock(lock) + +#define pw_trylock_local (lock) \ + local_trylock(lock) + +#define pw_trylock_irqsave (lock, flags, cpu) \ + local_trylock_irqsave(lock, flags) + +#define pw_unlock (lock, cpu) \ + local_unlock(lock) + +#define pw_unlock_local (lock) \ + local_unlock(lock) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ + local_unlock_irqrestore(lock, flags) + +#define pw_unlock_local_irqrestore (lock, flags) \ + local_unlock_irqrestore(lock, flags) + +#define pw_lockdep_assert_held (lock) \ + lockdep_assert_held(lock) + +#define pw_queue_on (c, wq, pw) \ + queue_work_on(c, wq, &(pw)->work) + +#define pw_flush (pw) \ + flush_work(&(pw)->work) + +#define pw_get_cpu (pw) smp_processor_id() + +#define pw_is_cpu_remote (cpu) (false) + +#define INIT_PW (pw, func, c) \ + INIT_WORK(&(pw)->work, (func)) + +#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl);
include/linux/pwlocks.h line 85
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+ +typedef union { +spinlock_t sl; +local_lock_t ll; +}pw_lock_t ; + +typedef union { +spinlock_t sl; +local_trylock_t ll; +}pw_trylock_t ;
Why do you use local_trylock_t ? Its use case is different compared to local_lock_t. _IF_ you are fine with local_trylock_t then you should be able to deal with a per-CPU spinlock_t and none of this should be needed.
include/linux/pwlocks.h line 98
Quoted 12 lines
include/linux/pwlocks.h new file +12
-0+struct pw_struct { +struct work_struct work; +int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT +#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disable +#define preempt_or_migrate_enable preempt_enable +#endif
if then () but this looks terrible.
Quoted 143 lines
include/linux/pwlocks.h new file +143
-0+ +#define pw_lock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0) + +#define pw_trylock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_trylock_init(lock.ll); \ +} while (0) + +#define pw_lock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_lock(lock.ll); \ +} while (0) + +#define pw_lock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock(this_cpu_ptr(lock.sl)); \ + } else { \ + local_lock(lock.ll); \ + } \ +} while (0) + +#define pw_lock_irqsave (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_lock_irqsave(lock.ll, flags); \ +} while (0) + +#define pw_lock_local_irqsave (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock_irqsave(this_cpu_ptr(lock.sl), flags); \ + } else { \ + local_lock_irqsave(lock.ll, flags); \ + } \ +} while (0) + +#define pw_trylock (lock, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + t = local_trylock(lock.ll); \ + t; \ +}) + +#define pw_trylock_local (lock) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + t = spin_trylock(this_cpu_ptr(lock.sl)); \ + if (!t) \ + preempt_or_migrate_enable(); \ + } else { \ + t = local_trylock(lock.ll); \ + } \ + t; \ +}) + +#define pw_trylock_irqsave (lock, flags, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + t = local_trylock_irqsave(lock.ll, flags); \ + t; \ +}) + +#define pw_unlock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_unlock(lock.ll); \ +} while (0) + +#define pw_unlock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock(this_cpu_ptr(lock.sl)); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock(lock.ll); \ + } \ +} while (0) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock_irqrestore(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_unlock_irqrestore(lock.ll, flags); \ +} while (0) + +#define pw_unlock_local_irqrestore (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock_irqrestore(this_cpu_ptr(lock.sl), flags); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock_irqrestore(lock.ll, flags); \ + } \ +} while (0) + +#define pw_lockdep_assert_held (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + lockdep_assert_held(this_cpu_ptr(lock.sl)); \ + else \ + lockdep_assert_held(this_cpu_ptr(lock.ll)); \ +} while (0) + +#define pw_queue_on (c, wq, pw) \ +do { \ + int __c = c; \ + struct pw_struct *__pw = (pw); \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + WARN_ON((__c) != __pw->cpu); \ + __pw->work.func(&__pw->work); \ + } else { \ + queue_work_on(__c, wq, &(__pw)->work); \ + } \ +} while (0) +
include/linux/pwlocks.h line 251
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+/* +* Does nothing if PWLOCKS is set to use spinlock, as the task is already done at the +* time pw_queue_on() returns. +*/ +#define pw_flush (pw) \ +do { \ + struct pw_struct *__pw = (pw); \ + if (!static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + flush_work(&__pw->work); \ +} while (0)
I don't think this should be a collection of macros. Either proper functions or static inline _if_ this is performance critical for some reason.
Quoted 52 lines
include/linux/pwlocks.h new file +14
-0+ +#define pw_get_cpu (w) container_of((w), struct pw_struct, work)->cpu + +#define pw_is_cpu_remote (cpu) ((cpu) != smp_processor_id()) + +#define INIT_PW (pw, func, c) \ +do { \ + struct pw_struct *__pw = (pw); \ + INIT_WORK(&__pw->work, (func)); \ + __pw->cpu = (c); \ +} while (0) + +#endif /* CONFIG_PWLOCKS */ +#endif /* LINUX_PWLOCKS_H */ kernel/pwlocks.c new file +32
-0@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "linux/export.h" +#include <linux/sched.h> +#include <linux/pwlocks.h> +#include <linux/string.h> +#include <linux/sched/isolation.h> + +DEFINE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); +EXPORT_SYMBOL (pw_sl); + +static bool pwlocks_param_specified; + +static int __init pwlocks_setup (char *str) +{ +int opt; + +if (!get_option (&str, &opt)) { +pr_warn ("PWLOCKS: invalid pwlocks parameter: %s, ignoring.\n" , str); +return 0 ; + } + +if (opt) +static_branch_enable (&pw_sl); +else +static_branch_disable (&pw_sl); + + pwlocks_param_specified = true; + +return 1 ; +} +__setup ("pwlocks=" , pwlocks_setup); +
kernel/pwlocks.c line 42
Quoted 10 lines
kernel/pwlocks.c new file +10
-0+/* +* Enable PWLOCKS if CPUs want to avoid kernel noise. +*/ +static int __init pwlocks_init (void ) +{ +if (pwlocks_param_specified) +return 0 ; + +if (housekeeping_enabled (HK_TYPE_KERNEL_NOISE )) +static_branch_enable (&pw_sl);
How likely is it, that you you had users before late_initcall()? Also can it happen that one of them uses one function to lock and the other unlock in this brief window? There is no check if this was used before static_branch usage.
kernel/pwlocks.c line 47
Quoted 5 lines
kernel/pwlocks.c new file +5
-0+ +return 0 ; +} + +late_initcall (pwlocks_init);
Sebastian
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
Quoted 4 lines · Sebastian Andrzej Siewior
How likely is it, that you you had users before
late_initcall()? Also can it happen that one of them uses one function to lock and the other unlock in this brief window? There is no check if this was used beforestatic_branchusage.
Or let alone initialization on the wrong member of the union.
Signature · 2 lines
Frederic Weisbecker SUSE Labs
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Leonardo BrasQuoted 4 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's
mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 10 lines
mm/slub.c +1
-1* not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; } -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs() can be called from kmalloc_nolock()/ NMI context. I don't remember why exactly local_trylock_t was introduced here instead of a per-CPU spinlock_t. But there should be nothing wrong with a trylock on it from NMI as you do here.
One thing worth noting, on !PREEMPT_RT, spin_trylock() always succeeds on UP. kmalloc_nolock() checks for it, not sure about other callers.
Sebastian
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 3/4] swap: apply new pw_queue_on() interface
In reply to Leonardo BrasOn 2026-05-18 22:27:49 [-0300], Leonardo Bras wrote:
after digesting the slub patch,
Quoted 26 lines
mm/swap.c +3
-3@@ -882,38 +879,38 @@ static inline void __lru_add_drain_all(bool force_all_cpus)* If the paired barrier is done at any later step, e.g. after the * loop, CPU #x will just exit at (C) and miss flushing out all of its * added pages. */ WRITE_ONCE (lru_drain_gen, lru_drain_gen +1 );smp_mb ();cpumask_clear (&has_mm_work);cpumask_clear (&has_bh_work);for_each_online_cpu (cpu) { -struct work_struct *mm_work = &per_cpu (lru_add_drain_work, cpu); +struct pw_struct *mm_pw = &per_cpu (lru_add_drain_pw, cpu);struct work_struct *bh_work = &per_cpu (bh_add_drain_work, cpu);if (cpu_needs_mm_drain (cpu)) { -INIT_WORK (mm_work, lru_add_drain_per_cpu); -queue_work_on (cpu, mm_percpu_wq, mm_work); +INIT_PW (mm_pw, lru_add_drain_per_cpu, cpu); +pw_queue_on (cpu, mm_percpu_wq, mm_pw);__cpumask_set_cpu (cpu, &has_mm_work); }if (cpu_needs_bh_drain (cpu)) {INIT_WORK (bh_work, bh_add_drain_per_cpu);queue_work_on (cpu, mm_percpu_wq, bh_work);
mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 909
Quoted 10 lines
mm/swap.c +1
-1__cpumask_set_cpu (cpu, &has_bh_work); } }for_each_cpu (cpu, &has_mm_work) -flush_work (&per_cpu (lru_add_drain_work, cpu)); +pw_flush (&per_cpu (lru_add_drain_pw, cpu));for_each_cpu (cpu, &has_bh_work)flush_work (&per_cpu (bh_add_drain_work, cpu));
Why do we have two iterations here? Is it just a proof of concept that is not complete yet? I am curious why it is okay/needed to "remove" the one workqueue but not the other. Maybe the other does not bother as much as the other does.
But essentially we can't use a spin_lock_t here because due to the hotpath nature of the code it will kill performance. So instead we do it anyway but behind a switch so that only those suffer from this that do not want to suffer from workqueue interruption on a NOHZ full system, right?
I thought that this improved since commit
ff042f4a9b050 ("mm: lru_cache_disable: replace work queue synchronization with synchronize_rcu")
Did it get worse or was it not entirely gone?
mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 916
Quoted 6 lines
mm/swap.c +0
-0done:mutex_unlock (&lock); }void lru_add_drain_all (void ) {
Sebastian
Randy Dunlap
reviewerRe: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Leonardo BrasQuoted 11 lines · Leonardo Bras
MAINTAINERS +0
-0@@ -21775,20 +21775,27 @@ QORIQ DPAA2 FSL-MC BUS DRIVER M: Ioana Ciornei <[email protected]> L: [email protected] L: [email protected] S: Maintained F: Documentation/ABI/stable/sysfs-bus-fsl-mc F: Documentation/devicetree/bindings/misc/fsl,qoriq-mc.yaml
MAINTAINERS QORIQ DPAA2 FSL-MC BUS DRIVER · line 21790
Quoted 10 lines
MAINTAINERS +6
-0F: Documentation/networking/device_drivers/ethernet/freescale/dpaa2/overview.rst F: drivers/bus/fsl-mc/ F: include/uapi/linux/fsl_mc.h +PW Locks +M: Leonardo Bras <[email protected]> +S: Supported +F: Documentation/locking/pwlocks.rst +F: include/linux/pwlocks.h +F: kernel/pwlocks.c
MAINTAINERS entries should be in alphabetical order: PW is not in the middle of the Q entries.
Quoted 19 lines
MAINTAINERS +1
-0+ QT1010 MEDIA DRIVER L: [email protected] S: Orphan W: https://linuxtv.org Q: http://patchwork.linuxtv.org/project/linux-media/list/ F: drivers/media/tuners/qt1010* QUALCOMM ATH12K WIRELESS DRIVER M: Jeff Johnson <[email protected]> L: [email protected]Documentation/locking/pwlocks.rst new file +0
-0
Documentation/locking/pwlocks.rst line 5
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +=========
Overline and underline should be at least as long as the heading text.
Documentation/locking/pwlocks.rst line 9
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+ +Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low since
on a target CPU.
Documentation/locking/pwlocks.rst line 15
Quoted 6 lines
Documentation/locking/pwlocks.rst new file +6
-0+cacheline tends to be mostly local, and avoids the cost of locks in non-RT +kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem: +scheduling work on remote cpu that are executing low latency tasks
CPUs
Documentation/locking/pwlocks.rst line 16
Quoted 1 line
Documentation/locking/pwlocks.rst new file +1
-0+is undesired and can introduce unexpected deadline misses.
undesirable
?
Documentation/locking/pwlocks.rst line 20
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+ +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks.
to spinlocks.
Quoted 36 lines
Documentation/locking/pwlocks.rst new file +36
-0+ +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter). + +The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter. + +For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter): + - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock + - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_work + +* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock
Documentation/locking/pwlocks.rst line 66
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock + - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock + - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty + +pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu.
CPU.
Documentation/locking/pwlocks.rst line 76
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must only +be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead. + +These should only be used when accessing per-CPU data of the local CPU. +
Running "make htmldocs" with this patch says:
Documentation/locking/pwlocks.rst: WARNING: document isn't included in any toctree [toc.not_included]
Quoted 15 lines
init/Kconfig +0
-0@@ -764,20 +764,55 @@ config CPU_ISOLATION depends on SMP default y help Make sure that CPUs running critical tasks are not disturbed by any source of "noise" such as unbound workqueues, timers, kthreads... Unbound jobs get offloaded to housekeeping CPUs. This is driven by the "isolcpus=" boot parameter. Say Y if unsure.
init/Kconfig config CPU_ISOLATION · line 783
Quoted 10 lines
init/Kconfig +10
-0+config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST + default n + help + Allow changing the behavior on per-CPU resource sharing with cache, + from the regular local_locks() + queue_work_on(remote_cpu) to using + per-CPU spinlocks on both local and remote operations. + + This is useful to give user the option on reducing IPIs to CPUs, and
to give the user
init/Kconfig config CPU_ISOLATION · line 788
Quoted 5 lines
init/Kconfig +5
-0+ thus reduce interruptions and context switches. On the other hand, it + increases generated code and will use atomic operations if spinlocks + are selected. + + If set, will use the default behavior set in PWLOCKS_DEFAULT unless boot
unless the boot
Quoted 9 lines
init/Kconfig +9
-0+ parameter pwlocks is passed with a different behavior. + + If unset, will use the local_lock() + queue_work_on() strategy, + regardless of the boot parameter or PWLOCKS_DEFAULT. + + Say N if unsure. + +config PWLOCKS_DEFAULT + bool "Use per-CPU spinlocks by default on PWLOCKS"
init/Kconfig config CPU_ISOLATION · line 807
Quoted 10 lines
init/Kconfig +10
-0+ depends on PWLOCKS + default n + help + If set, will use per-CPU spinlocks as default behavior for per-CPU + remote operations. + + If unset, will use local_lock() + queue_work_on(cpu) as default + behavior for remote operations. + + Say N if unsure
unsure.
Quoted 11 lines
init/Kconfig +1
-0+ source "kernel/rcu/Kconfig" config IKCONFIG tristate "Kernel .config support" help This option enables the complete Linux kernel ".config" file contents to be saved in the kernel. It provides documentation of which kernel options are used in a running kernel or in an on-disk kernel. This information can be extracted from the kernel image file with the script scripts/extract-ikconfig and used as
Signature · 1 line
~Randy
Jonathan Corbet
maintainerRe: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Leonardo BrasQuoted 6 lines · Leonardo Bras
Some places in the kernel implement a parallel programming strategy consisting on
local_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
A couple of documentation-related nits:
Quoted 4 lines
--- MAINTAINERS | 7 + .../admin-guide/kernel-parameters.txt | 10 + Documentation/locking/pwlocks.rst | 76 +++++
You have added a new RST file here, but haven't added it to the table of contents in index.rst. So it won't be part of the docs build.
Quoted 8 lines
init/Kconfig | 35 +++ kernel/Makefile | 2 + include/linux/pwlocks.h | 265 ++++++++++++++++++ kernel/pwlocks.c | 47 ++++ 7 files changed, 442 insertions(+) create mode 100644 Documentation/locking/pwlocks.rst create mode 100644 include/linux/pwlocks.h create mode 100644 kernel/pwlocks.c
[...]
Documentation/locking/pwlocks.rst line 5
Quoted 11 lines
Documentation/locking/pwlocks.rst new file +5
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +=========
The over/underlines should match the text in length.
Quoted 9 lines
Documentation/locking/pwlocks.rst new file +9
-0+Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low since +cacheline tends to be mostly local, and avoids the cost of locks in non-RT +kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem: +scheduling work on remote cpu that are executing low latency tasks
Documentation/locking/pwlocks.rst line 25
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+is undesired and can introduce unexpected deadline misses. + +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks. + +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter).
Did you want that to be an itemized list? If so, put "- " in front of each line.
Quoted 15 lines
Documentation/locking/pwlocks.rst new file +15
-0+The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter. + +For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter):
Documentation/locking/pwlocks.rst line 51
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock + - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_work
This will not render the way you expect it to. You want a literal block ere. So end the text with "...is as follows::" and indent the entire literal block.
Quoted 7 lines
Documentation/locking/pwlocks.rst new file +7
-0+* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock + - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock
Documentation/locking/pwlocks.rst line 69
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock + - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty + +pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu. + +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must only
If you write functions like pw_lock_local(), you'll get automatic cross links to the kerneldoc documentation ... which I'm sure must exist ...
Quoted 7 lines
Documentation/locking/pwlocks.rst new file +7
-0+be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead. + +These should only be used when accessing per-CPU data of the local CPU. +
[...]
Quoted 24 lines
Documentation/locking/pwlocks.rst new file +24
-0+#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE(CONFIG_PWLOCKS_DEFAULT, pw_sl); + +typedef union { + spinlock_t sl; + local_lock_t ll; +} pw_lock_t; + +typedef union { + spinlock_t sl; + local_trylock_t ll; +} pw_trylock_t; + +struct pw_struct { + struct work_struct work; + int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT +#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disable
Documentation/locking/pwlocks.rst
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+#define preempt_or_migrate_enable preempt_enable +#endif + +#define pw_lock_init(lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0)
Sigh, I guess I was over-optimistic about kerneldoc comments.
Is there a reason why these aren't inline functions?
Thanks,
jon
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Leonardo Bras
The problem:
Some places in the kernel implement a parallel programming strategy consisting onlocal_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
Quoted 6 lines
Quoted 6 lines
On the other hand, for RT workloads this can represent a problem: getting an important workload scheduled out to deal with remote requests is sure to introduce unexpected deadline misses.
The idea:
Currently with PREEMPT_RT=y,local_locks()become per-cpu spinlocks.
Hi Sebastian, thank you for reviewing!
(Sorry for the delay)
Quoted 1 line
It does not become a
_spin_lockbecause it does not spin. It sleeps.
Right, it's a per-cpu mutex.
My point is that it's a full lock, and we could use it instead of doing the whole scheduling thing, since we are already paying the 'atomic overhead' to get the lock here.
Quoted 8 lines
Quoted 5 lines
In this case, instead of scheduling work on a remote cpu, it should be safe to grab that remote cpu's per-cpu spinlock and run the required work locally. That major cost, which is un/locking in every local function, already happens in
PREEMPT_RT.We did have this before but only in the RT tree. It was a bit messy from the naming because it started with
local_but then it was a remote CPU.
Had the same naming issue here. This idea was initially a expansion to local_lock() mechanism, about the same way you were planning in the past.
Quoted 5 lines
The main issue was the different code path which led to a few deadlocks back then.
By the timelocal_lock_twent upstream, the cross-CPU locking was removed. As far as I remember, the cross-CPU user which did schedule work on a remote CPU and annoyed NOHZ folks were replaced.
I understand this could be a big issue if used in a generic way.
What I am proposing here a mechanism that standardizes those local_lock()+IPI strategies based on how they are done today, so we are only explected to get 'remote-cpu' pwlocks in the 'IPI replacement' operations.
The idea is pwlock_local* in every local function, and pwlock*(,cpu) in operations that can be remote.
Maybe being used in a more constrained way, it has less chance of being an issue. Also, the whole idea is to improve CPU isolation numbers by reducing IPIs, so maybe NOHZ people will be happier with that :)
Thanks again!
Leo
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Frederic WeisbeckerQuoted 55 lines · Frederic Weisbecker
Quoted 54 lines · Leonardo Bras
Some places in the kernel implement a parallel programming strategy consisting on
local_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.On the other hand, for RT workloads this can represent a problem:
scheduling work on remote cpu that are executing low latency tasks is undesired and can introduce unexpected deadline misses.It's interesting, though, that local_lock()s in RT kernels become
spinlock(). We can make use of those to avoid scheduling work on a remote cpu by directly updating another cpu'sper_cpustructure, while holding it'sspinlock().In order to do that, it's necessary to introduce a new set of functions to make it possible to get another cpu's per-cpu "local" lock (pw_{un,}lock*) and also do the corresponding queueing (
pw_queue_on()) and flushing (pw_flush()) helpers to run the remote work.Users of non-RT kernels but with low latency requirements can select similar functionality by using the
CONFIG_PWLOCKScompile time option.On
CONFIG_PWLOCKSdisabled kernels, no changes are expected, as every one of the introduced helpers work the exactly same as the current implementation:
pw_{un,}lock*() -> local_{un,}lock*() (ignores cpu parameter)pw_queue_on()->queue_work_on()pw_flush()->flush_work()For PWLOCKS enabled kernels, though, pw_{un,}lock*() will use the extra cpu parameter to select the correct per-cpu structure to work on, and acquire the spinlock for that cpu.
pw_queue_on()will just call the requested function in the current cpu, which will operate in another cpu's per-cpu object. Since thelocal_locks()become spinlock()s in PWLOCKS enabled kernels, we are safe doing that.
pw_flush()then becomes a no-op since no work is actually scheduled on a remote cpu.Some minimal code rework is needed in order to make this mechanism work:
The calls for local_{un,}lock*() on the functions that are currently scheduled on remote cpus need to be replaced by either pw_{un,}lock_*(), PWLOCKS enabled kernels they can reference a different cpu. It's also necessary to use apw_structinstead of awork_struct, but it just contains a work struct and, inCONFIG_PWLOCKS, the target cpu.This should have almost no impact on non-CONFIG_PWLOCKS kernels: few
this_cpu_ptr()will become per_cpu_ptr(,smp_processor_id()) on non-hotpath functions.
Quoted 8 lines
Quoted 5 lines
On
CONFIG_PWLOCKSkernels, this should avoid deadlines misses by removing scheduling noise.Signed-off-by Leonardo Bras <[email protected]>Signed-off-by Marcelo Tosatti <[email protected]>I like it! Just a few observations:
Hi Frederic, thanks for reviewing!
Quoted 22 lines
Quoted 7 lines
include/linux/pwlocks.h new file +7
-0+#ifndef CONFIG_PWLOCKS + +typedef local_lock_t pw_lock_t; +typedef local_trylock_t pw_trylock_t; + +struct pw_struct { + struct work_struct work;include/linux/pwlocks.h line 25
Quoted 10 lines
include/linux/pwlocks.h new file +10
-0+}; + +#define pw_lock_init (lock) \ + local_lock_init(lock) + +#define pw_trylock_init (lock) \ + local_trylock_init(lock) + +#define pw_lock (lock, cpu) \ + local_lock(lock)For debugging purpose, it would be nice to ensure that in those off-case, cpu is indeed the local one. Basically all the non-local functions, those that take a cpu, should verify:
Quoted 1 line
lockdep_assert(cpu == smp_processor_id())
I see the point, should not be an issue, as non-local functions are not hotpath.
Quoted 68 lines
Quoted 68 lines
include/linux/pwlocks.h new file +67
-0+ +#define pw_lock_local (lock) \ + local_lock(lock) + +#define pw_lock_irqsave (lock, flags, cpu) \ + local_lock_irqsave(lock, flags) + +#define pw_lock_local_irqsave (lock, flags) \ + local_lock_irqsave(lock, flags) + +#define pw_trylock (lock, cpu) \ + local_trylock(lock) + +#define pw_trylock_local (lock) \ + local_trylock(lock) + +#define pw_trylock_irqsave (lock, flags, cpu) \ + local_trylock_irqsave(lock, flags) + +#define pw_unlock (lock, cpu) \ + local_unlock(lock) + +#define pw_unlock_local (lock) \ + local_unlock(lock) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ + local_unlock_irqrestore(lock, flags) + +#define pw_unlock_local_irqrestore (lock, flags) \ + local_unlock_irqrestore(lock, flags) + +#define pw_lockdep_assert_held (lock) \ + lockdep_assert_held(lock) + +#define pw_queue_on (c, wq, pw) \ + queue_work_on(c, wq, &(pw)->work) + +#define pw_flush (pw) \ + flush_work(&(pw)->work) + +#define pw_get_cpu (pw) smp_processor_id() + +#define pw_is_cpu_remote (cpu) (false) + +#define INIT_PW (pw, func, c) \ + INIT_WORK(&(pw)->work, (func)) + +#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); + +typedef union { +spinlock_t sl; +local_lock_t ll; +}pw_lock_t ; + +typedef union { +spinlock_t sl; +local_trylock_t ll; +}pw_trylock_t ; + +struct pw_struct { +struct work_struct work; +int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT
Quoted 9 lines
include/linux/pwlocks.h line 97
Quoted 5 lines
include/linux/pwlocks.h new file +5
-0+#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disable +#define preempt_or_migrate_enable preempt_enableThis can be no-op in !CONFIG_PREEMPT_RT because non-rt spinlocks disable preemption already.
I remember this was useful because we were getting the per-cpu variable before we preempt_disable in the lock code, and that could be bad if we get preempted after getting the per-cpu variable, and migrated to another core.
This is supposed to cost very little, because it's planned to match whatever is inside the lock, which would just be incrementing a nesting counter.
Quoted 12 lines
include/linux/pwlocks.h line 106
Quoted 9 lines
include/linux/pwlocks.h new file +9
-0+#endif + +#define pw_lock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0)It looks like all these macros could be inline functions.
I think the one with irqsave does not :(
So I ended up staying with defines all around.
Do you suggest replacing all except the irqsave ones with inline functions? (I tried that, and the macro version looked easier to read. I have no issue reusing the inline version again)
Quoted 5 lines
Quoted 5 lines
include/linux/pwlocks.h new file +5
-0+ +#define pw_trylock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \
Quoted 9 lines
Quoted 6 lines
include/linux/pwlocks.h new file +5
-0+ else \ + local_trylock_init(lock.ll); \ +}while (0 ) + +#define pw_lock(lock, cpu)\
And those could have the same local CPU debug check.
Sure
Quoted 62 lines
Quoted 53 lines
include/linux/pwlocks.h new file +53
-0+do { \ +if (static_branch_maybe (CONFIG_PWLOCKS_DEFAULT , &pw_sl)) \ +spin_lock (per_cpu_ptr (lock.sl, cpu)); \ +else \ +local_lock (lock.ll); \ +} while (0) + +#define pw_lock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock(this_cpu_ptr(lock.sl)); \ + } else { \ + local_lock(lock.ll); \ + } \ +} while (0) + +#define pw_lock_irqsave (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_lock_irqsave(lock.ll, flags); \ +} while (0) + +#define pw_lock_local_irqsave (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock_irqsave(this_cpu_ptr(lock.sl), flags); \ + } else { \ + local_lock_irqsave(lock.ll, flags); \ + } \ +} while (0) + +#define pw_trylock (lock, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + t = local_trylock(lock.ll); \ + t; \ +}) + +#define pw_trylock_local (lock) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + t = spin_trylock(this_cpu_ptr(lock.sl)); \ + if (!t) \ + preempt_or_migrate_enable();Quoted 1 line
\
This is duplicating the RT logic in
local_lock_internal.hand it would be tempting to proposespin_local_lock_tthat both pw and RTlocal_lockcould rely upon. But I'm afraid that would create a less readable result:- we would need to check the
CONFIG_PREEMPT_RTthere before doing the migrate_disable/enable
Quoted 8 lines
- RT local lock don't take the lock on IRQ/NMI, which is fine as pw is not expected to be used on the non-threaded parts of IRQs not NMIs. Still that's one more conditional to add there.
- we'll need to differenciate local/remote operations.
Well let's stick to what you did for now (Peter might have a different opinion though).
I get your point. I will take a look on that.
Quoted 143 lines
Quoted 143 lines
include/linux/pwlocks.h new file +96
-0+ } else { \ + t =local_trylock (lock.ll); \ + } \ + t; \ +}) + +#define pw_trylock_irqsave (lock, flags, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + t = local_trylock_irqsave(lock.ll, flags); \ + t; \ +}) + +#define pw_unlock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_unlock(lock.ll); \ +} while (0) + +#define pw_unlock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock(this_cpu_ptr(lock.sl)); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock(lock.ll); \ + } \ +} while (0) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock_irqrestore(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_unlock_irqrestore(lock.ll, flags); \ +} while (0) + +#define pw_unlock_local_irqrestore (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock_irqrestore(this_cpu_ptr(lock.sl), flags); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock_irqrestore(lock.ll, flags); \ + } \ +} while (0) + +#define pw_lockdep_assert_held (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + lockdep_assert_held(this_cpu_ptr(lock.sl)); \ + else \ + lockdep_assert_held(this_cpu_ptr(lock.ll)); \ +} while (0) + +#define pw_queue_on (c, wq, pw) \ +do { \ + int __c = c; \ + struct pw_struct *__pw = (pw); \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + WARN_ON((__c) != __pw->cpu); \ + __pw->work.func(&__pw->work); \ + } else { \ + queue_work_on(__c, wq, &(__pw)->work); \ + } \ +} while (0) + +/* +* Does nothing if PWLOCKS is set to use spinlock, as the task is already done at the +* time pw_queue_on() returns. +*/ +#define pw_flush (pw) \ +do { \ + struct pw_struct *__pw = (pw); \ + if (!static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + flush_work(&__pw->work); \ +} while (0) + +#define pw_get_cpu (w) container_of((w), struct pw_struct, work)->cpu + +#define pw_is_cpu_remote (cpu) ((cpu) != smp_processor_id()) + +#define INIT_PW (pw, func, c) \ +do { \ + struct pw_struct *__pw = (pw); \ + INIT_WORK(&__pw->work, (func)); \ + __pw->cpu = (c); \ +} while (0) + +#endif /* CONFIG_PWLOCKS */ +#endif /* LINUX_PWLOCKS_H */ kernel/pwlocks.c new file +41
-0@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "linux/export.h" +#include <linux/sched.h> +#include <linux/pwlocks.h> +#include <linux/string.h> +#include <linux/sched/isolation.h> + +DEFINE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); +EXPORT_SYMBOL (pw_sl); + +static bool pwlocks_param_specified; + +static int __init pwlocks_setup (char *str) +{ +int opt; + +if (!get_option (&str, &opt)) { +pr_warn ("PWLOCKS: invalid pwlocks parameter: %s, ignoring.\n" , str); +return 0 ; + } + +if (opt) +static_branch_enable (&pw_sl); +else +static_branch_disable (&pw_sl); + + pwlocks_param_specified = true; + +return 1 ; +} +__setup ("pwlocks=" , pwlocks_setup); + +/* +* Enable PWLOCKS if CPUs want to avoid kernel noise. +*/ +static int __init pwlocks_init (void ) +{ +if (pwlocks_param_specified) +return 0 ; + +if (housekeeping_enabled (HK_TYPE_KERNEL_NOISE ))
Quoted 9 lines
kernel/pwlocks.c line 47
Quoted 6 lines
kernel/pwlocks.c new file +6
-0+static_branch_enable (&pw_sl); + +return 0 ; +} + +late_initcall (pwlocks_init);That should be a pre-SMP initcall. Otherwise you risk some asymetric calls.
Noted.
Will take a deeper look on that part for next version
Thanks!
Leo
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Sebastian Andrzej SiewiorQuoted 16 lines · Sebastian Andrzej Siewior
Quoted 15 lines · Leonardo Bras
Documentation/admin-guide/kernel-parameters.txt +0
-0@@ -2810,20 +2810,30 @@ Kernel parameters If a queue's affinity mask contains only isolated CPUs then this parameter has no effect on the interrupt routing decision, though interrupts are only delivered when tasks running on those isolated CPUs submit IO. IO submitted on housekeeping CPUs has no influence on those queues. The format of <cpu-list> is described above.
Quoted 14 lines
Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 2829
Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10
-0+ pwlocks= [KNL,SMP] Select a behavior on per-CPU resource sharing + and remote interference mechanism on a kernel built with + CONFIG_PWLOCKS. + Format: { "0" | "1" } + 0 - local_lock() + queue_work_on(remote_cpu) + 1 - spin_lock() for both local and remote operations + + Selecting 1 may be interesting for systems that want + to avoid interruption & context switches from IPIs. +This documentation is supposed to be for an administrator/ user of the system. Exposing him to underlying kernel technique shouldn't happen. It does not explain the users/ outcome so it sounds like best hope.
Noted, will try to improve the explanation to target a user/sysadmin public.
Quoted 22 lines
Quoted 22 lines
Documentation/admin-guide/kernel-parameters.txt +0
-0iucv= [HW,NET] ivrs_ioapic [HW,X86-64] Provide an override to the IOAPIC-ID<->DEVICE-ID mapping provided in the IVRS ACPI table. By default, PCI segment is 0, and can be omitted. For example, to map IOAPIC-ID decimal 10 to PCI segment 0x1 and PCI device 00:14.0, write the parameter as:Documentation/locking/pwlocks.rst new file +5
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +=========
Quoted 9 lines
Documentation/locking/pwlocks.rst line 10
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0+ +Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low since +cacheline tends to be mostly local, and avoids the cost of locks in non-RT
PREEMPT_RTcan be spelled out if you mean it so it is not confused with other meanings of the two letters.
Will do!
Quoted 4 lines
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem:
Quoted 9 lines
Documentation/locking/pwlocks.rst line 20
Quoted 6 lines
Documentation/locking/pwlocks.rst new file +6
-0+scheduling work on remote cpu that are executing low latency tasks +is undesired and can introduce unexpected deadline misses. + +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks.not spinlocks.
If CONFIG_RT=n, and PWLOCKS=1, it becomes a spinlock.
IIUC in PREEMPT_RT=1 spinlocks become mutexes. I get that it does not actually spins, but it should behave as much as a spinlock could in PREEMPT_RT systems, right?
Quoted 8 lines
Documentation/locking/pwlocks.rst line 25
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0+ +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter).If it is a function, it should end with ()
Right, will correct those.
Quoted 4 lines
Documentation/locking/pwlocks.rst line 28
Quoted 2 lines
Documentation/locking/pwlocks.rst new file +2
-0+The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter.CPU.
right
Quoted 41 lines
Quoted 41 lines
Documentation/locking/pwlocks.rst new file +40
-0+For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter): + - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock + - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_work + +* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock + - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock + - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock + - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty + +pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu. + +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must only
Quoted 8 lines
Documentation/locking/pwlocks.rst line 73
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead.Why do you need to either the one or the other? My only guess is that
migrate_disable()is sufficient but you preferpreempt_disable()on !PREEMPT_RT because it is cheaper.
Correct.
One goal of this change is not introduce overheads in the user code, in special on hotpath code. Using preempt_disable in !PREEMPT_RT just nests with the one in the lock, and it becomes almost free
Quoted 18 lines
Quoted 18 lines
Documentation/locking/pwlocks.rst new file +2
-0+These should only be used when accessing per-CPU data of the local CPU. +init/Kconfig +0
-0@@ -764,20 +764,55 @@ config CPU_ISOLATION depends on SMP default y help Make sure that CPUs running critical tasks are not disturbed by any source of "noise" such as unbound workqueues, timers, kthreads... Unbound jobs get offloaded to housekeeping CPUs. This is driven by the "isolcpus=" boot parameter. Say Y if unsure.
Quoted 16 lines
init/Kconfig config CPU_ISOLATION · line 786
Quoted 13 lines
init/Kconfig +13
-0+config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST + default n + help + Allow changing the behavior on per-CPU resource sharing with cache, + from the regular local_locks() + queue_work_on(remote_cpu) to using + per-CPU spinlocks on both local and remote operations. + + This is useful to give user the option on reducing IPIs to CPUs, and + thus reduce interruptions and context switches. On the other hand, it + increases generated code and will use atomic operations if spinlocks + are selected.I think the goal is to avoid scheduling a task on a remote CPU to get something done.
Correct, it makes an isolated CPU less noisy.
Quoted 10 lines
init/Kconfig config CPU_ISOLATION · line 792
Quoted 7 lines
init/Kconfig +6
-0+ + If set, will use the default behavior set in PWLOCKS_DEFAULT unless boot + parameter pwlocks is passed with a different behavior. + + If unset, will use the local_lock() + queue_work_on() strategy, + regardless of the boot parameter or PWLOCKS_DEFAULT.This sounds like it affects the greater kernel.
It should affect only code converted to use pwlocks.
Quoted 8 lines
init/Kconfig config CPU_ISOLATION · line 799
Quoted 6 lines
init/Kconfig +6
-0+ Say N if unsure. + +config PWLOCKS_DEFAULT + bool "Use per-CPU spinlocks by default on PWLOCKS" + depends on PWLOCKS + default nn is default.
You can set PWLOCKS=n, then it compiles out the mechanism. You can set PWLOCKS=y, and then it will use PWLOCKS_DEFAULT + pwlocks command-line argument to decide on using either local_lock+IPI or spinlocks (that become mutexes in PREEMPT_RT=y).
PWLOCKS_DEFAULT is just a way of letting whoever builds the kernel to decide the default mode, while letting the user decide to disable/enable the mechanism on cmdline as desired.
Quoted 52 lines
Quoted 52 lines
init/Kconfig +9
-0+ help + If set, will use per-CPU spinlocks as default behavior for per-CPU + remote operations. + + If unset, will use local_lock() + queue_work_on(cpu) as default + behavior for remote operations. + + Say N if unsure + source "kernel/rcu/Kconfig" config IKCONFIG tristate "Kernel .config support" help This option enables the complete Linux kernel ".config" file contents to be saved in the kernel. It provides documentation of which kernel options are used in a running kernel or in an on-disk kernel. This information can be extracted from the kernel image file with the script scripts/extract-ikconfig and used asinclude/linux/pwlocks.h new file +26
-0@@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PWLOCKS_H +#define _LINUX_PWLOCKS_H + +#include "linux/spinlock.h" +#include "linux/local_lock.h" +#include "linux/workqueue.h" + +#ifndef CONFIG_PWLOCKS + +typedef local_lock_t pw_lock_t ; +typedef local_trylock_t pw_trylock_t ; + +struct pw_struct { +struct work_struct work; +}; + +#define pw_lock_init (lock) \ + local_lock_init(lock) + +#define pw_trylock_init (lock) \ + local_trylock_init(lock) + +#define pw_lock (lock, cpu) \ + local_lock(lock) +
Quoted 9 lines
include/linux/pwlocks.h line 31
Quoted 5 lines
include/linux/pwlocks.h new file +5
-0+#define pw_lock_local (lock) \ + local_lock(lock) + +#define pw_lock_irqsave (lock, flags, cpu) \ + local_lock_irqsave(lock, flags)The part where you have a `cpu' argument which is not used is entirely confusing.
That is how we compile-out when CONFIG_PWLOCKS=n, it has to receive the cpu parameter as the version with CONFIG_PWLOCKS=y below use the cpu parameter to decide where to find the per-cpu data.
Quoted 51 lines
Quoted 51 lines
include/linux/pwlocks.h new file +51
-0+ +#define pw_lock_local_irqsave(lock, flags) \ + local_lock_irqsave(lock, flags) + +#define pw_trylock(lock, cpu) \ + local_trylock(lock) + +#define pw_trylock_local(lock) \ + local_trylock(lock) + +#define pw_trylock_irqsave(lock, flags, cpu) \ + local_trylock_irqsave(lock, flags) + +#define pw_unlock(lock, cpu) \ + local_unlock(lock) + +#define pw_unlock_local(lock) \ + local_unlock(lock) + +#define pw_unlock_irqrestore(lock, flags, cpu) \ + local_unlock_irqrestore(lock, flags) + +#define pw_unlock_local_irqrestore(lock, flags) \ + local_unlock_irqrestore(lock, flags) + +#define pw_lockdep_assert_held(lock) \ + lockdep_assert_held(lock) + +#define pw_queue_on(c, wq, pw) \ + queue_work_on(c, wq, &(pw)->work) + +#define pw_flush(pw) \ + flush_work(&(pw)->work) + +#define pw_get_cpu(pw) smp_processor_id() + +#define pw_is_cpu_remote(cpu) (false) + +#define INIT_PW(pw, func, c) \ + INIT_WORK(&(pw)->work, (func)) + +#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE(CONFIG_PWLOCKS_DEFAULT, pw_sl); + +typedef union { + spinlock_t sl; + local_lock_t ll; +} pw_lock_t; + +typedef union {
Quoted 8 lines
include/linux/pwlocks.h line 85
Quoted 3 lines
include/linux/pwlocks.h new file +3
-0+spinlock_t sl; +local_trylock_t ll; +} pw_trylock_t;Why do you use
local_trylock_t? Its use case is different compared tolocal_lock_t. _IF_ you are fine withlocal_trylock_tthen you should be able to deal with a per-CPUspinlock_tand none of this should be needed.
IIRC there is code that use both local_trylock and and local_lock, so we needed this to be able to convert the user completely.
Quoted 15 lines
include/linux/pwlocks.h line 98
Quoted 13 lines
include/linux/pwlocks.h new file +12
-0+struct pw_struct { +struct work_struct work; +int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT +#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disable +#define preempt_or_migrate_enable preempt_enable +#endif if then () but this looks terrible.
Agree, have to figure a better naming.
Quoted 150 lines
Quoted 150 lines
include/linux/pwlocks.h new file +149
-0+ +#define pw_lock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0) + +#define pw_trylock_init (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_trylock_init(lock.ll); \ +} while (0) + +#define pw_lock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_lock(lock.ll); \ +} while (0) + +#define pw_lock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock(this_cpu_ptr(lock.sl)); \ + } else { \ + local_lock(lock.ll); \ + } \ +} while (0) + +#define pw_lock_irqsave (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_lock_irqsave(lock.ll, flags); \ +} while (0) + +#define pw_lock_local_irqsave (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + spin_lock_irqsave(this_cpu_ptr(lock.sl), flags); \ + } else { \ + local_lock_irqsave(lock.ll, flags); \ + } \ +} while (0) + +#define pw_trylock (lock, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + t = local_trylock(lock.ll); \ + t; \ +}) + +#define pw_trylock_local (lock) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + preempt_or_migrate_disable(); \ + t = spin_trylock(this_cpu_ptr(lock.sl)); \ + if (!t) \ + preempt_or_migrate_enable(); \ + } else { \ + t = local_trylock(lock.ll); \ + } \ + t; \ +}) + +#define pw_trylock_irqsave (lock, flags, cpu) \ +({ \ + int t; \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + t = spin_trylock_irqsave(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + t = local_trylock_irqsave(lock.ll, flags); \ + t; \ +}) + +#define pw_unlock (lock, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock(per_cpu_ptr(lock.sl, cpu)); \ + else \ + local_unlock(lock.ll); \ +} while (0) + +#define pw_unlock_local (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock(this_cpu_ptr(lock.sl)); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock(lock.ll); \ + } \ +} while (0) + +#define pw_unlock_irqrestore (lock, flags, cpu) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_unlock_irqrestore(per_cpu_ptr(lock.sl, cpu), flags); \ + else \ + local_unlock_irqrestore(lock.ll, flags); \ +} while (0) + +#define pw_unlock_local_irqrestore (lock, flags) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + spin_unlock_irqrestore(this_cpu_ptr(lock.sl), flags); \ + preempt_or_migrate_enable(); \ + } else { \ + local_unlock_irqrestore(lock.ll, flags); \ + } \ +} while (0) + +#define pw_lockdep_assert_held (lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + lockdep_assert_held(this_cpu_ptr(lock.sl)); \ + else \ + lockdep_assert_held(this_cpu_ptr(lock.ll)); \ +} while (0) + +#define pw_queue_on (c, wq, pw) \ +do { \ + int __c = c; \ + struct pw_struct *__pw = (pw); \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) { \ + WARN_ON((__c) != __pw->cpu); \ + __pw->work.func(&__pw->work); \ + } else { \ + queue_work_on(__c, wq, &(__pw)->work); \ + } \ +} while (0) + +/* +* Does nothing if PWLOCKS is set to use spinlock, as the task is already done at the +* time pw_queue_on() returns. +*/ +#define pw_flush (pw) \ +do { \
Quoted 9 lines
include/linux/pwlocks.h line 251
Quoted 4 lines
include/linux/pwlocks.h new file +4
-0+struct pw_struct *__pw = (pw); \ +if (!static_branch_maybe (CONFIG_PWLOCKS_DEFAULT , &pw_sl)) \ +flush_work (&__pw->work); \ +}while (0 )I don't think this should be a collection of macros. Either proper functions or static inline _if_ this is performance critical for some reason.
Agree, I remember the only macro that was hard to do so was _irqsave() versions, as it uses an output as a parameter, and I ended up letting all of them being macro due to them looking similar.
Will convert on next version.
(Only the _local*() funcs are performance-critial IIRC.
Quoted 59 lines
Quoted 59 lines
include/linux/pwlocks.h new file +14
-0+ +#define pw_get_cpu (w) container_of((w), struct pw_struct, work)->cpu + +#define pw_is_cpu_remote (cpu) ((cpu) != smp_processor_id()) + +#define INIT_PW (pw, func, c) \ +do { \ + struct pw_struct *__pw = (pw); \ + INIT_WORK(&__pw->work, (func)); \ + __pw->cpu = (c); \ +} while (0) + +#endif /* CONFIG_PWLOCKS */ +#endif /* LINUX_PWLOCKS_H */ kernel/pwlocks.c new file +39
-0@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "linux/export.h" +#include <linux/sched.h> +#include <linux/pwlocks.h> +#include <linux/string.h> +#include <linux/sched/isolation.h> + +DEFINE_STATIC_KEY_MAYBE (CONFIG_PWLOCKS_DEFAULT , pw_sl); +EXPORT_SYMBOL (pw_sl); + +static bool pwlocks_param_specified; + +static int __init pwlocks_setup (char *str) +{ +int opt; + +if (!get_option (&str, &opt)) { +pr_warn ("PWLOCKS: invalid pwlocks parameter: %s, ignoring.\n" , str); +return 0 ; + } + +if (opt) +static_branch_enable (&pw_sl); +else +static_branch_disable (&pw_sl); + + pwlocks_param_specified = true; + +return 1 ; +} +__setup ("pwlocks=" , pwlocks_setup); + +/* +* Enable PWLOCKS if CPUs want to avoid kernel noise. +*/ +static int __init pwlocks_init (void ) +{ +if (pwlocks_param_specified) +return 0 ;
Quoted 9 lines
kernel/pwlocks.c line 42
Quoted 3 lines
kernel/pwlocks.c new file +3
-0+ +if (housekeeping_enabled (HK_TYPE_KERNEL_NOISE )) +static_branch_enable (&pw_sl);How likely is it, that you you had users before
late_initcall()? Also can it happen that one of them uses one function to lock and the other unlock in this brief window? There is no check if this was used beforestatic_branchusage.
I don't really understand that much of initcall :(
That part was done by Marcelo, I have to reach out for him for more understanding of his decision here.
I have to take a better look here :)
Thanks!
Leo
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Frederic WeisbeckerQuoted 7 lines · Frederic Weisbecker
Quoted 4 lines · Sebastian Andrzej Siewior
How likely is it, that you you had users before
late_initcall()? Also can it happen that one of them uses one function to lock and the other unlock in this brief window? There is no check if this was used beforestatic_branchusage.Or let alone initialization on the wrong member of the union.
Right!
Quoted 4 lines
Signature · 3 lines
-- Frederic Weisbecker SUSE Labs
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Randy DunlapQuoted 18 lines · Randy Dunlap
Quoted 15 lines · Leonardo Bras
MAINTAINERS +0
-0@@ -21775,20 +21775,27 @@ QORIQ DPAA2 FSL-MC BUS DRIVER M: Ioana Ciornei <[email protected]> L: [email protected] L: [email protected] S: Maintained F: Documentation/ABI/stable/sysfs-bus-fsl-mc F: Documentation/devicetree/bindings/misc/fsl,qoriq-mc.yaml F: Documentation/networking/device_drivers/ethernet/freescale/dpaa2/overview.rst F: drivers/bus/fsl-mc/ F: include/uapi/linux/fsl_mc.h
Quoted 9 lines
MAINTAINERS QORIQ DPAA2 FSL-MC BUS DRIVER · line 21790
Quoted 6 lines
MAINTAINERS +6
-0+PW Locks +M: Leonardo Bras <[email protected]> +S: Supported +F: Documentation/locking/pwlocks.rst +F: include/linux/pwlocks.h +F: kernel/pwlocks.c
MAINTAINERSentries should be in alphabetical order: PW is not in the middle of the Q entries.
Hi Randy, thank you for reviewing!
Argh, sorry, it used to be called QPW. I renamed and forgot to reorder.
Quoted 14 lines
Quoted 12 lines
MAINTAINERS +1
-0+ QT1010 MEDIA DRIVER L: [email protected] S: Orphan W: https://linuxtv.org Q: http://patchwork.linuxtv.org/project/linux-media/list/ F: drivers/media/tuners/qt1010* QUALCOMM ATH12K WIRELESS DRIVER M: Jeff Johnson <[email protected]> L: [email protected]
Quoted 14 lines
Documentation/locking/pwlocks.rst line 5
Quoted 11 lines
Documentation/locking/pwlocks.rst new file +5
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +=========Overline and underline should be at least as long as the heading text.
Noted!
Quoted 6 lines
Documentation/locking/pwlocks.rst line 9
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+ +Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low sinceon a target CPU.
Right
Quoted 9 lines
Documentation/locking/pwlocks.rst line 15
Quoted 7 lines
Documentation/locking/pwlocks.rst new file +6
-0+cacheline tends to be mostly local, and avoids the cost of locks in non-RT +kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem: +scheduling work on remote cpu that are executing low latency tasksCPUs
Will fix
Quoted 5 lines
Documentation/locking/pwlocks.rst line 16
Quoted 2 lines
Documentation/locking/pwlocks.rst new file +1
-0+is undesired and can introduce unexpected deadline misses.undesirable
?
Yes
Quoted 8 lines
Documentation/locking/pwlocks.rst line 20
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +4
-0+ +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks.to spinlocks.
Right
Quoted 40 lines
Quoted 40 lines
Documentation/locking/pwlocks.rst new file +40
-0+ +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter). + +The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter. + +For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter): + - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock + - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_work + +* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock + - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock + - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock
Quoted 9 lines
Documentation/locking/pwlocks.rst line 66
Quoted 6 lines
Documentation/locking/pwlocks.rst new file +6
-0+ - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty + +pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu.CPU.
Will fix
Quoted 13 lines
Documentation/locking/pwlocks.rst line 76
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+ +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must only +be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead. + +These should only be used when accessing per-CPU data of the local CPU. +Running "make htmldocs" with this patch says:
Quoted 2 lines
Documentation/locking/pwlocks.rst: WARNING: document isn't included in any toctree [toc.not_included]
Humm, I will figure out a way of including it in a relevant toctree.
Quoted 15 lines
Quoted 15 lines
init/Kconfig +0
-0@@ -764,20 +764,55 @@ config CPU_ISOLATION depends on SMP default y help Make sure that CPUs running critical tasks are not disturbed by any source of "noise" such as unbound workqueues, timers, kthreads... Unbound jobs get offloaded to housekeeping CPUs. This is driven by the "isolcpus=" boot parameter. Say Y if unsure.
Quoted 12 lines
init/Kconfig config CPU_ISOLATION · line 783
Quoted 10 lines
init/Kconfig +10
-0+config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST + default n + help + Allow changing the behavior on per-CPU resource sharing with cache, + from the regular local_locks() + queue_work_on(remote_cpu) to using + per-CPU spinlocks on both local and remote operations. + + This is useful to give user the option on reducing IPIs to CPUs, andto give the user
Right
Quoted 9 lines
init/Kconfig config CPU_ISOLATION · line 788
Quoted 6 lines
init/Kconfig +5
-0+ thus reduce interruptions and context switches. On the other hand, it + increases generated code and will use atomic operations if spinlocks + are selected. + + If set, will use the default behavior set in PWLOCKS_DEFAULT unless bootunless the boot
Correct
Quoted 13 lines
Quoted 13 lines
init/Kconfig +13
-0+ parameter pwlocks is passed with a different behavior. + + If unset, will use the local_lock() + queue_work_on() strategy, + regardless of the boot parameter or PWLOCKS_DEFAULT. + + Say N if unsure. + +config PWLOCKS_DEFAULT + bool "Use per-CPU spinlocks by default on PWLOCKS" + depends on PWLOCKS + default n + help + If set, will use per-CPU spinlocks as default behavior for per-CPU
Quoted 9 lines
init/Kconfig config CPU_ISOLATION · line 807
Quoted 6 lines
init/Kconfig +6
-0+ remote operations. + + If unset, will use local_lock() + queue_work_on(cpu) as default + behavior for remote operations. + + Say N if unsureunsure.
Yeap :)
Quoted 15 lines
Quoted 11 lines
init/Kconfig +1
-0+ source "kernel/rcu/Kconfig" config IKCONFIG tristate "Kernel .config support" help This option enables the complete Linux kernel ".config" file contents to be saved in the kernel. It provides documentation of which kernel options are used in a running kernel or in an on-disk kernel. This information can be extracted from the kernel image file with the script scripts/extract-ikconfig and used asSignature · 1 line
~Randy
Thanks!
Leo
Re: [PATCH v4 1/4] Introducing pw_lock() and per-cpu queue & flush work
In reply to Jonathan CorbetQuoted 8 lines · Jonathan Corbet
Quoted 6 lines · Leonardo Bras
Some places in the kernel implement a parallel programming strategy consisting on
local_locks()for most of the work, and some rare remote operations are scheduled on target cpu. This keeps cache bouncing low since cacheline tends to be mostly local, and avoids the cost of locks in non-RT kernels, even though the very few remote operations will be expensive due to scheduling overhead.
Hi Jonathan, thanks for reviewing!
Quoted 10 lines
A couple of documentation-related nits:
Quoted 4 lines
--- MAINTAINERS | 7 + .../admin-guide/kernel-parameters.txt | 10 + Documentation/locking/pwlocks.rst | 76 +++++You have added a new RST file here, but haven't added it to the table of contents in
index.rst. So it won't be part of the docs build.
Ooops, will find a place to add it then :)
This part was added by Marcelo, I have no experience on adding docs to the kernel tree. Your suggestions will be really useful :)
Quoted 12 lines
Quoted 9 lines
init/Kconfig | 35 +++ kernel/Makefile | 2 + include/linux/pwlocks.h | 265 ++++++++++++++++++ kernel/pwlocks.c | 47 ++++ 7 files changed, 442 insertions(+) create mode 100644 Documentation/locking/pwlocks.rst create mode 100644 include/linux/pwlocks.h create mode 100644 kernel/pwlocks.c[...]
Quoted 14 lines
Documentation/locking/pwlocks.rst line 5
Quoted 11 lines
Documentation/locking/pwlocks.rst new file +5
-0@@ -0,0 +1,76 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========= +PW (Per-CPU Work) locks +=========The over/underlines should match the text in length.
Will change!
Quoted 14 lines
Quoted 14 lines
Documentation/locking/pwlocks.rst new file +14
-0+Some places in the kernel implement a parallel programming strategy +consisting on local_locks() for most of the work, and some rare remote +operations are scheduled on target cpu. This keeps cache bouncing low since +cacheline tends to be mostly local, and avoids the cost of locks in non-RT +kernels, even though the very few remote operations will be expensive due +to scheduling overhead. + +On the other hand, for RT workloads this can represent a problem: +scheduling work on remote cpu that are executing low latency tasks +is undesired and can introduce unexpected deadline misses. + +PW locks help to convert sites that use local_locks (for cpu local operations) +and queue_work_on (for queueing work remotely, to be executed +locally on the owner cpu of the lock) to a spinlocks.
Quoted 8 lines
Documentation/locking/pwlocks.rst line 25
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0+ +The lock is declared pw_lock_t type. +The lock is initialized with pw_lock_init. +The lock is locked with pw_lock (takes a lock and cpu as a parameter). +The lock is unlocked with pw_unlock (takes a lock and cpu as a parameter).Did you want that to be an itemized list? If so, put "- " in front of each line.
Oh, good idea!
Quoted 22 lines
Quoted 22 lines
Documentation/locking/pwlocks.rst new file +21
-0+The pw_lock_irqsave function disables interrupts and saves current interrupt state, +cpu as a parameter. + +For trylock variant, there is the pw_trylock_t type, initialized with +pw_trylock_init. Then the corresponding pw_trylock and pw_trylock_irqsave. + +work_struct should be replaced by pw_struct, which contains a cpu parameter +(owner cpu of the lock), initialized by INIT_PW. + +The queue work related functions (analogous to queue_work_on and flush_work) are: +pw_queue_on and pw_flush. + +The behaviour of the PW lock functions is as follows: + +* !CONFIG_PWLOCKS (or CONFIG_PWLOCKS and pwlocks=off kernel boot parameter): + - pw_lock: local_lock + - pw_lock_irqsave: local_lock_irqsave + - pw_trylock: local_trylock + - pw_trylock_irqsave: local_trylock_irqsave + - pw_unlock: local_unlock + - pw_lock_local: local_lock
Quoted 8 lines
Documentation/locking/pwlocks.rst line 51
Quoted 4 lines
Documentation/locking/pwlocks.rst new file +4
-0+ - pw_trylock_local: local_trylock + - pw_unlock_local: local_unlock + - pw_queue_on: queue_work_on + - pw_flush: flush_workThis will not render the way you expect it to. You want a literal block ere. So end the text with "...is as follows::" and indent the entire literal block.
Will do like this!
Quoted 13 lines
Quoted 13 lines
Documentation/locking/pwlocks.rst new file +12
-0+* CONFIG_PWLOCKS (and CONFIG_PWLOCKS_DEFAULT=y or pwlocks=on kernel boot parameter), + - pw_lock: spin_lock + - pw_lock_irqsave: spin_lock_irqsave + - pw_trylock: spin_trylock + - pw_trylock_irqsave: spin_trylock_irqsave + - pw_unlock: spin_unlock + - pw_lock_local: preempt_disable OR migrate_disable + spin_lock + - pw_trylock_local: preempt_disable OR migrate_disable + spin_trylock + - pw_unlock_local: preempt_enable OR migrate_enable + spin_unlock + - pw_queue_on: executes work function on caller cpu + - pw_flush: empty +
Quoted 8 lines
Documentation/locking/pwlocks.rst line 69
Quoted 5 lines
Documentation/locking/pwlocks.rst new file +5
-0+pw_get_cpu(work_struct), to be called from within per-cpu work function, +returns the target cpu. + +On the locking functions above, there are the local locking functions +(pw_lock_local, pw_trylock_local and pw_unlock_local) that must onlyIf you write functions like
pw_lock_local(), you'll get automatic cross links to the kerneldoc documentation ... which I'm sure must exist ...
They will be added at this patch, so I will add the () so the link works :)
Quoted 48 lines
Quoted 8 lines
Documentation/locking/pwlocks.rst new file +7
-0+be used to access per-CPU data from the CPU that owns that data, +and never remotely. They disable preemption/migration and don't require +a cpu parameter, making them a replacement for local_lock functions that +does not introduce overhead. + +These should only be used when accessing per-CPU data of the local CPU. +[...]
Quoted 24 lines
Documentation/locking/pwlocks.rst new file +24
-0+#else /* CONFIG_PWLOCKS */ + +DECLARE_STATIC_KEY_MAYBE(CONFIG_PWLOCKS_DEFAULT, pw_sl); + +typedef union { + spinlock_t sl; + local_lock_t ll; +} pw_lock_t; + +typedef union { + spinlock_t sl; + local_trylock_t ll; +} pw_trylock_t; + +struct pw_struct { + struct work_struct work; + int cpu; +}; + +#ifdef CONFIG_PREEMPT_RT +#define preempt_or_migrate_disable migrate_disable +#define preempt_or_migrate_enable migrate_enable +#else +#define preempt_or_migrate_disable preempt_disableDocumentation/locking/pwlocks.rst
Quoted 10 lines
Documentation/locking/pwlocks.rst new file +10
-0+#define preempt_or_migrate_enable preempt_enable +#endif + +#define pw_lock_init(lock) \ +do { \ + if (static_branch_maybe(CONFIG_PWLOCKS_DEFAULT, &pw_sl)) \ + spin_lock_init(lock.sl); \ + else \ + local_lock_init(lock.ll); \ +} while (0)Sigh, I guess I was over-optimistic about kerneldoc comments.
Quoted 2 lines
Is there a reason why these aren't inline functions?
I could not find a way of making *_irqsave() an inline function, so ended up using macros as they looked simple enough (and the underlying functions would take care of type check). Already working on converting as many of the above as possible.
Thanks again for reviewing!
Leo
Re: [PATCH v4 3/4] swap: apply new pw_queue_on() interface
In reply to Sebastian Andrzej SiewiorQuoted 38 lines · Sebastian Andrzej Siewior
On 2026-05-18 22:27:49 [-0300], Leonardo Bras wrote:
after digesting the slub patch,
Quoted 34 lines
mm/swap.c +4
-4@@ -882,38 +879,38 @@ static inline void __lru_add_drain_all(bool force_all_cpus)* If the paired barrier is done at any later step, e.g. after the * loop, CPU #x will just exit at (C) and miss flushing out all of its * added pages. */ WRITE_ONCE (lru_drain_gen, lru_drain_gen +1 );smp_mb ();cpumask_clear (&has_mm_work);cpumask_clear (&has_bh_work);for_each_online_cpu (cpu) { -struct work_struct *mm_work = &per_cpu (lru_add_drain_work, cpu); +struct pw_struct *mm_pw = &per_cpu (lru_add_drain_pw, cpu);struct work_struct *bh_work = &per_cpu (bh_add_drain_work, cpu);if (cpu_needs_mm_drain (cpu)) { -INIT_WORK (mm_work, lru_add_drain_per_cpu); -queue_work_on (cpu, mm_percpu_wq, mm_work); +INIT_PW (mm_pw, lru_add_drain_per_cpu, cpu); +pw_queue_on (cpu, mm_percpu_wq, mm_pw);__cpumask_set_cpu (cpu, &has_mm_work); }if (cpu_needs_bh_drain (cpu)) {INIT_WORK (bh_work, bh_add_drain_per_cpu);queue_work_on (cpu, mm_percpu_wq, bh_work);__cpumask_set_cpu (cpu, &has_bh_work); } }for_each_cpu (cpu, &has_mm_work) -flush_work (&per_cpu (lru_add_drain_work, cpu)); +pw_flush (&per_cpu (lru_add_drain_pw, cpu));
Quoted 7 lines
mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 909
Quoted 2 lines
mm/swap.c +0
-0for_each_cpu (cpu, &has_bh_work)flush_work (&per_cpu (bh_add_drain_work, cpu));Why do we have two iterations here? Is it just a proof of concept that is not complete yet? I am curious why it is okay/needed to "remove" the one workqueue but not the other. Maybe the other does not bother as much as the other does.
Argh, sorry about the above :/
Slub was converted on a previous patchset version, and rebasing worked fine. I should have properly checked if any other mechanism was added in the file, but ended up just checking the one that was already converted.
Quoted 3 lines
But essentially we can't use a
spin_lock_there because due to the hotpath nature of the code it will kill performance.
Correct
Quoted 4 lines
So instead we do it
anyway but behind a switch so that only those suffer from this that do not want to suffer from workqueue interruption on a NOHZ full system, right?
Yes, one can choose to pay the price to use spinlocks here and have less workqueue interruptions, if !CONFIG_PREEMPT_RT
In case of CONFIG_PREEMPT_RT, we have the local_lock() already becoming a rt_spinlock(), so it already pays the price to hold the lock. So it should be basically free on this scenario, and save time by doing the operation remotelly instead of scheduling it.
Quoted 6 lines
I thought that this improved since commit
ff042f4a9b050("mm:lru_cache_disable: replace work queue synchronization withsynchronize_rcu")Did it get worse or was it not entirely gone?
I worked in this patchset majorly after that commit date, and it was still an issue up to last time Marcelo tested. Not sure of the impact of above commit, but I suppose it may have brought some improvements without fully fixing it.
Thanks!
Leo
Re: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Sebastian Andrzej SiewiorQuoted 12 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's * not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; }
Quoted 7 lines
mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 3 lines
mm/slub.c +1
-1-if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.
Probably to save the cost of using atomic operations on locking, and having about the same restrictions that would allow using local_locks
Quoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.
Awesome!
Quoted 3 lines
One thing worth noting, on !PREEMPT_RT,
spin_trylock()always succeeds on UP.kmalloc_nolock()checks for it, not sure about other callers.
Sorry, I did not sure I understand that part.
You mean we have since it always returns true, we may be in NMI context, after it was interrupted holding this lock, and it will return true which will use the protected area even though the lock should avoid it?
Humm, but if that scenario exist, then is it actually ok to return true on trylock() in that scenario?
Thanks!
Leo
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 3/4] swap: apply new pw_queue_on() interface
In reply to Leonardo BrasQuoted 10 lines · Leonardo Bras
Quoted 5 lines
I thought that this improved since commit
ff042f4a9b050("mm:lru_cache_disable: replace work queue synchronization withsynchronize_rcu")Did it get worse or was it not entirely gone?
I worked in this patchset majorly after that commit date, and it was still an issue up to last time Marcelo tested. Not sure of the impact of above commit, but I suppose it may have brought some improvements without fully fixing it.
It would be good to know what is still missing and maybe it can be addressed without introducing this remote locking.
Quoted 2 lines
Thanks!
Leo
Sebastian
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Leonardo BrasQuoted 21 lines · Leonardo Bras
Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's * not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; }Quoted 7 lines
mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 3 lines
mm/slub.c +1
-1-if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.
Quoted 7 lines
Probably to save the cost of using atomic operations on locking, and having about the same restrictions that would allow using
local_locksQuoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.Awesome!
The problem is always the unlock which requires full locking and is usually the problem from NMI.
Quoted 9 lines
Quoted 3 lines
One thing worth noting, on !PREEMPT_RT,
spin_trylock()always succeeds on UP.kmalloc_nolock()checks for it, not sure about other callers.Sorry, I did not sure I understand that part.
You mean we have since it always returns true, we may be in NMI context, after it was interrupted holding this lock, and it will return true which will use the protected area even though the lock should avoid it?
from include/linux/spinlock_api_up.h:
| static __always_inline int _raw_spin_trylock(raw_spinlock_t *lock)
| __cond_acquires(true, lock)
| {
| __LOCK(lock);
| return 1;
| }on UP a spin_trylock() always succeeds.
Quoted 5 lines
Humm, but if that scenario exist, then is it actually ok to return true on
trylock()in that scenario?Thanks!
Leo
Sebastian
Sebastian Andrzej Siewior
maintainerQuoted 6 lines · Leonardo Bras
Quoted 2 lines
Quoted 2 lines
The idea:
Currently with PREEMPT_RT=y,local_locks()become per-cpu spinlocks.Hi Sebastian, thank you for reviewing!
(Sorry for the delay)
Quoted 6 lines
Quoted 1 line
It does not become a
_spin_lockbecause it does not spin. It sleeps.Right, it's a per-cpu mutex.
My point is that it's a full lock, and we could use it instead of doing the whole scheduling thing, since we are already paying the 'atomic overhead' to get the lock here.
The whole lock is a spinlock_t. There is also raw_spinlock_t and bit_spin_lock(). All three are considered spinlocks.
Quoted 24 lines
Quoted 7 lines
Quoted 4 lines
In this case, instead of scheduling work on a remote cpu, it should be safe to grab that remote cpu's per-cpu spinlock and run the required work locally. That major cost, which is un/locking in every local function, already happens in
PREEMPT_RT.We did have this before but only in the RT tree. It was a bit messy from the naming because it started with
local_but then it was a remote CPU.Had the same naming issue here. This idea was initially a expansion to
local_lock()mechanism, about the same way you were planning in the past.Quoted 5 lines
The main issue was the different code path which led to a few deadlocks back then.
By the timelocal_lock_twent upstream, the cross-CPU locking was removed. As far as I remember, the cross-CPU user which did schedule work on a remote CPU and annoyed NOHZ folks were replaced.I understand this could be a big issue if used in a generic way.
What I am proposing here a mechanism that standardizes those local_lock()+IPI strategies based on how they are done today, so we are only explected to get 'remote-cpu' pwlocks in the 'IPI replacement' operations.
Quoted 6 lines
The idea is pwlock_local* in every local function, and pwlock*(,cpu) in operations that can be remote.
Maybe being used in a more constrained way, it has less chance of being an issue. Also, the whole idea is to improve CPU isolation numbers by reducing IPIs, so maybe NOHZ people will be happier with that :)
I get that part. The local_lock_t part is cheap on !RT and becomes a full lock on RT. While the lock details change the overall expectation remain the same. With this change it is possible to acquire the lock cross-CPU but this depends on the config/ setup. This might not be easy in terms of testing and maintenance.
The more potential users you have, the better it might become in terms of a selling argument. If you have just (say) two users it might be simpler to address just those.
Quoted 2 lines
Thanks again!
Leo
Sebastian
Vlastimil Babka (SUSE)
maintainerRe: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Sebastian Andrzej SiewiorQuoted 22 lines · Sebastian Andrzej Siewior
Quoted 21 lines · Leonardo Bras
Quoted 19 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 10 lines
mm/slub.c +1
-1* not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; } -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.
Quoted 1 line
Quoted 1 line
Probably to save the cost of using atomic operations on locking, and having
Yes. Also function call overhead as spinlocks are often not inlined.
Quoted 1 line
Quoted 1 line
about the same restrictions that would allow using
local_locks
Indeed.
Quoted 19 lines
Quoted 4 lines
Quoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.Awesome!
The problem is always the unlock which requires full locking and is usually the problem from NMI.
Quoted 9 lines
Quoted 3 lines
One thing worth noting, on !PREEMPT_RT,
spin_trylock()always succeeds on UP.kmalloc_nolock()checks for it, not sure about other callers.Sorry, I did not sure I understand that part.
You mean we have since it always returns true, we may be in NMI context, after it was interrupted holding this lock, and it will return true which will use the protected area even though the lock should avoid it?from
include/linux/spinlock_api_up.h:
Quoted 8 lines
| static __always_inline int _raw_spin_trylock(raw_spinlock_t *lock) | __cond_acquires(true, lock) | { | __LOCK(lock); | return 1; | }on UP a
spin_trylock()always succeeds.
Indeed. This was a problem in the page allocator, because there we don't even disable irqs, so it wasn't just nmi, but an irq that could get a false-positive spin_trylock(). See 038a102535eb ("mm/page_alloc: prevent pcp corruption with SMP=n") which was a hot fix.
This was later cleaned up with 3 commits starting with a373f371166d ("mm/page_alloc: effectively disable pcp with CONFIG_SMP=n").
Since here you're also replacing local_trylock() (with no _irqsave) with effectively spin_trylock(), the problem also won't be limited to NMIs and thus the checks in kmalloc_nolock() won't help.
But I see in Patch 1:
+config PWLOCKS
+ bool "Per-CPU Work locks"
+ depends on SMP || COMPILE_TEST
So that's basically avoiding the problem in the same way as the page allocator after the clean up.
Maybe just remove the COMPILE_TEST part? It's not clear to me what it achieves. Just make it require SMP as there's no point for this on !SMP.
Quoted 7 lines
Quoted 5 lines
Humm, but if that scenario exist, then is it actually ok to return true on
trylock()in that scenario?Thanks!
LeoSebastian
Quoted 48 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Leonardo Bras
Quoted 2 lines
Quoted 2 lines
The idea:
Currently with PREEMPT_RT=y,local_locks()become per-cpu spinlocks.Hi Sebastian, thank you for reviewing!
(Sorry for the delay)Quoted 6 lines
Quoted 1 line
It does not become a
_spin_lockbecause it does not spin. It sleeps.Right, it's a per-cpu mutex.
My point is that it's a full lock, and we could use it instead of doing the whole scheduling thing, since we are already paying the 'atomic overhead' to get the lock here.The whole lock is a
spinlock_t. There is alsoraw_spinlock_tandbit_spin_lock(). All three are considered spinlocks.Quoted 24 lines
Quoted 7 lines
Quoted 4 lines
In this case, instead of scheduling work on a remote cpu, it should be safe to grab that remote cpu's per-cpu spinlock and run the required work locally. That major cost, which is un/locking in every local function, already happens in
PREEMPT_RT.We did have this before but only in the RT tree. It was a bit messy from the naming because it started with
local_but then it was a remote CPU.Had the same naming issue here. This idea was initially a expansion to
local_lock()mechanism, about the same way you were planning in the past.Quoted 5 lines
The main issue was the different code path which led to a few deadlocks back then.
By the timelocal_lock_twent upstream, the cross-CPU locking was removed. As far as I remember, the cross-CPU user which did schedule work on a remote CPU and annoyed NOHZ folks were replaced.I understand this could be a big issue if used in a generic way.
What I am proposing here a mechanism that standardizes those local_lock()+IPI strategies based on how they are done today, so we are only explected to get 'remote-cpu' pwlocks in the 'IPI replacement' operations.
Quoted 6 lines
The idea is pwlock_local* in every local function, and pwlock*(,cpu) in operations that can be remote.
Maybe being used in a more constrained way, it has less chance of being an issue. Also, the whole idea is to improve CPU isolation numbers by reducing IPIs, so maybe NOHZ people will be happier with that :)
Quoted 9 lines
I get that part. The
local_lock_tpart is cheap on !RT and becomes a full lock on RT. While the lock details change the overall expectation remain the same. With this change it is possible to acquire the lock cross-CPU but this depends on the config/ setup. This might not be easy in terms of testing and maintenance.
The more potential users you have, the better it might become in terms of a selling argument. If you have just (say) two users it might be simpler to address just those.
IIRC there are multiple users of this mechanism around the kernel. I remember picking the first three (swap, slub and memcontrol) as examples of how to use pwlocks (QPW at the time). With the concept proven, I could then proceed to work with other potential users to replace it. (We ended up dropping memcontrol in the process)
The main idea is to replace as many potential users as possible to reduce as much as possible the amount of IPIs in isolated cpus, and allow Linux to run workloads which require much lower latency.
This idea came as a general solution to a bunch of latency violations Marcelo and I were coming across.
Thanks!
Leo
Re: [PATCH v4 3/4] swap: apply new pw_queue_on() interface
In reply to Sebastian Andrzej SiewiorQuoted 7 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Leonardo Bras
Quoted 5 lines
I thought that this improved since commit
ff042f4a9b050("mm:lru_cache_disable: replace work queue synchronization withsynchronize_rcu")Did it get worse or was it not entirely gone?
Quoted 8 lines
Quoted 4 lines
I worked in this patchset majorly after that commit date, and it was still an issue up to last time Marcelo tested. Not sure of the impact of above commit, but I suppose it may have brought some improvements without fully fixing it.
It would be good to know what is still missing and maybe it can be addressed without introducing this remote locking.
I get the point, but the main idea is to make this as proof of concept of the mechanism, that can use to solve the latency introduced by IPIs from per-cpu workqueues.
I remember discussing all those examples with Marcelo in the past, and IIRC to solve this we would either have to use per-cpu spinlocks, which is undesired in terms of performance on !RT, or skip remote operations in isolated cpus, which may have weird behaviors, or disable caches in isolated cpus which is terrible for performance there.
In any case, the main goal is to get something generic enough to deal with most cases, not an individual solution.
Thanks!
Leo
Re: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Sebastian Andrzej SiewiorQuoted 25 lines · Sebastian Andrzej Siewior
Quoted 24 lines · Leonardo Bras
Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's * not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; }Quoted 7 lines
mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 3 lines
mm/slub.c +1
-1-if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.Probably to save the cost of using atomic operations on locking, and having about the same restrictions that would allow using
local_locks
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.Awesome!
The problem is always the unlock which requires full locking and is usually the problem from NMI.
You mean, like, the trylock succeeds in the NMI handle, does the per-cpu operations, and then unlock()s?
Or by full locking you mean local_lock() instead of local_trylock() ?
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
One thing worth noting, on !PREEMPT_RT,
spin_trylock()always succeeds on UP.kmalloc_nolock()checks for it, not sure about other callers.Sorry, I did not sure I understand that part.
You mean we have since it always returns true, we may be in NMI context, after it was interrupted holding this lock, and it will return true which will use the protected area even though the lock should avoid it?from
include/linux/spinlock_api_up.h:
Quoted 9 lines
| static __always_inline int _raw_spin_trylock(raw_spinlock_t *lock) | __cond_acquires(true, lock) | { | __LOCK(lock); | return 1; | }on UP a
spin_trylock()always succeeds.
Right, I got that part, I was wondering the scenarios in which would that be an issue.
Thanks!
Leo
Re: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Vlastimil Babka (SUSE)Quoted 23 lines · Vlastimil Babka (SUSE)
Quoted 22 lines · Sebastian Andrzej Siewior
Quoted 21 lines · Leonardo Bras
Quoted 19 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 10 lines
mm/slub.c +1
-1* not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; } -if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Probably to save the cost of using atomic operations on locking, and having
Yes. Also function call overhead as spinlocks are often not inlined.
Oh, right, I remember getting numbers on that. Inlined per-cpu spinlocks take much less time.
Quoted 51 lines
Quoted 2 lines
Quoted 2 lines
about the same restrictions that would allow using
local_locksIndeed.
Quoted 19 lines
Quoted 4 lines
Quoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.Awesome!
The problem is always the unlock which requires full locking and is usually the problem from NMI.
Quoted 9 lines
Quoted 3 lines
One thing worth noting, on !PREEMPT_RT,
spin_trylock()always succeeds on UP.kmalloc_nolock()checks for it, not sure about other callers.Sorry, I did not sure I understand that part.
You mean we have since it always returns true, we may be in NMI context, after it was interrupted holding this lock, and it will return true which will use the protected area even though the lock should avoid it?from
include/linux/spinlock_api_up.h:Quoted 8 lines
| static __always_inline int _raw_spin_trylock(raw_spinlock_t *lock) | __cond_acquires(true, lock) | { | __LOCK(lock); | return 1; | }on UP a
spin_trylock()always succeeds.Indeed. This was a problem in the page allocator, because there we don't even disable irqs, so it wasn't just nmi, but an irq that could get a false-positive
spin_trylock(). See038a102535eb("mm/page_alloc: prevent pcp corruption with SMP=n") which was a hot fix.This was later cleaned up with 3 commits starting with
a373f371166d("mm/page_alloc: effectively disable pcp withCONFIG_SMP=n").Since here you're also replacing
local_trylock()(with no_irqsave) with effectivelyspin_trylock(), the problem also won't be limited to NMIs and thus the checks inkmalloc_nolock()won't help.But I see in Patch 1:
(quoted diff) +3
-0+config PWLOCKS + bool "Per-CPU Work locks" + depends on SMP || COMPILE_TEST
Quoted 5 lines
So that's basically avoiding the problem in the same way as the page allocator after the clean up.
Maybe just remove theCOMPILE_TESTpart? It's not clear to me what it achieves. Just make it require SMP as there's no point for this on !SMP.
I don't mind removing it, but IIRC that was added by Marcelo, I have to ask him the reason he added it.
Thanks!
Leo
Sebastian Andrzej Siewior
maintainerRe: [PATCH v4 4/4] slub: apply new pw_queue_on() interface
In reply to Leonardo BrasQuoted 29 lines · Leonardo Bras
Quoted 28 lines · Sebastian Andrzej Siewior
Quoted 27 lines · Leonardo Bras
Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Leonardo Bras
mm/slub.c +0
-0@@ -4733,121 +4735,121 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node)/* * We assume the percpu sheaves contain only local objects although it's * not completely guaranteed, so we verify later. */ if (unlikely (node_requested && node !=numa_mem_id ())) {stat (s,ALLOC_NODE_MISMATCH );return NULL ; }Quoted 7 lines
mm/slub.c void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, int node) · line 4746
Quoted 3 lines
mm/slub.c +1
-1-if (!local_trylock (&s->cpu_sheaves->lock)) +if (!pw_trylock_local (&s->cpu_sheaves->lock))return NULL ;
alloc_from_pcs()can be called from kmalloc_nolock()/ NMI context. I don't remember why exactlylocal_trylock_twas introduced here instead of a per-CPUspinlock_t.Probably to save the cost of using atomic operations on locking, and having about the same restrictions that would allow using
local_locksQuoted 2 lines
But there should be nothing wrong with a
trylock on it from NMI as you do here.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Awesome!
The problem is always the unlock which requires full locking and is usually the problem from NMI.
You mean, like, the trylock succeeds in the NMI handle, does the per-cpu operations, and then unlock()s?
Yes.
I'm not sure what I was thinking when I wrote it back then. Try lock with local_trylock() might work under RT from NMI. However the unlock path (which needs to follow) requires an unconditional lock of the inner-lock plus a potential wakeup of the waiter.
:
…
Quoted 2 lines
Thanks!
Leo
Sebastian
Sebastian Andrzej Siewior
maintainerQuoted 9 lines · Leonardo Bras
IIRC there are multiple users of this mechanism around the kernel. I remember picking the first three (swap, slub and memcontrol) as examples of how to use pwlocks (QPW at the time). With the concept proven, I could then proceed to work with other potential users to replace it. (We ended up dropping memcontrol in the process)
The main idea is to replace as many potential users as possible to reduce as much as possible the amount of IPIs in isolated cpus, and allow Linux to run workloads which require much lower latency.
Right. I am not entirely opposed to this idea. If you can't improve the situation by other means I would consider this as the last resort. If the mm folks are on board with this, then even better.
Quoted 5 lines
This idea came as a general solution to a bunch of latency violations Marcelo and I were coming across.
Thanks!
Leo
Sebastian