Skip to content

Commit 55a55fe

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Add ability to pass instruction pointer of access to reporting
Add the ability to pass an explicitly set instruction pointer of access from check_access() all the way through to reporting. In preparation of using it in reporting. Signed-off-by: Marco Elver <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent ade3a58 commit 55a55fe

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

kernel/kcsan/core.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ void kcsan_restore_irqtrace(struct task_struct *task)
350350
static noinline void kcsan_found_watchpoint(const volatile void *ptr,
351351
size_t size,
352352
int type,
353+
unsigned long ip,
353354
atomic_long_t *watchpoint,
354355
long encoded_watchpoint)
355356
{
@@ -396,7 +397,7 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
396397

397398
if (consumed) {
398399
kcsan_save_irqtrace(current);
399-
kcsan_report_set_info(ptr, size, type, watchpoint - watchpoints);
400+
kcsan_report_set_info(ptr, size, type, ip, watchpoint - watchpoints);
400401
kcsan_restore_irqtrace(current);
401402
} else {
402403
/*
@@ -416,7 +417,7 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
416417
}
417418

418419
static noinline void
419-
kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
420+
kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned long ip)
420421
{
421422
const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
422423
const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
@@ -568,8 +569,8 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
568569
if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
569570
atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
570571

571-
kcsan_report_known_origin(ptr, size, type, value_change,
572-
watchpoint - watchpoints,
572+
kcsan_report_known_origin(ptr, size, type, ip,
573+
value_change, watchpoint - watchpoints,
573574
old, new, access_mask);
574575
} else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
575576
/* Inferring a race, since the value should not have changed. */
@@ -578,8 +579,10 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
578579
if (is_assert)
579580
atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
580581

581-
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
582-
kcsan_report_unknown_origin(ptr, size, type, old, new, access_mask);
582+
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert) {
583+
kcsan_report_unknown_origin(ptr, size, type, ip,
584+
old, new, access_mask);
585+
}
583586
}
584587

585588
/*
@@ -596,8 +599,8 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
596599
user_access_restore(ua_flags);
597600
}
598601

599-
static __always_inline void check_access(const volatile void *ptr, size_t size,
600-
int type)
602+
static __always_inline void
603+
check_access(const volatile void *ptr, size_t size, int type, unsigned long ip)
601604
{
602605
const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
603606
atomic_long_t *watchpoint;
@@ -625,13 +628,12 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
625628
*/
626629

