Skip to content

Commit 6119418

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Avoid blocking producers in prepare_report()
To avoid deadlock in case watchers can be interrupted, we need to ensure that producers of the struct other_info can never be blocked by an unrelated consumer. (Likely to occur with KCSAN_INTERRUPT_WATCHER.) There are several cases that can lead to this scenario, for example: 1. A watchpoint A was set up by task T1, but interrupted by interrupt I1. Some other thread (task or interrupt) finds watchpoint A consumes it, and sets other_info. Then I1 also finds some unrelated watchpoint B, consumes it, but is blocked because other_info is in use. T1 cannot consume other_info because I1 never returns -> deadlock. 2. A watchpoint A was set up by task T1, but interrupted by interrupt I1, which also sets up a watchpoint B. Some other thread finds watchpoint A, and consumes it and sets up other_info with its information. Similarly some other thread finds watchpoint B and consumes it, but is then blocked because other_info is in use. When I1 continues it sees its watchpoint was consumed, and that it must wait for other_info, which currently contains information to be consumed by T1. However, T1 cannot unblock other_info because I1 never returns -> deadlock. To avoid this, we need to ensure that producers of struct other_info always have a usable other_info entry. This is obviously not the case with only a single instance of struct other_info, as concurrent producers must wait for the entry to be released by some consumer (which may be locked up as illustrated above). While it would be nice if producers could simply call kmalloc() and append their instance of struct other_info to a list, we are very limited in this code path: since KCSAN can instrument the allocators themselves, calling kmalloc() could lead to deadlock or corrupted allocator state. Since producers of the struct other_info will always succeed at try_consume_watchpoint(), preceding the call into kcsan_report(), we know that the particular watchpoint slot cannot simply be reused or consumed by another potential other_info producer. If we move removal of a watchpoint after reporting (by the consumer of struct other_info), we can see a consumed watchpoint as a held lock on elements of other_info, if we create a one-to-one mapping of a watchpoint to an other_info element. Therefore, the simplest solution is to create an array of struct other_info that is as large as the watchpoints array in core.c, and pass the watchpoint index to kcsan_report() for producers and consumers, and change watchpoints to be removed after reporting is done. With a default config on a 64-bit system, the array other_infos consumes ~37KiB. For most systems today this is not a problem. On smaller memory constrained systems, the config value CONFIG_KCSAN_NUM_WATCHPOINTS can be reduced appropriately. Overall, this change is a simplification of the prepare_report() code, and makes some of the checks (such as checking if at least one access is a write) redundant. Tested: $ tools/testing/selftests/rcutorture/bin/kvm.sh \ --cpus 12 --duration 10 --kconfig "CONFIG_DEBUG_INFO=y \ CONFIG_KCSAN=y CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n \ CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=n \ CONFIG_KCSAN_REPORT_ONCE_IN_MS=100000 CONFIG_KCSAN_VERBOSE=y \ CONFIG_KCSAN_INTERRUPT_WATCHER=y CONFIG_PROVE_LOCKING=y" \ --configs TREE03 => No longer hangs and runs to completion as expected. Reported-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 135c087 commit 6119418

3 files changed

Lines changed: 124 additions & 122 deletions

File tree

‎kernel/kcsan/core.c‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
6969
* slot=9: [10, 11, 9]
7070
* slot=63: [64, 65, 63]
7171
*/
72-
#define NUM_SLOTS (1 + 2*KCSAN_CHECK_ADJACENT)
7372
#define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS))
7473

