]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fsverity: add support for info in fs-specific part of inode
authorEric Biggers <ebiggers@kernel.org>
Sun, 10 Aug 2025 07:57:01 +0000 (00:57 -0700)
committerChristian Brauner <brauner@kernel.org>
Thu, 21 Aug 2025 11:58:07 +0000 (13:58 +0200)
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>
fs/verity/enable.c
fs/verity/fsverity_private.h
fs/verity/open.c
fs/verity/verify.c
include/linux/fsverity.h

index 503268cf429627fdd3fd111d49de2e86629ba5b4..89eccc4becf902518bf6671b5ea1bf0fae25913a 100644 (file)
@@ -284,9 +284,9 @@ static int enable_verity(struct file *filp,
                /* Successfully enabled verity */
 
                /*
-                * Readers can start using ->i_verity_info immediately, so it
-                * can't be rolled back once set.  So don't set it until just
-                * after the filesystem has successfully enabled verity.
+                * Readers can start using the inode's verity info immediately,
+                * so it can't be rolled back once set.  So don't set it until
+                * just after the filesystem has successfully enabled verity.
                 */
                fsverity_set_info(inode, vi);
        }
index 5fe854a5b9ad3d1da8d9b761b0894002cd6b8725..bc1d887c532e74da245551ad9ebc3413e291c34f 100644 (file)
@@ -63,10 +63,11 @@ struct merkle_tree_params {
  * fsverity_info - cached verity metadata for an inode
  *
  * When a verity file is first opened, an instance of this struct is allocated
- * and stored in ->i_verity_info; it remains until the inode is evicted.  It
- * caches information about the Merkle tree that's needed to efficiently verify
- * data read from the file.  It also caches the file digest.  The Merkle tree
- * pages themselves are not cached here, but the filesystem may cache them.
+ * and a pointer to it is stored in the file's in-memory inode.  It remains
+ * until the inode is evicted.  It caches information about the Merkle tree
+ * that's needed to efficiently verify data read from the file.  It also caches
+ * the file digest.  The Merkle tree pages themselves are not cached here, but
+ * the filesystem may cache them.
  */
 struct fsverity_info {
        struct merkle_tree_params tree_params;
index c561e130cd0c61c405f964ef24837745b1222987..77b1c977af025693024bd619d7e6ed9bcdecd374 100644 (file)
@@ -244,17 +244,17 @@ fail:
 void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
 {
        /*
-        * Multiple tasks may race to set ->i_verity_info, so use
-        * cmpxchg_release().  This pairs with the smp_load_acquire() in
-        * fsverity_get_info().  I.e., here we publish ->i_verity_info with a
-        * RELEASE barrier so that other tasks can ACQUIRE it.
+        * Multiple tasks may race to set the inode's verity info pointer, so
+        * use cmpxchg_release().  This pairs with the smp_load_acquire() in
+        * fsverity_get_info().  I.e., publish the pointer with a RELEASE
+        * barrier so that other tasks can ACQUIRE it.
         */
-       if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
-               /* Lost the race, so free the fsverity_info we allocated. */
+       if (cmpxchg_release(fsverity_info_addr(inode), NULL, vi) != NULL) {
+               /* Lost the race, so free the verity info we allocated. */
                fsverity_free_info(vi);
                /*
-                * Afterwards, the caller may access ->i_verity_info directly,
-                * so make sure to ACQUIRE the winning fsverity_info.
+                * Afterwards, the caller may access the inode's verity info
+                * directly, so make sure to ACQUIRE the winning verity info.
                 */
                (void)fsverity_get_info(inode);
        }
@@ -350,7 +350,6 @@ int fsverity_get_descriptor(struct inode *inode,
        return 0;
 }
 
-/* Ensure the inode has an ->i_verity_info */
 static int ensure_verity_info(struct inode *inode)
 {
        struct fsverity_info *vi = fsverity_get_info(inode);
@@ -395,8 +394,10 @@ EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr);
 
 void __fsverity_cleanup_inode(struct inode *inode)
 {
-       fsverity_free_info(inode->i_verity_info);
-       inode->i_verity_info = NULL;
+       struct fsverity_info **vi_addr = fsverity_info_addr(inode);
+
+       fsverity_free_info(*vi_addr);
+       *vi_addr = NULL;
 }
 EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);
 
index a1f00c3fd3b2765eca730bf10ff5ef6db9c411e0..affc307eb6a6b16e9ef9a5adbce1901c8c33c0f4 100644 (file)
@@ -245,7 +245,7 @@ verify_data_blocks(struct folio *data_folio, size_t len, size_t offset,
                   unsigned long max_ra_pages)
 {
        struct inode *inode = data_folio->mapping->host;
-       struct fsverity_info *vi = inode->i_verity_info;
+       struct fsverity_info *vi = *fsverity_info_addr(inode);
        const unsigned int block_size = vi->tree_params.block_size;
        u64 pos = (u64)data_folio->index << PAGE_SHIFT;
 
index 1eb7eae580be70d96724d8286c4394fe5220e1a3..e0f132cb7839327226f7bed493566456df5915c1 100644 (file)
 
 /* Verity operations for filesystems */
 struct fsverity_operations {
+       /**
+        * The offset of the pointer to struct fsverity_info in the
+        * filesystem-specific part of the inode, relative to the beginning of
+        * the common part of the inode (the 'struct inode').
+        */
+       ptrdiff_t inode_info_offs;
 
        /**
         * Begin enabling verity on the given file.
@@ -124,15 +130,33 @@ struct fsverity_operations {
 
 #ifdef CONFIG_FS_VERITY
 
+static inline struct fsverity_info **
+fsverity_info_addr(const struct inode *inode)
+{
+       if (inode->i_sb->s_vop->inode_info_offs == 0)
+               return (struct fsverity_info **)&inode->i_verity_info;
+       return (void *)inode + inode->i_sb->s_vop->inode_info_offs;
+}
+
 static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
 {
        /*
-        * Pairs with the cmpxchg_release() in fsverity_set_info().
-        * I.e., another task may publish ->i_verity_info concurrently,
-        * executing a RELEASE barrier.  We need to use smp_load_acquire() here
-        * to safely ACQUIRE the memory the other task published.
+        * Since this function can be called on inodes belonging to filesystems
+        * that don't support fsverity at all, and fsverity_info_addr() doesn't
+        * work on such filesystems, we have to start with an IS_VERITY() check.
+        * Checking IS_VERITY() here is also useful to minimize the overhead of
+        * fsverity_active() on non-verity files.
+        */
+       if (!IS_VERITY(inode))
+               return NULL;
+
+       /*
+        * Pairs with the cmpxchg_release() in fsverity_set_info().  I.e.,
+        * another task may publish the inode's verity info concurrently,
+        * executing a RELEASE barrier.  Use smp_load_acquire() here to safely
+        * ACQUIRE the memory the other task published.
         */
-       return smp_load_acquire(&inode->i_verity_info);
+       return smp_load_acquire(fsverity_info_addr(inode));
 }
 
 /* enable.c */
@@ -156,11 +180,11 @@ void __fsverity_cleanup_inode(struct inode *inode);
  * fsverity_cleanup_inode() - free the inode's verity info, if present
  * @inode: an inode being evicted
  *
- * Filesystems must call this on inode eviction to free ->i_verity_info.
+ * Filesystems must call this on inode eviction to free the inode's verity info.
  */
 static inline void fsverity_cleanup_inode(struct inode *inode)
 {
-       if (inode->i_verity_info)
+       if (*fsverity_info_addr(inode))
                __fsverity_cleanup_inode(inode);
 }
 
@@ -267,12 +291,12 @@ static inline bool fsverity_verify_page(struct page *page)
  * fsverity_active() - do reads from the inode need to go through fs-verity?
  * @inode: inode to check
  *
- * This checks whether ->i_verity_info has been set.
+ * This checks whether the inode's verity info has been set.
  *
  * Filesystems call this from ->readahead() to check whether the pages need to
  * be verified or not.  Don't use IS_VERITY() for this purpose; it's subject to
  * a race condition where the file is being read concurrently with
- * FS_IOC_ENABLE_VERITY completing.  (S_VERITY is set before ->i_verity_info.)
+ * FS_IOC_ENABLE_VERITY completing.  (S_VERITY is set before the verity info.)
  *
  * Return: true if reads need to go through fs-verity, otherwise false
  */
@@ -287,7 +311,7 @@ static inline bool fsverity_active(const struct inode *inode)
  * @filp: the struct file being set up
  *
  * When opening a verity file, deny the open if it is for writing.  Otherwise,
- * set up the inode's ->i_verity_info if not already done.
+ * set up the inode's verity info if not already done.
  *
  * When combined with fscrypt, this must be called after fscrypt_file_open().
  * Otherwise, we won't have the key set up to decrypt the verity metadata.