Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Data verification functions, i.e. hooks for ->readahead()
  4 *
  5 * Copyright 2019 Google LLC
  6 */
  7
  8#include "fsverity_private.h"
  9
 10#include <crypto/hash.h>
 11#include <linux/bio.h>
 
 12
 13static struct workqueue_struct *fsverity_read_workqueue;
 14
 15/*
 16 * Returns true if the hash block with index @hblock_idx in the tree, located in
 17 * @hpage, has already been verified.
 
 
 
 
 
 18 */
 19static bool is_hash_block_verified(struct fsverity_info *vi, struct page *hpage,
 20				   unsigned long hblock_idx)
 
 21{
 22	bool verified;
 23	unsigned int blocks_per_page;
 24	unsigned int i;
 25
 26	/*
 27	 * When the Merkle tree block size and page size are the same, then the
 28	 * ->hash_block_verified bitmap isn't allocated, and we use PG_checked
 29	 * to directly indicate whether the page's block has been verified.
 30	 *
 31	 * Using PG_checked also guarantees that we re-verify hash pages that
 32	 * get evicted and re-instantiated from the backing storage, as new
 33	 * pages always start out with PG_checked cleared.
 34	 */
 35	if (!vi->hash_block_verified)
 36		return PageChecked(hpage);
 37
 38	/*
 39	 * When the Merkle tree block size and page size differ, we use a bitmap
 40	 * to indicate whether each hash block has been verified.
 41	 *
 42	 * However, we still need to ensure that hash pages that get evicted and
 43	 * re-instantiated from the backing storage are re-verified.  To do
 44	 * this, we use PG_checked again, but now it doesn't really mean
 45	 * "checked".  Instead, now it just serves as an indicator for whether
 46	 * the hash page is newly instantiated or not.
 47	 *
 48	 * The first thread that sees PG_checked=0 must clear the corresponding
 49	 * bitmap bits, then set PG_checked=1.  This requires a spinlock.  To
 50	 * avoid having to take this spinlock in the common case of
 51	 * PG_checked=1, we start with an opportunistic lockless read.
 52	 */
 53	if (PageChecked(hpage)) {
 54		/*
 55		 * A read memory barrier is needed here to give ACQUIRE
 56		 * semantics to the above PageChecked() test.
 57		 */
 58		smp_rmb();
 59		return test_bit(hblock_idx, vi->hash_block_verified);
 60	}
 61	spin_lock(&vi->hash_page_init_lock);
 62	if (PageChecked(hpage)) {
 63		verified = test_bit(hblock_idx, vi->hash_block_verified);
 64	} else {
 65		blocks_per_page = vi->tree_params.blocks_per_page;
 66		hblock_idx = round_down(hblock_idx, blocks_per_page);
 67		for (i = 0; i < blocks_per_page; i++)
 68			clear_bit(hblock_idx + i, vi->hash_block_verified);
 69		/*
 70		 * A write memory barrier is needed here to give RELEASE
 71		 * semantics to the below SetPageChecked() operation.
 72		 */
 73		smp_wmb();
 74		SetPageChecked(hpage);
 75		verified = false;
 76	}
 77	spin_unlock(&vi->hash_page_init_lock);
 78	return verified;
 79}
 80
 81/*
 82 * Verify a single data block against the file's Merkle tree.
 83 *
 84 * In principle, we need to verify the entire path to the root node.  However,
 85 * for efficiency the filesystem may cache the hash blocks.  Therefore we need
 86 * only ascend the tree until an already-verified hash block is seen, and then
 87 * verify the path to that block.
 
 
 
 
 88 *
 89 * Return: %true if the data block is valid, else %false.
 
 
 
 90 */
 91static bool
 92verify_data_block(struct inode *inode, struct fsverity_info *vi,
 93		  const void *data, u64 data_pos, unsigned long max_ra_pages)
 94{
 95	const struct merkle_tree_params *params = &vi->tree_params;
 96	const unsigned int hsize = params->digest_size;
 
 97	int level;
 98	u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
 99	const u8 *want_hash;
100	u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
101	/* The hash blocks that are traversed, indexed by level */
102	struct {
103		/* Page containing the hash block */
104		struct page *page;
105		/* Mapped address of the hash block (will be within @page) */
106		const void *addr;
107		/* Index of the hash block in the tree overall */
108		unsigned long index;
109		/* Byte offset of the wanted hash relative to @addr */
110		unsigned int hoffset;
111	} hblocks[FS_VERITY_MAX_LEVELS];
112	/*
113	 * The index of the previous level's block within that level; also the
114	 * index of that block's hash within the current level.
115	 */
116	u64 hidx = data_pos >> params->log_blocksize;
117
118	/* Up to 1 + FS_VERITY_MAX_LEVELS pages may be mapped at once */
119	BUILD_BUG_ON(1 + FS_VERITY_MAX_LEVELS > KM_MAX_IDX);
120
121	if (unlikely(data_pos >= inode->i_size)) {
122		/*
123		 * This can happen in the data page spanning EOF when the Merkle
124		 * tree block size is less than the page size.  The Merkle tree
125		 * doesn't cover data blocks fully past EOF.  But the entire
126		 * page spanning EOF can be visible to userspace via a mmap, and
127		 * any part past EOF should be all zeroes.  Therefore, we need
128		 * to verify that any data blocks fully past EOF are all zeroes.
129		 */
130		if (memchr_inv(data, 0, params->block_size)) {
131			fsverity_err(inode,
132				     "FILE CORRUPTED!  Data past EOF is not zeroed");
133			return false;
134		}
135		return true;
136	}
137
138	/*
139	 * Starting at the leaf level, ascend the tree saving hash blocks along
140	 * the way until we find a hash block that has already been verified, or
141	 * until we reach the root.
142	 */
143	for (level = 0; level < params->num_levels; level++) {
144		unsigned long next_hidx;
145		unsigned long hblock_idx;
146		pgoff_t hpage_idx;
147		unsigned int hblock_offset_in_page;
148		unsigned int hoffset;
149		struct page *hpage;
150		const void *haddr;
151
152		/*
153		 * The index of the block in the current level; also the index
154		 * of that block's hash within the next level.
155		 */
156		next_hidx = hidx >> params->log_arity;
157
158		/* Index of the hash block in the tree overall */
159		hblock_idx = params->level_start[level] + next_hidx;
160
161		/* Index of the hash page in the tree overall */
162		hpage_idx = hblock_idx >> params->log_blocks_per_page;
163
164		/* Byte offset of the hash block within the page */
165		hblock_offset_in_page =
166			(hblock_idx << params->log_blocksize) & ~PAGE_MASK;
167
168		/* Byte offset of the hash within the block */
169		hoffset = (hidx << params->log_digestsize) &
170			  (params->block_size - 1);
171
172		hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode,
173				hpage_idx, level == 0 ? min(max_ra_pages,
174					params->tree_pages - hpage_idx) : 0);
175		if (IS_ERR(hpage)) {
 
176			fsverity_err(inode,
177				     "Error %ld reading Merkle tree page %lu",
178				     PTR_ERR(hpage), hpage_idx);
179			goto error;
180		}
181		haddr = kmap_local_page(hpage) + hblock_offset_in_page;
182		if (is_hash_block_verified(vi, hpage, hblock_idx)) {
183			memcpy(_want_hash, haddr + hoffset, hsize);
184			want_hash = _want_hash;
185			kunmap_local(haddr);
186			put_page(hpage);
 
 
 
187			goto descend;
188		}
189		hblocks[level].page = hpage;
190		hblocks[level].addr = haddr;
191		hblocks[level].index = hblock_idx;
192		hblocks[level].hoffset = hoffset;
193		hidx = next_hidx;
194	}
195
196	want_hash = vi->root_hash;
 
 
197descend:
198	/* Descend the tree verifying hash blocks. */
199	for (; level > 0; level--) {
200		struct page *hpage = hblocks[level - 1].page;
201		const void *haddr = hblocks[level - 1].addr;
202		unsigned long hblock_idx = hblocks[level - 1].index;
203		unsigned int hoffset = hblocks[level - 1].hoffset;
204
205		if (fsverity_hash_block(params, inode, haddr, real_hash) != 0)
206			goto error;
207		if (memcmp(want_hash, real_hash, hsize) != 0)
208			goto corrupted;
209		/*
210		 * Mark the hash block as verified.  This must be atomic and
211		 * idempotent, as the same hash block might be verified by
212		 * multiple threads concurrently.
213		 */
214		if (vi->hash_block_verified)
215			set_bit(hblock_idx, vi->hash_block_verified);
216		else
217			SetPageChecked(hpage);
218		memcpy(_want_hash, haddr + hoffset, hsize);
219		want_hash = _want_hash;
220		kunmap_local(haddr);
221		put_page(hpage);
 
 
222	}
223
224	/* Finally, verify the data block. */
225	if (fsverity_hash_block(params, inode, data, real_hash) != 0)
226		goto error;
227	if (memcmp(want_hash, real_hash, hsize) != 0)
228		goto corrupted;
229	return true;
230
231corrupted:
232	fsverity_err(inode,
233		     "FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
234		     data_pos, level - 1,
235		     params->hash_alg->name, hsize, want_hash,
236		     params->hash_alg->name, hsize, real_hash);
237error:
238	for (; level > 0; level--) {
239		kunmap_local(hblocks[level - 1].addr);
240		put_page(hblocks[level - 1].page);
241	}
242	return false;
243}
244
245static bool
246verify_data_blocks(struct folio *data_folio, size_t len, size_t offset,
247		   unsigned long max_ra_pages)
248{
249	struct inode *inode = data_folio->mapping->host;
250	struct fsverity_info *vi = inode->i_verity_info;
251	const unsigned int block_size = vi->tree_params.block_size;
252	u64 pos = (u64)data_folio->index << PAGE_SHIFT;
253
254	if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offset, block_size)))
255		return false;
256	if (WARN_ON_ONCE(!folio_test_locked(data_folio) ||
257			 folio_test_uptodate(data_folio)))
258		return false;
259	do {
260		void *data;
261		bool valid;
262
263		data = kmap_local_folio(data_folio, offset);
264		valid = verify_data_block(inode, vi, data, pos + offset,
265					  max_ra_pages);
266		kunmap_local(data);
267		if (!valid)
268			return false;
269		offset += block_size;
270		len -= block_size;
271	} while (len);
272	return true;
273}
274
275/**
276 * fsverity_verify_blocks() - verify data in a folio
277 * @folio: the folio containing the data to verify
278 * @len: the length of the data to verify in the folio
279 * @offset: the offset of the data to verify in the folio
280 *
281 * Verify data that has just been read from a verity file.  The data must be
282 * located in a pagecache folio that is still locked and not yet uptodate.  The
283 * length and offset of the data must be Merkle tree block size aligned.
284 *
285 * Return: %true if the data is valid, else %false.
 
 
 
286 */
287bool fsverity_verify_blocks(struct folio *folio, size_t len, size_t offset)
288{
289	return verify_data_blocks(folio, len, offset, 0);
 
 
 
 
 
 
 
 
 
 
 
 
290}
291EXPORT_SYMBOL_GPL(fsverity_verify_blocks);
292
293#ifdef CONFIG_BLOCK
294/**
295 * fsverity_verify_bio() - verify a 'read' bio that has just completed
296 * @bio: the bio to verify
297 *
298 * Verify the bio's data against the file's Merkle tree.  All bio data segments
299 * must be aligned to the file's Merkle tree block size.  If any data fails
300 * verification, then bio->bi_status is set to an error status.
 
301 *
302 * This is a helper function for use by the ->readahead() method of filesystems
303 * that issue bios to read data directly into the page cache.  Filesystems that
304 * populate the page cache without issuing bios (e.g. non block-based
305 * filesystems) must instead call fsverity_verify_page() directly on each page.
306 * All filesystems must also call fsverity_verify_page() on holes.
307 */
308void fsverity_verify_bio(struct bio *bio)
309{
310	struct folio_iter fi;
 
 
 
 
 
311	unsigned long max_ra_pages = 0;
312
 
 
 
313	if (bio->bi_opf & REQ_RAHEAD) {
314		/*
315		 * If this bio is for data readahead, then we also do readahead
316		 * of the first (largest) level of the Merkle tree.  Namely,
317		 * when a Merkle tree page is read, we also try to piggy-back on
318		 * some additional pages -- up to 1/4 the number of data pages.
319		 *
320		 * This improves sequential read performance, as it greatly
321		 * reduces the number of I/O requests made to the Merkle tree.
322		 */
323		max_ra_pages = bio->bi_iter.bi_size >> (PAGE_SHIFT + 2);
 
 
324	}
325
326	bio_for_each_folio_all(fi, bio) {
327		if (!verify_data_blocks(fi.folio, fi.length, fi.offset,
328					max_ra_pages)) {
329			bio->bi_status = BLK_STS_IOERR;
330			break;
331		}
 
 
 
332	}
 
 
333}
334EXPORT_SYMBOL_GPL(fsverity_verify_bio);
335#endif /* CONFIG_BLOCK */
336
337/**
338 * fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
339 * @work: the work to enqueue
340 *
341 * Enqueue verification work for asynchronous processing.
342 */
343void fsverity_enqueue_verify_work(struct work_struct *work)
344{
345	queue_work(fsverity_read_workqueue, work);
346}
347EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
348
349void __init fsverity_init_workqueue(void)
350{
351	/*
352	 * Use a high-priority workqueue to prioritize verification work, which
353	 * blocks reads from completing, over regular application tasks.
 
354	 *
355	 * For performance reasons, don't use an unbound workqueue.  Using an
356	 * unbound workqueue for crypto operations causes excessive scheduler
357	 * latency on ARM64.
358	 */
359	fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
360						  WQ_HIGHPRI,
361						  num_online_cpus());
362	if (!fsverity_read_workqueue)
363		panic("failed to allocate fsverity_read_queue");
 
 
 
 
 
 
 