7574
/*
@@ -171,12 +170,16 @@ try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint)
171170
return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT);
172171
}
173172

174-
/*
175-
* Return true if watchpoint was not touched, false if consumed.
176-
*/
177-
static inline bool remove_watchpoint(atomic_long_t *watchpoint)
173+
/* Return true if watchpoint was not touched, false if already consumed. */
174+
static inline bool consume_watchpoint(atomic_long_t *watchpoint)
178175
{
179-
return atomic_long_xchg_relaxed(watchpoint, INVALID_WATCHPOINT) != CONSUMED_WATCHPOINT;
176+
return atomic_long_xchg_relaxed(watchpoint, CONSUMED_WATCHPOINT) != CONSUMED_WATCHPOINT;
177+
}
178+
179+
/* Remove the watchpoint -- its slot may be reused after. */
180+
static inline void remove_watchpoint(atomic_long_t *watchpoint)
181+
{
182+
atomic_long_set(watchpoint, INVALID_WATCHPOINT);
180183
}
181184

182185
static __always_inline struct kcsan_ctx *get_ctx(void)
@@ -322,7 +325,8 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
322325

323326
if (consumed) {
324327
kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_MAYBE,
325-
KCSAN_REPORT_CONSUMED_WATCHPOINT);
328+
KCSAN_REPORT_CONSUMED_WATCHPOINT,
329+
watchpoint - watchpoints);
326330
} else {
327331
/*
328332
* The other thread may not print any diagnostics, as it has
@@ -470,7 +474,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
470474
value_change = KCSAN_VALUE_CHANGE_TRUE;
471475

472476
/* Check if this access raced with another. */
473-
if (!remove_watchpoint(watchpoint)) {
477+
if (!consume_watchpoint(watchpoint)) {
474478
/*
475479
* Depending on the access type, map a value_change of MAYBE to
476480
* TRUE (always report) or FALSE (never report).
@@ -500,7 +504,8 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
500504
if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
501505
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
502506

503-
kcsan_report(ptr, size, type, value_change, KCSAN_REPORT_RACE_SIGNAL);
507+
kcsan_report(ptr, size, type, value_change, KCSAN_REPORT_RACE_SIGNAL,
508+
watchpoint - watchpoints);
504509
} else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
505510
/* Inferring a race, since the value should not have changed. */
506511

@@ -510,9 +515,15 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
510515

511516
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
512517
kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_TRUE,
513-
KCSAN_REPORT_RACE_UNKNOWN_ORIGIN);
518+
KCSAN_REPORT_RACE_UNKNOWN_ORIGIN,
519+
watchpoint - watchpoints);
514520
}
515521

522+
/*
523+
* Remove watchpoint; must be after reporting, since the slot may be
524+
* reused after this point.
525+
*/
526+
remove_watchpoint(watchpoint);
516527
kcsan_counter_dec(KCSAN_COUNTER_USED_WATCHPOINTS);
517528
out_unlock:
518529
if (!kcsan_interrupt_watcher)

‎kernel/kcsan/kcsan.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/* The number of adjacent watchpoints to check. */
1414
#define KCSAN_CHECK_ADJACENT 1
15+
#define NUM_SLOTS (1 + 2*KCSAN_CHECK_ADJACENT)
1516

1617
extern unsigned int kcsan_udelay_task;
1718
extern unsigned int kcsan_udelay_interrupt;
@@ -136,6 +137,6 @@ enum kcsan_report_type {
136137
*/
137138
extern void kcsan_report(const volatile void *ptr, size_t size, int access_type,
138139
enum kcsan_value_change value_change,
139-
enum kcsan_report_type type);
140+
enum kcsan_report_type type, int watchpoint_idx);
140141

141142
#endif /* _KERNEL_KCSAN_KCSAN_H */

‎kernel/kcsan/report.c‎

