Skip to content

Commit 98dc08b

Browse files
committed
fsverity: stop using PG_error to track error status
As a step towards freeing the PG_error flag for other uses, change ext4 and f2fs to stop using PG_error to track verity errors. Instead, if a verity error occurs, just mark the whole bio as failed. The coarser granularity isn't really a problem since it isn't any worse than what the block layer provides, and errors from a multi-page readahead aren't reported to applications unless a single-page read fails too. f2fs supports compression, which makes the f2fs changes a bit more complicated than desired, but the basic premise still works. Note: there are still a few uses of PageError in f2fs, but they are on the write path, so they are unrelated and this patch doesn't touch them. Reviewed-by: Chao Yu <[email protected]> Acked-by: Jaegeuk Kim <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f0c4d9f commit 98dc08b

File tree

4 files changed

+72
-65
lines changed

4 files changed

+72
-65
lines changed

fs/ext4/readpage.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,10 @@ static void __read_end_io(struct bio *bio)
7575
bio_for_each_segment_all(bv, bio, iter_all) {
7676
page = bv->bv_page;
7777

78-
/* PG_error was set if verity failed. */
79-
if (bio->bi_status || PageError(page)) {
78+
if (bio->bi_status)
8079
ClearPageUptodate(page);
81-
/* will re-read again later */
82-
ClearPageError(page);
83-
} else {
80+
else
8481
SetPageUptodate(page);
85-
}
8682
unlock_page(page);
8783
}
8884
if (bio->bi_private)

fs/f2fs/compress.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,50 +1711,27 @@ static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
17111711
}
17121712
}
17131713

1714-
/*
1715-
* Update and unlock the cluster's pagecache pages, and release the reference to
1716-
* the decompress_io_ctx that was being held for I/O completion.
1717-
*/
1718-
static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1719-
bool in_task)
1714+
static void f2fs_verify_cluster(struct work_struct *work)
17201715
{
1716+
struct decompress_io_ctx *dic =
1717+
container_of(work, struct decompress_io_ctx, verity_work);
17211718
int i;
17221719

1720+
/* Verify, update, and unlock the decompressed pages. */
17231721
for (i = 0; i < dic->cluster_size; i++) {
17241722
struct page *rpage = dic->rpages[i];
17251723

17261724
if (!rpage)
17271725
continue;
17281726

1729-
/* PG_error was set if verity failed. */
1730-
if (failed || PageError(rpage)) {
1731-
ClearPageUptodate(rpage);
1732-
/* will re-read again later */
1733-
ClearPageError(rpage);
1734-
} else {
1727+
if (fsverity_verify_page(rpage))
17351728
SetPageUptodate(rpage);
1736-
}
1729+
else
1730+
ClearPageUptodate(rpage);
17371731
unlock_page(rpage);
17381732
}
17391733

1740-
f2fs_put_dic(dic, in_task);
1741-
}
1742-
1743-
static void f2fs_verify_cluster(struct work_struct *work)
1744-
{
1745-
struct decompress_io_ctx *dic =
1746-
container_of(work, struct decompress_io_ctx, verity_work);
1747-
int i;
1748-
1749-
/* Verify the cluster's decompressed pages with fs-verity. */
1750-
for (i = 0; i < dic->cluster_size; i++) {
1751-
struct page *rpage = dic->rpages[i];
1752-
1753-
if (rpage && !fsverity_verify_page(rpage))
1754-
SetPageError(rpage);
1755-
}
1756-
1757-
__f2fs_decompress_end_io(dic, false, true);
1734+
f2fs_put_dic(dic, true);
17581735
}
17591736

