Skip to content

mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)

Michal Clapinski linux-doc linux-kselftest linux-mm lkml oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest2 patches 3 repliesIn review

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)Documentation/userspace-api/ioctl/ioctl-number.rst fs/hugetlbfs/inode.c include/linux/memfd.h +2 more+40 -00 replies
2/2selftests: test ioctl(MEMFD_CHECK_IF_ORIGINAL)tools/testing/selftests/memfd/memfd_test.c+32 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)

This change introduces a way to check if an fd points to a memfd's original open fd (the one created by memfd_create).

We encountered an issue with migrating memfds in CRIU (checkpoint restore in userspace - it migrates running processes between machines). Imagine a scenario:
1. Create a memfd. By default it's open with O_RDWR and yet one can exec() to it (unlike with regular files, where one would get ETXTBSY).
2. Reopen that memfd with O_RDWR via /proc/self/fd/<fd>.

Now those 2 fds are indistinguishable from userspace. You can't exec() to either of them (since the reopen incremented inode->i_writecount) and their /proc/self/fdinfo/ are exactly the same. Unfortunately they are not the same. If you close the second one, the first one becomes exec()able again. If you close the first one, the other doesn't become exec()able. Therefore during migration it does matter which is recreated first and which is reopened but there is no way for CRIU to tell which was first.

---
Changes since v1 at [1]:
- Rewrote it from fcntl to ioctl. This was requested by filesystems maintainer.

Links:
[1] https://lore.kernel.org/all/[email protected]/

Michal Clapinski (2):
mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)
selftests: test ioctl(MEMFD_CHECK_IF_ORIGINAL)

 .../userspace-api/ioctl/ioctl-number.rst      |  1 +
 fs/hugetlbfs/inode.c                          |  9 ++++++
 include/linux/memfd.h                         | 12 +++++++
 mm/memfd.c                                    |  9 ++++++
 mm/shmem.c                                    |  9 ++++++
 tools/testing/selftests/memfd/memfd_test.c    | 32 +++++++++++++++++++
 6 files changed, 72 insertions(+)
Signature · 1 line
2.42.0.283.g2d96d420d3-goog