forked from mirrors/linux
		
	pstore: Convert buf_lock to semaphore
Instead of running with interrupts disabled, use a semaphore. This should
make it easier for backends that may need to sleep (e.g. EFI) when
performing a write:
|BUG: sleeping function called from invalid context at kernel/sched/completion.c:99
|in_atomic(): 1, irqs_disabled(): 1, pid: 2236, name: sig-xstate-bum
|Preemption disabled at:
|[<ffffffff99d60512>] pstore_dump+0x72/0x330
|CPU: 26 PID: 2236 Comm: sig-xstate-bum Tainted: G      D           4.20.0-rc3 #45
|Call Trace:
| dump_stack+0x4f/0x6a
| ___might_sleep.cold.91+0xd3/0xe4
| __might_sleep+0x50/0x90
| wait_for_completion+0x32/0x130
| virt_efi_query_variable_info+0x14e/0x160
| efi_query_variable_store+0x51/0x1a0
| efivar_entry_set_safe+0xa3/0x1b0
| efi_pstore_write+0x109/0x140
| pstore_dump+0x11c/0x330
| kmsg_dump+0xa4/0xd0
| oops_exit+0x22/0x30
...
Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Fixes: 21b3ddd39f ("efi: Don't use spinlocks for efi vars")
Signed-off-by: Kees Cook <keescook@chromium.org>
			
			
This commit is contained in:
		
							parent
							
								
									69596433bc
								
							
						
					
					
						commit
						ea84b580b9
					
				
					 6 changed files with 27 additions and 32 deletions
				
			
		| 
						 | 
					@ -563,8 +563,6 @@ static int nvram_pstore_init(void)
 | 
				
			||||||
	nvram_pstore_info.buf = oops_data;
 | 
						nvram_pstore_info.buf = oops_data;
 | 
				
			||||||
	nvram_pstore_info.bufsize = oops_data_sz;
 | 
						nvram_pstore_info.bufsize = oops_data_sz;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	spin_lock_init(&nvram_pstore_info.buf_lock);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rc = pstore_register(&nvram_pstore_info);
 | 
						rc = pstore_register(&nvram_pstore_info);
 | 
				
			||||||
	if (rc && (rc != -EPERM))
 | 
						if (rc && (rc != -EPERM))
 | 
				
			||||||
		/* Print error only when pstore.backend == nvram */
 | 
							/* Print error only when pstore.backend == nvram */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1176,7 +1176,6 @@ static int __init erst_init(void)
 | 
				
			||||||
	"Error Record Serialization Table (ERST) support is initialized.\n");
 | 
						"Error Record Serialization Table (ERST) support is initialized.\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	buf = kmalloc(erst_erange.size, GFP_KERNEL);
 | 
						buf = kmalloc(erst_erange.size, GFP_KERNEL);
 | 
				
			||||||
	spin_lock_init(&erst_info.buf_lock);
 | 
					 | 
				
			||||||
	if (buf) {
 | 
						if (buf) {
 | 
				
			||||||
		erst_info.buf = buf + sizeof(struct cper_pstore_record);
 | 
							erst_info.buf = buf + sizeof(struct cper_pstore_record);
 | 
				
			||||||
		erst_info.bufsize = erst_erange.size -
 | 
							erst_info.bufsize = erst_erange.size -
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -259,8 +259,7 @@ static int efi_pstore_write(struct pstore_record *record)
 | 
				
			||||||
		efi_name[i] = name[i];
 | 
							efi_name[i] = name[i];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
 | 
						ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
 | 
				
			||||||
			      !pstore_cannot_block_path(record->reason),
 | 
								      preemptible(), record->size, record->psi->buf);
 | 
				
			||||||
			      record->size, record->psi->buf);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (record->reason == KMSG_DUMP_OOPS)
 | 
						if (record->reason == KMSG_DUMP_OOPS)
 | 
				
			||||||
		efivar_run_worker();
 | 
							efivar_run_worker();
 | 
				
			||||||
