Skip to content

Commit 9883576

Browse files
Tzung-Bi Shihgregkh
authored andcommitted
revocable: Add KUnit test for concurrent access
Add a test case to verify correct synchronization between concurrent readers and a revocation. The test setup involves: 1. Consumer 1 enters the critical section (SRCU read lock) and verifies access to the resource. 2. Provider attempts to revoke the resource. This should block until Consumer 1 releases the lock. 3. Consumer 2 attempts to enter the critical section while revocation is pending. It should see the resource as revoked (NULL). 4. Consumer 1 exits, allowing the revocation to complete. This ensures that the SRCU mechanism correctly enforces grace periods and that new readers are properly prevented from accessing the resource once revocation has begun. A way to run the test: $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \ --kconfig_add CONFIG_PROVE_LOCKING=y \ --kconfig_add CONFIG_DEBUG_KERNEL=y \ --kconfig_add CONFIG_DEBUG_INFO=y \ --kconfig_add CONFIG_DEBUG_INFO_DWARF5=y \ --kconfig_add CONFIG_KASAN=y \ --kconfig_add CONFIG_DETECT_HUNG_TASK=y \ --kconfig_add CONFIG_DEFAULT_HUNG_TASK_TIMEOUT="10" \ --arch=x86_64 --raw_output=all \ revocable_test Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org> Link: https://patch.msgid.link/20260129143733.45618-5-tzungbi@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 377563c commit 9883576

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

drivers/base/revocable_test.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
*
1818
* - Provider Use-after-free: Verifies revocable_init() correctly handles
1919
* race conditions where the provider is being released.
20+
*
21+
* - Concurrent Access: Verifies multiple threads can access the resource.
2022
*/
2123

2224
#include <kunit/test.h>
25+
#include <linux/completion.h>
26+
#include <linux/delay.h>
27+
#include <linux/kthread.h>
2328
#include <linux/refcount.h>
2429
#include <linux/revocable.h>
2530

@@ -160,12 +165,111 @@ static void revocable_test_provider_use_after_free(struct kunit *test)
160165
KUNIT_EXPECT_NE(test, ret, 0);
161166
}
162167

168+
struct test_concurrent_access_context {
169+
struct kunit *test;
170+
struct revocable_provider __rcu *rp;
171+
struct revocable rev;
172+
struct completion started, enter, exit;
173+
struct task_struct *thread;
174+
void *expected_res;
175+
};
176+
177+
static int test_concurrent_access_provider(void *data)
178+
{
179+
struct test_concurrent_access_context *ctx = data;
180+
181+
complete(&ctx->started);
182+
183+
wait_for_completion(&ctx->enter);
184+
revocable_provider_revoke(&ctx->rp);
185+
KUNIT_EXPECT_PTR_EQ(ctx->test, unrcu_pointer(ctx->rp), NULL);
186+
187+
return 0;
188+
}
189+
190+
static int test_concurrent_access_consumer(void *data)
191+
{
192+
struct test_concurrent_access_context *ctx = data;
193+
void *res;
194+
195+
complete(&ctx->started);
196+
197+
wait_for_completion(&ctx->enter);
198+
res = revocable_try_access(&ctx->rev);
199+
KUNIT_EXPECT_PTR_EQ(ctx->test, res, ctx->expected_res);
200+
201+
wait_for_completion(&ctx->exit);
202+
revocable_withdraw_access(&ctx->rev);
203+
204+
return 0;
205+
}
206+
207+
static void revocable_test_concurrent_access(struct kunit *test)
208+
{
209+
struct revocable_provider __rcu *rp;
210+
void *real_res = (void *)0x12345678;
211+
struct test_concurrent_access_context *ctx;
212+
int ret, i;
213+
214+
rp = revocable_provider_alloc(real_res);
215+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp);
216+
217+
ctx = kunit_kmalloc_array(test, 3, sizeof(*ctx), GFP_KERNEL);
218+
KUNIT_ASSERT_NOT_NULL(test, ctx);
219+
220+
for (i = 0; i < 3; ++i) {
221+
ctx[i].test = test;
222+
init_completion(&ctx[i].started);
223+
init_completion(&ctx[i].enter);
224+
init_completion(&ctx[i].exit);
225+
226+
if (i == 0) {
227+
ctx[i].rp = rp;
228+
ctx[i].thread = kthread_run(
229+
test_concurrent_access_provider, ctx + i,
230+
"revocable_provider_%d", i);
231+
} else {
232+
ret = revocable_init(rp, &ctx[i].rev);
233+
KUNIT_ASSERT_EQ(test, ret, 0);
234+
235+
ctx[i].thread = kthread_run(
236+
test_concurrent_access_consumer, ctx + i,
237+
"revocable_consumer_%d", i);
238+
}
239+
KUNIT_ASSERT_FALSE(test, IS_ERR(ctx[i].thread));
240+
241+
wait_for_completion(&ctx[i].started);
242+
}
243+
ctx[1].expected_res = real_res;
244+
ctx[2].expected_res = NULL;
245+
246+
/* consumer1 enters read-side critical section */
247+
complete(&ctx[1].enter);
248+
msleep(100);
249+
/* provider0 revokes the resource */
250+
complete(&ctx[0].enter);
251+
msleep(100);
252+
/* consumer2 enters read-side critical section */
253+
complete(&ctx[2].enter);
254+
msleep(100);
255+
256+
/* consumer{1,2} exit read-side critical section */
257+
complete(&ctx[1].exit);
258+
complete(&ctx[2].exit);
259+
260+
for (i = 0; i < 3; ++i)
261+
kthread_stop(ctx[i].thread);
262+
for (i = 1; i < 3; ++i)
263+
revocable_deinit(&ctx[i].rev);
264+
}
265+
163266
static struct kunit_case revocable_test_cases[] = {
164267
KUNIT_CASE(revocable_test_basic),
165268
KUNIT_CASE(revocable_test_revocation),
166269
KUNIT_CASE(revocable_test_try_access_macro),
167270
KUNIT_CASE(revocable_test_try_access_macro2),
168271
KUNIT_CASE(revocable_test_provider_use_after_free),
272+
KUNIT_CASE(revocable_test_concurrent_access),
169273
{}
170274
};
171275

0 commit comments

Comments
 (0)