Skip to content

Commit cd2eda5

Browse files
David Hildenbrandgregkh
authored andcommitted
powerpc/powernv/memtrace: Fix crashing the kernel when enabling concurrently
commit d671894 upstream. It's very easy to crash the kernel right now by simply trying to enable memtrace concurrently, hammering on the "enable" interface loop.sh: #!/bin/bash dmesg --console-off while true; do echo 0x40000000 > /sys/kernel/debug/powerpc/memtrace/enable done [root@localhost ~]# loop.sh & [root@localhost ~]# loop.sh & Resulting quickly in a kernel crash. Let's properly protect using a mutex. Fixes: 9d5171a ("powerpc/powernv: Enable removal of memory for in memory tracing") Cc: stable@vger.kernel.org# v4.14+ Signed-off-by: David Hildenbrand <david@redhat.com> Reviewed-by: Oscar Salvador <osalvador@suse.de> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20201111145322.15793-3-david@redhat.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4b8dcb0 commit cd2eda5

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

‎arch/powerpc/platforms/powernv/memtrace.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct memtrace_entry {
3030
char name[16];
3131
};
3232

33+
static DEFINE_MUTEX(memtrace_mutex);
3334
static u64 memtrace_size;
3435

3536
static struct memtrace_entry *memtrace_array;
@@ -279,6 +280,7 @@ static int memtrace_online(void)
279280

280281
static int memtrace_enable_set(void *data, u64 val)
281282
{
283+
int rc = -EAGAIN;
282284
u64 bytes;
283285

284286
/*
@@ -291,25 +293,31 @@ static int memtrace_enable_set(void *data, u64 val)
291293
return -EINVAL;
292294
}
293295

296+
mutex_lock(&memtrace_mutex);
297+
294298
/* Re-add/online previously removed/offlined memory */
295299
if (memtrace_size) {
296300
if (memtrace_online())
297-
return -EAGAIN;
301+
goto out_unlock;
298302
}
299303

300-
if (!val)
301-
return 0;
304+
if (!val) {
305+
rc = 0;
306+
goto out_unlock;
307+
}
302308

303309
/* Offline and remove memory */
304310
if (memtrace_init_regions_runtime(val))
305-
return -EINVAL;
311+
goto out_unlock;
306312

307313
if (memtrace_init_debugfs())
308-
return -EINVAL;
314+
goto out_unlock;
309315

310316
memtrace_size = val;
311-
312-
return 0;
317+
rc = 0;
318+
out_unlock:
319+
mutex_unlock(&memtrace_mutex);
320+
return rc;
313321
}
314322

315323
static int memtrace_enable_get(void *data, u64 *val)

0 commit comments

Comments
 (0)