util/userfaultfd: Support /dev/userfaultfd
v2 cover util/userfaultfd: Support /dev/userfaultfd
v2:
- Added R-bs for Phil
- Move open_mode into uffd_detect_open_mode() [Phil]
- Document uffd_open() in the header file [Phil]
- [Discussed with Daniel/Michal, decided to leave fd support for later]
The new /dev/userfaultfd handle is superior to the system call with a better permission control and also works for a restricted seccomp environment.
The new device was only introduced in v6.1 so we need a header update.
Please have a look, thanks.
Peter Xu (3):
linux-headers: Update to v6.1
util/userfaultfd: Add uffd_open()
util/userfaultfd: Support /dev/userfaultfd
include/qemu/userfaultfd.h | 8 + include/standard-headers/drm/drm_fourcc.h | 34 ++++- include/standard-headers/linux/ethtool.h | 63 +++++++- include/standard-headers/linux/fuse.h | 6 +- .../linux/input-event-codes.h | 1 + include/standard-headers/linux/virtio_blk.h | 19 +++ linux-headers/asm-generic/hugetlb_encode.h | 26 ++-- linux-headers/asm-generic/mman-common.h | 2 + linux-headers/asm-mips/mman.h | 2 + linux-headers/asm-riscv/kvm.h | 4 + linux-headers/linux/kvm.h | 1 + linux-headers/linux/psci.h | 14 ++ linux-headers/linux/userfaultfd.h | 4 + linux-headers/linux/vfio.h | 142 ++++++++++++++++++ migration/postcopy-ram.c | 11 +- tests/qtest/migration-test.c | 3 +- util/trace-events | 1 + util/userfaultfd.c | 50 +++++- 18 files changed, 362 insertions(+), 29 deletions(-)
Signature · 1 line
2.37.3
PATCH v2 1/3 linux-headers: Update to v6.1
--- include/standard-headers/drm/drm_fourcc.h | 34 ++++- include/standard-headers/linux/ethtool.h | 63 +++++++- include/standard-headers/linux/fuse.h | 6 +- .../linux/input-event-codes.h | 1 + include/standard-headers/linux/virtio_blk.h | 19 +++ linux-headers/asm-generic/hugetlb_encode.h | 26 ++-- linux-headers/asm-generic/mman-common.h | 2 + linux-headers/asm-mips/mman.h | 2 + linux-headers/asm-riscv/kvm.h | 4 + linux-headers/linux/kvm.h | 1 + linux-headers/linux/psci.h | 14 ++ linux-headers/linux/userfaultfd.h | 4 + linux-headers/linux/vfio.h | 142 ++++++++++++++++++ 13 files changed, 298 insertions(+), 20 deletions(-)
include/standard-headers/drm/drm_fourcc.h +30 -4
@@ -98,18 +98,42 @@ extern "C" {#define DRM_FORMAT_INVALID 0/* color index */ +#define DRM_FORMAT_C1 fourcc_code('C', '1', ' ', ' ')/* [7:0] C0:C1:C2:C3:C4:C5:C6:C7 1:1:1:1:1:1:1:1 eight pixels/byte */ +#define DRM_FORMAT_C2 fourcc_code('C', '2', ' ', ' ')/* [7:0] C0:C1:C2:C3 2:2:2:2 four pixels/byte */ +#define DRM_FORMAT_C4 fourcc_code('C', '4', ' ', ' ')/* [7:0] C0:C1 4:4 two pixels/byte */ #define DRM_FORMAT_C8 fourcc_code('C', '8', ' ', ' ')/* [7:0] C */ -/* 8 bpp Red */ +/* 1 bpp Darkness (inverse relationship between channel value and brightness) */ +#define DRM_FORMAT_D1 fourcc_code('D', '1', ' ', ' ')/* [7:0] D0:D1:D2:D3:D4:D5:D6:D7 1:1:1:1:1:1:1:1 eight pixels/byte */ + +/* 2 bpp Darkness (inverse relationship between channel value and brightness) */ +#define DRM_FORMAT_D2 fourcc_code('D', '2', ' ', ' ')/* [7:0] D0:D1:D2:D3 2:2:2:2 four pixels/byte */ + +/* 4 bpp Darkness (inverse relationship between channel value and brightness) */ +#define DRM_FORMAT_D4 fourcc_code('D', '4', ' ', ' ')/* [7:0] D0:D1 4:4 two pixels/byte */ + +/* 8 bpp Darkness (inverse relationship between channel value and brightness) */ +#define DRM_FORMAT_D8 fourcc_code('D', '8', ' ', ' ')/* [7:0] D */ + +/* 1 bpp Red (direct relationship between channel value and brightness) */ +#define DRM_FORMAT_R1 fourcc_code('R', '1', ' ', ' ')/* [7:0] R0:R1:R2:R3:R4:R5:R6:R7 1:1:1:1:1:1:1:1 eight pixels/byte */ + +/* 2 bpp Red (direct relationship between channel value and brightness) */ +#define DRM_FORMAT_R2 fourcc_code('R', '2', ' ', ' ')/* [7:0] R0:R1:R2:R3 2:2:2:2 four pixels/byte */ + +/* 4 bpp Red (direct relationship between channel value and brightness) */ +#define DRM_FORMAT_R4 fourcc_code('R', '4', ' ', ' ')/* [7:0] R0:R1 4:4 two pixels/byte */ + +/* 8 bpp Red (direct relationship between channel value and brightness) */ #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ')/* [7:0] R */ -/* 10 bpp Red */ +/* 10 bpp Red (direct relationship between channel value and brightness) */ #define DRM_FORMAT_R10 fourcc_code('R', '1', '0', ' ')/* [15:0] x:R 6:10 little endian */ -/* 12 bpp Red */ +/* 12 bpp Red (direct relationship between channel value and brightness) */ #define DRM_FORMAT_R12 fourcc_code('R', '1', '2', ' ')/* [15:0] x:R 4:12 little endian */ -/* 16 bpp Red */ +/* 16 bpp Red (direct relationship between channel value and brightness) */ #define DRM_FORMAT_R16 fourcc_code('R', '1', '6', ' ')/* [15:0] R little endian */ /* 16 bpp RG */
@@ -204,7 +228,9 @@ extern "C" {#define DRM_FORMAT_VYUY fourcc_code('V', 'Y', 'U', 'Y')/* [31:0] Y1:Cb0:Y0:Cr0 8:8:8:8 little endian */ #define DRM_FORMAT_AYUV fourcc_code('A', 'Y', 'U', 'V')/* [31:0] A:Y:Cb:Cr 8:8:8:8 little endian */ +#define DRM_FORMAT_AVUY8888 fourcc_code('A', 'V', 'U', 'Y')/* [31:0] A:Cr:Cb:Y 8:8:8:8 little endian */ #define DRM_FORMAT_XYUV8888 fourcc_code('X', 'Y', 'U', 'V')/* [31:0] X:Y:Cb:Cr 8:8:8:8 little endian */ +#define DRM_FORMAT_XVUY8888 fourcc_code('X', 'V', 'U', 'Y')/* [31:0] X:Cr:Cb:Y 8:8:8:8 little endian */ #define DRM_FORMAT_VUY888 fourcc_code('V', 'U', '2', '4')/* [23:0] Cr:Cb:Y 8:8:8 little endian */ #define DRM_FORMAT_VUY101010 fourcc_code('V', 'U', '3', '0')/* Y followed by U then V, 10:10:10. Non-linear modifier only */
include/standard-headers/linux/ethtool.h +61 -2
88 lines not on this page · raw message
include/standard-headers/linux/fuse.h +5 -1
@@ -194,6 +194,9 @@* - add FUSE_SECURITY_CTX init flag * - add security context to create, mkdir, symlink, and mknod requests * - add FUSE_HAS_INODE_DAX, FUSE_ATTR_DAX +* +* 7.37 +* - add FUSE_TMPFILE */ #ifndef _LINUX_FUSE_H
@@ -225,7 +228,7 @@#define FUSE_KERNEL_VERSION 7/** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 36 +#define FUSE_KERNEL_MINOR_VERSION 37/** The node ID of the root inode */ #define FUSE_ROOT_ID 1
@@ -533,6 +536,7 @@ enum fuse_opcode {FUSE_SETUPMAPPING =48 ,FUSE_REMOVEMAPPING =49 ,FUSE_SYNCFS =50 , +FUSE_TMPFILE =51 ,/* CUSE specific operations */ CUSE_INIT = 4096,
include/standard-headers/linux/input-event-codes.h +1 -0
@@ -862,6 +862,7 @@#define ABS_TOOL_WIDTH 0x1c#define ABS_VOLUME 0x20 +#define ABS_PROFILE 0x21#define ABS_MISC 0x28
include/standard-headers/linux/virtio_blk.h +19 -0
@@ -40,6 +40,7 @@#define VIRTIO_BLK_F_MQ 12/* support more than one vq */ #define VIRTIO_BLK_F_DISCARD 13/* DISCARD is supported */ #define VIRTIO_BLK_F_WRITE_ZEROES 14/* WRITE ZEROES is supported */ +#define VIRTIO_BLK_F_SECURE_ERASE 16/* Secure Erase is supported */ /* Legacy feature bits */ #ifndef VIRTIO_BLK_NO_LEGACY
@@ -119,6 +120,21 @@ struct virtio_blk_config {uint8_t write_zeroes_may_unmap;uint8_t unused1[3 ]; + +/* the next 3 entries are guarded by VIRTIO_BLK_F_SECURE_ERASE */ +/* +* The maximum secure erase sectors (in 512-byte sectors) for +* one segment. +*/ +__virtio32 max_secure_erase_sectors; +/* +* The maximum number of secure erase segments in a +* secure erase command. +*/ +__virtio32 max_secure_erase_seg; +/* Secure erase commands must be aligned to this number of sectors. */ +__virtio32 secure_erase_sector_alignment; + }QEMU_PACKED ;/*
@@ -153,6 +169,9 @@ struct virtio_blk_config {/* Write zeroes command */ #define VIRTIO_BLK_T_WRITE_ZEROES 13 +/* Secure erase command */ +#define VIRTIO_BLK_T_SECURE_ERASE 14 +#ifndef VIRTIO_BLK_NO_LEGACY /* Barrier before this op. */ #define VIRTIO_BLK_T_BARRIER 0x80000000
linux-headers/asm-generic/hugetlb_encode.h +13 -13
@@ -20,18 +20,18 @@#define HUGETLB_FLAG_ENCODE_SHIFT 26#define HUGETLB_FLAG_ENCODE_MASK 0x3f -#define HUGETLB_FLAG_ENCODE_16KB (14 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_64KB (16 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_512KB (19 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_1MB (20 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_2MB (21 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_8MB (23 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_16MB (24 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_32MB (25 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_256MB (28 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_512MB (29 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_1GB (30 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_2GB (31 << HUGETLB_FLAG_ENCODE_SHIFT) -#define HUGETLB_FLAG_ENCODE_16GB (34 << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_16KB (14U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_64KB (16U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_512KB (19U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_1MB (20U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_2MB (21U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_8MB (23U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_16MB (24U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_32MB (25U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_256MB (28U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_512MB (29U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_1GB (30U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_2GB (31U << HUGETLB_FLAG_ENCODE_SHIFT) +#define HUGETLB_FLAG_ENCODE_16GB (34U << HUGETLB_FLAG_ENCODE_SHIFT)#endif /* _ASM_GENERIC_HUGETLB_ENCODE_H_ */
linux-headers/asm-generic/mman-common.h +2 -0
@@ -77,6 +77,8 @@#define MADV_DONTNEED_LOCKED 24/* like DONTNEED, but drop locked pages too */ +#define MADV_COLLAPSE 25/* Synchronous hugepage collapse */ +/* compatibility flags */ #define MAP_FILE 0
linux-headers/asm-mips/mman.h +2 -0
@@ -103,6 +103,8 @@#define MADV_DONTNEED_LOCKED 24/* like DONTNEED, but drop locked pages too */ +#define MADV_COLLAPSE 25/* Synchronous hugepage collapse */ +/* compatibility flags */ #define MAP_FILE 0
linux-headers/asm-riscv/kvm.h +4 -0
@@ -48,6 +48,7 @@ struct kvm_sregs {/* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ struct kvm_riscv_config {unsigned long isa; +unsigned long zicbom_block_size; };/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
@@ -98,6 +99,9 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_M ,KVM_RISCV_ISA_EXT_SVPBMT ,KVM_RISCV_ISA_EXT_SSTC , +KVM_RISCV_ISA_EXT_SVINVAL , +KVM_RISCV_ISA_EXT_ZIHINTPAUSE , +KVM_RISCV_ISA_EXT_ZICBOM , KVM_RISCV_ISA_EXT_MAX, };
linux-headers/linux/kvm.h +1 -0
@@ -1175,6 +1175,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223#ifdef KVM_CAP_IRQ_ROUTING
linux-headers/linux/psci.h +14 -0
@@ -48,12 +48,26 @@#define PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU PSCI_0_2_FN64(7)#define PSCI_1_0_FN_PSCI_FEATURES PSCI_0_2_FN(10) +#define PSCI_1_0_FN_CPU_FREEZE PSCI_0_2_FN(11) +#define PSCI_1_0_FN_CPU_DEFAULT_SUSPEND PSCI_0_2_FN(12) +#define PSCI_1_0_FN_NODE_HW_STATE PSCI_0_2_FN(13)#define PSCI_1_0_FN_SYSTEM_SUSPEND PSCI_0_2_FN(14)#define PSCI_1_0_FN_SET_SUSPEND_MODE PSCI_0_2_FN(15) +#define PSCI_1_0_FN_STAT_RESIDENCY PSCI_0_2_FN(16) +#define PSCI_1_0_FN_STAT_COUNT PSCI_0_2_FN(17) +#define PSCI_1_1_FN_SYSTEM_RESET2 PSCI_0_2_FN(18) +#define PSCI_1_1_FN_MEM_PROTECT PSCI_0_2_FN(19) +#define PSCI_1_1_FN_MEM_PROTECT_CHECK_RANGE PSCI_0_2_FN(19) +#define PSCI_1_0_FN64_CPU_DEFAULT_SUSPEND PSCI_0_2_FN64(12) +#define PSCI_1_0_FN64_NODE_HW_STATE PSCI_0_2_FN64(13)#define PSCI_1_0_FN64_SYSTEM_SUSPEND PSCI_0_2_FN64(14) +#define PSCI_1_0_FN64_STAT_RESIDENCY PSCI_0_2_FN64(16) +#define PSCI_1_0_FN64_STAT_COUNT PSCI_0_2_FN64(17) +#define PSCI_1_1_FN64_SYSTEM_RESET2 PSCI_0_2_FN64(18) +#define PSCI_1_1_FN64_MEM_PROTECT_CHECK_RANGE PSCI_0_2_FN64(19)/* PSCI v0.2 power state encoding for CPU_SUSPEND function */ #define PSCI_0_2_POWER_STATE_ID_MASK 0xffff
linux-headers/linux/userfaultfd.h +4 -0
@@ -12,6 +12,10 @@#include <linux/types.h> +/* ioctls for /dev/userfaultfd */ +#define USERFAULTFD_IOC 0xAA +#define USERFAULTFD_IOC_NEW _IO(USERFAULTFD_IOC, 0x00) +/* * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR. In
linux-headers/linux/vfio.h +142 -0
148 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v2 1/3] linux-headers: Update to v6.1
Quoted 1 line · Peter Xu
Signed-off-by Peter Xu <[email protected]>
How does this change gets into the tree?
I know that it is "automagically" generated, but who decides when that goes into the tree?
As we need that for the following patch?
Later, Juan.
Re: [PATCH v2 1/3] linux-headers: Update to v6.1
Quoted 9 lines · Juan Quintela
Quoted 1 line · Peter Xu
Signed-off-by Peter Xu <[email protected]>How does this change gets into the tree?
I know that it is "automagically" generated, but who decides when that goes into the tree?
As we need that for the following patch?
Copy PeterM. Peter, could you share how we used to do linux header updates?
Thanks,
Signature · 1 line
Peter Xu
PATCH v2 2/3 util/userfaultfd: Add uffd_open()
In reply to Peter XuAdd a helper to create the uffd handle.
--- include/qemu/userfaultfd.h | 8 ++++++++ migration/postcopy-ram.c | 11 +++++------ tests/qtest/migration-test.c | 3 ++- util/userfaultfd.c | 13 +++++++++++-- 4 files changed, 26 insertions(+), 9 deletions(-)
include/qemu/userfaultfd.h +8 -0
@@ -17,6 +17,14 @@#include "exec/hwaddr.h" #include <linux/userfaultfd.h> +/** +* uffd_open(): Open an userfaultfd handle for current context. +* +* @flags: The flags we want to pass in when creating the handle. +* +* Returns: the uffd handle if >=0, or <0 if error happens. +*/ +int uffd_open (int flags);int uffd_query_features (uint64_t *features);int uffd_create_fd (uint64_t features,bool non_blocking);void uffd_close_fd (int uffd_fd);
migration/postcopy-ram.c +5 -6
@@ -37,6 +37,7 @@#include "qemu-file.h" #include "yank_functions.h" #include "tls.h" +#include "qemu/userfaultfd.h" /* Arbitrary limit on size of each discard command, * keeps them around ~200 bytes
@@ -226,11 +227,9 @@ static bool receive_ufd_features(uint64_t *features)int ufd;bool ret = true; -/* if we are here __NR_userfaultfd should exists */ - ufd =syscall (__NR_userfaultfd,O_CLOEXEC ); + ufd =uffd_open (O_CLOEXEC );if (ufd ==-1 ) { -error_report ("%s: syscall __NR_userfaultfd failed: %s" , __func__, -strerror (errno)); +error_report ("%s: uffd_open() failed: %s" , __func__,strerror (errno));return false; }
@@ -375,7 +374,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis) goto out; } - ufd =syscall (__NR_userfaultfd,O_CLOEXEC ); + ufd =uffd_open (O_CLOEXEC );if (ufd ==-1 ) {error_report ("%s: userfaultfd not available: %s" , __func__,strerror (errno));
@@ -1160,7 +1159,7 @@ static int postcopy_temp_pages_setup(MigrationIncomingState *mis)int postcopy_ram_incoming_setup (MigrationIncomingState *mis) {/* Open the fd for the kernel to give us userfaults */ - mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); + mis->userfault_fd =uffd_open (O_CLOEXEC |O_NONBLOCK );if (mis->userfault_fd ==-1 ) {error_report ("%s: Failed to open userfault fd: %s" , __func__,strerror (errno));
tests/qtest/migration-test.c +2 -1
@@ -62,13 +62,14 @@ static bool uffd_feature_thread_id;#include <sys/eventfd.h> #include <sys/ioctl.h> #include <linux/userfaultfd.h> +#include "qemu/userfaultfd.h" static bool ufd_version_check (void ) {struct uffdio_api api_struct;uint64_t ioctl_mask; -int ufd =syscall (__NR_userfaultfd,O_CLOEXEC ); +int ufd =uffd_open (O_CLOEXEC );if (ufd ==-1 ) {g_test_message ("Skipping test: userfaultfd not available" );
util/userfaultfd.c +11 -2
@@ -19,6 +19,15 @@#include <sys/syscall.h> #include <sys/ioctl.h> +int uffd_open (int flags) +{ +#if defined(__linux__) && defined(__NR_userfaultfd) +return syscall (__NR_userfaultfd, flags); +#else +return -EINVAL ; +#endif +} +/** * uffd_query_features: query UFFD features *
@@ -32,7 +41,7 @@ int uffd_query_features(uint64_t *features)struct uffdio_api api_struct = {0 };int ret =-1 ; - uffd_fd =syscall (__NR_userfaultfd,O_CLOEXEC ); + uffd_fd =uffd_open (O_CLOEXEC );if (uffd_fd <0 ) {trace_uffd_query_features_nosys (errno);return -1 ;
@@ -69,7 +78,7 @@ int uffd_create_fd(uint64_t features, bool non_blocking)uint64_t ioctl_mask =BIT (_UFFDIO_REGISTER) |BIT (_UFFDIO_UNREGISTER); flags =O_CLOEXEC | (non_blocking ?O_NONBLOCK :0 ); - uffd_fd =syscall (__NR_userfaultfd, flags); + uffd_fd =uffd_open (flags);if (uffd_fd <0 ) {trace_uffd_create_fd_nosys (errno);return -1 ;
Signature · 1 line
2.37.3
Re: [PATCH v2 2/3] util/userfaultfd: Add uffd_open()
Quoted 4 lines · Peter Xu
Add a helper to create the uffd handle.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>
I can get this one through migration tree.
PATCH v2 3/3 util/userfaultfd: Support /dev/userfaultfd
In reply to Peter XuTeach QEMU to use /dev/userfaultfd when it existed and fallback to the system call if either it's not there or doesn't have enough permission.
Firstly, as long as the app has permission to access /dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.
--- util/trace-events | 1 + util/userfaultfd.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
util/trace-events +1 -0
@@ -93,6 +93,7 @@ qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_siz qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%"PRIx64" size 0x%"PRIx64" ofs 0x%x host %p" #userfaultfd.c +uffd_detect_open_mode(int mode) "%d" uffd_query_features_nosys(int err) "errno: %i" uffd_query_features_api_failed(int err) "errno: %i" uffd_create_fd_nosys(int err) "errno: %i"
util/userfaultfd.c +37 -0
@@ -18,10 +18,47 @@#include <poll.h> #include <sys/syscall.h> #include <sys/ioctl.h> +#include <fcntl.h> + +typedef enum { +UFFD_UNINITIALIZED =0 , +UFFD_USE_DEV_PATH , +UFFD_USE_SYSCALL , +}uffd_open_mode ; + +static int uffd_dev; + +static uffd_open_mode uffd_detect_open_mode (void ) +{ +static uffd_open_mode open_mode; + +if (open_mode ==UFFD_UNINITIALIZED ) { +/* +* Make /dev/userfaultfd the default approach because it has better +* permission controls, meanwhile allows kernel faults without any +* privilege requirement (e.g. SYS_CAP_PTRACE). +*/ + uffd_dev =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); +if (uffd_dev >=0 ) { + open_mode =UFFD_USE_DEV_PATH ; + }else { +/* Fallback to the system call */ + open_mode =UFFD_USE_SYSCALL ; + } +trace_uffd_detect_open_mode (open_mode); + } + +return open_mode; +}int uffd_open (int flags) {#if defined(__linux__) && defined(__NR_userfaultfd) +if (uffd_detect_open_mode () ==UFFD_USE_DEV_PATH ) { +assert (uffd_dev >=0 ); +return ioctl (uffd_dev,USERFAULTFD_IOC_NEW , flags); + } +return syscall (__NR_userfaultfd, flags);#else return -EINVAL ;
Signature · 1 line
2.37.3
Quoted 11 lines · Peter Xu
Teach QEMU to use
/dev/userfaultfdwhen it existed and fallback to the system call if either it's not there or doesn't have enough permission.Firstly, as long as the app has permission to access
/dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>
Hi
Can we change this code to not use the global variable.
Quoted 59 lines
--- util/trace-events | 1 + util/userfaultfd.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)util/trace-events +1
-0@@ -93,6 +93,7 @@ qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_siz qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%"PRIx64" size 0x%"PRIx64" ofs 0x%x host %p" #userfaultfd.c +uffd_detect_open_mode(int mode) "%d" uffd_query_features_nosys(int err) "errno: %i" uffd_query_features_api_failed(int err) "errno: %i" uffd_create_fd_nosys(int err) "errno: %i"util/userfaultfd.c +32
-0@@ -18,10 +18,47 @@#include <poll.h> #include <sys/syscall.h> #include <sys/ioctl.h> +#include <fcntl.h> + +typedef enum { +UFFD_UNINITIALIZED =0 , +UFFD_USE_DEV_PATH , +UFFD_USE_SYSCALL , +}uffd_open_mode ; + +static int uffd_dev; + +static uffd_open_mode uffd_detect_open_mode (void ) +{ +static uffd_open_mode open_mode; + +if (open_mode ==UFFD_UNINITIALIZED ) { +/* +* Make /dev/userfaultfd the default approach because it has better +* permission controls, meanwhile allows kernel faults without any +* privilege requirement (e.g. SYS_CAP_PTRACE). +*/ + uffd_dev =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); +if (uffd_dev >=0 ) { + open_mode =UFFD_USE_DEV_PATH ; + }else { +/* Fallback to the system call */ + open_mode =UFFD_USE_SYSCALL ; + } +trace_uffd_detect_open_mode (open_mode); + } + +return open_mode; +}int uffd_open (int flags)
util/userfaultfd.c line 64
Quoted 10 lines
util/userfaultfd.c +5
-0{#if defined(__linux__) && defined(__NR_userfaultfd) +if (uffd_detect_open_mode () ==UFFD_USE_DEV_PATH ) { +assert (uffd_dev >=0 ); +return ioctl (uffd_dev,USERFAULTFD_IOC_NEW , flags); + } +return syscall (__NR_userfaultfd, flags);#else return -EINVAL ;
static int open_userfaultd(void)
{
/*
* Make /dev/userfaultfd the default approach because it has better
* permission controls, meanwhile allows kernel faults without any
* privilege requirement (e.g. SYS_CAP_PTRACE).
*/
int uffd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
if (uffd >= 0) {
return uffd;
}
return -1;
}int uffd_open(int flags)
{
#if defined(__linux__) && defined(__NR_userfaultfd)
static int uffd = -2;
if (uffd == -2) {
uffd = open_userfaultd();
}
if (uffd >= 0) {
return ioctl(uffd, USERFAULTFD_IOC_NEW, flags);
}
return syscall(__NR_userfaultfd, flags);
#else
return -EINVAL;27 lines vs 42
No need for enum type
No need for global variable
What do you think?
Later, Juan.
Quoted 10 lines · Juan Quintela
Quoted 9 lines · Peter Xu
Teach QEMU to use
/dev/userfaultfdwhen it existed and fallback to the system call if either it's not there or doesn't have enough permission.Firstly, as long as the app has permission to access
/dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.
Quoted 5 lines
Quoted 2 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>Hi
Hi, Juan,
Quoted 101 lines
Can we change this code to not use the global variable.
Quoted 59 lines
--- util/trace-events | 1 + util/userfaultfd.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)util/trace-events +1
-0@@ -93,6 +93,7 @@ qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_siz qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%"PRIx64" size 0x%"PRIx64" ofs 0x%x host %p" #userfaultfd.c +uffd_detect_open_mode(int mode) "%d" uffd_query_features_nosys(int err) "errno: %i" uffd_query_features_api_failed(int err) "errno: %i" uffd_create_fd_nosys(int err) "errno: %i"util/userfaultfd.c +32
-0@@ -18,10 +18,47 @@#include <poll.h> #include <sys/syscall.h> #include <sys/ioctl.h> +#include <fcntl.h> + +typedef enum { +UFFD_UNINITIALIZED =0 , +UFFD_USE_DEV_PATH , +UFFD_USE_SYSCALL , +}uffd_open_mode ; + +static int uffd_dev; + +static uffd_open_mode uffd_detect_open_mode (void ) +{ +static uffd_open_mode open_mode; + +if (open_mode ==UFFD_UNINITIALIZED ) { +/* +* Make /dev/userfaultfd the default approach because it has better +* permission controls, meanwhile allows kernel faults without any +* privilege requirement (e.g. SYS_CAP_PTRACE). +*/ + uffd_dev =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); +if (uffd_dev >=0 ) { + open_mode =UFFD_USE_DEV_PATH ; + }else { +/* Fallback to the system call */ + open_mode =UFFD_USE_SYSCALL ; + } +trace_uffd_detect_open_mode (open_mode); + } + +return open_mode; +}int uffd_open (int flags)util/userfaultfd.c line 64
Quoted 10 lines
util/userfaultfd.c +5
-0{#if defined(__linux__) && defined(__NR_userfaultfd) +if (uffd_detect_open_mode () ==UFFD_USE_DEV_PATH ) { +assert (uffd_dev >=0 ); +return ioctl (uffd_dev,USERFAULTFD_IOC_NEW , flags); + } +return syscall (__NR_userfaultfd, flags);#else return -EINVAL ;static int
open_userfaultd(void){ /* * Make /dev/userfaultfd the default approach because it has better * permission controls, meanwhile allows kernel faults without any * privilege requirement (e.g. SYS_CAP_PTRACE). */ int uffd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); if (uffd >= 0) { return uffd; } return -1; }int
uffd_open(int flags)
{#if defined(__linux__) && defined(__NR_userfaultfd) static int uffd = -2; if (uffd == -2) { uffd = open_userfaultd(); } if (uffd >= 0) { return ioctl(uffd, USERFAULTFD_IOC_NEW, flags); } return syscall(__NR_userfaultfd, flags); #else return -EINVAL;
Quoted 6 lines
27 lines vs 42
No need for enum type
No need for global variableWhat do you think?
Yes, as I used to reply to Phil I think it can be simplified. I did this major for (1) better readability, and (2) being crystal clear on which way we used to open /dev/userfaultfd, then guarantee we're keeping using it. so at least I prefer keeping things like trace_uffd_detect_open_mode().
I also plan to add another mode when fd-mode is there even if it'll reuse the same USERFAULTFD_IOC_NEW; they can be useful information when a failure happens.
Though if you insist, I can switch to the simple version too.
Signature · 1 line
Peter Xu
Quoted 11 lines · Peter Xu
Quoted 10 lines · Juan Quintela
Quoted 9 lines · Peter Xu
Teach QEMU to use
/dev/userfaultfdwhen it existed and fallback to the system call if either it's not there or doesn't have enough permission.Firstly, as long as the app has permission to access
/dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.
Quoted 26 lines
Quoted 5 lines
Quoted 2 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>Hi
Hi, Juan,
Quoted 17 lines
static int
open_userfaultd(void){ /* * Make /dev/userfaultfd the default approach because it has better * permission controls, meanwhile allows kernel faults without any * privilege requirement (e.g. SYS_CAP_PTRACE). */ int uffd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); if (uffd >= 0) { return uffd; } return -1; }int
uffd_open(int flags)
{
#ifdefined(__linux__)&&defined(__NR_userfaultfd)
Just an incise, checkpatch don't liue that you use __linux__
This file is compiled under CONFIG_LINUX, so you can drop it.
Quoted 16 lines
Quoted 16 lines
static int uffd = -2; if (uffd == -2) { uffd = open_userfaultd(); } if (uffd >= 0) { return ioctl(uffd, USERFAULTFD_IOC_NEW, flags); } return syscall(__NR_userfaultfd, flags); #else return -EINVAL;27 lines vs 42
No need for enum type
No need for global variable
Quoted 6 lines
Quoted 1 line
What do you think?
Yes, as I used to reply to Phil I think it can be simplified. I did this major for (1) better readability, and (2) being crystal clear on which way we used to open
/dev/userfaultfd, then guarantee we're keeping using it. so at least I prefer keeping things liketrace_uffd_detect_open_mode().
The trace is ok for me. I just forgot to copy it on the rework, sorry.
Quoted 3 lines
I also plan to add another mode when fd-mode is there even if it'll reuse the same
USERFAULTFD_IOC_NEW; they can be useful information when a failure happens.
The other fd mode will change the uffd.
What I *kind* of object is:
- Using a global variable when it is not needed
i.e. for me using a global variable means that anything else is worse. Not the case IMHO.
- Call uffd_open_mode() for every call, when we know that it can change, it is going to return always the same value, so cache it.
Quoted 1 line
Though if you insist, I can switch to the simple version too.
I always told that the person who did the patch has the last word on style. I preffer my version, but it is up to you to take it or not.
Later, Juan.
Quoted 35 lines · Juan Quintela
Quoted 34 lines · Peter Xu
Quoted 10 lines · Juan Quintela
Quoted 9 lines · Peter Xu
Teach QEMU to use
/dev/userfaultfdwhen it existed and fallback to the system call if either it's not there or doesn't have enough permission.Firstly, as long as the app has permission to access
/dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.Quoted 5 lines
Quoted 2 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>Hi
Hi, Juan,
Quoted 14 lines
static int
open_userfaultd(void){ /* * Make /dev/userfaultfd the default approach because it has better * permission controls, meanwhile allows kernel faults without any * privilege requirement (e.g. SYS_CAP_PTRACE). */ int uffd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); if (uffd >= 0) { return uffd; } return -1; }
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
int
uffd_open(int flags)
{
#ifdefined(__linux__)&&defined(__NR_userfaultfd)Just an incise, checkpatch don't liue that you use
__linux__This file is compiled under
CONFIG_LINUX, so you can drop it.
Yes indeed. I'll drop it.
Quoted 30 lines
Quoted 17 lines
Quoted 17 lines
static int uffd = -2; if (uffd == -2) { uffd = open_userfaultd(); } if (uffd >= 0) { return ioctl(uffd, USERFAULTFD_IOC_NEW, flags); } return syscall(__NR_userfaultfd, flags); #else return -EINVAL;27 lines vs 42
No need for enum type
No need for global variableQuoted 6 lines
Quoted 1 line
What do you think?
Yes, as I used to reply to Phil I think it can be simplified. I did this major for (1) better readability, and (2) being crystal clear on which way we used to open
/dev/userfaultfd, then guarantee we're keeping using it. so at least I prefer keeping things liketrace_uffd_detect_open_mode().The trace is ok for me. I just forgot to copy it on the rework, sorry.
Quoted 3 lines
I also plan to add another mode when fd-mode is there even if it'll reuse the same
USERFAULTFD_IOC_NEW; they can be useful information when a failure happens.
Quoted 6 lines
The other fd mode will change the uffd.
What I *kind* of object is:
- Using a global variable when it is not needed
i.e. for me using a global variable means that anything else is worse. Not the case IMHO.
IMHO globals are evil when they're used in multiple places; that's bad to readability. Here it's not the case because it's set once and for all. I wanted to have an easy and clear way to peek what's the mode chosen even without tracing enabled (e.g. from a dump or a live process).
Quoted 2 lines
- Call
uffd_open_mode()for every call, when we know that it can change, it is going to return always the same value, so cache it.
uffd_detect_open_mode() caches the result already? Or maybe you meant something else?
Quoted 5 lines
Quoted 2 lines
Though if you insist, I can switch to the simple version too.
I always told that the person who did the patch has the last word on style. I preffer my version, but it is up to you to take it or not.
Thanks,
Signature · 1 line
Peter Xu
Quoted 78 lines · Peter Xu
Quoted 35 lines · Juan Quintela
Quoted 34 lines · Peter Xu
Quoted 10 lines · Juan Quintela
Quoted 9 lines · Peter Xu
Teach QEMU to use
/dev/userfaultfdwhen it existed and fallback to the system call if either it's not there or doesn't have enough permission.Firstly, as long as the app has permission to access
/dev/userfaultfd, it always have the ability to trap kernel faults which QEMU mostly wants. Meanwhile, in some context (e.g. containers) the userfaultfd syscall can be forbidden, so it can be the major way to use postcopy in a restricted environment with strict seccomp setup.Quoted 5 lines
Quoted 2 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Peter Xu <[email protected]>Hi
Hi, Juan,
Quoted 14 lines
static int
open_userfaultd(void){ /* * Make /dev/userfaultfd the default approach because it has better * permission controls, meanwhile allows kernel faults without any * privilege requirement (e.g. SYS_CAP_PTRACE). */ int uffd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); if (uffd >= 0) { return uffd; } return -1; }Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
int
uffd_open(int flags)
{
#ifdefined(__linux__)&&defined(__NR_userfaultfd)Just an incise, checkpatch don't liue that you use
__linux__This file is compiled under
CONFIG_LINUX, so you can drop it.Yes indeed. I'll drop it.
Quoted 32 lines
Quoted 17 lines
Quoted 17 lines
static int uffd = -2; if (uffd == -2) { uffd = open_userfaultd(); } if (uffd >= 0) { return ioctl(uffd, USERFAULTFD_IOC_NEW, flags); } return syscall(__NR_userfaultfd, flags); #else return -EINVAL;27 lines vs 42
No need for enum type
No need for global variableQuoted 6 lines
Quoted 1 line
What do you think?
Yes, as I used to reply to Phil I think it can be simplified. I did this major for (1) better readability, and (2) being crystal clear on which way we used to open
/dev/userfaultfd, then guarantee we're keeping using it. so at least I prefer keeping things liketrace_uffd_detect_open_mode().The trace is ok for me. I just forgot to copy it on the rework, sorry.
Quoted 3 lines
I also plan to add another mode when fd-mode is there even if it'll reuse the same
USERFAULTFD_IOC_NEW; they can be useful information when a failure happens.The other fd mode will change the uffd.
Quoted 8 lines
Quoted 4 lines
What I *kind* of object is:
- Using a global variable when it is not needed
i.e. for me using a global variable means that anything else is worse. Not the case IMHO.IMHO globals are evil when they're used in multiple places; that's bad to readability. Here it's not the case because it's set once and for all.
That is part of the problem.
int foo;
I need to search all the code to see where it is used.
int bar(...)
{
static int foo;
....
}I am really sure that:
- foo value is preserved between calls
- it is not used anywhere else
without a single grep through the code.
Quoted 3 lines
I
wanted to have an easy and clear way to peek what's the mode chosen even without tracing enabled (e.g. from a dump or a live process).
I haven't thought about this. But you want something different than this?
(fada)$ cat /tmp/kk.c
int bar(void)
{
static int foo = 42;
return foo++;
}int main(void)
{
int a = 7 + 1;
return a + bar();
}(fada)$ gcc -Wall /tmp/kk.c -o /tmp/kkk -g (fada)$ gdb /tmp/kkk
(gdb) b main
Breakpoint 1 at 0x401123: file /tmp/kk.c, line 10. (gdb) p bar::foo
$1 = 42
(gdb)
And yes, I have to search how this is done O:-)
Quoted 5 lines
Quoted 2 lines
- Call
uffd_open_mode()for every call, when we know that it can change, it is going to return always the same value, so cache it.
uffd_detect_open_mode()caches the result already? Or maybe you meant something else?
What I did. Only call the equilavent function once. You are calling it every time that uffd_open() is called.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Though if you insist, I can switch to the simple version too.
I always told that the person who did the patch has the last word on style. I preffer my version, but it is up to you to take it or not.
Thanks,
Later, Juan.