Skip to content

Commit 6ff1610

Browse files
prati0100akpm00
authored andcommitted
mm: shmem: use SHMEM_F_* flags instead of VM_* flags
shmem_inode_info::flags can have the VM flags VM_NORESERVE and VM_LOCKED. These are used to suppress pre-accounting or to lock the pages in the inode respectively. Using the VM flags directly makes it difficult to add shmem-specific flags that are unrelated to VM behavior since one would need to find a VM flag not used by shmem and re-purpose it. Introduce SHMEM_F_NORESERVE and SHMEM_F_LOCKED which represent the same information, but their bits are independent of the VM flags. Callers can still pass VM_NORESERVE to shmem_get_inode(), but it gets transformed to the shmem-specific flag internally. No functional changes intended. Link: https://lkml.kernel.org/r/20251125165850.3389713-11-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>
1 parent 7a5afa7 commit 6ff1610

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

include/linux/shmem_fs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/xattr.h>
1111
#include <linux/fs_parser.h>
1212
#include <linux/userfaultfd_k.h>
13+
#include <linux/bits.h>
1314

1415
struct swap_iocb;
1516

@@ -19,6 +20,11 @@ struct swap_iocb;
1920
#define SHMEM_MAXQUOTAS 2
2021
#endif
2122

23+
/* Suppress pre-accounting of the entire object size. */
24+
#define SHMEM_F_NORESERVE BIT(0)
25+
/* Disallow swapping. */
26+
#define SHMEM_F_LOCKED BIT(1)
27+
2228
struct shmem_inode_info {
2329
spinlock_t lock;
2430
unsigned int seals; /* shmem seals */

mm/shmem.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
175175
*/
176176
static inline int shmem_acct_size(unsigned long flags, loff_t size)
177177
{
178-
return (flags & VM_NORESERVE) ?
178+
return (flags & SHMEM_F_NORESERVE) ?
179179
0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
180180
}
181181

182182
static inline void shmem_unacct_size(unsigned long flags, loff_t size)
183183
{
184-
if (!(flags & VM_NORESERVE))
184+
if (!(flags & SHMEM_F_NORESERVE))
185185
vm_unacct_memory(VM_ACCT(size));
186186
}
187187

188188
static inline int shmem_reacct_size(unsigned long flags,
189189
loff_t oldsize, loff_t newsize)
190190
{
191-
if (!(flags & VM_NORESERVE)) {
191+
if (!(flags & SHMEM_F_NORESERVE)) {
192192
if (VM_ACCT(newsize) > VM_ACCT(oldsize))
193193
return security_vm_enough_memory_mm(current->mm,
194194
VM_ACCT(newsize) - VM_ACCT(oldsize));
@@ -206,7 +206,7 @@ static inline int shmem_reacct_size(unsigned long flags,
206206
*/
207207
static inline int shmem_acct_blocks(unsigned long flags, long pages)
208208
{
209-
if (!(flags & VM_NORESERVE))
209+
if (!(flags & SHMEM_F_NORESERVE))
210210
return 0;
211211

212212
return security_vm_enough_memory_mm(current->mm,
@@ -215,7 +215,7 @@ static inline int shmem_acct_blocks(unsigned long flags, long pages)
215215

216216
static inline void shmem_unacct_blocks(unsigned long flags, long pages)
217217
{
218-
if (flags & VM_NORESERVE)
218+
if (flags & SHMEM_F_NORESERVE)
219219
vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
220220
}
221221

@@ -1551,7 +1551,7 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
15511551
int nr_pages;
15521552
bool split = false;
15531553

1554-
if ((info->flags & VM_LOCKED) || sbinfo->noswap)
1554+
if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap)
15551555
goto redirty;
15561556

15571557
if (!total_swap_pages)
@@ -2910,15 +2910,15 @@ int shmem_lock(struct file *file, int lock, struct ucounts *ucounts)
29102910
* ipc_lock_object() when called from shmctl_do_lock(),
29112911
* no serialization needed when called from shm_destroy().
29122912
*/
2913-
if (lock && !(info->flags & VM_LOCKED)) {
2913+
if (lock && !(info->flags & SHMEM_F_LOCKED)) {
29142914
if (!user_shm_lock(inode->i_size, ucounts))
29152915
goto out_nomem;
2916-
info->flags |= VM_LOCKED;
2916+
info->flags |= SHMEM_F_LOCKED;
29172917
mapping_set_unevictable(file->f_mapping);
29182918
}
2919-
if (!lock && (info->flags & VM_LOCKED) && ucounts) {
2919+
if (!lock && (info->flags & SHMEM_F_LOCKED) && ucounts) {
29202920
user_shm_unlock(inode->i_size, ucounts);
2921-
info->flags &= ~VM_LOCKED;
2921+
info->flags &= ~SHMEM_F_LOCKED;
29222922
mapping_clear_unevictable(file->f_mapping);
29232923
}
29242924
retval = 0;
@@ -3062,7 +3062,7 @@ static struct inode *__shmem_get_inode(struct mnt_idmap *idmap,
30623062
spin_lock_init(&info->lock);
30633063
atomic_set(&info->stop_eviction, 0);
30643064
info->seals = F_SEAL_SEAL;
3065-
info->flags = flags & VM_NORESERVE;
3065+
info->flags = (flags & VM_NORESERVE) ? SHMEM_F_NORESERVE : 0;
30663066
info->i_crtime = inode_get_mtime(inode);
30673067
info->fsflags = (dir == NULL) ? 0 :
30683068
SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED;
@@ -5804,8 +5804,10 @@ static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap,
58045804
/* common code */
58055805

58065806
static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name,
5807-
loff_t size, unsigned long flags, unsigned int i_flags)
5807+
loff_t size, unsigned long vm_flags,
5808+
unsigned int i_flags)
58085809
{
5810+
unsigned long flags = (vm_flags & VM_NORESERVE) ? SHMEM_F_NORESERVE : 0;
58095811
struct inode *inode;
58105812
struct file *res;
58115813

@@ -5822,7 +5824,7 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name,
58225824
return ERR_PTR(-ENOMEM);
58235825

58245826
inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL,
5825-
S_IFREG | S_IRWXUGO, 0, flags);
5827+
S_IFREG | S_IRWXUGO, 0, vm_flags);
58265828
if (IS_ERR(inode)) {
58275829
shmem_unacct_size(flags, size);
58285830
return ERR_CAST(inode);

0 commit comments

Comments
 (0)