Skip to content

Commit 7bb5da0

Browse files
valschneiderakpm00
authored andcommitted
kexec: turn all kexec_mutex acquisitions into trylocks
Patch series "kexec, panic: Making crash_kexec() NMI safe", v4. This patch (of 2): Most acquistions of kexec_mutex are done via mutex_trylock() - those were a direct "translation" from: 8c5a1cf ("kexec: use a mutex for locking rather than xchg()") there have however been two additions since then that use mutex_lock(): crash_get_memory_size() and crash_shrink_memory(). A later commit will replace said mutex with an atomic variable, and locking operations will become atomic_cmpxchg(). Rather than having those mutex_lock() become while (atomic_cmpxchg(&lock, 0, 1)), turn them into trylocks that can return -EBUSY on acquisition failure. This does halve the printable size of the crash kernel, but that's still neighbouring 2G for 32bit kernels which should be ample enough. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Valentin Schneider <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: "Eric W . Biederman" <[email protected]> Cc: Juri Lelli <[email protected]> Cc: Luis Claudio R. Goncalves <[email protected]> Cc: Miaohe Lin <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Baoquan He <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 9847f21 commit 7bb5da0

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

include/linux/kexec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ extern int kexec_load_disabled;
427427
extern bool kexec_in_progress;
428428

429429
int crash_shrink_memory(unsigned long new_size);
430-
size_t crash_get_memory_size(void);
430+
ssize_t crash_get_memory_size(void);
431431

432432
#ifndef arch_kexec_protect_crashkres
433433
/*

kernel/kexec_core.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,16 @@ void crash_kexec(struct pt_regs *regs)
10041004
}
10051005
}
10061006

1007-
size_t crash_get_memory_size(void)
1007+
ssize_t crash_get_memory_size(void)
10081008
{
1009-
size_t size = 0;
1009+
ssize_t size = 0;
1010+
1011+
if (!mutex_trylock(&kexec_mutex))
1012+
return -EBUSY;
10101013

1011-
mutex_lock(&kexec_mutex);
10121014
if (crashk_res.end != crashk_res.start)
10131015
size = resource_size(&crashk_res);
1016+
10141017
mutex_unlock(&kexec_mutex);
10151018
return size;
10161019
}
@@ -1022,7 +1025,8 @@ int crash_shrink_memory(unsigned long new_size)
10221025
unsigned long old_size;
10231026
struct resource *ram_res;
10241027

1025-
mutex_lock(&kexec_mutex);
1028+
if (!mutex_trylock(&kexec_mutex))
1029+
return -EBUSY;
10261030

10271031
if (kexec_crash_image) {
10281032
ret = -ENOENT;

kernel/ksysfs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ KERNEL_ATTR_RO(kexec_crash_loaded);
105105
static ssize_t kexec_crash_size_show(struct kobject *kobj,
106106
struct kobj_attribute *attr, char *buf)
107107
{
108-
return sprintf(buf, "%zu\n", crash_get_memory_size());
108+
ssize_t size = crash_get_memory_size();
109+
110+
if (size < 0)
111+
return size;
112+
113+
return sprintf(buf, "%zd\n", size);
109114
}
110115
static ssize_t kexec_crash_size_store(struct kobject *kobj,
111116
struct kobj_attribute *attr,

0 commit comments

Comments
 (0)