627630
if (unlikely(watchpoint != NULL))
628-
kcsan_found_watchpoint(ptr, size, type, watchpoint,
629-
encoded_watchpoint);
631+
kcsan_found_watchpoint(ptr, size, type, ip, watchpoint, encoded_watchpoint);
630632
else {
631633
struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
632634

633635
if (unlikely(should_watch(ptr, size, type, ctx)))
634-
kcsan_setup_watchpoint(ptr, size, type);
636+
kcsan_setup_watchpoint(ptr, size, type, ip);
635637
else if (unlikely(ctx->scoped_accesses.prev))
636638
kcsan_check_scoped_accesses();
637639
}
@@ -757,7 +759,7 @@ kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
757759
{
758760
struct kcsan_ctx *ctx = get_ctx();
759761

760-
__kcsan_check_access(ptr, size, type);
762+
check_access(ptr, size, type, _RET_IP_);
761763

762764
ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
763765

@@ -802,7 +804,7 @@ EXPORT_SYMBOL(kcsan_end_scoped_access);
802804

803805
void __kcsan_check_access(const volatile void *ptr, size_t size, int type)
804806
{
805-
check_access(ptr, size, type);
807+
check_access(ptr, size, type, _RET_IP_);
806808
}
807809
EXPORT_SYMBOL(__kcsan_check_access);
808810

@@ -823,7 +825,7 @@ EXPORT_SYMBOL(__kcsan_check_access);
823825
void __tsan_read##size(void *ptr); \
824826
void __tsan_read##size(void *ptr) \
825827
{ \
826-
check_access(ptr, size, 0); \
828+
check_access(ptr, size, 0, _RET_IP_); \
827829
} \
828830
EXPORT_SYMBOL(__tsan_read##size); \
829831
void __tsan_unaligned_read##size(void *ptr) \
@@ -832,7 +834,7 @@ EXPORT_SYMBOL(__kcsan_check_access);
832834
void __tsan_write##size(void *ptr); \
833835
void __tsan_write##size(void *ptr) \
834836
{ \
835-
check_access(ptr, size, KCSAN_ACCESS_WRITE); \
837+
check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_); \
836838
} \
837839
EXPORT_SYMBOL(__tsan_write##size); \
838840
void __tsan_unaligned_write##size(void *ptr) \
@@ -842,7 +844,8 @@ EXPORT_SYMBOL(__kcsan_check_access);
842844
void __tsan_read_write##size(void *ptr) \
843845
{ \
844846
check_access(ptr, size, \
845-
KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE); \
847+
KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, \
848+
_RET_IP_); \
846849
} \
847850
EXPORT_SYMBOL(__tsan_read_write##size); \
848851
void __tsan_unaligned_read_write##size(void *ptr) \
@@ -858,14 +861,14 @@ DEFINE_TSAN_READ_WRITE(16);
858861
void __tsan_read_range(void *ptr, size_t size);
859862
void __tsan_read_range(void *ptr, size_t size)
860863
{
861-
check_access(ptr, size, 0);
864+
check_access(ptr, size, 0, _RET_IP_);
862865
}
863866
EXPORT_SYMBOL(__tsan_read_range);
864867

865868
void __tsan_write_range(void *ptr, size_t size);
866869
void __tsan_write_range(void *ptr, size_t size)
867870
{
868-
check_access(ptr, size, KCSAN_ACCESS_WRITE);
871+
check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_);
869872
}
870873
EXPORT_SYMBOL(__tsan_write_range);
871874

@@ -886,7 +889,8 @@ EXPORT_SYMBOL(__tsan_write_range);
886889
IS_ALIGNED((unsigned long)ptr, size); \
887890
if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic) \
888891
return; \
889-
check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0); \
892+
check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0, \
893+
_RET_IP_); \
890894
} \
891895
EXPORT_SYMBOL(__tsan_volatile_read##size); \
892896
void __tsan_unaligned_volatile_read##size(void *ptr) \
@@ -901,7 +905,8 @@ EXPORT_SYMBOL(__tsan_write_range);
901905
return; \
902906
check_access(ptr, size, \
903907
KCSAN_ACCESS_WRITE | \
904-
(is_atomic ? KCSAN_ACCESS_ATOMIC : 0)); \
908+
(is_atomic ? KCSAN_ACCESS_ATOMIC : 0), \
909+
_RET_IP_); \
905910
} \
906911
EXPORT_SYMBOL(__tsan_volatile_write##size); \
907912
void __tsan_unaligned_volatile_write##size(void *ptr) \
@@ -955,7 +960,7 @@ EXPORT_SYMBOL(__tsan_init);
955960
u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder) \
956961
{ \
957962
if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
958-
check_access(ptr, bits / BITS_PER_BYTE, KCSAN_ACCESS_ATOMIC); \
963+
check_access(ptr, bits / BITS_PER_BYTE, KCSAN_ACCESS_ATOMIC, _RET_IP_); \
959964
} \
960965
return __atomic_load_n(ptr, memorder); \
961966
} \
@@ -965,7 +970,7 @@ EXPORT_SYMBOL(__tsan_init);
965970
{ \
966971
if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
967972
check_access(ptr, bits / BITS_PER_BYTE, \
968-
KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC); \
973+
KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC, _RET_IP_); \
969974
} \
970975
__atomic_store_n(ptr, v, memorder); \
971976
} \
@@ -978,7 +983,7 @@ EXPORT_SYMBOL(__tsan_init);
978983
if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
979984
check_access(ptr, bits / BITS_PER_BYTE, \
980985
KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
981-
KCSAN_ACCESS_ATOMIC); \
986+
KCSAN_ACCESS_ATOMIC, _RET_IP_); \
982987
} \
983988
return __atomic_##op##suffix(ptr, v, memorder); \
984989
} \
@@ -1010,7 +1015,7 @@ EXPORT_SYMBOL(__tsan_init);
10101015
if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
10111016
check_access(ptr, bits / BITS_PER_BYTE, \
10121017
KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
1013-
KCSAN_ACCESS_ATOMIC); \
1018+
KCSAN_ACCESS_ATOMIC, _RET_IP_); \
10141019
} \
10151020
return __atomic_compare_exchange_n(ptr, exp, val, weak, mo, fail_mo); \
10161021
} \
@@ -1025,7 +1030,7 @@ EXPORT_SYMBOL(__tsan_init);
10251030
if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
10261031
check_access(ptr, bits / BITS_PER_BYTE, \
10271032
KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
1028-
KCSAN_ACCESS_ATOMIC); \
1033+
KCSAN_ACCESS_ATOMIC, _RET_IP_); \
10291034
} \
10301035
__atomic_compare_exchange_n(ptr, &exp, val, 0, mo, fail_mo); \
10311036
return exp; \

