Skip to content

Commit a18c835

Browse files
zx2c4shuahkh
authored andcommitted
selftests: vDSO: align getrandom states to cache line
This prevents false sharing, which makes a large difference on machines with several NUMA nodes, such as on a dual socket Intel(R) Xeon(R) Gold 6338 CPU @ 2.00GHz, where the "bench-multi" test goes from 2.7s down to 1.9s. While this is just test code, it also forms the basis of how folks will wind up implementing this in libraries, so we should implement this simple cache alignment improvement here. Suggested-by: Florian Weimer <[email protected]> Cc: Adhemerval Zanella <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 45a8897 commit a18c835

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tools/testing/selftests/vDSO/vdso_test_getrandom.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ static void *vgetrandom_get_state(void)
5959
size_t page_size = getpagesize();
6060
size_t new_cap;
6161
size_t alloc_size, num = sysconf(_SC_NPROCESSORS_ONLN); /* Just a decent heuristic. */
62+
size_t state_size_aligned, cache_line_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ?: 1;
6263
void *new_block, *new_states;
6364

64-
alloc_size = (num * vgrnd.params.size_of_opaque_state + page_size - 1) & (~(page_size - 1));
65-
num = (page_size / vgrnd.params.size_of_opaque_state) * (alloc_size / page_size);
65+
state_size_aligned = (vgrnd.params.size_of_opaque_state + cache_line_size - 1) & (~(cache_line_size - 1));
66+
alloc_size = (num * state_size_aligned + page_size - 1) & (~(page_size - 1));
67+
num = (page_size / state_size_aligned) * (alloc_size / page_size);
6668
new_block = mmap(0, alloc_size, vgrnd.params.mmap_prot, vgrnd.params.mmap_flags, -1, 0);
6769
if (new_block == MAP_FAILED)
6870
goto out;
@@ -78,7 +80,7 @@ static void *vgetrandom_get_state(void)
7880
if (((uintptr_t)new_block & (page_size - 1)) + vgrnd.params.size_of_opaque_state > page_size)
7981
new_block = (void *)(((uintptr_t)new_block + page_size - 1) & (~(page_size - 1)));
8082
vgrnd.states[i] = new_block;
81-
new_block += vgrnd.params.size_of_opaque_state;
83+
new_block += state_size_aligned;
8284
}
8385
vgrnd.len = num;
8486
goto success;

0 commit comments

Comments
 (0)