Skip to content

Commit 1d1f6cc

Browse files
committed
pstore/blk: Include zone in pstore_device_info
Information was redundant between struct pstore_zone_info and struct pstore_device_info. Use struct pstore_zone_info, with member name "zone". Additionally untangle the logic for the "best effort" block device instance. Signed-off-by: Kees Cook <[email protected]> Fixed-by: Pu Lehui <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]
1 parent c811659 commit 1d1f6cc

File tree

3 files changed

+87
-93
lines changed

3 files changed

+87
-93
lines changed

drivers/mtd/mtdpstore.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,13 @@ static void mtdpstore_notify_add(struct mtd_info *mtd)
423423
longcnt = BITS_TO_LONGS(div_u64(mtd->size, mtd->erasesize));
424424
cxt->badmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
425425

426-
cxt->dev.total_size = mtd->size;
427426
/* just support dmesg right now */
428427
cxt->dev.flags = PSTORE_FLAGS_DMESG;
429-
cxt->dev.read = mtdpstore_read;
430-
cxt->dev.write = mtdpstore_write;
431-
cxt->dev.erase = mtdpstore_erase;
432-
cxt->dev.panic_write = mtdpstore_panic_write;
428+
cxt->dev.zone.read = mtdpstore_read;
429+
cxt->dev.zone.write = mtdpstore_write;
430+
cxt->dev.zone.erase = mtdpstore_erase;
431+
cxt->dev.zone.panic_write = mtdpstore_panic_write;
432+
cxt->dev.zone.total_size = mtd->size;
433433

434434
ret = register_pstore_device(&cxt->dev);
435435
if (ret) {

fs/pstore/blk.c

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ MODULE_PARM_DESC(blkdev, "block device for pstore storage");
7070
*/
7171
static DEFINE_MUTEX(pstore_blk_lock);
7272
static struct file *psblk_file;
73-
static struct pstore_zone_info *pstore_zone_info;
73+
static struct pstore_device_info *pstore_device_info;
7474

7575
#define check_size(name, alignsize) ({ \
7676
long _##name_ = (name); \
@@ -91,7 +91,7 @@ static struct pstore_zone_info *pstore_zone_info;
9191
_##name_ = 0; \
9292
/* Synchronize module parameters with resuls. */ \
9393
name = _##name_ / 1024; \
94-
pstore_zone_info->name = _##name_; \
94+
dev->zone.name = _##name_; \
9595
}
9696

