Skip to content

Commit 95207d5

Browse files
committed
Merge tag 'iomap-5.5-merge-14' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
Pull iomap fixes from Darrick Wong: "Fix a race condition and a use-after-free error: - Fix a UAF when reporting writeback errors - Fix a race condition when handling page uptodate on fragmented file with blocksize < pagesize" * tag 'iomap-5.5-merge-14' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux: iomap: stop using ioend after it's been freed in iomap_finish_ioend() iomap: fix sub-page uptodate handling
2 parents 50caca9 + c275779 commit 95207d5

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

fs/iomap/buffered-io.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
struct iomap_page {
2929
atomic_t read_count;
3030
atomic_t write_count;
31+
spinlock_t uptodate_lock;
3132
DECLARE_BITMAP(uptodate, PAGE_SIZE / 512);
3233
};
3334

@@ -51,6 +52,7 @@ iomap_page_create(struct inode *inode, struct page *page)
5152
iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
5253
atomic_set(&iop->read_count, 0);
5354
atomic_set(&iop->write_count, 0);
55+
spin_lock_init(&iop->uptodate_lock);
5456
bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
5557

5658
/*
@@ -139,25 +141,38 @@ iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
139141
}
140142

141143
static void
142-
iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
144+
iomap_iop_set_range_uptodate(struct page *page, unsigned off, unsigned len)
143145
{
144146
struct iomap_page *iop = to_iomap_page(page);
145147
struct inode *inode = page->mapping->host;
146148
unsigned first = off >> inode->i_blkbits;
147149
unsigned last = (off + len - 1) >> inode->i_blkbits;
148-
unsigned int i;
149150
bool uptodate = true;
151+
unsigned long flags;
152+
unsigned int i;
150153

151-
if (iop) {
152-
for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
153-
if (i >= first && i <= last)
154-
set_bit(i, iop->uptodate);
155-
else if (!test_bit(i, iop->uptodate))
156-
uptodate = false;
157-
}
154+
spin_lock_irqsave(&iop->uptodate_lock, flags);
155+
for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
156+
if (i >= first && i <= last)
157+
set_bit(i, iop->uptodate);
158+
else if (!test_bit(i, iop->uptodate))
159+
uptodate = false;
158160
}
159161

160-
if (uptodate && !PageError(page))
162+
if (uptodate)
163+
SetPageUptodate(page);
164+
spin_unlock_irqrestore(&iop->uptodate_lock, flags);
165+
}
166+
167+
static void
168+
iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
169+
{
170+
if (PageError(page))
171+
return;
172+
173+
if (page_has_private(page))
174+
iomap_iop_set_range_uptodate(page, off, len);
175+
else
161176
SetPageUptodate(page);
162177
}
163178

@@ -1128,6 +1143,7 @@ iomap_finish_ioend(struct iomap_ioend *ioend, int error)
11281143
struct bio *bio = &ioend->io_inline_bio;
11291144
struct bio *last = ioend->io_bio, *next;
11301145
u64 start = bio->bi_iter.bi_sector;
1146+
loff_t offset = ioend->io_offset;
11311147
bool quiet = bio_flagged(bio, BIO_QUIET);
11321148

11331149
for (bio = &ioend->io_inline_bio; bio; bio = next) {
@@ -1148,12 +1164,12 @@ iomap_finish_ioend(struct iomap_ioend *ioend, int error)
11481164
iomap_finish_page_writeback(inode, bv->bv_page, error);
11491165
bio_put(bio);
11501166
}
1167+
/* The ioend has been freed by bio_put() */
11511168

11521169
if (unlikely(error && !quiet)) {
11531170
printk_ratelimited(KERN_ERR
11541171
"%s: writeback error on inode %lu, offset %lld, sector %llu",
1155-
inode->i_sb->s_id, inode->i_ino, ioend->io_offset,
1156-
start);
1172+
inode->i_sb->s_id, inode->i_ino, offset, start);
11571173
}
11581174
}
11591175

0 commit comments

Comments
 (0)