[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 04/13] mm/shmem: Restrict MFD_INACCESSIBLE memory against RLIM
From: |
Chao Peng |
Subject: |
[PATCH v5 04/13] mm/shmem: Restrict MFD_INACCESSIBLE memory against RLIMIT_MEMLOCK |
Date: |
Thu, 10 Mar 2022 22:09:02 +0800 |
Since page migration / swapping is not supported yet, MFD_INACCESSIBLE
memory behave like longterm pinned pages and thus should be accounted to
mm->pinned_vm and be restricted by RLIMIT_MEMLOCK.
Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
---
mm/shmem.c | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 7b43e274c9a2..ae46fb96494b 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -915,14 +915,17 @@ static void notify_fallocate(struct inode *inode, pgoff_t
start, pgoff_t end)
static void notify_invalidate_page(struct inode *inode, struct folio *folio,
pgoff_t start, pgoff_t end)
{
-#ifdef CONFIG_MEMFILE_NOTIFIER
struct shmem_inode_info *info = SHMEM_I(inode);
+#ifdef CONFIG_MEMFILE_NOTIFIER
start = max(start, folio->index);
end = min(end, folio->index + folio_nr_pages(folio));
memfile_notifier_invalidate(&info->memfile_notifiers, start, end);
#endif
+
+ if (info->xflags & SHM_F_INACCESSIBLE)
+ atomic64_sub(end - start, ¤t->mm->pinned_vm);
}
/*
@@ -2680,6 +2683,20 @@ static loff_t shmem_file_llseek(struct file *file,
loff_t offset, int whence)
return offset;
}
+static bool memlock_limited(unsigned long npages)
+{
+ unsigned long lock_limit;
+ unsigned long pinned;
+
+ lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+ pinned = atomic64_add_return(npages, ¤t->mm->pinned_vm);
+ if (pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
+ atomic64_sub(npages, ¤t->mm->pinned_vm);
+ return true;
+ }
+ return false;
+}
+
static long shmem_fallocate(struct file *file, int mode, loff_t offset,
loff_t len)
{
@@ -2753,6 +2770,12 @@ static long shmem_fallocate(struct file *file, int mode,
loff_t offset,
goto out;
}
+ if ((info->xflags & SHM_F_INACCESSIBLE) &&
+ memlock_limited(end - start)) {
+ error = -ENOMEM;
+ goto out;
+ }
+
shmem_falloc.waitq = NULL;
shmem_falloc.start = start;
shmem_falloc.next = start;
--
2.17.1
- [PATCH v5 00/13] KVM: mm: fd-based approach for supporting KVM guest private memory, Chao Peng, 2022/03/10
- [PATCH v5 01/13] mm/memfd: Introduce MFD_INACCESSIBLE flag, Chao Peng, 2022/03/10
- [PATCH v5 02/13] mm: Introduce memfile_notifier, Chao Peng, 2022/03/10
- [PATCH v5 03/13] mm/shmem: Support memfile_notifier, Chao Peng, 2022/03/10
- [PATCH v5 04/13] mm/shmem: Restrict MFD_INACCESSIBLE memory against RLIMIT_MEMLOCK,
Chao Peng <=
- [PATCH v5 05/13] KVM: Extend the memslot to support fd-based private memory, Chao Peng, 2022/03/10
- [PATCH v5 06/13] KVM: Use kvm_userspace_memory_region_ext, Chao Peng, 2022/03/10
- [PATCH v5 07/13] KVM: Add KVM_EXIT_MEMORY_ERROR exit, Chao Peng, 2022/03/10
- [PATCH v5 08/13] KVM: Use memfile_pfn_ops to obtain pfn for private pages, Chao Peng, 2022/03/10
- [PATCH v5 10/13] KVM: Register private memslot to memory backing store, Chao Peng, 2022/03/10