Lines changed: 101 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ struct access_info {
3030

3131
/*
3232
* Other thread info: communicated from other racing thread to thread that set
33-
* up the watchpoint, which then prints the complete report atomically. Only
34-
* need one struct, as all threads should to be serialized regardless to print
35-
* the reports, with reporting being in the slow-path.
33+
* up the watchpoint, which then prints the complete report atomically.
3634
*/
3735
struct other_info {
3836
struct access_info ai;
@@ -59,7 +57,11 @@ struct other_info {
5957
struct task_struct *task;
6058
};
6159

62-
static struct other_info other_infos[1];
60+
/*
61+
* To never block any producers of struct other_info, we need as many elements
62+
* as we have watchpoints (upper bound on concurrent races to report).
63+
*/
64+
static struct other_info other_infos[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
6365

6466
/*
6567
* Information about reported races; used to rate limit reporting.
@@ -96,10 +98,11 @@ struct report_time {
9698
static struct report_time report_times[REPORT_TIMES_SIZE];
9799

98100
/*
99-
* This spinlock protects reporting and other_info, since other_info is usually
100-
* required when reporting.
101+
* Spinlock serializing report generation, and access to @other_infos. Although
102+
* it could make sense to have a finer-grained locking story for @other_infos,
103+
* report generation needs to be serialized either way, so not much is gained.
101104
*/
102-
static DEFINE_SPINLOCK(report_lock);
105+
static DEFINE_RAW_SPINLOCK(report_lock);
103106

104107
/*
105108
* Checks if the race identified by thread frames frame1 and frame2 has
@@ -395,9 +398,13 @@ static bool print_report(enum kcsan_value_change value_change,
395398
static void release_report(unsigned long *flags, struct other_info *other_info)
396399
{
397400
if (other_info)
398-
other_info->ai.ptr = NULL; /* Mark for reuse. */
401+
/*
402+
* Use size to denote valid/invalid, since KCSAN entirely
403+
* ignores 0-sized accesses.
404+
*/
405+
other_info->ai.size = 0;
399406

400-
spin_unlock_irqrestore(&report_lock, *flags);
407+
raw_spin_unlock_irqrestore(&report_lock, *flags);
401408
}
402409

403410
/*
@@ -435,14 +442,14 @@ static void set_other_info_task_blocking(unsigned long *flags,
435442
*/
436443
set_current_state(TASK_UNINTERRUPTIBLE);
437444
}
438-
spin_unlock_irqrestore(&report_lock, *flags);
445+
raw_spin_unlock_irqrestore(&report_lock, *flags);
439446
/*
440447
* We cannot call schedule() since we also cannot reliably
441448
* determine if sleeping here is permitted -- see in_atomic().
442449
*/
443450

444451
udelay(1);
445-
spin_lock_irqsave(&report_lock, *flags);
452+
raw_spin_lock_irqsave(&report_lock, *flags);
446453
if (timeout-- < 0) {
447454
/*
448455
* Abort. Reset @other_info->task to NULL, since it
@@ -454,128 +461,107 @@ static void set_other_info_task_blocking(unsigned long *flags,
454461
break;
455462
}
456463
/*
457-
* If @ptr nor @current matches, then our information has been
458-
* consumed and we may continue. If not, retry.
464+
* If invalid, or @ptr nor @current matches, then @other_info
465+
* has been consumed and we may continue. If not, retry.
459466
*/
460-
} while (other_info->ai.ptr == ai->ptr && other_info->task == current);
467+
} while (other_info->ai.size && other_info->ai.ptr == ai->ptr &&
468+
other_info->task == current);
461469
if (is_running)
462470
set_current_state(TASK_RUNNING);
463471
}
464472