9797
static int __register_pstore_device(struct pstore_device_info *dev)
@@ -104,50 +104,42 @@ static int __register_pstore_device(struct pstore_device_info *dev)
104104
pr_err("NULL device info\n");
105105
return -EINVAL;
106106
}
107-
if (!dev->total_size) {
107+
if (!dev->zone.total_size) {
108108
pr_err("zero sized device\n");
109109
return -EINVAL;
110110
}
111-
if (!dev->read) {
111+
if (!dev->zone.read) {
112112
pr_err("no read handler for device\n");
113113
return -EINVAL;
114114
}
115-
if (!dev->write) {
115+
if (!dev->zone.write) {
116116
pr_err("no write handler for device\n");
117117
return -EINVAL;
118118
}
119119

120120
/* someone already registered before */
121-
if (pstore_zone_info)
121+
if (pstore_device_info)
122122
return -EBUSY;
123123

124-
pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
125-
if (!pstore_zone_info)
126-
return -ENOMEM;
127-
128124
/* zero means not limit on which backends to attempt to store. */
129125
if (!dev->flags)
130126
dev->flags = UINT_MAX;
131127

128+
/* Copy in module parameters. */
132129
verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
133130
verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
134131
verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
135132
verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
133+
dev->zone.max_reason = max_reason;
134+
135+
/* Initialize required zone ownership details. */
136+
dev->zone.name = KBUILD_MODNAME;
137+
dev->zone.owner = THIS_MODULE;
138+
139+
ret = register_pstore_zone(&dev->zone);
140+
if (ret == 0)
141+
pstore_device_info = dev;
136142

137-
pstore_zone_info->total_size = dev->total_size;
138-
pstore_zone_info->max_reason = max_reason;
139-
pstore_zone_info->read = dev->read;
140-
pstore_zone_info->write = dev->write;
141-
pstore_zone_info->erase = dev->erase;
142-
pstore_zone_info->panic_write = dev->panic_write;
143-
pstore_zone_info->name = KBUILD_MODNAME;
144-
pstore_zone_info->owner = THIS_MODULE;
145-
146-
ret = register_pstore_zone(pstore_zone_info);
147-
if (ret) {
148-
kfree(pstore_zone_info);
149-
pstore_zone_info = NULL;
150-
}
151143
return ret;
152144
}
153145
/**
@@ -174,10 +166,9 @@ EXPORT_SYMBOL_GPL(register_pstore_device);
174166
static void __unregister_pstore_device(struct pstore_device_info *dev)
175167
{
176168
lockdep_assert_held(&pstore_blk_lock);
177-
if (pstore_zone_info && pstore_zone_info->read == dev->read) {
178-
unregister_pstore_zone(pstore_zone_info);
179-
kfree(pstore_zone_info);
180-
pstore_zone_info = NULL;
169+
if (pstore_device_info && pstore_device_info == dev) {
170+
unregister_pstore_zone(&dev->zone);
171+
pstore_device_info = NULL;
181172
}
182173
}
183174

@@ -211,12 +202,9 @@ static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
211202
/*
212203
* This takes its configuration only from the module parameters now.
213204
*/
214-
static int __register_pstore_blk(const char *devpath)
205+
static int __register_pstore_blk(struct pstore_device_info *dev,
206+
const char *devpath)
215207
{
216-
struct pstore_device_info dev = {
217-
.read = psblk_generic_blk_read,
218-
.write = psblk_generic_blk_write,
219-
};
220208
struct inode *inode;
221209
int ret = -ENODEV;
222210

@@ -236,9 +224,9 @@ static int __register_pstore_blk(const char *devpath)
236224
}
237225

238226
inode = I_BDEV(psblk_file->f_mapping->host)->bd_inode;
239-
dev.total_size = i_size_read(inode);
227+
dev->zone.total_size = i_size_read(inode);
240228

241-
ret = __register_pstore_device(&dev);
229+
ret = __register_pstore_device(dev);
242230
if (ret)
243231
goto err_fput;
244232

@@ -252,18 +240,6 @@ static int __register_pstore_blk(const char *devpath)
252240
return ret;
253241
}
254242

255-
static void __unregister_pstore_blk(struct file *device)
256-
{
257-
struct pstore_device_info dev = { .read = psblk_generic_blk_read };
258-
259-
lockdep_assert_held(&pstore_blk_lock);
260-
if (psblk_file && psblk_file == device) {
261-
__unregister_pstore_device(&dev);
262-
fput(psblk_file);
263-
psblk_file = NULL;
264-
}
265-
}
266-
267243
/* get information of pstore/blk */
268244
int pstore_blk_get_config(struct pstore_blk_config *info)
269245
{
@@ -308,18 +284,63 @@ static inline const char *early_boot_devpath(const char *initial_devname)
308284
}
309285
#endif
310286

287+
static int __init __best_effort_init(void)
288+
{
289+
struct pstore_device_info *best_effort_dev;
290+
int ret;
291+
292+
/* No best-effort mode requested. */
293+
if (!best_effort)
294+
return 0;
295+
296+
/* Reject an empty blkdev. */
297+
if (!blkdev[0]) {
298+
pr_err("blkdev empty with best_effort=Y\n");
299+
return -EINVAL;
300+
}
301+
302+
best_effort_dev = kzalloc(sizeof(*best_effort_dev), GFP_KERNEL);
303+
if (!best_effort_dev)
304+
return -ENOMEM;
305+
306+
best_effort_dev->zone.read = psblk_generic_blk_read;
307+
best_effort_dev->zone.write = psblk_generic_blk_write;
308+
309+
ret = __register_pstore_blk(best_effort_dev,
310+
early_boot_devpath(blkdev));
311+
if (ret)
312+
kfree(best_effort_dev);
313+
else
314+
pr_info("attached %s (%zu) (no dedicated panic_write!)\n",
315+
blkdev, best_effort_dev->zone.total_size);
316+
317+
return ret;
318+
}
319+
320+
static void __exit __best_effort_exit(void)
321+
{
322+
/*
323+
* Currently, the only user of psblk_file is best_effort, so
324+
* we can assume that pstore_device_info is associated with it.
325+
* Once there are "real" blk devices, there will need to be a
326+
* dedicated pstore_blk_info, etc.
327+
*/
328+
if (psblk_file) {
329+
struct pstore_device_info *dev = pstore_device_info;
330+
331+
__unregister_pstore_device(dev);
332+
kfree(dev);
333+
fput(psblk_file);
334+
psblk_file = NULL;
335+
}
336+
}
337+
311338
static int __init pstore_blk_init(void)
312339
{
313-
int ret = 0;
340+
int ret;
314341

315342
mutex_lock(&pstore_blk_lock);
316-
if (!pstore_zone_info && best_effort && blkdev[0]) {
317-
ret = __register_pstore_blk(early_boot_devpath(blkdev));
318-
if (ret == 0 && pstore_zone_info)
319-
pr_info("attached %s:%s (%zu) (no dedicated panic_write!)\n",
320-
pstore_zone_info->name, blkdev,
321-
pstore_zone_info->total_size);
322-
}
343+
ret = __best_effort_init();
323344
mutex_unlock(&pstore_blk_lock);
324345

325346
return ret;
@@ -329,15 +350,9 @@ late_initcall(pstore_blk_init);
329350
static void __exit pstore_blk_exit(void)
330351
{
331352
mutex_lock(&pstore_blk_lock);
332-
if (psblk_file)
333-
__unregister_pstore_blk(psblk_file);
334-
else {
335-
struct pstore_device_info dev = { };
336-
337-
if (pstore_zone_info)
338-
dev.read = pstore_zone_info->read;
339-
__unregister_pstore_device(&dev);
340-
}
353+
__best_effort_exit();
354+
/* If we've been asked to unload, unregister any remaining device. */
355+
__unregister_pstore_device(pstore_device_info);
341356
mutex_unlock(&pstore_blk_lock);
342357
}
343358
module_exit(pstore_blk_exit);

include/linux/pstore_blk.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,15 @@
1010
/**
1111
* struct pstore_device_info - back-end pstore/blk driver structure.
1212
*
13-
* @total_size: The total size in bytes pstore/blk can use. It must be greater
14-
* than 4096 and be multiple of 4096.
1513
* @flags: Refer to macro starting with PSTORE_FLAGS defined in
1614
* linux/pstore.h. It means what front-ends this device support.
1715
* Zero means all backends for compatible.
18-
* @read: The general read operation. Both of the function parameters
19-
* @size and @offset are relative value to bock device (not the
20-
* whole disk).
21-
* On success, the number of bytes should be returned, others
22-
* means error.
23-
* @write: The same as @read, but the following error number:
24-
* -EBUSY means try to write again later.
25-
* -ENOMSG means to try next zone.
26-
* @erase: The general erase operation for device with special removing
27-
* job. Both of the function parameters @size and @offset are
28-
* relative value to storage.
29-
* Return 0 on success and others on failure.
30-
* @panic_write:The write operation only used for panic case. It's optional
31-
* if you do not care panic log. The parameters are relative
32-
* value to storage.
33-
* On success, the number of bytes should be returned, others
34-
* excluding -ENOMSG mean error. -ENOMSG means to try next zone.
16+
* @zone: The struct pstore_zone_info details.
17+
*
3518
*/
3619
struct pstore_device_info {
37-
unsigned long total_size;
3820
unsigned int flags;
39-
pstore_zone_read_op read;
40-
pstore_zone_write_op write;
41-
pstore_zone_erase_op erase;
42-
pstore_zone_write_op panic_write;
21+
struct pstore_zone_info zone;
4322
};
4423

4524
int register_pstore_device(struct pstore_device_info *dev);

0 commit comments

Comments
 (0)