kernel/locking: Add mutual exclusion self-test
Subsystems (3)
cover kernel/locking: Add mutual exclusion self-test
Hi,
Please consider the following patch for the v7.3 merge window.
This patch adds a stress-based selftest for kernel locking and atomic synchronization primitives. The test runs concurrent workers against an array of counters while maintaining a simple invariant: every increment is paired with a decrement, so the sum of all counters must remain zero.
A common test interface allows the same workload to exercise a range of locking primitives, including spinlocks and rwlocks, as well as variants using atomic and bit operations without an explicit lock. After the workers complete, the module checks the invariant and reports a failure if the resulting sum is nonzero.
The test provides regression coverage for memory-ordering issues that may otherwise be difficult to reproduce. In particular, it can expose the regression fixed by commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure") on weakly ordered architectures such as ARM. It also includes a variant modeling the incorrect acquire/release ordering formerly used by RDS before commit 1422f28826d2 ("rds: introduce acquire/release ordering in acquire/release_in_xmit()"). This variant fails on ARM systems.
The patch also adds a kselftest script that:
* first runs an intentionally unsynchronized variant to verify that the workload is capable of exposing lost updates;
* obtains the available test variants from the module;
* runs each non-broken variant; and
* supports low, medium, and high effort levels for interactive and CI use.
The module exposes parameters for selecting the test variant, run duration, number of elements, and data-structure padding. Varying the padding is useful because the reproducibility of some ordering failures depends on the relative placement of the tested state in memory.
A successful run does not prove correctness, but the test provides a practical way to detect synchronization regressions and reproduce known classes of ordering bugs under load.
If this commit gets accepted, I have the intention, subject to other work priorities, to expand on the locking primitives, and in particular add a double set of ww_mutexes for each counter and attempt to acquire both in a random order. Using both the Wound-Wait and Wait-Die variants.
Thxs, Håkon
Håkon Bugge (1):
kernel/locking: Add mutual exclusion self-test
kernel/locking/Makefile | 1 + kernel/locking/mx_test.c | 707 +++++++++++++++++++++ lib/Kconfig.debug | 9 + tools/testing/selftests/locking/Makefile | 2 +- tools/testing/selftests/locking/mx_test.sh | 181 ++++++ 5 files changed, 899 insertions(+), 1 deletion(-) create mode 100644 kernel/locking/mx_test.c create mode 100755 tools/testing/selftests/locking/mx_test.sh
--
2.43.5
PATCH 1/1 kernel/locking: Add mutual exclusion self-test
Add a stress test for locking and atomic synchronization primitives.
The test maintains an array of elements containing counters initialized to zero. It creates four worker threads per online CPU. On each iteration, a worker chooses two elements at random, increments the first counter, and decrements the second. Each completed pair of updates therefore preserves the sum of all counters.
The harness supports variants that protect the counters with an explicit locking primitive and variants that use allegedly lock-free primitives. A common operations interface allows the same workload to exercise different primitives.
The module accepts the following parameters:
mx_nmbr_elems:
Number of elements in the array. The default is 1000.
mx_test:
Test variant to run. Supported variants include spin_lock, spin_lock_irq, spin_lock_irqsave, and write_lock. Specifying an unknown variant causes the module to print the complete list of supported variants. The default is spin_lock.
mx_scnds_per_test:
Duration of each test run, in seconds. The default is 1.
mx_min_padding:
Minimum padding value to test. The default is 8 bytes.
mx_max_padding:
Maximum padding value to test. The default is 8 bytes.
After all workers have stopped, the test sums the counters and reports a failure if the result is nonzero. Such a result indicates that the expected invariant was violated. Because this is a stress test, a zero result means only that no failure was observed during that run.
The test can expose ordering regressions in atomic bit operations. For example, reverting commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure") causes the test_and_clear_bit_innov and test_and_set_bit_innov variants to fail on weakly ordered ARM systems. These variants rely on a failing test_and_*_bit() operation to enforce visibility of updates to other processors, rather than using smp_mb__before_atomic(). They therefore provide regression coverage for commit 415d83249709.
The harness also includes variants that reproduce known incorrect uses of these primitives. Before commit 1422f28826d2 ("rds: introduce acquire/release ordering in acquire/release_in_xmit()"), RDS used them without the required acquire/release ordering. The rds_busted variant models that bug and fails on ARM systems.
The padding parameters allow the layout of the test data to be varied. Empirically, the known failures are reproduced only when the padding is congruent to 8 modulo 16. The precise reason has not been established, but the relative placement of the lock and counter across cache-line or page boundaries appears to matter.
The patch also adds a shell script for the CI kselftest framework. The script first runs the busted variant, which performs the updates without synchronization and is expected to fail. If that variant does not fail, the script skips the remaining tests because the run did not expose even the intentional race, making successful results from the other variants inconclusive.
The script then loads the module with an unsupported mx_test value to obtain the list of available variants from the kernel log. It parses that list and runs every variant whose name does not contain "busted".
For interactive use, the script accepts "-e low", "-e medium", or "-e high" to select the test effort.
--- kernel/locking/Makefile | 1 + kernel/locking/mx_test.c | 707 +++++++++++++++++++++ lib/Kconfig.debug | 9 + tools/testing/selftests/locking/Makefile | 2 +- tools/testing/selftests/locking/mx_test.sh | 181 ++++++ 5 files changed, 899 insertions(+), 1 deletion(-) create mode 100644 kernel/locking/mx_test.c create mode 100755 tools/testing/selftests/locking/mx_test.sh
kernel/locking/Makefile +1 -0
@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_MX_TEST) += mx_test.o
kernel/locking/mx_test.c new file +707 -0
707 lines not on this page · raw message
lib/Kconfig.debug +9 -0
@@ -1734,6 +1734,15 @@ config CSD_LOCK_WAIT_DEBUG_DEFAULT This option causes the csdlock_debug= kernel boot parameter to default to 1 (basic debugging) instead of 0 (no debugging). +config MX_TEST + tristate "miscellaneous lock-full and lock-less selftests" + help + This option provides a kernel module that runs tests using + different locking primitives and lockless operations. + + Say M if you want these self tests to build as a module. + Say N if you are unsure. + endmenu # lock debugging config TRACE_IRQFLAGS
tools/testing/selftests/locking/Makefile +1 -1
@@ -5,6 +5,6 @@ # No binaries, but make sure arg-less "make" doesn't trigger "run_tests" all: -TEST_PROGS := ww_mutex.sh +TEST_PROGS := ww_mutex.sh mx_test.sh include ../lib.mk
tools/testing/selftests/locking/mx_test.sh new file +181 -0
181 lines not on this page · raw message
Signature · 1 line
2.43.5
kernel/locking/Makefile obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o · line 41
Quoted 9 lines · Håkon Bugge
kernel/locking/Makefile +1
-0@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_MX_TEST) += mx_test.o
Total bikeshed item, but both CONFIG_MX_TEST and mx_test are not very obvious names for someone looking at this make file.
Quoted 5 lines
kernel/locking/mx_test.c new file +0
-0
...
Quoted 130 lines
kernel/locking/mx_test.c new file +130
-0+static int __init mx_test_init (void ) +{ +unsigned int threads =4 *num_online_cpus (); +enum mx_test test =MX_ILLEGAL ; +char *mnemonic; +unsigned int i; +unsigned int p; +struct { +enum mx_test test; +char *mnemonic; + } mx_test_types[] = { + { + .test =MX_BUSTED , + .mnemonic ="busted" , + }, + { + .test =MX_SPIN_LOCK , + .mnemonic ="spin_lock" , + }, + { + .test =MX_SPIN_LOCK_IRQ , + .mnemonic ="spin_lock_irq" , + }, + { + .test =MX_SPIN_LOCK_IRQSAVE , + .mnemonic ="spin_lock_irqsave" , + }, + { + .test =MX_RW_LOCK_W , + .mnemonic ="write_lock" , + }, + { + .test =MX_RW_LOCK_W_BH , + .mnemonic ="write_lock_bh" , + }, + { + .test =MX_RW_LOCK_TRW , + .mnemonic ="read_trylock_write_lock" , + }, + { + .test =MX_RW_LOCK_TRW_BH , + .mnemonic ="read_trylock_write_lock_bh" , + }, + { + .test =MX_MUTEX , + .mnemonic ="mutex" , + }, + { + .test =MX_ATOMIC_ADD , + .mnemonic ="atomic_add" , + }, + { + .test =MX_CMPXCHG , + .mnemonic ="cmpxchg" , + }, + { + .test =MX_RDS_BUSTED , + .mnemonic ="rds_busted" , + }, + { + .test =MX_TEST_AND_SET_BIT_LOCK , + .mnemonic ="test_and_set_bit_lock" , + }, + { + .test =MX_TEST_AND_SET_BIT_INNOV , + .mnemonic ="test_and_set_bit_innov" , + }, + { + .test =MX_TEST_AND_SET_BIT_PLAIN , + .mnemonic ="test_and_set_bit_plain" , + }, + { + .test =MX_TEST_AND_CLEAR_BIT_INNOV , + .mnemonic ="test_and_clear_bit_innov" , + }, + { + .test =MX_TEST_AND_CLEAR_BIT_PLAIN , + .mnemonic ="test_and_clear_bit_plain" , + }, + { + .test =MX_SINGLE_WW_MUTEX_WW , + .mnemonic ="single_ww_mutex_wound_wait" , + }, + { + .test =MX_SINGLE_WW_MUTEX_WD , + .mnemonic ="single_ww_mutex_wait_die" , + }, + }; + +/* Select the test type */ +for (i =0 ; i <ARRAY_SIZE (mx_test_types); ++i) +if (!strcmp (mx_test, mx_test_types[i].mnemonic)) { + test = mx_test_types[i].test; + mnemonic = mx_test_types[i].mnemonic; +break ; + } + +if (test ==MX_ILLEGAL ) { +pr_err ("mx_test: unknown test type %s\n" , mx_test); +pr_notice ("mx_test: legitimate test types:\n" ); +for (i =0 ; i <ARRAY_SIZE (mx_test_types); ++i) +pr_notice ("mx_test_types: %s\n" , mx_test_types[i].mnemonic); +return -ENOPROTOOPT ; + } + +if ((mx_min_padding %8 ) || (mx_max_padding %8 )) { +pr_err ("mx_test: padding must be a multiple of 8 bytes\n" ); +return -EINVAL ; + } + +if (mx_min_padding > mx_max_padding) { +pr_err ("mx_test: mx_min_padding must be less than equal to mx_max_padding\n" ); +return -EINVAL ; + } + +if (mx_max_padding >L1_CACHE_BYTES ) { +pr_err ("mx_test: mx_max_padding must be less than equal to L1_CACHE_BYTES (%d)\n" , +L1_CACHE_BYTES ); +return -EINVAL ; + } + +if (mx_nmbr_elems <1 ) { +pr_err ("Number of elements must be greater than equal to one\n" ); +return -EINVAL ; + } + +if (mx_scnds_per_test >MAX_JIFFY_OFFSET /HZ ) { +pr_err ("mx_scnds_per_test too large\n" ); +return -EINVAL ; + }
kernel/locking/mx_test.c line 695
Quoted 10 lines
kernel/locking/mx_test.c new file +10
-0+ +for (p = mx_min_padding; p <= mx_max_padding; p +=8 ) { +int sts =run_test (test, mnemonic, threads, p); + +if (sts) +return sts; + } + +return 0 ; +}
Apologies, I've not had a chance to apply and try this yet.
So this looks like the test has to be run via module load? I almost never use modules in my test environment, so its often nice to allow it to be built in and be triggered/re-triggered without having to unload/reload (assuming this is not something like locktorture which never finishes - and if that is the case, it might be good to integrate into the locktorture framework instead of adding a new one). Maybe something to consider?
thanks
-john
Quoted 17 lines · John Stultz
Quoted 9 lines · Håkon Bugge
kernel/locking/Makefile +0
-0@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.oobj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_MX_TEST) += mx_test.o
Total bikeshed item, but both
CONFIG_MX_TESTandmx_testare not very obvious names for someone looking at this make file.
I am terrible with names. It's short for Mutual eXclusion. Can of course be spelled out if preferred.
[snip]
Quoted 1 line
Apologies, I've not had a chance to apply and try this yet.
NP.
Quoted 1 line
So this looks like the test has to be run via module load?
It can be run built-in as well, but then only the default test is run.
Quoted 6 lines
I almost
never use modules in my test environment, so its often nice to allow it to be built in and be triggered/re-triggered without having to unload/reload (assuming this is not something like locktorture which never finishes - and if that is the case, it might be good to integrate into the locktorture framework instead of adding a new one).
No, this selftest terminates.
The script, tools/testing/selftests/locking/mx_test.sh, is written to be robust wrt. module load/unload failures. The reason I prefer to run it as module, is the turn-around time. From an edit, it takes literally only a second to compile the module out-of-tree, another second to copy it to the *updates* on the test server.
But if the community prefers that all the tests are run when the test is built-in, the loop looping though the eligible tests can be moved from the script to the C source instead.
Thxs, Håkon
Quoted 36 lines · Haakon Bugge
Quoted 15 lines · John Stultz
Quoted 9 lines · Håkon Bugge
kernel/locking/Makefile +0
-0@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.oobj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_MX_TEST) += mx_test.o
Total bikeshed item, but both
CONFIG_MX_TESTandmx_testare not very obvious names for someone looking at this make file.I am terrible with names. It's short for Mutual eXclusion. Can of course be spelled out if preferred.
[snip]
Quoted 1 line
Apologies, I've not had a chance to apply and try this yet.
NP.
Quoted 1 line
So this looks like the test has to be run via module load?
It can be run built-in as well, but then only the default test is run.
Quoted 6 lines
I almost
never use modules in my test environment, so its often nice to allow it to be built in and be triggered/re-triggered without having to unload/reload (assuming this is not something like locktorture which never finishes - and if that is the case, it might be good to integrate into the locktorture framework instead of adding a new one).
Quoted 7 lines
No, this selftest terminates.
The script,
tools/testing/selftests/locking/mx_test.sh, is written to be robust wrt. module load/unload failures. The reason I prefer to run it as module, is the turn-around time. From an edit, it takes literally only a second to compile the module out-of-tree, another second to copy it to the *updates* on the test server.
Yeah, I can see the benefit in the case where you're testing on the machine you build with. However, I've found in cases where you're building on a different machine then your testing with, modules can be more of a pain to work with.
That's why I've added sysfs interfaces to some tests (test-ww_mutex, and my out of tree ksched_football), so this sort of module-load-time-testing can be repeatedly re-triggered in a static build config.
Quoted 3 lines
But if the community prefers that all the tests are run when the test is built-in, the loop looping though the eligible tests can be moved from the script to the C source instead.
I'm not sure if there's a clear community preference. Especially as some thought is probably needed as to the sysfs conventions, as littering /sys/kerenel/ with a bunch of test driver interfaces might not be a great long term solution. But this was just something I wanted to raise, as I've found it useful, especially when issues only show up after many many iterations of the test.
thanks
-john
Quoted 54 lines · John Stultz
Quoted 36 lines · Haakon Bugge
Quoted 15 lines · John Stultz
Quoted 9 lines · Håkon Bugge
kernel/locking/Makefile +0
-0@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.oobj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_MX_TEST) += mx_test.o
Total bikeshed item, but both
CONFIG_MX_TESTandmx_testare not very obvious names for someone looking at this make file.I am terrible with names. It's short for Mutual eXclusion. Can of course be spelled out if preferred.
[snip]
Quoted 1 line
Apologies, I've not had a chance to apply and try this yet.
NP.
Quoted 1 line
So this looks like the test has to be run via module load?
It can be run built-in as well, but then only the default test is run.
Quoted 6 lines
I almost
never use modules in my test environment, so its often nice to allow it to be built in and be triggered/re-triggered without having to unload/reload (assuming this is not something like locktorture which never finishes - and if that is the case, it might be good to integrate into the locktorture framework instead of adding a new one).Quoted 7 lines
No, this selftest terminates.
The script,
tools/testing/selftests/locking/mx_test.sh, is written to be robust wrt. module load/unload failures. The reason I prefer to run it as module, is the turn-around time. From an edit, it takes literally only a second to compile the module out-of-tree, another second to copy it to the *updates* on the test server.Yeah, I can see the benefit in the case where you're testing on the machine you build with. However, I've found in cases where you're building on a different machine then your testing with, modules can be more of a pain to work with.
Quoted 4 lines
That's why I've added sysfs interfaces to some tests (test-ww_mutex, and my out of tree
ksched_football), so this sort of module-load-time-testing can be repeatedly re-triggered in a static build config.
Those are all good points. But I am alluding to, that since you can invoke a shell script in a loop loading/unloading a module, a sysfs interface is strictly not required for testing thoroughly.
Quoted 10 lines
Quoted 3 lines
But if the community prefers that all the tests are run when the test is built-in, the loop looping though the eligible tests can be moved from the script to the C source instead.
I'm not sure if there's a clear community preference. Especially as some thought is probably needed as to the sysfs conventions, as littering
/sys/kerenel/with a bunch of test driver interfaces might not be a great long term solution. But this was just something I wanted to raise, as I've found it useful, especially when issues only show up after many many iterations of the test.
Let's shelf this until v7.3-rc1 comes out and hopefully a maintainer can shed some thoughts on this.
Thxs, Håkon
Quoted 7 lines · On Mon, 17 Aug 2026 15:02:36 +0200 Håkon Bugge <[email protected]> wrote:
Add a stress test for locking and atomic synchronization primitives.
The test maintains an array of elements containing counters initialized to zero. It creates four worker threads per online CPU. On each iteration, a worker chooses two elements at random, increments the first counter, and decrements the second. Each completed pair of updates therefore preserves the sum of all counters.
...
I think 1000 elements/locks/counters is far too many.
You are trying to hit contention so you want a relatively small number. Possibly similar to the number of threads running the test, but maybe less. Perhaps nearer the number of cpus.
Also you are only going to see an issue if the RMW of the 'counter += 1' is split by the same RMW of a different thread.
That is pretty unlikely even without a lock.
It would be better to force a short delay between the R and W.
If you use change the MX_ATOMIC_ADD to use the atomic_long functions (I've forgotten the exact name) then all the counter are the same type and can be removed from the union.
The default 'just use +=' code can then be moved to the bottom of mx_add().
David
Quoted 6 lines · David Laight
Quoted 2 lines · On Mon, 17 Aug 2026 15:02:36 +0200 Håkon Bugge <[email protected]> wrote:
Add a stress test for locking and atomic synchronization primitives.
Quoted 8 lines
Quoted 5 lines
The test maintains an array of elements containing counters initialized to zero. It creates four worker threads per online CPU. On each iteration, a worker chooses two elements at random, increments the first counter, and decrements the second. Each completed pair of updates therefore preserves the sum of all counters.
...
I think 1000 elements/locks/counters is far too many.
I do not have a strong opinion. My experience is that it doesn't matter much. For example, modifying the "atomic_xchg" test to:
Lock acquire:
while (atomic_xchg_acquire(&el->mx_atomic_xchg.lock, 1) != 0) cpu_relax();
Lock release:
atomic_set(&el->mx_atomic_xchg.lock, 0);
(that is, removing "_release" above).
This fails within one second of runtime for all tested number of elements:
2
32
512
8192
131072
2097152
33554432
on a 160 CPU Arm BM system. What _is_ paramount, is the padding. None of the failing tests fails if padding is zero. So, in other words, on this particular micro architecture, there must be cases where the lock and the counter resides in different cache-lines. Trust me, I got severely confused after Matthew Wilcox' commit 25500ba7e77c ("locking/mutex: Remove the list_head from struct mutex"). Suddenly, none of my known failing cases failed anymore...
Quoted 7 lines
You are trying to hit contention so you want a relatively small number. Possibly similar to the number of threads running the test, but maybe less. Perhaps nearer the number of cpus.
Also you are only going to see an issue if the RMW of the 'counter += 1' is split by the same RMW of a different thread.
That is pretty unlikely even without a lock.
Not in my opinion. I have the "mx_busted" test which does exactly this, and it fails in all cases. It is actually used by the selftest script to verify that the test harness is able to detect errors.
Also, the added selftest script permutates many of the parameters.
Quoted 1 line
It would be better to force a short delay between the R and W.
That may be a good idea. But unless someone can come up with a case where it is needed, I am alluding to not put in any delays.
Quoted 4 lines
If you use change the
MX_ATOMIC_ADDto use theatomic_longfunctions (I've forgotten the exact name) then all the counter are the same type and can be removed from the union.
The default 'just use +=' code can then be moved to the bottom ofmx_add().
That is a good idea, but we then misses test coverage for atomic_t. But, what about:
struct mx_elem {
union {
spinlock_t spin_lock;
:
struct mutex mutex;
};
union {
long counter;
atomic_t atom_counter;
atomic64_t atom64_counter;
};
};Then all lock-full cases will have a single " += " code.
Thxs, Håkon
Quoted 1 line
Quoted 5 lines · Haakon Bugge
On 14 Sep 2026, at 11:35, David Laight <[email protected]> wrote:
[snip]
Quoted 7 lines
Quoted 4 lines
If you use change the
MX_ATOMIC_ADDto use theatomic_longfunctions (I've forgotten the exact name) then all the counter are the same type and can be removed from the union.
The default 'just use +=' code can then be moved to the bottom ofmx_add().That is a good idea, but we then misses test coverage for
atomic_t. But, what about:
[snip]
I ended up with:
struct mx_elem {
/* This union contains locks and lock-free data types */
union {
spinlock_t spinlock;
rwlock_t rwlock;
struct mutex mutex;
atomic_t atomic_lock;
atomic_t atomic_counter;
atomic64_t atomic64_counter;
long cmpxchg_counter;
unsigned long bits;
struct ww_mutex ww_mutex;
struct optimistic_spin_queue osq_lock;
};
/* A counter protected by one of the locks above */
long counter;
};This became quite simpler. I'll test somewhat more, and send out a v2 tomorrow.
Thxs, Håkon
Quoted 36 lines · On Mon, 14 Sep 2026 17:21:41 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 3 lines · Haakon Bugge
On 14 Sep 2026, at 11:35, David Laight <[email protected]> wrote:
[snip]
Quoted 7 lines
Quoted 4 lines
If you use change the
MX_ATOMIC_ADDto use theatomic_longfunctions (I've forgotten the exact name) then all the counter are the same type and can be removed from the union.
The default 'just use +=' code can then be moved to the bottom ofmx_add().That is a good idea, but we then misses test coverage for
atomic_t. But, what about:[snip]
I ended up with:
struct mx_elem { /* This union contains locks and lock-free data types */ union { spinlock_t spinlock; rwlock_t rwlock; struct mutex mutex; atomic_t atomic_lock; atomic_t atomic_counter; atomic64_t atomic64_counter; long cmpxchg_counter; unsigned long bits; struct ww_mutex ww_mutex; struct optimistic_spin_queue osq_lock; }; /* A counter protected by one of the locks above */ long counter; };
Quoted 2 lines
This became quite simpler. I'll test somewhat more, and send out a v2 tomorrow.
If you split the 'long counter' into a separate array then it won't be in the same cache line as the associated lock.
That should mean the alignment changes aren't needed.
When I mentioned a delay in the RMW for xxx->counter++ I was thinking of a few clocks, perhaps something like:
c = xxx->counter; for (auto i = c + 10; i != c; i--) OPTIMIZER_HIDE_VAR(i); OPTIMIZER_HIDE_VAR(i); xxx->counter = i + delta;
I may have a '3am can't sleep' part model for the arm cache.
It might be that when one cpu writes to a 'shared' cache line the 'invalidate' that is broadcast is only partial; the invalidate is remembered, but the contents of the cache line are still used to satisfy reads.
So a memory read for a different cache line could easily pick up a write that was done later.
A read barrier actually invalidates all the 'invalidated' cache lines so later reads can't use the 'stale' data.
Just need a model for the write barrier now.
It might just that each 'cache line wide' entry in the store buffer fifo can have multiple addresses associated with different groups of writes. Then the writes for one fifo entry could happen in any order (or be merged into a single wider write.
(But that is real guesswork.)
It is nice to a have a simple model that mostly matches the observed behaviour.
David
Quoted 4 lines
Thxs, Håkon
Quoted 3 lines · David Laight
[snip]
Quoted 3 lines
If you split the 'long counter' into a separate array then it won't be in the same cache line as the associated lock.
That should mean the alignment changes aren't needed.
Yes. I tested that, and the module finds the same "errors". A good simplification.
Quoted 8 lines
When I mentioned a delay in the RMW for xxx->counter++ I was thinking of a few clocks, perhaps something like:
c =xxx->counter;
for (auto i = c + 10; i != c; i--)OPTIMIZER_HIDE_VAR(i);
OPTIMIZER_HIDE_VAR(i);xxx->counter= i + delta;
For the lock-full cases, there are already some instructions to be executed, the return from mx_acquire(), the call to mx_add(), and the beginning of mx_release(). So, I am reluctant to add further delays, unless it can be proven useful for better error detection.
I also looked into your statement that the default number of locks/counters was too high. I compiled the table below from empirical testing on a 160-CPU Arm bare-metal system.
For each element count, I ran the test 100 times, with each run lasting one second. I used the "osq_lock_busted" test which is the failing test that is least likely to fail.
#elements percent failures
===================================
1024 90
2048 95
4096 92
8192 94
16384 85
32768 79
65536 66Hence, I yanked the default *up* to 2048.
Thxs, Håkon
Peter Zijlstra
maintainerQuoted 3 lines · Håkon Bugge
Hi,
Please consider the following patch for the v7.3 merge window.
You are a few weeks too late for v7.3. At best I'll end up looking at this after -rc1 at which point it could possibly make v7.4
Quoted 11 lines · Peter Zijlstra
Quoted 3 lines · Håkon Bugge
Hi,
Please consider the following patch for the v7.3 merge window.
You are a few weeks too late for v7.3. At best I'll end up looking at this after -rc1 at which point it could possibly make v7.4
OK, my bad. Anyway, thanks for the diligent response then :-)
Thxs, Håkon