465-
/*
466-
* Depending on the report type either sets other_info and returns false, or
467-
* acquires the matching other_info and returns true. If other_info is not
468-
* required for the report type, simply acquires report_lock and returns true.
469-
*/
470-
static bool prepare_report(unsigned long *flags, enum kcsan_report_type type,
471-
const struct access_info *ai, struct other_info *other_info)
473+
/* Populate @other_info; requires that the provided @other_info not in use. */
474+
static void prepare_report_producer(unsigned long *flags,
475+
const struct access_info *ai,
476+
struct other_info *other_info)
472477
{
473-
if (type != KCSAN_REPORT_CONSUMED_WATCHPOINT &&
474-
type != KCSAN_REPORT_RACE_SIGNAL) {
475-
/* other_info not required; just acquire report_lock */
476-
spin_lock_irqsave(&report_lock, *flags);
477-
return true;
478-
}
478+
raw_spin_lock_irqsave(&report_lock, *flags);
479479

480-
retry:
481-
spin_lock_irqsave(&report_lock, *flags);
480+
/*
481+
* The same @other_infos entry cannot be used concurrently, because
482+
* there is a one-to-one mapping to watchpoint slots (@watchpoints in
483+
* core.c), and a watchpoint is only released for reuse after reporting
484+
* is done by the consumer of @other_info. Therefore, it is impossible
485+
* for another concurrent prepare_report_producer() to set the same
486+
* @other_info, and are guaranteed exclusivity for the @other_infos
487+
* entry pointed to by @other_info.
488+
*
489+
* To check this property holds, size should never be non-zero here,
490+
* because every consumer of struct other_info resets size to 0 in
491+
* release_report().
492+
*/
493+
WARN_ON(other_info->ai.size);
482494

483-
switch (type) {
484-
case KCSAN_REPORT_CONSUMED_WATCHPOINT:
485-
if (other_info->ai.ptr)
486-
break; /* still in use, retry */
495+
other_info->ai = *ai;
496+
other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 2);
487497

488-
other_info->ai = *ai;
489-
other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 1);
498+
if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
499+
set_other_info_task_blocking(flags, ai, other_info);
490500

491-
if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
492-
set_other_info_task_blocking(flags, ai, other_info);
501+
raw_spin_unlock_irqrestore(&report_lock, *flags);
502+
}
493503

494-
spin_unlock_irqrestore(&report_lock, *flags);
504+
/* Awaits producer to fill @other_info and then returns. */
505+
static bool prepare_report_consumer(unsigned long *flags,
506+
const struct access_info *ai,
507+
struct other_info *other_info)
508+
{
495509

496-
/*
497-
* The other thread will print the summary; other_info may now
498-
* be consumed.
499-
*/
500-
return false;
510+
raw_spin_lock_irqsave(&report_lock, *flags);
511+
while (!other_info->ai.size) { /* Await valid @other_info. */
512+
raw_spin_unlock_irqrestore(&report_lock, *flags);
513+
cpu_relax();
514+
raw_spin_lock_irqsave(&report_lock, *flags);
515+
}
501516

502-
case KCSAN_REPORT_RACE_SIGNAL:
503-
if (!other_info->ai.ptr)
504-
break; /* no data available yet, retry */
517+
/* Should always have a matching access based on watchpoint encoding. */
518+
if (WARN_ON(!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size,
519+
(unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size)))
520+
goto discard;
505521

522+
if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size,
523+
(unsigned long)ai->ptr, ai->size)) {
506524
/*
507-
* First check if this is the other_info we are expecting, i.e.
508-
* matches based on how watchpoint was encoded.
525+
* If the actual accesses to not match, this was a false
526+
* positive due to watchpoint encoding.
509527
*/
510-
if (!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size,
511-
(unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size))
512-
break; /* mismatching watchpoint, retry */
513-
514-
if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size,
515-
(unsigned long)ai->ptr, ai->size)) {
516-
/*
517-
* If the actual accesses to not match, this was a false
518-
* positive due to watchpoint encoding.
519-
*/
520-
kcsan_counter_inc(KCSAN_COUNTER_ENCODING_FALSE_POSITIVES);
521-
522-
/* discard this other_info */
523-
release_report(flags, other_info);
524-
return false;
525-
}
528+
kcsan_counter_inc(KCSAN_COUNTER_ENCODING_FALSE_POSITIVES);
529+
goto discard;
530+
}
526531