17601737
/*
@@ -1764,6 +1741,8 @@ static void f2fs_verify_cluster(struct work_struct *work)
17641741
void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
17651742
bool in_task)
17661743
{
1744+
int i;
1745+
17671746
if (!failed && dic->need_verity) {
17681747
/*
17691748
* Note that to avoid deadlocks, the verity work can't be done
@@ -1773,9 +1752,28 @@ void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
17731752
*/
17741753
INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
17751754
fsverity_enqueue_verify_work(&dic->verity_work);
1776-
} else {
1777-
__f2fs_decompress_end_io(dic, failed, in_task);
1755+
return;
1756+
}
1757+
1758+
/* Update and unlock the cluster's pagecache pages. */
1759+
for (i = 0; i < dic->cluster_size; i++) {
1760+
struct page *rpage = dic->rpages[i];
1761+
1762+
if (!rpage)
1763+
continue;
1764+
1765+
if (failed)
1766+
ClearPageUptodate(rpage);
1767+
else
1768+
SetPageUptodate(rpage);
1769+
unlock_page(rpage);
17781770
}
1771+
1772+
/*
1773+
* Release the reference to the decompress_io_ctx that was being held
1774+
* for I/O completion.
1775+
*/
1776+
f2fs_put_dic(dic, in_task);
17791777
}
17801778

17811779
/*

fs/f2fs/data.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,56 @@ struct bio_post_read_ctx {
116116
struct f2fs_sb_info *sbi;
117117
struct work_struct work;
118118
unsigned int enabled_steps;
119+
/*
120+
* decompression_attempted keeps track of whether
121+
* f2fs_end_read_compressed_page() has been called on the pages in the
122+
* bio that belong to a compressed cluster yet.
123+
*/
124+
bool decompression_attempted;
119125
block_t fs_blkaddr;
120126
};
121127

128+
/*
129+
* Update and unlock a bio's pages, and free the bio.
130+
*
131+
* This marks pages up-to-date only if there was no error in the bio (I/O error,
132+
* decryption error, or verity error), as indicated by bio->bi_status.
133+
*
134+
* "Compressed pages" (pagecache pages backed by a compressed cluster on-disk)
135+
* aren't marked up-to-date here, as decompression is done on a per-compression-
136+
* cluster basis rather than a per-bio basis. Instead, we only must do two
137+
* things for each compressed page here: call f2fs_end_read_compressed_page()
138+
* with failed=true if an error occurred before it would have normally gotten
139+
* called (i.e., I/O error or decryption error, but *not* verity error), and
140+
* release the bio's reference to the decompress_io_ctx of the page's cluster.
141+
*/
122142
static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
123143
{
124144
struct bio_vec *bv;
125145
struct bvec_iter_all iter_all;
146+
struct bio_post_read_ctx *ctx = bio->bi_private;
126147

127-
/*
128-
* Update and unlock the bio's pagecache pages, and put the
129-
* decompression context for any compressed pages.
130-
*/
131148
bio_for_each_segment_all(bv, bio, iter_all) {
132149
struct page *page = bv->bv_page;
133150

134151
if (f2fs_is_compressed_page(page)) {
135-
if (bio->bi_status)
152+
if (ctx && !ctx->decompression_attempted)
136153
f2fs_end_read_compressed_page(page, true, 0,
137154
in_task);
138155
f2fs_put_page_dic(page, in_task);
139156
continue;
140157
}
141158

142-
/* PG_error was set if verity failed. */
143-
if (bio->bi_status || PageError(page)) {
159+
if (bio->bi_status)
144160
ClearPageUptodate(page);
145-
/* will re-read again later */
146-
ClearPageError(page);
147-
} else {
161+
else
148162
SetPageUptodate(page);
149-
}
150163
dec_page_count(F2FS_P_SB(page), __read_io_type(page));
151164
unlock_page(page);
152165
}
153166

154-
if (bio->bi_private)
155-
mempool_free(bio->bi_private, bio_post_read_ctx_pool);
167+
if (ctx)
168+
mempool_free(ctx, bio_post_read_ctx_pool);
156169
bio_put(bio);
157170
}
158171

@@ -185,8 +198,10 @@ static void f2fs_verify_bio(struct work_struct *work)
185198
struct page *page = bv->bv_page;
186199

