Skip to content

Commit e2eedde

Browse files
committed
pstore: inode: Convert mutex usage to guard(mutex)
Replace open-coded mutex handling with cleanup.h guard(mutex) and scoped_guard(mutex, ...). Cc: Guilherme G. Piccoli <[email protected]> Cc: Tony Luck <[email protected]> Cc: <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 6ba6ee8 commit e2eedde

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

fs/pstore/inode.c

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,21 @@ static int pstore_unlink(struct inode *dir, struct dentry *dentry)
180180
{
181181
struct pstore_private *p = d_inode(dentry)->i_private;
182182
struct pstore_record *record = p->record;
183-
int rc = 0;
184183

185184
if (!record->psi->erase)
186185
return -EPERM;
187186

188187
/* Make sure we can't race while removing this file. */
189-
mutex_lock(&records_list_lock);
190-
if (!list_empty(&p->list))
191-
list_del_init(&p->list);
192-
else
193-
rc = -ENOENT;
194-
p->dentry = NULL;
195-
mutex_unlock(&records_list_lock);
196-
if (rc)
197-
return rc;
198-
199-
mutex_lock(&record->psi->read_mutex);
200-
record->psi->erase(record);
201-
mutex_unlock(&record->psi->read_mutex);
188+
scoped_guard(mutex, &records_list_lock) {
189+
if (!list_empty(&p->list))
190+
list_del_init(&p->list);
191+
else
192+
return -ENOENT;
193+
p->dentry = NULL;
194+
}
195+
196+
scoped_guard(mutex, &record->psi->read_mutex)
197+
record->psi->erase(record);
202198

203199
return simple_unlink(dir, dentry);
204200
}
@@ -290,19 +286,16 @@ static struct dentry *psinfo_lock_root(void)
290286
{
291287
struct dentry *root;
292288

293-
mutex_lock(&pstore_sb_lock);
289+
guard(mutex)(&pstore_sb_lock);
294290
/*
295291
* Having no backend is fine -- no records appear.
296292
* Not being mounted is fine -- nothing to do.
297293
*/
298-
if (!psinfo || !pstore_sb) {
299-
mutex_unlock(&pstore_sb_lock);
294+
if (!psinfo || !pstore_sb)
300295
return NULL;
301-
}
302296

303297
root = pstore_sb->s_root;
304298
inode_lock(d_inode(root));
305-
mutex_unlock(&pstore_sb_lock);
306299

307300
return root;
308301
}
@@ -317,19 +310,19 @@ int pstore_put_backend_records(struct pstore_info *psi)
317310
if (!root)
318311
return 0;
319312

320-
mutex_lock(&records_list_lock);
321-
list_for_each_entry_safe(pos, tmp, &records_list, list) {
322-
if (pos->record->psi == psi) {
323-
list_del_init(&pos->list);
324-
rc = simple_unlink(d_inode(root), pos->dentry);
325-
if (WARN_ON(rc))
326-
break;
327-
d_drop(pos->dentry);
328-
dput(pos->dentry);
329-
pos->dentry = NULL;
313+
scoped_guard(mutex, &records_list_lock) {
314+
list_for_each_entry_safe(pos, tmp, &records_list, list) {
315+
if (pos->record->psi == psi) {
316+
list_del_init(&pos->list);
317+
rc = simple_unlink(d_inode(root), pos->dentry);
318+
if (WARN_ON(rc))
319+
break;
320+
d_drop(pos->dentry);
321+
dput(pos->dentry);
322+
pos->dentry = NULL;
323+
}
330324
}
331325
}
332-
mutex_unlock(&records_list_lock);
333326

334327
inode_unlock(d_inode(root));
335328

@@ -353,20 +346,20 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
353346
if (WARN_ON(!inode_is_locked(d_inode(root))))
354347
return -EINVAL;
355348

356-
rc = -EEXIST;
349+
guard(mutex)(&records_list_lock);
350+
357351
/* Skip records that are already present in the filesystem. */
358-
mutex_lock(&records_list_lock);
359352
list_for_each_entry(pos, &records_list, list) {
360353
if (pos->record->type == record->type &&
361354
pos->record->id == record->id &&
362355
pos->record->psi == record->psi)
363-
goto fail;
356+
return -EEXIST;
364357
}
365358

366359
rc = -ENOMEM;
367360
inode = pstore_get_inode(root->d_sb);
368361
if (!inode)
369-
goto fail;
362+
return -ENOMEM;
370363
inode->i_mode = S_IFREG | 0444;
371364
inode->i_fop = &pstore_file_operations;
372365
scnprintf(name, sizeof(name), "%s-%s-%llu%s",
@@ -394,16 +387,13 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
394387
d_add(dentry, inode);
395388

396389
list_add(&private->list, &records_list);
397-
mutex_unlock(&records_list_lock);
398390

399391
return 0;
400392

401393
fail_private:
402394
free_pstore_private(private);
403395
fail_inode:
404396
iput(inode);
405-
fail:
406-
mutex_unlock(&records_list_lock);
407397
return rc;
408398
}
409399

@@ -449,9 +439,8 @@ static int pstore_fill_super(struct super_block *sb, void *data, int silent)
449439
if (!sb->s_root)
450440
return -ENOMEM;
451441

452-
mutex_lock(&pstore_sb_lock);
453-
pstore_sb = sb;
454-
mutex_unlock(&pstore_sb_lock);
442+
scoped_guard(mutex, &pstore_sb_lock)
443+
pstore_sb = sb;
455444

456445
pstore_get_records(0);
457446

@@ -466,17 +455,14 @@ static struct dentry *pstore_mount(struct file_system_type *fs_type,
466455

467456
static void pstore_kill_sb(struct super_block *sb)
468457
{
469-
mutex_lock(&pstore_sb_lock);
458+
guard(mutex)(&pstore_sb_lock);
470459
WARN_ON(pstore_sb && pstore_sb != sb);
471460

472461
kill_litter_super(sb);
473462
pstore_sb = NULL;
474463

475-
mutex_lock(&records_list_lock);
464+
guard(mutex)(&records_list_lock);
476465
INIT_LIST_HEAD(&records_list);
477-
mutex_unlock(&records_list_lock);
478-
479-
mutex_unlock(&pstore_sb_lock);
480466
}
481467

482468
static struct file_system_type pstore_fs_type = {

0 commit comments

Comments
 (0)