Skip to content

Commit 3d9f55c

Browse files
committed
Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
Pull ext2, writeback, and quota fixes and cleanups from Jan Kara: "A fix for race in writeback code and two cleanups in quota and ext2" * tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs: quota: Prevent memory allocation recursion while holding dq_lock writeback: Fix inode->i_io_list not be protected by inode->i_lock error fs: Fix syntax errors in comments
2 parents 95fc76c + 537e11c commit 3d9f55c

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

fs/ext2/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ static int __ext2_write_inode(struct inode *inode, int do_sync)
15491549
if (IS_ERR(raw_inode))
15501550
return -EIO;
15511551

1552-
/* For fields not not tracking in the in-memory inode,
1552+
/* For fields not tracking in the in-memory inode,
15531553
* initialise them to zero for new inodes. */
15541554
if (ei->i_state & EXT2_STATE_NEW)
15551555
memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);

fs/fs-writeback.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ static bool inode_io_list_move_locked(struct inode *inode,
120120
struct list_head *head)
121121
{
122122
assert_spin_locked(&wb->list_lock);
123+
assert_spin_locked(&inode->i_lock);
123124

124125
list_move(&inode->i_io_list, head);
125126

@@ -1365,9 +1366,9 @@ static int move_expired_inodes(struct list_head *delaying_queue,
13651366
inode = wb_inode(delaying_queue->prev);
13661367
if (inode_dirtied_after(inode, dirtied_before))
13671368
break;
1369+
spin_lock(&inode->i_lock);
13681370
list_move(&inode->i_io_list, &tmp);
13691371
moved++;
1370-
spin_lock(&inode->i_lock);
13711372
inode->i_state |= I_SYNC_QUEUED;
13721373
spin_unlock(&inode->i_lock);
13731374
if (sb_is_blkdev_sb(inode->i_sb))
@@ -1383,7 +1384,12 @@ static int move_expired_inodes(struct list_head *delaying_queue,
13831384
goto out;
13841385
}
13851386

1386-
/* Move inodes from one superblock together */
1387+
/*
1388+
* Although inode's i_io_list is moved from 'tmp' to 'dispatch_queue',
1389+
* we don't take inode->i_lock here because it is just a pointless overhead.
1390+
* Inode is already marked as I_SYNC_QUEUED so writeback list handling is
1391+
* fully under our control.
1392+
*/
13871393
while (!list_empty(&tmp)) {
13881394
sb = wb_inode(tmp.prev)->i_sb;
13891395
list_for_each_prev_safe(pos, node, &tmp) {
@@ -1826,8 +1832,8 @@ static long writeback_sb_inodes(struct super_block *sb,
18261832
* We'll have another go at writing back this inode
18271833
* when we completed a full scan of b_io.
18281834
*/
1829-
spin_unlock(&inode->i_lock);
18301835
requeue_io(inode, wb);
1836+
spin_unlock(&inode->i_lock);
18311837
trace_writeback_sb_inodes_requeue(inode);
18321838
continue;
18331839
}
@@ -2358,6 +2364,7 @@ void __mark_inode_dirty(struct inode *inode, int flags)
23582364
{
23592365
struct super_block *sb = inode->i_sb;
23602366
int dirtytime = 0;
2367+
struct bdi_writeback *wb = NULL;
23612368

23622369
trace_writeback_mark_inode_dirty(inode, flags);
23632370

@@ -2409,37 +2416,45 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24092416
inode->i_state &= ~I_DIRTY_TIME;
24102417
inode->i_state |= flags;
24112418

2419+
/*
2420+
* Grab inode's wb early because it requires dropping i_lock and we
2421+
* need to make sure following checks happen atomically with dirty
2422+
* list handling so that we don't move inodes under flush worker's
2423+
* hands.
2424+
*/
2425+
if (!was_dirty) {
2426+
wb = locked_inode_to_wb_and_lock_list(inode);
2427+
spin_lock(&inode->i_lock);
2428+
}
2429+
24122430
/*
24132431
* If the inode is queued for writeback by flush worker, just
24142432
* update its dirty state. Once the flush worker is done with
24152433
* the inode it will place it on the appropriate superblock
24162434
* list, based upon its state.
24172435
*/
24182436
if (inode->i_state & I_SYNC_QUEUED)
2419-
goto out_unlock_inode;
2437+
goto out_unlock;
24202438

24212439
/*
24222440
* Only add valid (hashed) inodes to the superblock's
24232441
* dirty list. Add blockdev inodes as well.
24242442
*/
24252443
if (!S_ISBLK(inode->i_mode)) {
24262444
if (inode_unhashed(inode))
2427-
goto out_unlock_inode;
2445+
goto out_unlock;
24282446
}
24292447
if (inode->i_state & I_FREEING)
2430-
goto out_unlock_inode;
2448+
goto out_unlock;
24312449

24322450
/*
24332451
* If the inode was already on b_dirty/b_io/b_more_io, don't
24342452
* reposition it (that would break b_dirty time-ordering).
24352453
*/
24362454
if (!was_dirty) {
2437-
struct bdi_writeback *wb;
24382455
struct list_head *dirty_list;
24392456
bool wakeup_bdi = false;
24402457

2441-
wb = locked_inode_to_wb_and_lock_list(inode);
2442-
24432458
inode->dirtied_when = jiffies;
24442459
if (dirtytime)
24452460
inode->dirtied_time_when = jiffies;
@@ -2453,6 +2468,7 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24532468
dirty_list);
24542469

24552470
spin_unlock(&wb->list_lock);
2471+
spin_unlock(&inode->i_lock);
24562472
trace_writeback_dirty_inode_enqueue(inode);
24572473

24582474
/*
@@ -2467,6 +2483,9 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24672483
return;
24682484
}
24692485
}
2486+
out_unlock:
2487+
if (wb)
2488+
spin_unlock(&wb->list_lock);
24702489
out_unlock_inode:
24712490
spin_unlock(&inode->i_lock);
24722491
}

fs/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Inode locking rules:
2828
*
2929
* inode->i_lock protects:
30-
* inode->i_state, inode->i_hash, __iget()
30+
* inode->i_state, inode->i_hash, __iget(), inode->i_io_list
3131
* Inode LRU list locks protect:
3232
* inode->i_sb->s_inode_lru, inode->i_lru
3333
* inode->i_sb->s_inode_list_lock protects:

fs/quota/dquot.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include <linux/capability.h>
8080
#include <linux/quotaops.h>
8181
#include <linux/blkdev.h>
82+
#include <linux/sched/mm.h>
8283
#include "../internal.h" /* ugh */
8384

8485
#include <linux/uaccess.h>
@@ -425,9 +426,11 @@ EXPORT_SYMBOL(mark_info_dirty);
425426
int dquot_acquire(struct dquot *dquot)
426427
{
427428
int ret = 0, ret2 = 0;
429+
unsigned int memalloc;
428430
struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
429431

430432
mutex_lock(&dquot->dq_lock);
433+
memalloc = memalloc_nofs_save();
431434
if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
432435
ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
433436
if (ret < 0)
@@ -458,6 +461,7 @@ int dquot_acquire(struct dquot *dquot)
458461
smp_mb__before_atomic();
459462
set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
460463
out_iolock:
464+
memalloc_nofs_restore(memalloc);
461465
mutex_unlock(&dquot->dq_lock);
462466
return ret;
463467
}
@@ -469,9 +473,11 @@ EXPORT_SYMBOL(dquot_acquire);
469473
int dquot_commit(struct dquot *dquot)
470474
{
471475
int ret = 0;
476+
unsigned int memalloc;
472477
struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
473478

474479
mutex_lock(&dquot->dq_lock);
480+
memalloc = memalloc_nofs_save();
475481
if (!clear_dquot_dirty(dquot))
476482
goto out_lock;
477483
/* Inactive dquot can be only if there was error during read/init
@@ -481,6 +487,7 @@ int dquot_commit(struct dquot *dquot)
481487
else
482488
ret = -EIO;
483489
out_lock:
490+
memalloc_nofs_restore(memalloc);
484491
mutex_unlock(&dquot->dq_lock);
485492
return ret;
486493
}
@@ -492,9 +499,11 @@ EXPORT_SYMBOL(dquot_commit);
492499
int dquot_release(struct dquot *dquot)
493500
{
494501
int ret = 0, ret2 = 0;
502+
unsigned int memalloc;
495503
struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
496504

497505
mutex_lock(&dquot->dq_lock);
506+
memalloc = memalloc_nofs_save();
498507
/* Check whether we are not racing with some other dqget() */
499508
if (dquot_is_busy(dquot))
500509
goto out_dqlock;
@@ -510,6 +519,7 @@ int dquot_release(struct dquot *dquot)
510519
}
511520
clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
512521
out_dqlock:
522+
memalloc_nofs_restore(memalloc);
513523
mutex_unlock(&dquot->dq_lock);
514524
return ret;
515525
}

0 commit comments

Comments
 (0)