To prepare a hugetlb inode for live update, its index -> folio mappings
must be serialized. Once the mappings are serialized, they cannot change
since it would cause the serialized data to become inconsistent. This
can be done by pinning the folios to avoid migration, and by making sure
no folios can be added to or removed from the inode.

While mechanisms to pin folios already exist, the only way to stop folios
being added or removed are the grow and shrink file seals.  But file seals
come with their own semantics, one of which is that they can't be removed.
This doesn't work with liveupdate since it can be cancelled or error out,
which would need the seals to be removed and the file's normal
functionality to be restored.

Introduce a frozen flag that indicates this status. It is internal to
hugetlbfs and is not directly exposed to userspace. It functions similar
to F_SEAL_GROW | F_SEAL_SHRINK, but additionally disallows hole
punching, and can be removed.

Signed-off-by: Pratyush Yadav <[email protected]>
---
 fs/hugetlbfs/inode.c    | 14 +++++++++++++-
 include/linux/hugetlb.h |  8 ++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index f42548ee9083..9af0372c7aea 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -673,6 +673,11 @@ static long hugetlbfs_punch_hole(struct inode *inode, 
loff_t offset, loff_t len)
 
        inode_lock(inode);
 
+       if (info->frozen) {
+               inode_unlock(inode);
+               return -EPERM;
+       }
+
        /* protected by i_rwsem */
        if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
                inode_unlock(inode);
@@ -743,6 +748,11 @@ static long hugetlbfs_fallocate(struct file *file, int 
mode, loff_t offset,
 
        inode_lock(inode);
 
+       if (info->frozen) {
+               error = -EPERM;
+               goto out;
+       }
+
        /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
        error = inode_newsize_ok(inode, offset + len);
        if (error)
@@ -864,7 +874,8 @@ static int hugetlbfs_setattr(struct mnt_idmap *idmap,
                        return -EINVAL;
                /* protected by i_rwsem */
                if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
-                   (newsize > oldsize && (info->seals & F_SEAL_GROW)))
+                   (newsize > oldsize && (info->seals & F_SEAL_GROW)) ||
+                   ((newsize != oldsize) && info->frozen))
                        return -EPERM;
                hugetlb_vmtruncate(inode, newsize);
        }
@@ -933,6 +944,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block 
*sb,
                simple_inode_init_ts(inode);
                inode->i_mapping->i_private_data = resv_map;
                info->seals = F_SEAL_SEAL;
+               info->frozen = false;
                switch (mode & S_IFMT) {
                default:
                        init_special_inode(inode, mode, dev);
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 8e63e46b8e1f..d70a3015c759 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -511,6 +511,7 @@ static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct 
super_block *sb)
 struct hugetlbfs_inode_info {
        struct inode vfs_inode;
        unsigned int seals;
+       bool frozen;
 };
 
 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
@@ -531,6 +532,13 @@ static inline struct hstate *hstate_inode(struct inode *i)
 {
        return HUGETLBFS_SB(i->i_sb)->hstate;
 }
+
+/* Must be called with inode lock taken exclusive. */
+static inline void hugetlb_i_freeze(struct inode *inode, bool freeze)
+{
+       HUGETLBFS_I(inode)->frozen = freeze;
+}
+
 #else /* !CONFIG_HUGETLBFS */
 
 #define is_file_hugepages(file)                        false
-- 
2.43.0


Reply via email to