527-
if (!((ai->access_type | other_info->ai.access_type) & KCSAN_ACCESS_WRITE)) {
528-
/*
529-
* While the address matches, this is not the other_info
530-
* from the thread that consumed our watchpoint, since
531-
* neither this nor the access in other_info is a write.
532-
* It is invalid to continue with the report, since we
533-
* only have information about reads.
534-
*
535-
* This can happen due to concurrent races on the same
536-
* address, with at least 4 threads. To avoid locking up
537-
* other_info and all other threads, we have to consume
538-
* it regardless.
539-
*
540-
* A concrete case to illustrate why we might lock up if
541-
* we do not consume other_info:
542-
*
543-
* We have 4 threads, all accessing the same address
544-
* (or matching address ranges). Assume the following
545-
* watcher and watchpoint consumer pairs:
546-
* write1-read1, read2-write2. The first to populate
547-
* other_info is write2, however, write1 consumes it,
548-
* resulting in a report of write1-write2. This report
549-
* is valid, however, now read1 populates other_info;
550-
* read2-read1 is an invalid conflict, yet, no other
551-
* conflicting access is left. Therefore, we must
552-
* consume read1's other_info.
553-
*
554-
* Since this case is assumed to be rare, it is
555-
* reasonable to omit this report: one of the other
556-
* reports includes information about the same shared
557-
* data, and at this point the likelihood that we
558-
* re-report the same race again is high.
559-
*/
560-
release_report(flags, other_info);
561-
return false;
562-
}
532+
return true;
563533

564-
/* Matching access in other_info. */
565-
return true;
534+
discard:
535+
release_report(flags, other_info);
536+
return false;
537+
}
566538

539+
/*
540+
* Depending on the report type either sets @other_info and returns false, or
541+
* awaits @other_info and returns true. If @other_info is not required for the
542+
* report type, simply acquires @report_lock and returns true.
543+
*/
544+
static noinline bool prepare_report(unsigned long *flags,
545+
enum kcsan_report_type type,
546+
const struct access_info *ai,
547+
struct other_info *other_info)
548+
{
549+
switch (type) {
550+
case KCSAN_REPORT_CONSUMED_WATCHPOINT:
551+
prepare_report_producer(flags, ai, other_info);
552+
return false;
553+
case KCSAN_REPORT_RACE_SIGNAL:
554+
return prepare_report_consumer(flags, ai, other_info);
567555
default:
568-
BUG();
556+
/* @other_info not required; just acquire @report_lock. */
557+
raw_spin_lock_irqsave(&report_lock, *flags);
558+
return true;
569559
}
570-
571-
spin_unlock_irqrestore(&report_lock, *flags);
572-
573-
goto retry;
574560
}
575561

576562
void kcsan_report(const volatile void *ptr, size_t size, int access_type,
577563
enum kcsan_value_change value_change,
578-
enum kcsan_report_type type)
564+
enum kcsan_report_type type, int watchpoint_idx)
579565
{
580566
unsigned long flags = 0;
581567
const struct access_info ai = {
@@ -586,7 +572,11 @@ void kcsan_report(const volatile void *ptr, size_t size, int access_type,
586572
.cpu_id = raw_smp_processor_id()
587573
};
588574
struct other_info *other_info = type == KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
589-
? NULL : &other_infos[0];
575+
? NULL : &other_infos[watchpoint_idx];
576+
577+
kcsan_disable_current();
578+
if (WARN_ON(watchpoint_idx < 0 || watchpoint_idx >= ARRAY_SIZE(other_infos)))
579+
goto out;
590580

591581
/*
592582
* With TRACE_IRQFLAGS, lockdep's IRQ trace state becomes corrupted if
@@ -596,7 +586,6 @@ void kcsan_report(const volatile void *ptr, size_t size, int access_type,
596586
*/
597587
lockdep_off();
598588

599-
kcsan_disable_current();
600589
if (prepare_report(&flags, type, &ai, other_info)) {
601590
/*
602591
* Never report if value_change is FALSE, only if we it is
@@ -611,7 +600,8 @@ void kcsan_report(const volatile void *ptr, size_t size, int access_type,
611600

612601
release_report(&flags, other_info);
613602
}
614-
kcsan_enable_current();
615603

616604
lockdep_on();
605+
out:
606+
kcsan_enable_current();
617607
}

0 commit comments

Comments
 (0)