kernel/kcsan/kcsan.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,22 @@ enum kcsan_value_change {
121121
* to be consumed by the reporting thread. No report is printed yet.
122122
*/
123123
void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_type,
124-
int watchpoint_idx);
124+
unsigned long ip, int watchpoint_idx);
125125

126126
/*
127127
* The calling thread observed that the watchpoint it set up was hit and
128128
* consumed: print the full report based on information set by the racing
129129
* thread.
130130
*/
131131
void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access_type,
132-
enum kcsan_value_change value_change, int watchpoint_idx,
133-
u64 old, u64 new, u64 mask);
132+
unsigned long ip, enum kcsan_value_change value_change,
133+
int watchpoint_idx, u64 old, u64 new, u64 mask);
134134

135135
/*
136136
* No other thread was observed to race with the access, but the data value
137137
* before and after the stall differs. Reports a race of "unknown origin".
138138
*/
139139
void kcsan_report_unknown_origin(const volatile void *ptr, size_t size, int access_type,
140-
u64 old, u64 new, u64 mask);
140+
unsigned long ip, u64 old, u64 new, u64 mask);
141141

142142
#endif /* _KERNEL_KCSAN_KCSAN_H */

kernel/kcsan/report.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct access_info {
3131
int access_type;
3232
int task_pid;
3333
int cpu_id;
34+
unsigned long ip;
3435
};
3536

3637
/*
@@ -576,21 +577,22 @@ static bool prepare_report_consumer(unsigned long *flags,
576577
}
577578

578579
static struct access_info prepare_access_info(const volatile void *ptr, size_t size,
579-
int access_type)
580+
int access_type, unsigned long ip)
580581
{
581582
return (struct access_info) {
582583
.ptr = ptr,
583584
.size = size,
584585
.access_type = access_type,
585586
.task_pid = in_task() ? task_pid_nr(current) : -1,
586-
.cpu_id = raw_smp_processor_id()
587+
.cpu_id = raw_smp_processor_id(),
588+
.ip = ip,
587589
};
588590
}
589591

590592
void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_type,
591-
int watchpoint_idx)
593+
unsigned long ip, int watchpoint_idx)
592594
{
593-
const struct access_info ai = prepare_access_info(ptr, size, access_type);
595+
const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
594596
unsigned long flags;
595597

596598
kcsan_disable_current();
@@ -603,10 +605,10 @@ void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_typ
603605
}
604606

605607
void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access_type,
606-
enum kcsan_value_change value_change, int watchpoint_idx,
607-
u64 old, u64 new, u64 mask)
608+
unsigned long ip, enum kcsan_value_change value_change,
609+
int watchpoint_idx, u64 old, u64 new, u64 mask)
608610
{
609-
const struct access_info ai = prepare_access_info(ptr, size, access_type);
611+
const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
610612
struct other_info *other_info = &other_infos[watchpoint_idx];
611613
unsigned long flags = 0;
612614

@@ -637,9 +639,9 @@ void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access
637639
}
638640

639641
void kcsan_report_unknown_origin(const volatile void *ptr, size_t size, int access_type,
640-
u64 old, u64 new, u64 mask)
642+
unsigned long ip, u64 old, u64 new, u64 mask)
641643
{
642-
const struct access_info ai = prepare_access_info(ptr, size, access_type);
644+
const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
643645
unsigned long flags;
644646

645647
kcsan_disable_current();

0 commit comments

Comments
 (0)