Skip to content

Commit 2a7349a

Browse files
Eric Biggersbrauner
authored andcommitted
fsverity: add support for info in fs-specific part of inode
Add an inode_info_offs field to struct fsverity_operations, and update fs/verity/ to support it. When set to a nonzero value, it specifies the offset to the fsverity_info pointer within the filesystem-specific part of the inode structure, to be used instead of inode::i_verity_info. Since this makes inode::i_verity_info no longer necessarily used, update comments that mentioned it. This is a prerequisite for a later commit that removes inode::i_verity_info, saving memory and improving cache efficiency on filesystems that don't support fsverity. Co-developed-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Eric Biggers <ebiggers@kernel.org> Link: https://lore.kernel.org/20250810075706.172910-9-ebiggers@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent ab90c2d commit 2a7349a

5 files changed

Lines changed: 55 additions & 29 deletions

File tree

fs/verity/enable.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ static int enable_verity(struct file *filp,
284284
/* Successfully enabled verity */
285285

286286
/*
287-
* Readers can start using ->i_verity_info immediately, so it
288-
* can't be rolled back once set. So don't set it until just
289-
* after the filesystem has successfully enabled verity.
287+
* Readers can start using the inode's verity info immediately,
288+
* so it can't be rolled back once set. So don't set it until
289+
* just after the filesystem has successfully enabled verity.
290290
*/
291291
fsverity_set_info(inode, vi);
292292
}

