aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2017-07-21 09:19:14 -0400
committerKees Cook <keescook@chromium.org>2017-07-26 17:38:04 -0400
commitc7fea48876773603721f545f8c1a2f894291ef85 (patch)
tree8aec6293f664c8240af323fd017682f64d3b5b64
parent95925c99b9043d52db626645e6ef5ee5f62c97e4 (diff)
lkdtm: Provide timing tests for atomic_t vs refcount_t
While not a crash test, this does provide two tight atomic_t and refcount_t loops for performance comparisons: cd /sys/kernel/debug/provoke-crash perf stat -B -- cat <(echo ATOMIC_TIMING) > DIRECT perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT Looking a CPU cycles is the best way to example the fast-path (rather than instruction counts, since conditional jumps will be executed but will be negligible due to branch-prediction). Signed-off-by: Kees Cook <keescook@chromium.org>
-rw-r--r--drivers/misc/lkdtm.h2
-rw-r--r--drivers/misc/lkdtm_core.c2
-rw-r--r--drivers/misc/lkdtm_refcount.c44
3 files changed, 48 insertions, 0 deletions
diff --git a/drivers/misc/lkdtm.h b/drivers/misc/lkdtm.h
index 04ff8b23b3b0..063f5d651076 100644
--- a/drivers/misc/lkdtm.h
+++ b/drivers/misc/lkdtm.h
@@ -61,6 +61,8 @@ void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void);
61void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void); 61void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void);
62void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void); 62void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void);
63void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void); 63void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void);
64void lkdtm_REFCOUNT_TIMING(void);
65void lkdtm_ATOMIC_TIMING(void);
64 66
65/* lkdtm_rodata.c */ 67/* lkdtm_rodata.c */
66void lkdtm_rodata_do_nothing(void); 68void lkdtm_rodata_do_nothing(void);
diff --git a/drivers/misc/lkdtm_core.c b/drivers/misc/lkdtm_core.c
index 156a1a07c3db..51decc07eeda 100644
--- a/drivers/misc/lkdtm_core.c
+++ b/drivers/misc/lkdtm_core.c
@@ -238,6 +238,8 @@ struct crashtype crashtypes[] = {
238 CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED), 238 CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
239 CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED), 239 CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
240 CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED), 240 CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
241 CRASHTYPE(REFCOUNT_TIMING),
242 CRASHTYPE(ATOMIC_TIMING),
241 CRASHTYPE(USERCOPY_HEAP_SIZE_TO), 243 CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
242 CRASHTYPE(USERCOPY_HEAP_SIZE_FROM), 244 CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
243 CRASHTYPE(USERCOPY_HEAP_FLAG_TO), 245 CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
diff --git a/drivers/misc/lkdtm_refcount.c b/drivers/misc/lkdtm_refcount.c
index 313abea4bf9d..29af0152a337 100644
--- a/drivers/misc/lkdtm_refcount.c
+++ b/drivers/misc/lkdtm_refcount.c
@@ -354,3 +354,47 @@ void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
354 354
355 check_saturated(&sat); 355 check_saturated(&sat);
356} 356}
357
358/* Used to time the existing atomic_t when used for reference counting */
359void lkdtm_ATOMIC_TIMING(void)
360{
361 unsigned int i;
362 atomic_t count = ATOMIC_INIT(1);
363
364 for (i = 0; i < INT_MAX - 1; i++)
365 atomic_inc(&count);
366
367 for (i = INT_MAX; i > 0; i--)
368 if (atomic_dec_and_test(&count))
369 break;
370
371 if (i != 1)
372 pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
373 else
374 pr_info("atomic timing: done\n");
375}
376
377/*
378 * This can be compared to ATOMIC_TIMING when implementing fast refcount
379 * protections. Looking at the number of CPU cycles tells the real story
380 * about performance. For example:
381 * cd /sys/kernel/debug/provoke-crash
382 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
383 */
384void lkdtm_REFCOUNT_TIMING(void)
385{
386 unsigned int i;
387 refcount_t count = REFCOUNT_INIT(1);
388
389 for (i = 0; i < INT_MAX - 1; i++)
390 refcount_inc(&count);
391
392 for (i = INT_MAX; i > 0; i--)
393 if (refcount_dec_and_test(&count))
394 break;
395
396 if (i != 1)
397 pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
398 else
399 pr_info("refcount timing: done\n");
400}