364}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Data verification functions, i.e. hooks for ->readpages()
  4 *
  5 * Copyright 2019 Google LLC
  6 */
  7
  8#include "fsverity_private.h"
  9
 10#include <crypto/hash.h>
 11#include <linux/bio.h>
 12#include <linux/ratelimit.h>
 13
 14static struct workqueue_struct *fsverity_read_workqueue;
 15
 16/**
 17 * hash_at_level() - compute the location of the block's hash at the given level
 18 *
 19 * @params:	(in) the Merkle tree parameters
 20 * @dindex:	(in) the index of the data block being verified
 21 * @level:	(in) the level of hash we want (0 is leaf level)
 22 * @hindex:	(out) the index of the hash block containing the wanted hash
 23 * @hoffset:	(out) the byte offset to the wanted hash within the hash block
 24 */
 25static void hash_at_level(const struct merkle_tree_params *params,
 26			  pgoff_t dindex, unsigned int level, pgoff_t *hindex,
 27			  unsigned int *hoffset)
 28{
 29	pgoff_t position;
 
 
 30
 31	/* Offset of the hash within the level's region, in hashes */
 32	position = dindex >> (level * params->log_arity);
 
 
 
 
 
 
 
 
 
 33
 34	/* Index of the hash block in the tree overall */
 35	*hindex = params->level_start[level] + (position >> params->log_arity);
 36
 37	/* Offset of the wanted hash (in bytes) within the hash block */
 38	*hoffset = (position & ((1 << params->log_arity) - 1)) <<
 39		   (params->log_blocksize - params->log_arity);
 40}
 41
 42/* Extract a hash from a hash page */
 43static void extract_hash(struct page *hpage, unsigned int hoffset,
 44			 unsigned int hsize, u8 *out)
 45{
 46	void *virt = kmap_atomic(hpage);
 47
 48	memcpy(out, virt + hoffset, hsize);
 49	kunmap_atomic(virt);
 50}
 51
 52static inline int cmp_hashes(const struct fsverity_info *vi,
 53			     const u8 *want_hash, const u8 *real_hash,
 54			     pgoff_t index, int level)
 55{
 56	const unsigned int hsize = vi->tree_params.digest_size;
 57
 58	if (memcmp(want_hash, real_hash, hsize) == 0)
 59		return 0;
 60
 61	fsverity_err(vi->inode,
 62		     "FILE CORRUPTED! index=%lu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
 63		     index, level,
 64		     vi->tree_params.hash_alg->name, hsize, want_hash,
 65		     vi->tree_params.hash_alg->name, hsize, real_hash);
 66	return -EBADMSG;
 
 
 
 
 
 
 
 
 67}
 68
 69/*
 70 * Verify a single data page against the file's Merkle tree.
 71 *
 72 * In principle, we need to verify the entire path to the root node.  However,
 73 * for efficiency the filesystem may cache the hash pages.  Therefore we need
 74 * only ascend the tree until an already-verified page is seen, as indicated by
 75 * the PageChecked bit being set; then verify the path to that page.
 76 *
 77 * This code currently only supports the case where the verity block size is
 78 * equal to PAGE_SIZE.  Doing otherwise would be possible but tricky, since we
 79 * wouldn't be able to use the PageChecked bit.
 80 *
 81 * Note that multiple processes may race to verify a hash page and mark it
 82 * Checked, but it doesn't matter; the result will be the same either way.
 83 *
 84 * Return: true if the page is valid, else false.
 85 */
 86static bool verify_page(struct inode *inode, const struct fsverity_info *vi,
 87			struct ahash_request *req, struct page *data_page,
 88			unsigned long level0_ra_pages)
 89{
 90	const struct merkle_tree_params *params = &vi->tree_params;
 91	const unsigned int hsize = params->digest_size;
 92	const pgoff_t index = data_page->index;
 93	int level;
 94	u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
 95	const u8 *want_hash;
 96	u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
 97	struct page *hpages[FS_VERITY_MAX_LEVELS];
 98	unsigned int hoffsets[FS_VERITY_MAX_LEVELS];
 99	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101	if (WARN_ON_ONCE(!PageLocked(data_page) || PageUptodate(data_page)))
102		return false;
103
104	pr_debug_ratelimited("Verifying data page %lu...\n", index);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
106	/*
107	 * Starting at the leaf level, ascend the tree saving hash pages along
108	 * the way until we find a verified hash page, indicated by PageChecked;
109	 * or until we reach the root.
110	 */
111	for (level = 0; level < params->num_levels; level++) {
112		pgoff_t hindex;
 
 
 
113		unsigned int hoffset;
114		struct page *hpage;
 
 
 
 
 
 
 
 
 
 
 
 
 
115
116		hash_at_level(params, index, level, &hindex, &hoffset);
 
 
117
118		pr_debug_ratelimited("Level %d: hindex=%lu, hoffset=%u\n",
119				     level, hindex, hoffset);
 
120
121		hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode, hindex,
122				level == 0 ? level0_ra_pages : 0);
 
123		if (IS_ERR(hpage)) {
124			err = PTR_ERR(hpage);
125			fsverity_err(inode,
126				     "Error %d reading Merkle tree page %lu",
127				     err, hindex);
128			goto out;
129		}
130
131		if (PageChecked(hpage)) {
132			extract_hash(hpage, hoffset, hsize, _want_hash);
133			want_hash = _want_hash;
 
134			put_page(hpage);
135			pr_debug_ratelimited("Hash page already checked, want %s:%*phN\n",
136					     params->hash_alg->name,
137					     hsize, want_hash);
138			goto descend;
139		}
140		pr_debug_ratelimited("Hash page not yet checked\n");
141		hpages[level] = hpage;
142		hoffsets[level] = hoffset;
 
 
143	}
144
145	want_hash = vi->root_hash;
146	pr_debug("Want root hash: %s:%*phN\n",
147		 params->hash_alg->name, hsize, want_hash);
148descend:
149	/* Descend the tree verifying hash pages */
150	for (; level > 0; level--) {
151		struct page *hpage = hpages[level - 1];
152		unsigned int hoffset = hoffsets[level - 1];
153
154		err = fsverity_hash_page(params, inode, req, hpage, real_hash);
155		if (err)
156			goto out;
157		err = cmp_hashes(vi, want_hash, real_hash, index, level - 1);
158		if (err)
159			goto out;
160		SetPageChecked(hpage);
161		extract_hash(hpage, hoffset, hsize, _want_hash);
 
 
 
 
 
 
 
 
162		want_hash = _want_hash;
 
163		put_page(hpage);
164		pr_debug("Verified hash page at level %d, now want %s:%*phN\n",
165			 level - 1, params->hash_alg->name, hsize, want_hash);
166	}
167
168	/* Finally, verify the data page */
169	err = fsverity_hash_page(params, inode, req, data_page, real_hash);
170	if (err)
171		goto out;
172	err = cmp_hashes(vi, want_hash, real_hash, index, -1);
173out:
174	for (; level > 0; level--)
175		put_page(hpages[level - 1]);
 
 
 
 
 
 
 
 
 
 
 
 
176
177	return err == 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178}
179
180/**
181 * fsverity_verify_page() - verify a data page
182 * @page: the page to verity
 
 
 
 
 
 
183 *
184 * Verify a page that has just been read from a verity file.  The page must be a
185 * pagecache page that is still locked and not yet uptodate.
186 *
187 * Return: true if the page is valid, else false.
188 */
189bool fsverity_verify_page(struct page *page)
190{
191	struct inode *inode = page->mapping->host;
192	const struct fsverity_info *vi = inode->i_verity_info;
193	struct ahash_request *req;
194	bool valid;
195
196	/* This allocation never fails, since it's mempool-backed. */
197	req = fsverity_alloc_hash_request(vi->tree_params.hash_alg, GFP_NOFS);
198
199	valid = verify_page(inode, vi, req, page, 0);
200
201	fsverity_free_hash_request(vi->tree_params.hash_alg, req);
202
203	return valid;
204}
205EXPORT_SYMBOL_GPL(fsverity_verify_page);
206
207#ifdef CONFIG_BLOCK
208/**
209 * fsverity_verify_bio() - verify a 'read' bio that has just completed
210 * @bio: the bio to verify
211 *
212 * Verify a set of pages that have just been read from a verity file.  The pages
213 * must be pagecache pages that are still locked and not yet uptodate.  Pages
214 * that fail verification are set to the Error state.  Verification is skipped
215 * for pages already in the Error state, e.g. due to fscrypt decryption failure.
216 *
217 * This is a helper function for use by the ->readpages() method of filesystems
218 * that issue bios to read data directly into the page cache.  Filesystems that
219 * populate the page cache without issuing bios (e.g. non block-based
220 * filesystems) must instead call fsverity_verify_page() directly on each page.
221 * All filesystems must also call fsverity_verify_page() on holes.
222 */
223void fsverity_verify_bio(struct bio *bio)
224{
225	struct inode *inode = bio_first_page_all(bio)->mapping->host;
226	const struct fsverity_info *vi = inode->i_verity_info;
227	const struct merkle_tree_params *params = &vi->tree_params;
228	struct ahash_request *req;
229	struct bio_vec *bv;
230	struct bvec_iter_all iter_all;
231	unsigned long max_ra_pages = 0;
232
233	/* This allocation never fails, since it's mempool-backed. */
234	req = fsverity_alloc_hash_request(params->hash_alg, GFP_NOFS);
235
236	if (bio->bi_opf & REQ_RAHEAD) {
237		/*
238		 * If this bio is for data readahead, then we also do readahead
239		 * of the first (largest) level of the Merkle tree.  Namely,
240		 * when a Merkle tree page is read, we also try to piggy-back on
241		 * some additional pages -- up to 1/4 the number of data pages.
242		 *
243		 * This improves sequential read performance, as it greatly
244		 * reduces the number of I/O requests made to the Merkle tree.
245		 */
246		bio_for_each_segment_all(bv, bio, iter_all)
247			max_ra_pages++;
248		max_ra_pages /= 4;
249	}
250
251	bio_for_each_segment_all(bv, bio, iter_all) {
252		struct page *page = bv->bv_page;
253		unsigned long level0_index = page->index >> params->log_arity;
254		unsigned long level0_ra_pages =
255			min(max_ra_pages, params->level0_blocks - level0_index);
256
257		if (!PageError(page) &&
258		    !verify_page(inode, vi, req, page, level0_ra_pages))
259			SetPageError(page);
260	}
261
262	fsverity_free_hash_request(params->hash_alg, req);
263}
264EXPORT_SYMBOL_GPL(fsverity_verify_bio);
265#endif /* CONFIG_BLOCK */
266
267/**
268 * fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
269 * @work: the work to enqueue
270 *
271 * Enqueue verification work for asynchronous processing.
272 */
273void fsverity_enqueue_verify_work(struct work_struct *work)
274{
275	queue_work(fsverity_read_workqueue, work);
276}
277EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
278
279int __init fsverity_init_workqueue(void)
280{
281	/*
282	 * Use an unbound workqueue to allow bios to be verified in parallel
283	 * even when they happen to complete on the same CPU.  This sacrifices
284	 * locality, but it's worthwhile since hashing is CPU-intensive.
285	 *
286	 * Also use a high-priority workqueue to prioritize verification work,
287	 * which blocks reads from completing, over regular application tasks.
 
288	 */
289	fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
290						  WQ_UNBOUND | WQ_HIGHPRI,
291						  num_online_cpus());
292	if (!fsverity_read_workqueue)
293		return -ENOMEM;
294	return 0;
295}
296
297void __init fsverity_exit_workqueue(void)
298{
299	destroy_workqueue(fsverity_read_workqueue);
300	fsverity_read_workqueue = NULL;
301}