Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: shmem: allow freezing inode mapping

To prepare a shmem 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 SHMEM_F_MAPPING_FROZEN to indicate this instead. It is internal
to shmem 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.

Link: https://lkml.kernel.org/r/20251125165850.3389713-12-pasha.tatashin@soleen.com
Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: David Matlack <dmatlack@google.com>
Cc: Aleksander Lobakin <aleksander.lobakin@intel.com>
Cc: Alexander Graf <graf@amazon.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andriy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: anish kumar <yesanishhere@gmail.com>
Cc: Anna Schumaker <anna.schumaker@oracle.com>
Cc: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Borislav Betkov <bp@alien8.de>
Cc: Chanwoo Choi <cw00.choi@samsung.com>
Cc: Chen Ridong <chenridong@huawei.com>
Cc: Chris Li <chrisl@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Daniel Wagner <wagi@kernel.org>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Jeffery <djeffery@redhat.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Guixin Liu <kanie@linux.alibaba.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ira Weiny <ira.weiny@intel.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Joanthan Cameron <Jonathan.Cameron@huawei.com>
Cc: Joel Granados <joel.granados@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lennart Poettering <lennart@poettering.net>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Leon Romanovsky <leonro@nvidia.com>
Cc: Lukas Wunner <lukas@wunner.de>
Cc: Marc Rutland <mark.rutland@arm.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Matthew Maurer <mmaurer@google.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Myugnjoo Ham <myungjoo.ham@samsung.com>
Cc: Parav Pandit <parav@nvidia.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Saeed Mahameed <saeedm@nvidia.com>
Cc: Samiullah Khawaja <skhawaja@google.com>
Cc: Song Liu <song@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Stuart Hayes <stuart.w.hayes@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Thomas Weißschuh <linux@weissschuh.net>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: William Tu <witu@nvidia.com>
Cc: Yoann Congal <yoann.congal@smile.fr>
Cc: Zhu Yanjun <yanjun.zhu@linux.dev>
Cc: Zijun Hu <quic_zijuhu@quicinc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Pratyush Yadav and committed by
Andrew Morton
e165e2a2 6ff1610c

+28
+17
include/linux/shmem_fs.h
··· 24 24 #define SHMEM_F_NORESERVE BIT(0) 25 25 /* Disallow swapping. */ 26 26 #define SHMEM_F_LOCKED BIT(1) 27 + /* 28 + * Disallow growing, shrinking, or hole punching in the inode. Combined with 29 + * folio pinning, makes sure the inode's mapping stays fixed. 30 + * 31 + * In some ways similar to F_SEAL_GROW | F_SEAL_SHRINK, but can be removed and 32 + * isn't directly visible to userspace. 33 + */ 34 + #define SHMEM_F_MAPPING_FROZEN BIT(2) 27 35 28 36 struct shmem_inode_info { 29 37 spinlock_t lock; ··· 192 184 if (!file || !file->f_mapping) 193 185 return false; 194 186 return shmem_mapping(file->f_mapping); 187 + } 188 + 189 + /* Must be called with inode lock taken exclusive. */ 190 + static inline void shmem_freeze(struct inode *inode, bool freeze) 191 + { 192 + if (freeze) 193 + SHMEM_I(inode)->flags |= SHMEM_F_MAPPING_FROZEN; 194 + else 195 + SHMEM_I(inode)->flags &= ~SHMEM_F_MAPPING_FROZEN; 195 196 } 196 197 197 198 /*
+11
mm/shmem.c
··· 1297 1297 return -EPERM; 1298 1298 1299 1299 if (newsize != oldsize) { 1300 + if (info->flags & SHMEM_F_MAPPING_FROZEN) 1301 + return -EPERM; 1300 1302 error = shmem_reacct_size(SHMEM_I(inode)->flags, 1301 1303 oldsize, newsize); 1302 1304 if (error) ··· 3291 3289 return -EPERM; 3292 3290 } 3293 3291 3292 + if (unlikely((info->flags & SHMEM_F_MAPPING_FROZEN) && 3293 + pos + len > inode->i_size)) 3294 + return -EPERM; 3295 + 3294 3296 ret = shmem_get_folio(inode, index, pos + len, &folio, SGP_WRITE); 3295 3297 if (ret) 3296 3298 return ret; ··· 3667 3661 return -EOPNOTSUPP; 3668 3662 3669 3663 inode_lock(inode); 3664 + 3665 + if (info->flags & SHMEM_F_MAPPING_FROZEN) { 3666 + error = -EPERM; 3667 + goto out; 3668 + } 3670 3669 3671 3670 if (mode & FALLOC_FL_PUNCH_HOLE) { 3672 3671 struct address_space *mapping = file->f_mapping;