| 
						 | 
					@ -369,7 +368,6 @@ static __init int efivars_pstore_init(void)
 | 
				
			||||||
		return -ENOMEM;
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	efi_pstore_info.bufsize = 1024;
 | 
						efi_pstore_info.bufsize = 1024;
 | 
				
			||||||
	spin_lock_init(&efi_pstore_info.buf_lock);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (pstore_register(&efi_pstore_info)) {
 | 
						if (pstore_register(&efi_pstore_info)) {
 | 
				
			||||||
		kfree(efi_pstore_info.buf);
 | 
							kfree(efi_pstore_info.buf);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -161,26 +161,27 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
	 * In case of NMI path, pstore shouldn't be blocked
 | 
					 * Should pstore_dump() wait for a concurrent pstore_dump()? If
 | 
				
			||||||
	 * regardless of reason.
 | 
					 * not, the current pstore_dump() will report a failure to dump
 | 
				
			||||||
 | 
					 * and return.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* In NMI path, pstore shouldn't block regardless of reason. */
 | 
				
			||||||
	if (in_nmi())
 | 
						if (in_nmi())
 | 
				
			||||||
		return true;
 | 
							return true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (reason) {
 | 
						switch (reason) {
 | 
				
			||||||
	/* In panic case, other cpus are stopped by smp_send_stop(). */
 | 
						/* In panic case, other cpus are stopped by smp_send_stop(). */
 | 
				
			||||||
	case KMSG_DUMP_PANIC:
 | 
						case KMSG_DUMP_PANIC:
 | 
				
			||||||
	/* Emergency restart shouldn't be blocked by spin lock. */
 | 
						/* Emergency restart shouldn't be blocked. */
 | 
				
			||||||
	case KMSG_DUMP_EMERG:
 | 
						case KMSG_DUMP_EMERG:
 | 
				
			||||||
		return true;
 | 
							return true;
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
 | 
					#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
 | 
				
			||||||
static int zbufsize_deflate(size_t size)
 | 
					static int zbufsize_deflate(size_t size)
 | 
				
			||||||
| 
						 | 
					@ -400,23 +401,23 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 | 
				
			||||||
	unsigned long	total = 0;
 | 
						unsigned long	total = 0;
 | 
				
			||||||
	const char	*why;
 | 
						const char	*why;
 | 
				
			||||||
	unsigned int	part = 1;
 | 
						unsigned int	part = 1;
 | 
				
			||||||
	unsigned long	flags = 0;
 | 
					 | 
				
			||||||
	int		is_locked;
 | 
					 | 
				
			||||||
	int		ret;
 | 
						int		ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	why = get_reason_str(reason);
 | 
						why = get_reason_str(reason);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (pstore_cannot_block_path(reason)) {
 | 
						if (down_trylock(&psinfo->buf_lock)) {
 | 
				
			||||||
		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
 | 
							/* Failed to acquire lock: give up if we cannot wait. */
 | 
				
			||||||
		if (!is_locked) {
 | 
							if (pstore_cannot_wait(reason)) {
 | 
				
			||||||
			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
 | 
								pr_err("dump skipped in %s path: may corrupt error record\n",
 | 
				
			||||||
				       , in_nmi() ? "NMI" : why);
 | 
									in_nmi() ? "NMI" : why);
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
							if (down_interruptible(&psinfo->buf_lock)) {
 | 
				
			||||||
		spin_lock_irqsave(&psinfo->buf_lock, flags);
 | 
								pr_err("could not grab semaphore?!\n");
 | 
				
			||||||
		is_locked = 1;
 | 
								return;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	oopscount++;
 | 
						oopscount++;
 | 
				
			||||||
	while (total < kmsg_bytes) {
 | 
						while (total < kmsg_bytes) {
 | 
				
			||||||
		char *dst;
 | 
							char *dst;
 | 
				
			||||||
| 
						 | 
					@ -433,7 +434,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 | 
				
			||||||
		record.part = part;
 | 
							record.part = part;
 | 
				
			||||||
		record.buf = psinfo->buf;
 | 
							record.buf = psinfo->buf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (big_oops_buf && is_locked) {
 | 
							if (big_oops_buf) {
 | 
				
			||||||
			dst = big_oops_buf;
 | 
								dst = big_oops_buf;
 | 
				
			||||||
			dst_size = big_oops_buf_sz;
 | 
								dst_size = big_oops_buf_sz;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
| 
						 | 
					@ -451,7 +452,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 | 
				
			||||||
					  dst_size, &dump_size))
 | 
										  dst_size, &dump_size))
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (big_oops_buf && is_locked) {
 | 
							if (big_oops_buf) {
 | 
				
			||||||
			zipped_len = pstore_compress(dst, psinfo->buf,
 | 
								zipped_len = pstore_compress(dst, psinfo->buf,
 | 
				
			||||||
						header_size + dump_size,
 | 
											header_size + dump_size,
 | 
				
			||||||
						psinfo->bufsize);
 | 
											psinfo->bufsize);
 | 
				
			||||||
| 
						 | 
					@ -474,8 +475,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 | 
				
			||||||
		total += record.size;
 | 
							total += record.size;
 | 
				
			||||||
		part++;
 | 
							part++;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (is_locked)
 | 
					
 | 
				
			||||||
		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
 | 
						up(&psinfo->buf_lock);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct kmsg_dumper pstore_dumper = {
 | 
					static struct kmsg_dumper pstore_dumper = {
 | 
				
			||||||
| 
						 | 
					@ -594,6 +595,7 @@ int pstore_register(struct pstore_info *psi)
 | 
				
			||||||
		psi->write_user = pstore_write_user_compat;
 | 
							psi->write_user = pstore_write_user_compat;
 | 
				
			||||||
	psinfo = psi;
 | 
						psinfo = psi;
 | 
				
			||||||
	mutex_init(&psinfo->read_mutex);
 | 
						mutex_init(&psinfo->read_mutex);
 | 
				
			||||||
 | 
						sema_init(&psinfo->buf_lock, 1);
 | 
				
			||||||
	spin_unlock(&pstore_lock);
 | 
						spin_unlock(&pstore_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (owner && !try_module_get(owner)) {
 | 
						if (owner && !try_module_get(owner)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -815,7 +815,6 @@ static int ramoops_probe(struct platform_device *pdev)
 | 
				
			||||||
		err = -ENOMEM;
 | 
							err = -ENOMEM;
 | 
				
			||||||
		goto fail_clear;
 | 
							goto fail_clear;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	spin_lock_init(&cxt->pstore.buf_lock);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cxt->pstore.flags = PSTORE_FLAGS_DMESG;
 | 
						cxt->pstore.flags = PSTORE_FLAGS_DMESG;
 | 
				
			||||||
	if (cxt->console_size)
 | 
						if (cxt->console_size)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,7 +26,7 @@
 | 
				
			||||||
#include <linux/errno.h>
 | 
					#include <linux/errno.h>
 | 
				
			||||||
#include <linux/kmsg_dump.h>
 | 
					#include <linux/kmsg_dump.h>
 | 
				
			||||||
#include <linux/mutex.h>
 | 
					#include <linux/mutex.h>
 | 
				
			||||||
#include <linux/spinlock.h>
 | 
					#include <linux/semaphore.h>
 | 
				
			||||||
#include <linux/time.h>
 | 
					#include <linux/time.h>
 | 
				
			||||||
#include <linux/types.h>
 | 
					#include <linux/types.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -99,7 +99,7 @@ struct pstore_record {
 | 
				
			||||||
 * @owner:	module which is responsible for this backend driver
 | 
					 * @owner:	module which is responsible for this backend driver
 | 
				
			||||||
 * @name:	name of the backend driver
 | 
					 * @name:	name of the backend driver
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @buf_lock:	spinlock to serialize access to @buf
 | 
					 * @buf_lock:	semaphore to serialize access to @buf
 | 
				
			||||||
 * @buf:	preallocated crash dump buffer
 | 
					 * @buf:	preallocated crash dump buffer
 | 
				
			||||||
 * @bufsize:	size of @buf available for crash dump bytes (must match
 | 
					 * @bufsize:	size of @buf available for crash dump bytes (must match
 | 
				
			||||||
 *		smallest number of bytes available for writing to a
 | 
					 *		smallest number of bytes available for writing to a
 | 
				
			||||||
| 
						 | 
					@ -184,7 +184,7 @@ struct pstore_info {
 | 
				
			||||||
	struct module	*owner;
 | 
						struct module	*owner;
 | 
				
			||||||
	char		*name;
 | 
						char		*name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	spinlock_t	buf_lock;
 | 
						struct semaphore buf_lock;
 | 
				
			||||||
	char		*buf;
 | 
						char		*buf;
 | 
				
			||||||
	size_t		bufsize;
 | 
						size_t		bufsize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -210,7 +210,6 @@ struct pstore_info {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern int pstore_register(struct pstore_info *);
 | 
					extern int pstore_register(struct pstore_info *);
 | 
				
			||||||
extern void pstore_unregister(struct pstore_info *);
 | 
					extern void pstore_unregister(struct pstore_info *);
 | 
				
			||||||
extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct pstore_ftrace_record {
 | 
					struct pstore_ftrace_record {
 | 
				
			||||||
	unsigned long ip;
 | 
						unsigned long ip;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue