Skip to content

Commit 7c51f7b

Browse files
Tetsuo Handatorvalds
authored andcommitted
profiling: remove prof_cpu_mask
syzbot is reporting uninit-value at profile_hits(), for there is a race window between if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL)) return -ENOMEM; cpumask_copy(prof_cpu_mask, cpu_possible_mask); in profile_init() and cpumask_available(prof_cpu_mask) && cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)) in profile_tick(); prof_cpu_mask remains uninitialzed until cpumask_copy() completes while cpumask_available(prof_cpu_mask) returns true as soon as alloc_cpumask_var(&prof_cpu_mask) completes. We could replace alloc_cpumask_var() with zalloc_cpumask_var() and call cpumask_copy() from create_proc_profile() on only UP kernels, for profile_online_cpu() calls cpumask_set_cpu() as needed via cpuhp_setup_state(CPUHP_AP_ONLINE_DYN) on SMP kernels. But this patch removes prof_cpu_mask because it seems unnecessary. The cpumask_test_cpu(smp_processor_id(), prof_cpu_mask) test in profile_tick() is likely always true due to a CPU cannot call profile_tick() if that CPU is offline and cpumask_set_cpu(cpu, prof_cpu_mask) is called when that CPU becomes online and cpumask_clear_cpu(cpu, prof_cpu_mask) is called when that CPU becomes offline . This test could be false during transition between online and offline. But according to include/linux/cpuhotplug.h , CPUHP_PROFILE_PREPARE belongs to PREPARE section, which means that the CPU subjected to profile_dead_cpu() cannot be inside profile_tick() (i.e. no risk of use-after-free bug) because interrupt for that CPU is disabled during PREPARE section. Therefore, this test is guaranteed to be true, and can be removed. (Since profile_hits() checks prof_buffer != NULL, we don't need to check prof_buffer != NULL here unless get_irq_regs() or user_mode() is such slow that we want to avoid when prof_buffer == NULL). do_profile_hits() is called from profile_tick() from timer interrupt only if cpumask_test_cpu(smp_processor_id(), prof_cpu_mask) is true and prof_buffer is not NULL. But syzbot is also reporting that sometimes do_profile_hits() is called while current thread is still doing vzalloc(), where prof_buffer must be NULL at this moment. This indicates that multiple threads concurrently tried to write to /sys/kernel/profiling interface, which caused that somebody else try to re-allocate prof_buffer despite somebody has already allocated prof_buffer. Fix this by using serialization. Reported-by: syzbot <[email protected]> Closes: https://syzkaller.appspot.com/bug?extid=b1a83ab2a9eb9321fbdd Signed-off-by: Tetsuo Handa <[email protected]> Tested-by: syzbot <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 99d3bf5 commit 7c51f7b

File tree

2 files changed

+13
-40
lines changed

2 files changed

+13
-40
lines changed

kernel/ksysfs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ static ssize_t profiling_store(struct kobject *kobj,
9292
const char *buf, size_t count)
9393
{
9494
int ret;
95+
static DEFINE_MUTEX(lock);
9596

97+
/*
98+
* We need serialization, for profile_setup() initializes prof_on
99+
* value and profile_init() must not reallocate prof_buffer after
100+
* once allocated.
101+
*/
102+
guard(mutex)(&lock);
96103
if (prof_on)
97104
return -EEXIST;
98105
/*

kernel/profile.c

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ static unsigned short int prof_shift;
4747
int prof_on __read_mostly;
4848
EXPORT_SYMBOL_GPL(prof_on);
4949

50-
static cpumask_var_t prof_cpu_mask;
5150
#if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
5251
static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
5352
static DEFINE_PER_CPU(int, cpu_profile_flip);
@@ -114,11 +113,6 @@ int __ref profile_init(void)
114113

115114
buffer_bytes = prof_len*sizeof(atomic_t);
116115

117-
if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
118-
return -ENOMEM;
119-
120-
cpumask_copy(prof_cpu_mask, cpu_possible_mask);
121-
122116
prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
123117
if (prof_buffer)
124118
return 0;
@@ -132,7 +126,6 @@ int __ref profile_init(void)
132126
if (prof_buffer)
133127
return 0;
134128

135-
free_cpumask_var(prof_cpu_mask);
136129
return -ENOMEM;
137130
}
138131

@@ -267,9 +260,6 @@ static int profile_dead_cpu(unsigned int cpu)
267260
struct page *page;
268261
int i;
269262

270-
if (cpumask_available(prof_cpu_mask))
271-
cpumask_clear_cpu(cpu, prof_cpu_mask);
272-
273263
for (i = 0; i < 2; i++) {
274264
if (per_cpu(cpu_profile_hits, cpu)[i]) {
275265
page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]);
@@ -302,14 +292,6 @@ static int profile_prepare_cpu(unsigned int cpu)
302292
return 0;
303293
}
304294

305-
static int profile_online_cpu(unsigned int cpu)
306-
{
307-
if (cpumask_available(prof_cpu_mask))
308-
cpumask_set_cpu(cpu, prof_cpu_mask);
309-
310-
return 0;
311-
}
312-
313295
#else /* !CONFIG_SMP */
314296
#define profile_flip_buffers() do { } while (0)
315297
#define profile_discard_flip_buffers() do { } while (0)
@@ -334,8 +316,8 @@ void profile_tick(int type)
334316
{
335317
struct pt_regs *regs = get_irq_regs();
336318

337-
if (!user_mode(regs) && cpumask_available(prof_cpu_mask) &&
338-
cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
319+
/* This is the old kernel-only legacy profiling */
320+
if (!user_mode(regs))
339321
profile_hit(type, (void *)profile_pc(regs));
340322
}
341323

@@ -418,10 +400,6 @@ static const struct proc_ops profile_proc_ops = {
418400
int __ref create_proc_profile(void)
419401
{
420402
struct proc_dir_entry *entry;
421-
#ifdef CONFIG_SMP
422-
enum cpuhp_state online_state;
423-
#endif
424-
425403
int err = 0;
426404

427405
if (!prof_on)
@@ -431,26 +409,14 @@ int __ref create_proc_profile(void)
431409
profile_prepare_cpu, profile_dead_cpu);
432410
if (err)
433411
return err;
434-
435-
err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE",
436-
profile_online_cpu, NULL);
437-
if (err < 0)
438-
goto err_state_prep;
439-
online_state = err;
440-
err = 0;
441412
#endif
442413
entry = proc_create("profile", S_IWUSR | S_IRUGO,
443414
NULL, &profile_proc_ops);
444-
if (!entry)
445-
goto err_state_onl;
446-
proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
447-
448-
return err;
449-
err_state_onl:
415+
if (entry)
416+
proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
450417
#ifdef CONFIG_SMP
451-
cpuhp_remove_state(online_state);
452-
err_state_prep:
453-
cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
418+
else
419+
cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
454420
#endif
455421
return err;
456422
}

0 commit comments

Comments
 (0)