187200
if (!f2fs_is_compressed_page(page) &&
188-
!fsverity_verify_page(page))
189-
SetPageError(page);
201+
!fsverity_verify_page(page)) {
202+
bio->bi_status = BLK_STS_IOERR;
203+
break;
204+
}
190205
}
191206
} else {
192207
fsverity_verify_bio(bio);
@@ -245,6 +260,8 @@ static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
245260
blkaddr++;
246261
}
247262

263+
ctx->decompression_attempted = true;
264+
248265
/*
249266
* Optimization: if all the bio's pages are compressed, then scheduling
250267
* the per-bio verity work is unnecessary, as verity will be fully
@@ -1062,6 +1079,7 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
10621079
ctx->sbi = sbi;
10631080
ctx->enabled_steps = post_read_steps;
10641081
ctx->fs_blkaddr = blkaddr;
1082+
ctx->decompression_attempted = false;
10651083
bio->bi_private = ctx;
10661084
}
10671085
iostat_alloc_and_bind_ctx(sbi, bio, ctx);
@@ -1089,7 +1107,6 @@ static int f2fs_submit_page_read(struct inode *inode, struct page *page,
10891107
bio_put(bio);
10901108
return -EFAULT;
10911109
}
1092-
ClearPageError(page);
10931110
inc_page_count(sbi, F2FS_RD_DATA);
10941111
f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE);
10951112
__submit_bio(sbi, bio, DATA);
@@ -2141,7 +2158,6 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
21412158
inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
21422159
f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO,
21432160
F2FS_BLKSIZE);
2144-
ClearPageError(page);
21452161
*last_block_in_bio = block_nr;
21462162
goto out;
21472163
out:
@@ -2289,7 +2305,6 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
22892305

22902306
inc_page_count(sbi, F2FS_RD_DATA);
22912307
f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
2292-
ClearPageError(page);
22932308
*last_block_in_bio = blkaddr;
22942309
}
22952310

@@ -2306,7 +2321,6 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
23062321
for (i = 0; i < cc->cluster_size; i++) {
23072322
if (cc->rpages[i]) {
23082323
ClearPageUptodate(cc->rpages[i]);
2309-
ClearPageError(cc->rpages[i]);
23102324
unlock_page(cc->rpages[i]);
23112325
}
23122326
}
@@ -2403,7 +2417,6 @@ static int f2fs_mpage_readpages(struct inode *inode,
24032417
#ifdef CONFIG_F2FS_FS_COMPRESSION
24042418
set_error_page:
24052419
#endif
2406-
SetPageError(page);
24072420
zero_user_segment(page, 0, PAGE_SIZE);
24082421
unlock_page(page);
24092422
}

fs/verity/verify.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ EXPORT_SYMBOL_GPL(fsverity_verify_page);
200200
* @bio: the bio to verify
201201
*
202202
* Verify a set of pages that have just been read from a verity file. The pages
203-
* must be pagecache pages that are still locked and not yet uptodate. Pages
204-
* that fail verification are set to the Error state. Verification is skipped
205-
* for pages already in the Error state, e.g. due to fscrypt decryption failure.
203+
* must be pagecache pages that are still locked and not yet uptodate. If a
204+
* page fails verification, then bio->bi_status is set to an error status.
206205
*
207206
* This is a helper function for use by the ->readahead() method of filesystems
208207
* that issue bios to read data directly into the page cache. Filesystems that
@@ -244,9 +243,10 @@ void fsverity_verify_bio(struct bio *bio)
244243
unsigned long level0_ra_pages =
245244
min(max_ra_pages, params->level0_blocks - level0_index);
246245

247-
if (!PageError(page) &&
248-
!verify_page(inode, vi, req, page, level0_ra_pages))
249-
SetPageError(page);
246+
if (!verify_page(inode, vi, req, page, level0_ra_pages)) {
247+
bio->bi_status = BLK_STS_IOERR;
248+
break;
249+
}
250250
}
251251

252252
fsverity_free_hash_request(params->hash_alg, req);

0 commit comments

Comments
 (0)