Skip to content

Commit ad0d9da

Browse files
committed
Merge tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fsverity updates from Eric Biggers: "The main change this cycle is to stop using the PG_error flag to track verity failures, and instead just track failures at the bio level. This follows a similar fscrypt change that went into 6.1, and it is a step towards freeing up PG_error for other uses. There's also one other small cleanup" * tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: fsverity: simplify fsverity_get_digest() fsverity: stop using PG_error to track error status
2 parents 8129bac + a4bbf53 commit ad0d9da

File tree

7 files changed

+85
-82
lines changed

7 files changed

+85
-82
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/fsverity_private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ struct fsverity_hash_alg {
3232
unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
3333
unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
3434
mempool_t req_pool; /* mempool with a preallocated hash request */
35+
/*
36+
* The HASH_ALGO_* constant for this algorithm. This is different from
37+
* FS_VERITY_HASH_ALG_*, which uses a different numbering scheme.
38+
*/
39+
enum hash_algo algo_id;
3540
};
3641

3742
/* Merkle tree parameters: hash algorithm, initial hash state, and topology */

fs/verity/hash_algs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ struct fsverity_hash_alg fsverity_hash_algs[] = {
1616
.name = "sha256",
1717
.digest_size = SHA256_DIGEST_SIZE,
1818
.block_size = SHA256_BLOCK_SIZE,
19+
.algo_id = HASH_ALGO_SHA256,
1920
},
2021
[FS_VERITY_HASH_ALG_SHA512] = {
2122
.name = "sha512",
2223
.digest_size = SHA512_DIGEST_SIZE,
2324
.block_size = SHA512_BLOCK_SIZE,
25+
.algo_id = HASH_ALGO_SHA512,
2426
},
2527
};
2628

@@ -324,5 +326,9 @@ void __init fsverity_check_hash_algs(void)
324326
*/
325327
BUG_ON(!is_power_of_2(alg->digest_size));
326328
BUG_ON(!is_power_of_2(alg->block_size));
329+
330+
/* Verify that there is a valid mapping to HASH_ALGO_*. */
331+
BUG_ON(alg->algo_id == 0);
332+
BUG_ON(alg->digest_size != hash_digest_size[alg->algo_id]);
327333
}
328334
}

fs/verity/measure.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ EXPORT_SYMBOL_GPL(fsverity_ioctl_measure);
6565
* @alg: (out) pointer to the hash algorithm enumeration
6666
*
6767
* Return the file hash algorithm and digest of an fsverity protected file.
68-
* Assumption: before calling fsverity_get_digest(), the file must have been
69-
* opened.
68+
* Assumption: before calling this, the file must have been opened.
7069
*
7170
* Return: 0 on success, -errno on failure
7271
*/
@@ -76,27 +75,13 @@ int fsverity_get_digest(struct inode *inode,
7675
{
7776
const struct fsverity_info *vi;
7877
const struct fsverity_hash_alg *hash_alg;
79-
int i;
8078

8179
vi = fsverity_get_info(inode);
8280
if (!vi)
8381
return -ENODATA; /* not a verity file */
8482

8583
hash_alg = vi->tree_params.hash_alg;
86-
memset(digest, 0, FS_VERITY_MAX_DIGEST_SIZE);
87-
88-
/* convert the verity hash algorithm name to a hash_algo_name enum */
89-
i = match_string(hash_algo_name, HASH_ALGO__LAST, hash_alg->name);
90-
if (i < 0)
91-
return -EINVAL;
92-
*alg = i;
93-
94-
if (WARN_ON_ONCE(hash_alg->digest_size != hash_digest_size[*alg]))
95-
return -EINVAL;
9684
memcpy(digest, vi->file_digest, hash_alg->digest_size);
97-
98-
pr_debug("file digest %s:%*phN\n", hash_algo_name[*alg],
99-
hash_digest_size[*alg], digest);
100-
85+
*alg = hash_alg->algo_id;
10186
return 0;
10287
}

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)