2019-02-02 17:41:15 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
|
2013-02-08 23:48:51 +08:00
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pstore.h>
|
2013-05-02 08:51:54 +08:00
|
|
|
#include <linux/slab.h>
|
2013-04-30 18:30:24 +08:00
|
|
|
#include <linux/ucs2_string.h>
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
MODULE_IMPORT_NS(EFIVAR);
|
|
|
|
|
2017-05-20 04:21:07 +08:00
|
|
|
#define DUMP_NAME_LEN 66
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2020-09-23 15:56:14 +08:00
|
|
|
#define EFIVARS_DATA_SIZE_MAX 1024
|
|
|
|
|
2013-02-08 23:48:51 +08:00
|
|
|
static bool efivars_pstore_disable =
|
|
|
|
IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
|
|
|
|
|
|
|
|
module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
|
|
|
|
|
|
|
|
#define PSTORE_EFI_ATTRIBUTES \
|
|
|
|
(EFI_VARIABLE_NON_VOLATILE | \
|
|
|
|
EFI_VARIABLE_BOOTSERVICE_ACCESS | \
|
|
|
|
EFI_VARIABLE_RUNTIME_ACCESS)
|
|
|
|
|
|
|
|
static int efi_pstore_open(struct pstore_info *psi)
|
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = efivar_lock();
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
psi->data = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
|
|
|
|
if (!psi->data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-02-08 23:48:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int efi_pstore_close(struct pstore_info *psi)
|
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
efivar_unlock();
|
|
|
|
kfree(psi->data);
|
2013-02-08 23:48:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-15 06:50:52 +08:00
|
|
|
static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
|
2013-11-29 15:58:57 +08:00
|
|
|
{
|
2018-05-15 06:50:52 +08:00
|
|
|
return (timestamp * 100 + part) * 1000 + count;
|
2013-11-29 15:58:57 +08:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
static int efi_pstore_read_func(struct pstore_record *record,
|
|
|
|
efi_char16_t *varname)
|
2013-02-08 23:48:51 +08:00
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
unsigned long wlen, size = EFIVARS_DATA_SIZE_MAX;
|
2013-08-17 04:57:51 +08:00
|
|
|
char name[DUMP_NAME_LEN], data_type;
|
2022-06-20 19:21:26 +08:00
|
|
|
efi_status_t status;
|
2013-02-08 23:48:51 +08:00
|
|
|
int cnt;
|
|
|
|
unsigned int part;
|
2018-05-15 06:50:52 +08:00
|
|
|
u64 time;
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
ucs2_as_utf8(name, varname, DUMP_NAME_LEN);
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2018-05-15 06:50:52 +08:00
|
|
|
if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
|
2017-03-04 14:09:18 +08:00
|
|
|
&record->type, &part, &cnt, &time, &data_type) == 5) {
|
|
|
|
record->id = generic_id(time, part, cnt);
|
2017-05-19 04:07:49 +08:00
|
|
|
record->part = part;
|
2017-03-04 14:09:18 +08:00
|
|
|
record->count = cnt;
|
|
|
|
record->time.tv_sec = time;
|
|
|
|
record->time.tv_nsec = 0;
|
2013-08-17 04:57:51 +08:00
|
|
|
if (data_type == 'C')
|
2017-03-04 14:09:18 +08:00
|
|
|
record->compressed = true;
|
2013-08-17 04:57:51 +08:00
|
|
|
else
|
2017-03-04 14:09:18 +08:00
|
|
|
record->compressed = false;
|
|
|
|
record->ecc_notice_size = 0;
|
2018-05-15 06:50:52 +08:00
|
|
|
} else if (sscanf(name, "dump-type%u-%u-%d-%llu",
|
2017-03-04 14:09:18 +08:00
|
|
|
&record->type, &part, &cnt, &time) == 4) {
|
|
|
|
record->id = generic_id(time, part, cnt);
|
2017-05-19 04:07:49 +08:00
|
|
|
record->part = part;
|
2017-03-04 14:09:18 +08:00
|
|
|
record->count = cnt;
|
|
|
|
record->time.tv_sec = time;
|
|
|
|
record->time.tv_nsec = 0;
|
|
|
|
record->compressed = false;
|
|
|
|
record->ecc_notice_size = 0;
|
2018-05-15 06:50:52 +08:00
|
|
|
} else if (sscanf(name, "dump-type%u-%u-%llu",
|
2017-03-04 14:09:18 +08:00
|
|
|
&record->type, &part, &time) == 3) {
|
2013-02-08 23:48:51 +08:00
|
|
|
/*
|
|
|
|
* Check if an old format,
|
|
|
|
* which doesn't support holding
|
|
|
|
* multiple logs, remains.
|
|
|
|
*/
|
2017-03-04 14:09:18 +08:00
|
|
|
record->id = generic_id(time, part, 0);
|
2017-05-19 04:07:49 +08:00
|
|
|
record->part = part;
|
2017-03-04 14:09:18 +08:00
|
|
|
record->count = 0;
|
|
|
|
record->time.tv_sec = time;
|
|
|
|
record->time.tv_nsec = 0;
|
|
|
|
record->compressed = false;
|
|
|
|
record->ecc_notice_size = 0;
|
2013-02-08 23:48:51 +08:00
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
record->buf = kmalloc(size, GFP_KERNEL);
|
|
|
|
if (!record->buf)
|
|
|
|
return -ENOMEM;
|
efivars, efi-pstore: Hold off deletion of sysfs entry until the scan is completed
Currently, when mounting pstore file system, a read callback of
efi_pstore driver runs mutiple times as below.
- In the first read callback, scan efivar_sysfs_list from head and pass
a kmsg buffer of a entry to an upper pstore layer.
- In the second read callback, rescan efivar_sysfs_list from the entry
and pass another kmsg buffer to it.
- Repeat the scan and pass until the end of efivar_sysfs_list.
In this process, an entry is read across the multiple read function
calls. To avoid race between the read and erasion, the whole process
above is protected by a spinlock, holding in open() and releasing in
close().
At the same time, kmemdup() is called to pass the buffer to pstore
filesystem during it. And then, it causes a following lockdep warning.
To make the dynamic memory allocation runnable without taking spinlock,
holding off a deletion of sysfs entry if it happens while scanning it
via efi_pstore, and deleting it after the scan is completed.
To implement it, this patch introduces two flags, scanning and deleting,
to efivar_entry.
On the code basis, it seems that all the scanning and deleting logic is
not needed because __efivars->lock are not dropped when reading from the
EFI variable store.
But, the scanning and deleting logic is still needed because an
efi-pstore and a pstore filesystem works as follows.
In case an entry(A) is found, the pointer is saved to psi->data. And
efi_pstore_read() passes the entry(A) to a pstore filesystem by
releasing __efivars->lock.
And then, the pstore filesystem calls efi_pstore_read() again and the
same entry(A), which is saved to psi->data, is used for resuming to scan
a sysfs-list.
So, to protect the entry(A), the logic is needed.
[ 1.143710] ------------[ cut here ]------------
[ 1.144058] WARNING: CPU: 1 PID: 1 at kernel/lockdep.c:2740 lockdep_trace_alloc+0x104/0x110()
[ 1.144058] DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))
[ 1.144058] Modules linked in:
[ 1.144058] CPU: 1 PID: 1 Comm: systemd Not tainted 3.11.0-rc5 #2
[ 1.144058] 0000000000000009 ffff8800797e9ae0 ffffffff816614a5 ffff8800797e9b28
[ 1.144058] ffff8800797e9b18 ffffffff8105510d 0000000000000080 0000000000000046
[ 1.144058] 00000000000000d0 00000000000003af ffffffff81ccd0c0 ffff8800797e9b78
[ 1.144058] Call Trace:
[ 1.144058] [<ffffffff816614a5>] dump_stack+0x54/0x74
[ 1.144058] [<ffffffff8105510d>] warn_slowpath_common+0x7d/0xa0
[ 1.144058] [<ffffffff8105517c>] warn_slowpath_fmt+0x4c/0x50
[ 1.144058] [<ffffffff8131290f>] ? vsscanf+0x57f/0x7b0
[ 1.144058] [<ffffffff810bbd74>] lockdep_trace_alloc+0x104/0x110
[ 1.144058] [<ffffffff81192da0>] __kmalloc_track_caller+0x50/0x280
[ 1.144058] [<ffffffff815147bb>] ? efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff8115b260>] kmemdup+0x20/0x50
[ 1.144058] [<ffffffff815147bb>] efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff81514800>] ? efi_pstore_read_func.part.1+0x170/0x170
[ 1.144058] [<ffffffff815148b4>] efi_pstore_read_func+0xb4/0xe0
[ 1.144058] [<ffffffff81512b7b>] __efivar_entry_iter+0xfb/0x120
[ 1.144058] [<ffffffff8151428f>] efi_pstore_read+0x3f/0x50
[ 1.144058] [<ffffffff8128d7ba>] pstore_get_records+0x9a/0x150
[ 1.158207] [<ffffffff812af25c>] ? selinux_d_instantiate+0x1c/0x20
[ 1.158207] [<ffffffff8128ce30>] ? parse_options+0x80/0x80
[ 1.158207] [<ffffffff8128ced5>] pstore_fill_super+0xa5/0xc0
[ 1.158207] [<ffffffff811ae7d2>] mount_single+0xa2/0xd0
[ 1.158207] [<ffffffff8128ccf8>] pstore_mount+0x18/0x20
[ 1.158207] [<ffffffff811ae8b9>] mount_fs+0x39/0x1b0
[ 1.158207] [<ffffffff81160550>] ? __alloc_percpu+0x10/0x20
[ 1.158207] [<ffffffff811c9493>] vfs_kern_mount+0x63/0xf0
[ 1.158207] [<ffffffff811cbb0e>] do_mount+0x23e/0xa20
[ 1.158207] [<ffffffff8115b51b>] ? strndup_user+0x4b/0xf0
[ 1.158207] [<ffffffff811cc373>] SyS_mount+0x83/0xc0
[ 1.158207] [<ffffffff81673cc2>] system_call_fastpath+0x16/0x1b
[ 1.158207] ---[ end trace 61981bc62de9f6f4 ]---
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Tested-by: Madper Xie <cxie@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2013-10-31 03:27:26 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
status = efivar_get_variable(varname, &LINUX_EFI_CRASH_GUID, NULL,
|
|
|
|
&size, record->buf);
|
|
|
|
if (status != EFI_SUCCESS) {
|
|
|
|
kfree(record->buf);
|
|
|
|
return -EIO;
|
efivars, efi-pstore: Hold off deletion of sysfs entry until the scan is completed
Currently, when mounting pstore file system, a read callback of
efi_pstore driver runs mutiple times as below.
- In the first read callback, scan efivar_sysfs_list from head and pass
a kmsg buffer of a entry to an upper pstore layer.
- In the second read callback, rescan efivar_sysfs_list from the entry
and pass another kmsg buffer to it.
- Repeat the scan and pass until the end of efivar_sysfs_list.
In this process, an entry is read across the multiple read function
calls. To avoid race between the read and erasion, the whole process
above is protected by a spinlock, holding in open() and releasing in
close().
At the same time, kmemdup() is called to pass the buffer to pstore
filesystem during it. And then, it causes a following lockdep warning.
To make the dynamic memory allocation runnable without taking spinlock,
holding off a deletion of sysfs entry if it happens while scanning it
via efi_pstore, and deleting it after the scan is completed.
To implement it, this patch introduces two flags, scanning and deleting,
to efivar_entry.
On the code basis, it seems that all the scanning and deleting logic is
not needed because __efivars->lock are not dropped when reading from the
EFI variable store.
But, the scanning and deleting logic is still needed because an
efi-pstore and a pstore filesystem works as follows.
In case an entry(A) is found, the pointer is saved to psi->data. And
efi_pstore_read() passes the entry(A) to a pstore filesystem by
releasing __efivars->lock.
And then, the pstore filesystem calls efi_pstore_read() again and the
same entry(A), which is saved to psi->data, is used for resuming to scan
a sysfs-list.
So, to protect the entry(A), the logic is needed.
[ 1.143710] ------------[ cut here ]------------
[ 1.144058] WARNING: CPU: 1 PID: 1 at kernel/lockdep.c:2740 lockdep_trace_alloc+0x104/0x110()
[ 1.144058] DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))
[ 1.144058] Modules linked in:
[ 1.144058] CPU: 1 PID: 1 Comm: systemd Not tainted 3.11.0-rc5 #2
[ 1.144058] 0000000000000009 ffff8800797e9ae0 ffffffff816614a5 ffff8800797e9b28
[ 1.144058] ffff8800797e9b18 ffffffff8105510d 0000000000000080 0000000000000046
[ 1.144058] 00000000000000d0 00000000000003af ffffffff81ccd0c0 ffff8800797e9b78
[ 1.144058] Call Trace:
[ 1.144058] [<ffffffff816614a5>] dump_stack+0x54/0x74
[ 1.144058] [<ffffffff8105510d>] warn_slowpath_common+0x7d/0xa0
[ 1.144058] [<ffffffff8105517c>] warn_slowpath_fmt+0x4c/0x50
[ 1.144058] [<ffffffff8131290f>] ? vsscanf+0x57f/0x7b0
[ 1.144058] [<ffffffff810bbd74>] lockdep_trace_alloc+0x104/0x110
[ 1.144058] [<ffffffff81192da0>] __kmalloc_track_caller+0x50/0x280
[ 1.144058] [<ffffffff815147bb>] ? efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff8115b260>] kmemdup+0x20/0x50
[ 1.144058] [<ffffffff815147bb>] efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff81514800>] ? efi_pstore_read_func.part.1+0x170/0x170
[ 1.144058] [<ffffffff815148b4>] efi_pstore_read_func+0xb4/0xe0
[ 1.144058] [<ffffffff81512b7b>] __efivar_entry_iter+0xfb/0x120
[ 1.144058] [<ffffffff8151428f>] efi_pstore_read+0x3f/0x50
[ 1.144058] [<ffffffff8128d7ba>] pstore_get_records+0x9a/0x150
[ 1.158207] [<ffffffff812af25c>] ? selinux_d_instantiate+0x1c/0x20
[ 1.158207] [<ffffffff8128ce30>] ? parse_options+0x80/0x80
[ 1.158207] [<ffffffff8128ced5>] pstore_fill_super+0xa5/0xc0
[ 1.158207] [<ffffffff811ae7d2>] mount_single+0xa2/0xd0
[ 1.158207] [<ffffffff8128ccf8>] pstore_mount+0x18/0x20
[ 1.158207] [<ffffffff811ae8b9>] mount_fs+0x39/0x1b0
[ 1.158207] [<ffffffff81160550>] ? __alloc_percpu+0x10/0x20
[ 1.158207] [<ffffffff811c9493>] vfs_kern_mount+0x63/0xf0
[ 1.158207] [<ffffffff811cbb0e>] do_mount+0x23e/0xa20
[ 1.158207] [<ffffffff8115b51b>] ? strndup_user+0x4b/0xf0
[ 1.158207] [<ffffffff811cc373>] SyS_mount+0x83/0xc0
[ 1.158207] [<ffffffff81673cc2>] system_call_fastpath+0x16/0x1b
[ 1.158207] ---[ end trace 61981bc62de9f6f4 ]---
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Tested-by: Madper Xie <cxie@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2013-10-31 03:27:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
/*
|
|
|
|
* Store the name of the variable in the pstore_record priv field, so
|
|
|
|
* we can reuse it later if we need to delete the EFI variable from the
|
|
|
|
* variable store.
|
|
|
|
*/
|
|
|
|
wlen = (ucs2_strnlen(varname, DUMP_NAME_LEN) + 1) * sizeof(efi_char16_t);
|
|
|
|
record->priv = kmemdup(varname, wlen, GFP_KERNEL);
|
|
|
|
if (!record->priv) {
|
|
|
|
kfree(record->buf);
|
|
|
|
return -ENOMEM;
|
efivars, efi-pstore: Hold off deletion of sysfs entry until the scan is completed
Currently, when mounting pstore file system, a read callback of
efi_pstore driver runs mutiple times as below.
- In the first read callback, scan efivar_sysfs_list from head and pass
a kmsg buffer of a entry to an upper pstore layer.
- In the second read callback, rescan efivar_sysfs_list from the entry
and pass another kmsg buffer to it.
- Repeat the scan and pass until the end of efivar_sysfs_list.
In this process, an entry is read across the multiple read function
calls. To avoid race between the read and erasion, the whole process
above is protected by a spinlock, holding in open() and releasing in
close().
At the same time, kmemdup() is called to pass the buffer to pstore
filesystem during it. And then, it causes a following lockdep warning.
To make the dynamic memory allocation runnable without taking spinlock,
holding off a deletion of sysfs entry if it happens while scanning it
via efi_pstore, and deleting it after the scan is completed.
To implement it, this patch introduces two flags, scanning and deleting,
to efivar_entry.
On the code basis, it seems that all the scanning and deleting logic is
not needed because __efivars->lock are not dropped when reading from the
EFI variable store.
But, the scanning and deleting logic is still needed because an
efi-pstore and a pstore filesystem works as follows.
In case an entry(A) is found, the pointer is saved to psi->data. And
efi_pstore_read() passes the entry(A) to a pstore filesystem by
releasing __efivars->lock.
And then, the pstore filesystem calls efi_pstore_read() again and the
same entry(A), which is saved to psi->data, is used for resuming to scan
a sysfs-list.
So, to protect the entry(A), the logic is needed.
[ 1.143710] ------------[ cut here ]------------
[ 1.144058] WARNING: CPU: 1 PID: 1 at kernel/lockdep.c:2740 lockdep_trace_alloc+0x104/0x110()
[ 1.144058] DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))
[ 1.144058] Modules linked in:
[ 1.144058] CPU: 1 PID: 1 Comm: systemd Not tainted 3.11.0-rc5 #2
[ 1.144058] 0000000000000009 ffff8800797e9ae0 ffffffff816614a5 ffff8800797e9b28
[ 1.144058] ffff8800797e9b18 ffffffff8105510d 0000000000000080 0000000000000046
[ 1.144058] 00000000000000d0 00000000000003af ffffffff81ccd0c0 ffff8800797e9b78
[ 1.144058] Call Trace:
[ 1.144058] [<ffffffff816614a5>] dump_stack+0x54/0x74
[ 1.144058] [<ffffffff8105510d>] warn_slowpath_common+0x7d/0xa0
[ 1.144058] [<ffffffff8105517c>] warn_slowpath_fmt+0x4c/0x50
[ 1.144058] [<ffffffff8131290f>] ? vsscanf+0x57f/0x7b0
[ 1.144058] [<ffffffff810bbd74>] lockdep_trace_alloc+0x104/0x110
[ 1.144058] [<ffffffff81192da0>] __kmalloc_track_caller+0x50/0x280
[ 1.144058] [<ffffffff815147bb>] ? efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff8115b260>] kmemdup+0x20/0x50
[ 1.144058] [<ffffffff815147bb>] efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff81514800>] ? efi_pstore_read_func.part.1+0x170/0x170
[ 1.144058] [<ffffffff815148b4>] efi_pstore_read_func+0xb4/0xe0
[ 1.144058] [<ffffffff81512b7b>] __efivar_entry_iter+0xfb/0x120
[ 1.144058] [<ffffffff8151428f>] efi_pstore_read+0x3f/0x50
[ 1.144058] [<ffffffff8128d7ba>] pstore_get_records+0x9a/0x150
[ 1.158207] [<ffffffff812af25c>] ? selinux_d_instantiate+0x1c/0x20
[ 1.158207] [<ffffffff8128ce30>] ? parse_options+0x80/0x80
[ 1.158207] [<ffffffff8128ced5>] pstore_fill_super+0xa5/0xc0
[ 1.158207] [<ffffffff811ae7d2>] mount_single+0xa2/0xd0
[ 1.158207] [<ffffffff8128ccf8>] pstore_mount+0x18/0x20
[ 1.158207] [<ffffffff811ae8b9>] mount_fs+0x39/0x1b0
[ 1.158207] [<ffffffff81160550>] ? __alloc_percpu+0x10/0x20
[ 1.158207] [<ffffffff811c9493>] vfs_kern_mount+0x63/0xf0
[ 1.158207] [<ffffffff811cbb0e>] do_mount+0x23e/0xa20
[ 1.158207] [<ffffffff8115b51b>] ? strndup_user+0x4b/0xf0
[ 1.158207] [<ffffffff811cc373>] SyS_mount+0x83/0xc0
[ 1.158207] [<ffffffff81673cc2>] system_call_fastpath+0x16/0x1b
[ 1.158207] ---[ end trace 61981bc62de9f6f4 ]---
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Tested-by: Madper Xie <cxie@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2013-10-31 03:27:26 +08:00
|
|
|
}
|
2022-06-20 19:21:26 +08:00
|
|
|
|
efivars, efi-pstore: Hold off deletion of sysfs entry until the scan is completed
Currently, when mounting pstore file system, a read callback of
efi_pstore driver runs mutiple times as below.
- In the first read callback, scan efivar_sysfs_list from head and pass
a kmsg buffer of a entry to an upper pstore layer.
- In the second read callback, rescan efivar_sysfs_list from the entry
and pass another kmsg buffer to it.
- Repeat the scan and pass until the end of efivar_sysfs_list.
In this process, an entry is read across the multiple read function
calls. To avoid race between the read and erasion, the whole process
above is protected by a spinlock, holding in open() and releasing in
close().
At the same time, kmemdup() is called to pass the buffer to pstore
filesystem during it. And then, it causes a following lockdep warning.
To make the dynamic memory allocation runnable without taking spinlock,
holding off a deletion of sysfs entry if it happens while scanning it
via efi_pstore, and deleting it after the scan is completed.
To implement it, this patch introduces two flags, scanning and deleting,
to efivar_entry.
On the code basis, it seems that all the scanning and deleting logic is
not needed because __efivars->lock are not dropped when reading from the
EFI variable store.
But, the scanning and deleting logic is still needed because an
efi-pstore and a pstore filesystem works as follows.
In case an entry(A) is found, the pointer is saved to psi->data. And
efi_pstore_read() passes the entry(A) to a pstore filesystem by
releasing __efivars->lock.
And then, the pstore filesystem calls efi_pstore_read() again and the
same entry(A), which is saved to psi->data, is used for resuming to scan
a sysfs-list.
So, to protect the entry(A), the logic is needed.
[ 1.143710] ------------[ cut here ]------------
[ 1.144058] WARNING: CPU: 1 PID: 1 at kernel/lockdep.c:2740 lockdep_trace_alloc+0x104/0x110()
[ 1.144058] DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))
[ 1.144058] Modules linked in:
[ 1.144058] CPU: 1 PID: 1 Comm: systemd Not tainted 3.11.0-rc5 #2
[ 1.144058] 0000000000000009 ffff8800797e9ae0 ffffffff816614a5 ffff8800797e9b28
[ 1.144058] ffff8800797e9b18 ffffffff8105510d 0000000000000080 0000000000000046
[ 1.144058] 00000000000000d0 00000000000003af ffffffff81ccd0c0 ffff8800797e9b78
[ 1.144058] Call Trace:
[ 1.144058] [<ffffffff816614a5>] dump_stack+0x54/0x74
[ 1.144058] [<ffffffff8105510d>] warn_slowpath_common+0x7d/0xa0
[ 1.144058] [<ffffffff8105517c>] warn_slowpath_fmt+0x4c/0x50
[ 1.144058] [<ffffffff8131290f>] ? vsscanf+0x57f/0x7b0
[ 1.144058] [<ffffffff810bbd74>] lockdep_trace_alloc+0x104/0x110
[ 1.144058] [<ffffffff81192da0>] __kmalloc_track_caller+0x50/0x280
[ 1.144058] [<ffffffff815147bb>] ? efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff8115b260>] kmemdup+0x20/0x50
[ 1.144058] [<ffffffff815147bb>] efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff81514800>] ? efi_pstore_read_func.part.1+0x170/0x170
[ 1.144058] [<ffffffff815148b4>] efi_pstore_read_func+0xb4/0xe0
[ 1.144058] [<ffffffff81512b7b>] __efivar_entry_iter+0xfb/0x120
[ 1.144058] [<ffffffff8151428f>] efi_pstore_read+0x3f/0x50
[ 1.144058] [<ffffffff8128d7ba>] pstore_get_records+0x9a/0x150
[ 1.158207] [<ffffffff812af25c>] ? selinux_d_instantiate+0x1c/0x20
[ 1.158207] [<ffffffff8128ce30>] ? parse_options+0x80/0x80
[ 1.158207] [<ffffffff8128ced5>] pstore_fill_super+0xa5/0xc0
[ 1.158207] [<ffffffff811ae7d2>] mount_single+0xa2/0xd0
[ 1.158207] [<ffffffff8128ccf8>] pstore_mount+0x18/0x20
[ 1.158207] [<ffffffff811ae8b9>] mount_fs+0x39/0x1b0
[ 1.158207] [<ffffffff81160550>] ? __alloc_percpu+0x10/0x20
[ 1.158207] [<ffffffff811c9493>] vfs_kern_mount+0x63/0xf0
[ 1.158207] [<ffffffff811cbb0e>] do_mount+0x23e/0xa20
[ 1.158207] [<ffffffff8115b51b>] ? strndup_user+0x4b/0xf0
[ 1.158207] [<ffffffff811cc373>] SyS_mount+0x83/0xc0
[ 1.158207] [<ffffffff81673cc2>] system_call_fastpath+0x16/0x1b
[ 1.158207] ---[ end trace 61981bc62de9f6f4 ]---
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Tested-by: Madper Xie <cxie@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2013-10-31 03:27:26 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-03-04 14:09:18 +08:00
|
|
|
static ssize_t efi_pstore_read(struct pstore_record *record)
|
2013-02-08 23:48:51 +08:00
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
efi_char16_t *varname = record->psi->data;
|
|
|
|
efi_guid_t guid = LINUX_EFI_CRASH_GUID;
|
|
|
|
unsigned long varname_size;
|
|
|
|
efi_status_t status;
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
for (;;) {
|
|
|
|
varname_size = EFIVARS_DATA_SIZE_MAX;
|
efivars, efi-pstore: Hold off deletion of sysfs entry until the scan is completed
Currently, when mounting pstore file system, a read callback of
efi_pstore driver runs mutiple times as below.
- In the first read callback, scan efivar_sysfs_list from head and pass
a kmsg buffer of a entry to an upper pstore layer.
- In the second read callback, rescan efivar_sysfs_list from the entry
and pass another kmsg buffer to it.
- Repeat the scan and pass until the end of efivar_sysfs_list.
In this process, an entry is read across the multiple read function
calls. To avoid race between the read and erasion, the whole process
above is protected by a spinlock, holding in open() and releasing in
close().
At the same time, kmemdup() is called to pass the buffer to pstore
filesystem during it. And then, it causes a following lockdep warning.
To make the dynamic memory allocation runnable without taking spinlock,
holding off a deletion of sysfs entry if it happens while scanning it
via efi_pstore, and deleting it after the scan is completed.
To implement it, this patch introduces two flags, scanning and deleting,
to efivar_entry.
On the code basis, it seems that all the scanning and deleting logic is
not needed because __efivars->lock are not dropped when reading from the
EFI variable store.
But, the scanning and deleting logic is still needed because an
efi-pstore and a pstore filesystem works as follows.
In case an entry(A) is found, the pointer is saved to psi->data. And
efi_pstore_read() passes the entry(A) to a pstore filesystem by
releasing __efivars->lock.
And then, the pstore filesystem calls efi_pstore_read() again and the
same entry(A), which is saved to psi->data, is used for resuming to scan
a sysfs-list.
So, to protect the entry(A), the logic is needed.
[ 1.143710] ------------[ cut here ]------------
[ 1.144058] WARNING: CPU: 1 PID: 1 at kernel/lockdep.c:2740 lockdep_trace_alloc+0x104/0x110()
[ 1.144058] DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))
[ 1.144058] Modules linked in:
[ 1.144058] CPU: 1 PID: 1 Comm: systemd Not tainted 3.11.0-rc5 #2
[ 1.144058] 0000000000000009 ffff8800797e9ae0 ffffffff816614a5 ffff8800797e9b28
[ 1.144058] ffff8800797e9b18 ffffffff8105510d 0000000000000080 0000000000000046
[ 1.144058] 00000000000000d0 00000000000003af ffffffff81ccd0c0 ffff8800797e9b78
[ 1.144058] Call Trace:
[ 1.144058] [<ffffffff816614a5>] dump_stack+0x54/0x74
[ 1.144058] [<ffffffff8105510d>] warn_slowpath_common+0x7d/0xa0
[ 1.144058] [<ffffffff8105517c>] warn_slowpath_fmt+0x4c/0x50
[ 1.144058] [<ffffffff8131290f>] ? vsscanf+0x57f/0x7b0
[ 1.144058] [<ffffffff810bbd74>] lockdep_trace_alloc+0x104/0x110
[ 1.144058] [<ffffffff81192da0>] __kmalloc_track_caller+0x50/0x280
[ 1.144058] [<ffffffff815147bb>] ? efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff8115b260>] kmemdup+0x20/0x50
[ 1.144058] [<ffffffff815147bb>] efi_pstore_read_func.part.1+0x12b/0x170
[ 1.144058] [<ffffffff81514800>] ? efi_pstore_read_func.part.1+0x170/0x170
[ 1.144058] [<ffffffff815148b4>] efi_pstore_read_func+0xb4/0xe0
[ 1.144058] [<ffffffff81512b7b>] __efivar_entry_iter+0xfb/0x120
[ 1.144058] [<ffffffff8151428f>] efi_pstore_read+0x3f/0x50
[ 1.144058] [<ffffffff8128d7ba>] pstore_get_records+0x9a/0x150
[ 1.158207] [<ffffffff812af25c>] ? selinux_d_instantiate+0x1c/0x20
[ 1.158207] [<ffffffff8128ce30>] ? parse_options+0x80/0x80
[ 1.158207] [<ffffffff8128ced5>] pstore_fill_super+0xa5/0xc0
[ 1.158207] [<ffffffff811ae7d2>] mount_single+0xa2/0xd0
[ 1.158207] [<ffffffff8128ccf8>] pstore_mount+0x18/0x20
[ 1.158207] [<ffffffff811ae8b9>] mount_fs+0x39/0x1b0
[ 1.158207] [<ffffffff81160550>] ? __alloc_percpu+0x10/0x20
[ 1.158207] [<ffffffff811c9493>] vfs_kern_mount+0x63/0xf0
[ 1.158207] [<ffffffff811cbb0e>] do_mount+0x23e/0xa20
[ 1.158207] [<ffffffff8115b51b>] ? strndup_user+0x4b/0xf0
[ 1.158207] [<ffffffff811cc373>] SyS_mount+0x83/0xc0
[ 1.158207] [<ffffffff81673cc2>] system_call_fastpath+0x16/0x1b
[ 1.158207] ---[ end trace 61981bc62de9f6f4 ]---
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Tested-by: Madper Xie <cxie@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2013-10-31 03:27:26 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
/*
|
|
|
|
* If this is the first read() call in the pstore enumeration,
|
|
|
|
* varname will be the empty string, and the GetNextVariable()
|
|
|
|
* runtime service call will return the first EFI variable in
|
|
|
|
* its own enumeration order, ignoring the guid argument.
|
|
|
|
*
|
|
|
|
* Subsequent calls to GetNextVariable() must pass the name and
|
|
|
|
* guid values returned by the previous call, which is why we
|
|
|
|
* store varname in record->psi->data. Given that we only
|
|
|
|
* enumerate variables with the efi-pstore GUID, there is no
|
|
|
|
* need to record the guid return value.
|
|
|
|
*/
|
|
|
|
status = efivar_get_next_variable(&varname_size, varname, &guid);
|
|
|
|
if (status == EFI_NOT_FOUND)
|
|
|
|
return 0;
|
2017-03-04 14:09:18 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
/* skip variables that don't concern us */
|
|
|
|
if (efi_guidcmp(guid, LINUX_EFI_CRASH_GUID))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return efi_pstore_read_func(record, varname);
|
2017-03-04 14:09:18 +08:00
|
|
|
}
|
2013-02-08 23:48:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-04 15:28:53 +08:00
|
|
|
static int efi_pstore_write(struct pstore_record *record)
|
2013-02-08 23:48:51 +08:00
|
|
|
{
|
|
|
|
char name[DUMP_NAME_LEN];
|
|
|
|
efi_char16_t efi_name[DUMP_NAME_LEN];
|
2022-06-20 19:21:26 +08:00
|
|
|
efi_status_t status;
|
|
|
|
int i;
|
2013-02-08 23:48:51 +08:00
|
|
|
|
2017-05-19 04:07:49 +08:00
|
|
|
record->id = generic_id(record->time.tv_sec, record->part,
|
|
|
|
record->count);
|
|
|
|
|
2017-05-20 04:21:07 +08:00
|
|
|
/* Since we copy the entire length of name, make sure it is wiped. */
|
|
|
|
memset(name, 0, sizeof(name));
|
|
|
|
|
2018-05-15 06:50:52 +08:00
|
|
|
snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
|
2017-03-04 15:28:53 +08:00
|
|
|
record->type, record->part, record->count,
|
2018-05-15 06:50:52 +08:00
|
|
|
(long long)record->time.tv_sec,
|
|
|
|
record->compressed ? 'C' : 'D');
|
2013-02-08 23:48:51 +08:00
|
|
|
|
|
|
|
for (i = 0; i < DUMP_NAME_LEN; i++)
|
|
|
|
efi_name[i] = name[i];
|
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
if (efivar_trylock())
|
|
|
|
return -EBUSY;
|
|
|
|
status = efivar_set_variable_locked(efi_name, &LINUX_EFI_CRASH_GUID,
|
|
|
|
PSTORE_EFI_ATTRIBUTES,
|
|
|
|
record->size, record->psi->buf,
|
|
|
|
true);
|
|
|
|
efivar_unlock();
|
|
|
|
return status == EFI_SUCCESS ? 0 : -EIO;
|
2013-02-08 23:48:51 +08:00
|
|
|
};
|
|
|
|
|
2017-05-20 04:21:07 +08:00
|
|
|
static int efi_pstore_erase(struct pstore_record *record)
|
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
efi_status_t status;
|
2017-05-20 04:21:07 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
status = efivar_set_variable(record->priv, &LINUX_EFI_CRASH_GUID,
|
|
|
|
PSTORE_EFI_ATTRIBUTES, 0, NULL);
|
2017-05-20 04:21:07 +08:00
|
|
|
|
2022-06-20 19:21:26 +08:00
|
|
|
if (status != EFI_SUCCESS && status != EFI_NOT_FOUND)
|
|
|
|
return -EIO;
|
|
|
|
return 0;
|
2013-02-08 23:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pstore_info efi_pstore_info = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "efi",
|
2016-07-27 23:08:25 +08:00
|
|
|
.flags = PSTORE_FLAGS_DMESG,
|
2013-02-08 23:48:51 +08:00
|
|
|
.open = efi_pstore_open,
|
|
|
|
.close = efi_pstore_close,
|
|
|
|
.read = efi_pstore_read,
|
|
|
|
.write = efi_pstore_write,
|
|
|
|
.erase = efi_pstore_erase,
|
|
|
|
};
|
|
|
|
|
|
|
|
static __init int efivars_pstore_init(void)
|
|
|
|
{
|
2022-06-20 19:21:26 +08:00
|
|
|
if (!efivar_supports_writes())
|
2013-02-08 23:48:51 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (efivars_pstore_disable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
|
|
|
|
if (!efi_pstore_info.buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
efi_pstore_info.bufsize = 1024;
|
|
|
|
|
2013-06-29 04:14:11 +08:00
|
|
|
if (pstore_register(&efi_pstore_info)) {
|
|
|
|
kfree(efi_pstore_info.buf);
|
|
|
|
efi_pstore_info.buf = NULL;
|
|
|
|
efi_pstore_info.bufsize = 0;
|
|
|
|
}
|
2013-02-08 23:48:51 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __exit void efivars_pstore_exit(void)
|
|
|
|
{
|
2015-11-07 12:43:48 +08:00
|
|
|
if (!efi_pstore_info.bufsize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pstore_unregister(&efi_pstore_info);
|
|
|
|
kfree(efi_pstore_info.buf);
|
|
|
|
efi_pstore_info.buf = NULL;
|
|
|
|
efi_pstore_info.bufsize = 0;
|
2013-02-08 23:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(efivars_pstore_init);
|
|
|
|
module_exit(efivars_pstore_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("EFI variable backend for pstore");
|
|
|
|
MODULE_LICENSE("GPL");
|
2015-09-28 08:44:16 +08:00
|
|
|
MODULE_ALIAS("platform:efivars");
|