Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1456359 > unrolled thread
| Started by | Mike Rapoport <rppt@linux.vnet.ibm.com> |
|---|---|
| First post | 2016-08-04 13:50 +0200 |
| Last post | 2016-08-04 21:00 +0200 |
| Articles | 5 — 2 participants |
Back to article view | Back to linux.kernel
[PATCH 0/7] userfaultfd: add support for shared memory Mike Rapoport <rppt@linux.vnet.ibm.com> - 2016-08-04 13:50 +0200
[PATCH 3/7] userfaultfd: shmem: introduce vma_is_shmem Mike Rapoport <rppt@linux.vnet.ibm.com> - 2016-08-04 14:30 +0200
[PATCH 4/7] userfaultfd: shmem: use shmem_mcopy_atomic_pte for shared memory Mike Rapoport <rppt@linux.vnet.ibm.com> - 2016-08-04 15:30 +0200
[PATCH 7/7] userfaultfd: shmem: add userfaultfd_shmem test Mike Rapoport <rppt@linux.vnet.ibm.com> - 2016-08-04 16:00 +0200
Re: [PATCH 0/7] userfaultfd: add support for shared memory Andrea Arcangeli <aarcange@redhat.com> - 2016-08-04 21:00 +0200
| From | Mike Rapoport <rppt@linux.vnet.ibm.com> |
|---|---|
| Date | 2016-08-04 13:50 +0200 |
| Subject | [PATCH 0/7] userfaultfd: add support for shared memory |
| Message-ID | <s2mYO-6W6-37@gated-at.bofh.it> |
These patches enable userfaultfd support for shared memory mappings. The VMAs backed with shmem/tmpfs can be registered with userfaultfd which allows management of page faults in these areas by userland. This patch set adds implementation of shmem_mcopy_atomic_pte for proper handling of UFFDIO_COPY command. A callback to handle_userfault is added to shmem page fault handling path. The userfaultfd register/unregister methods are extended to allow shmem VMAs. The UFFDIO_ZEROPAGE and UFFDIO_REGISTER_MODE_WP are not implemented which is reflected by userfaultfd API handshake methods. The patches are based on current Andrea's tree: https://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git Mike Rapoport (7): userfaultfd: introduce vma_can_userfault userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support userfaultfd: shmem: introduce vma_is_shmem userfaultfd: shmem: use shmem_mcopy_atomic_pte for shared memory userfaultfd: shmem: add userfaultfd hook for shared memory faults userfaultfd: shmem: allow registration of shared memory ranges userfaultfd: shmem: add userfaultfd_shmem test fs/userfaultfd.c | 32 ++++--- include/linux/mm.h | 10 +++ include/linux/shmem_fs.h | 11 +++ include/uapi/linux/userfaultfd.h | 2 +- mm/shmem.c | 139 +++++++++++++++++++++++++++++-- mm/userfaultfd.c | 31 ++++--- tools/testing/selftests/vm/Makefile | 3 + tools/testing/selftests/vm/run_vmtests | 11 +++ tools/testing/selftests/vm/userfaultfd.c | 39 ++++++++- 9 files changed, 237 insertions(+), 41 deletions(-) -- 1.9.1
[toc] | [next] | [standalone]
| From | Mike Rapoport <rppt@linux.vnet.ibm.com> |
|---|---|
| Date | 2016-08-04 14:30 +0200 |
| Subject | [PATCH 3/7] userfaultfd: shmem: introduce vma_is_shmem |
| Message-ID | <s2pWF-yi-5@gated-at.bofh.it> |
| In reply to | #1456359 |
Currently userfault relies on vma_is_anonymous and vma_is_hugetlb to ensure
compatibility of a VMA with userfault. Introduction of vma_is_shmem allows
detection if tmpfs backed VMAs, so that they may be used with userfaultfd.
Current implementation presumes usage of vma_is_shmem only by slow path
routines in userfaultfd, therefore the vma_is_shmem is not made inline to
leave the few remaining free bits in vm_flags.
Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
include/linux/mm.h | 10 ++++++++++
mm/shmem.c | 5 +++++
2 files changed, 15 insertions(+)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 1dedeb8..7a20398 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1343,6 +1343,16 @@ static inline bool vma_is_anonymous(struct vm_area_struct *vma)
return !vma->vm_ops;
}
+#ifdef CONFIG_SHMEM
+/*
+ * The vma_is_shmem is not inline because it is used only by slow
+ * paths in userfault.
+ */
+bool vma_is_shmem(struct vm_area_struct *vma);
+#else
+static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
+#endif
+
static inline int stack_guard_page_start(struct vm_area_struct *vma,
unsigned long addr)
{
diff --git a/mm/shmem.c b/mm/shmem.c
index fcf560c..881b7a0 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -194,6 +194,11 @@ static const struct inode_operations shmem_dir_inode_operations;
static const struct inode_operations shmem_special_inode_operations;
static const struct vm_operations_struct shmem_vm_ops;
+bool vma_is_shmem(struct vm_area_struct *vma)
+{
+ return vma->vm_ops == &shmem_vm_ops;
+}
+
static LIST_HEAD(shmem_swaplist);
static DEFINE_MUTEX(shmem_swaplist_mutex);
--
1.9.1
[toc] | [prev] | [next] | [standalone]
| From | Mike Rapoport <rppt@linux.vnet.ibm.com> |
|---|---|
| Date | 2016-08-04 15:30 +0200 |
| Subject | [PATCH 4/7] userfaultfd: shmem: use shmem_mcopy_atomic_pte for shared memory |
| Message-ID | <s2qSK-1gA-29@gated-at.bofh.it> |
| In reply to | #1456359 |
The shmem_mcopy_atomic_pte implements low lever part of UFFDIO_COPY
operation for shared memory VMAs. It's based on mcopy_atomic_pte with
adjustments necessary for shared memory pages.
Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
mm/userfaultfd.c | 31 ++++++++++++++++++-------------
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index ae4a976..d9259ba 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -16,6 +16,7 @@
#include <linux/mmu_notifier.h>
#include <linux/hugetlb.h>
#include <linux/pagemap.h>
+#include <linux/shmem_fs.h>
#include <asm/tlbflush.h>
#include "internal.h"
@@ -348,7 +349,9 @@ retry:
*/
err = -EINVAL;
dst_vma = find_vma(dst_mm, dst_start);
- if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
+ if (!dst_vma)
+ goto out_unlock;
+ if (!vma_is_shmem(dst_vma) && dst_vma->vm_flags & VM_SHARED)
goto out_unlock;
if (dst_start < dst_vma->vm_start ||
dst_start + len > dst_vma->vm_end)
@@ -373,11 +376,7 @@ retry:
if (!dst_vma->vm_userfaultfd_ctx.ctx)
goto out_unlock;
- /*
- * FIXME: only allow copying on anonymous vmas, tmpfs should
- * be added.
- */
- if (!vma_is_anonymous(dst_vma))
+ if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
goto out_unlock;
/*
@@ -386,7 +385,7 @@ retry:
* dst_vma.
*/
err = -ENOMEM;
- if (unlikely(anon_vma_prepare(dst_vma)))
+ if (vma_is_anonymous(dst_vma) && unlikely(anon_vma_prepare(dst_vma)))
goto out_unlock;
while (src_addr < src_start + len) {
@@ -423,12 +422,18 @@ retry:
BUG_ON(pmd_none(*dst_pmd));
BUG_ON(pmd_trans_huge(*dst_pmd));
- if (!zeropage)
- err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
- dst_addr, src_addr, &page);
- else
- err = mfill_zeropage_pte(dst_mm, dst_pmd, dst_vma,
- dst_addr);
+ if (vma_is_anonymous(dst_vma)) {
+ if (!zeropage)
+ err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
+ dst_addr, src_addr,
+ &page);
+ else
+ err = mfill_zeropage_pte(dst_mm, dst_pmd,
+ dst_vma, dst_addr);
+ } else {
+ err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
+ dst_addr, src_addr, &page);
+ }
cond_resched();
--
1.9.1
[toc] | [prev] | [next] | [standalone]
| From | Mike Rapoport <rppt@linux.vnet.ibm.com> |
|---|---|
| Date | 2016-08-04 16:00 +0200 |
| Subject | [PATCH 7/7] userfaultfd: shmem: add userfaultfd_shmem test |
| Message-ID | <s2rlL-1t2-5@gated-at.bofh.it> |
| In reply to | #1456359 |
The test verifies that anonymous shared mapping can be used with userfault
using the existing testing method.
The shared memory area is allocated using mmap(..., MAP_SHARED |
MAP_ANONYMOUS, ...) and released using madvise(MADV_REMOVE)
Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
tools/testing/selftests/vm/Makefile | 3 +++
tools/testing/selftests/vm/run_vmtests | 11 ++++++++++
tools/testing/selftests/vm/userfaultfd.c | 37 ++++++++++++++++++++++++++++++--
3 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index aaa4225..12ff211 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -11,6 +11,7 @@ BINARIES += thuge-gen
BINARIES += transhuge-stress
BINARIES += userfaultfd
BINARIES += userfaultfd_hugetlb
+BINARIES += userfaultfd_shmem
all: $(BINARIES)
%: %.c
@@ -19,6 +20,8 @@ userfaultfd: userfaultfd.c ../../../../usr/include/linux/kernel.h
$(CC) $(CFLAGS) -O2 -o $@ $< -lpthread
userfaultfd_hugetlb: userfaultfd.c ../../../../usr/include/linux/kernel.h
$(CC) $(CFLAGS) -DHUGETLB_TEST -O2 -o $@ $< -lpthread
+userfaultfd_shmem: userfaultfd.c ../../../../usr/include/linux/kernel.h
+ $(CC) $(CFLAGS) -DSHMEM_TEST -O2 -o $@ $< -lpthread
../../../../usr/include/linux/kernel.h:
make -C ../../../.. headers_install
diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests
index 14d697e..c92f6cf 100755
--- a/tools/testing/selftests/vm/run_vmtests
+++ b/tools/testing/selftests/vm/run_vmtests
@@ -116,6 +116,17 @@ else
fi
rm -f $mnt/ufd_test_file
+echo "----------------------------"
+echo "running userfaultfd_shmem"
+echo "----------------------------"
+./userfaultfd_shmem 128 32
+if [ $? -ne 0 ]; then
+ echo "[FAIL]"
+ exitcode=1
+else
+ echo "[PASS]"
+fi
+
#cleanup
umount $mnt
rm -rf $mnt
diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c
index d753a91..a5e5808 100644
--- a/tools/testing/selftests/vm/userfaultfd.c
+++ b/tools/testing/selftests/vm/userfaultfd.c
@@ -101,8 +101,9 @@ pthread_attr_t attr;
~(unsigned long)(sizeof(unsigned long long) \
- 1)))
-#ifndef HUGETLB_TEST
+#if !defined(HUGETLB_TEST) && !defined(SHMEM_TEST)
+/* Anonymous memory */
#define EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
(1 << _UFFDIO_COPY) | \
(1 << _UFFDIO_ZEROPAGE))
@@ -127,10 +128,13 @@ static void allocate_area(void **alloc_area)
}
}
-#else /* HUGETLB_TEST */
+#else /* HUGETLB_TEST or SHMEM_TEST */
#define EXPECTED_IOCTLS UFFD_API_RANGE_IOCTLS_BASIC
+#ifdef HUGETLB_TEST
+
+/* HugeTLB memory */
static int release_pages(char *rel_area)
{
int ret = 0;
@@ -162,8 +166,37 @@ static void allocate_area(void **alloc_area)
huge_fd_off0 = *alloc_area;
}
+#elif defined(SHMEM_TEST)
+
+/* Shared memory */
+static int release_pages(char *rel_area)
+{
+ int ret = 0;
+
+ if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
+ perror("madvise");
+ ret = 1;
+ }
+
+ return ret;
+}
+
+static void allocate_area(void **alloc_area)
+{
+ *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+ if (*alloc_area == MAP_FAILED) {
+ fprintf(stderr, "shared memory mmap failed\n");
+ *alloc_area = NULL;
+ }
+}
+
+#else /* SHMEM_TEST */
+#error "Undefined test type"
#endif /* HUGETLB_TEST */
+#endif /* !defined(HUGETLB_TEST) && !defined(SHMEM_TEST) */
+
static int my_bcmp(char *str1, char *str2, size_t n)
{
unsigned long i;
--
1.9.1
[toc] | [prev] | [next] | [standalone]
| From | Andrea Arcangeli <aarcange@redhat.com> |
|---|---|
| Date | 2016-08-04 21:00 +0200 |
| Message-ID | <s2w25-4Tj-5@gated-at.bofh.it> |
| In reply to | #1456359 |
Hi Mike, On Thu, Aug 04, 2016 at 11:14:11AM +0300, Mike Rapoport wrote: > These patches enable userfaultfd support for shared memory mappings. The > VMAs backed with shmem/tmpfs can be registered with userfaultfd which > allows management of page faults in these areas by userland. > > This patch set adds implementation of shmem_mcopy_atomic_pte for proper > handling of UFFDIO_COPY command. A callback to handle_userfault is added > to shmem page fault handling path. The userfaultfd register/unregister > methods are extended to allow shmem VMAs. > > The UFFDIO_ZEROPAGE and UFFDIO_REGISTER_MODE_WP are not implemented which > is reflected by userfaultfd API handshake methods. This looks great. I'm getting rejects during rebase but not because of your changes, I think I'll fold some patches that I originally fixed up incrementally, in order to reduce the reject churn. Thanks, Andrea
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web