fs/verity/fsverity_private.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ struct merkle_tree_params {
6363
* fsverity_info - cached verity metadata for an inode
6464
*
6565
* When a verity file is first opened, an instance of this struct is allocated
66-
* and stored in ->i_verity_info; it remains until the inode is evicted. It
67-
* caches information about the Merkle tree that's needed to efficiently verify
68-
* data read from the file. It also caches the file digest. The Merkle tree
69-
* pages themselves are not cached here, but the filesystem may cache them.
66+
* and a pointer to it is stored in the file's in-memory inode. It remains
67+
* until the inode is evicted. It caches information about the Merkle tree
68+
* that's needed to efficiently verify data read from the file. It also caches
69+
* the file digest. The Merkle tree pages themselves are not cached here, but
70+
* the filesystem may cache them.
7071
*/
7172
struct fsverity_info {
7273
struct merkle_tree_params tree_params;

fs/verity/open.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ struct fsverity_info *fsverity_create_info(const struct inode *inode,
244244
void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
245245
{
246246
/*
247-
* Multiple tasks may race to set ->i_verity_info, so use
248-
* cmpxchg_release(). This pairs with the smp_load_acquire() in
249-
* fsverity_get_info(). I.e., here we publish ->i_verity_info with a
250-
* RELEASE barrier so that other tasks can ACQUIRE it.
247+
* Multiple tasks may race to set the inode's verity info pointer, so
248+
* use cmpxchg_release(). This pairs with the smp_load_acquire() in
249+
* fsverity_get_info(). I.e., publish the pointer with a RELEASE
250+
* barrier so that other tasks can ACQUIRE it.
251251
*/
252-
if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
253-
/* Lost the race, so free the fsverity_info we allocated. */
252+
if (cmpxchg_release(fsverity_info_addr(inode), NULL, vi) != NULL) {
253+
/* Lost the race, so free the verity info we allocated. */
254254
fsverity_free_info(vi);
255255
/*
256-
* Afterwards, the caller may access ->i_verity_info directly,
257-
* so make sure to ACQUIRE the winning fsverity_info.
256+
* Afterwards, the caller may access the inode's verity info
257+
* directly, so make sure to ACQUIRE the winning verity info.
258258
*/
259259
(void)fsverity_get_info(inode);
260260
}
@@ -350,7 +350,6 @@ int fsverity_get_descriptor(struct inode *inode,
350350
return 0;
351351
}
352352

353-
/* Ensure the inode has an ->i_verity_info */
354353
static int ensure_verity_info(struct inode *inode)
355354
{
356355
struct fsverity_info *vi = fsverity_get_info(inode);
@@ -395,8 +394,10 @@ EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr);
395394

396395
void __fsverity_cleanup_inode(struct inode *inode)
397396
{
398-
fsverity_free_info(inode->i_verity_info);
399-
inode->i_verity_info = NULL;
397+
struct fsverity_info **vi_addr = fsverity_info_addr(inode);
398+
399+
fsverity_free_info(*vi_addr);
400+
*vi_addr = NULL;
400401
}
401402
EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);
402403

fs/verity/verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ verify_data_blocks(struct folio *data_folio, size_t len, size_t offset,
245245
unsigned long max_ra_pages)
246246
{
247247
struct inode *inode = data_folio->mapping->host;
248-
struct fsverity_info *vi = inode->i_verity_info;
248+
struct fsverity_info *vi = *fsverity_info_addr(inode);
249249
const unsigned int block_size = vi->tree_params.block_size;
250250
u64 pos = (u64)data_folio->index << PAGE_SHIFT;
251251

include/linux/fsverity.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828

2929
/* Verity operations for filesystems */
3030
struct fsverity_operations {
31+
/**
32+
* The offset of the pointer to struct fsverity_info in the
33+
* filesystem-specific part of the inode, relative to the beginning of
34+
* the common part of the inode (the 'struct inode').
35+
*/
36+
ptrdiff_t inode_info_offs;
3137

3238
/**
3339
* Begin enabling verity on the given file.
@@ -124,15 +130,33 @@ struct fsverity_operations {
124130

125131
#ifdef CONFIG_FS_VERITY
126132

133+
static inline struct fsverity_info **
134+
fsverity_info_addr(const struct inode *inode)
135+
{
136+
if (inode->i_sb->s_vop->inode_info_offs == 0)
137+
return (struct fsverity_info **)&inode->i_verity_info;
138+
return (void *)inode + inode->i_sb->s_vop->inode_info_offs;
139+
}
140+
127141
static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
128142
{
129143
/*
130-
* Pairs with the cmpxchg_release() in fsverity_set_info().
131-
* I.e., another task may publish ->i_verity_info concurrently,
132-
* executing a RELEASE barrier. We need to use smp_load_acquire() here
133-
* to safely ACQUIRE the memory the other task published.
144+
* Since this function can be called on inodes belonging to filesystems
145+
* that don't support fsverity at all, and fsverity_info_addr() doesn't
146+
* work on such filesystems, we have to start with an IS_VERITY() check.
147+
* Checking IS_VERITY() here is also useful to minimize the overhead of
148+
* fsverity_active() on non-verity files.
149+
*/
150+
if (!IS_VERITY(inode))
151+
return NULL;
152+
153+
/*
154+
* Pairs with the cmpxchg_release() in fsverity_set_info(). I.e.,
155+
* another task may publish the inode's verity info concurrently,
156+
* executing a RELEASE barrier. Use smp_load_acquire() here to safely
157+
* ACQUIRE the memory the other task published.
134158
*/
135-
return smp_load_acquire(&inode->i_verity_info);
159+
return smp_load_acquire(fsverity_info_addr(inode));
136160
}
137161

138162
/* enable.c */
@@ -156,11 +180,11 @@ void __fsverity_cleanup_inode(struct inode *inode);
156180
* fsverity_cleanup_inode() - free the inode's verity info, if present
157181
* @inode: an inode being evicted
158182
*
159-
* Filesystems must call this on inode eviction to free ->i_verity_info.
183+
* Filesystems must call this on inode eviction to free the inode's verity info.
160184
*/
161185
static inline void fsverity_cleanup_inode(struct inode *inode)
162186
{
163-
if (inode->i_verity_info)
187+
if (*fsverity_info_addr(inode))
164188
__fsverity_cleanup_inode(inode);
165189
}
166190

@@ -267,12 +291,12 @@ static inline bool fsverity_verify_page(struct page *page)
267291
* fsverity_active() - do reads from the inode need to go through fs-verity?
268292
* @inode: inode to check
269293
*
270-
* This checks whether ->i_verity_info has been set.
294+
* This checks whether the inode's verity info has been set.
271295
*
272296
* Filesystems call this from ->readahead() to check whether the pages need to
273297
* be verified or not. Don't use IS_VERITY() for this purpose; it's subject to
274298
* a race condition where the file is being read concurrently with
275-
* FS_IOC_ENABLE_VERITY completing. (S_VERITY is set before ->i_verity_info.)
299+
* FS_IOC_ENABLE_VERITY completing. (S_VERITY is set before the verity info.)
276300
*
277301
* Return: true if reads need to go through fs-verity, otherwise false
278302
*/
@@ -287,7 +311,7 @@ static inline bool fsverity_active(const struct inode *inode)
287311
* @filp: the struct file being set up
288312
*
289313
* When opening a verity file, deny the open if it is for writing. Otherwise,
290-
* set up the inode's ->i_verity_info if not already done.
314+
* set up the inode's verity info if not already done.
291315
*
292316
* When combined with fscrypt, this must be called after fscrypt_file_open().
293317
* Otherwise, we won't have the key set up to decrypt the verity metadata.

0 commit comments

Comments
 (0)