UFFDIO_API2const
Enable operation of the userfaultfd and perform api handshake
Usage
#include <linux/userfaultfd.h> /* Definition of UFFD* constants */ #include <sys/ioctl.h>
int ioctl(int fd, UFFDIO_API, struct uffdio_api *argp);
#include <linux/userfaultfd.h>
struct uffdio_api {
__u64 api; /* Requested API version (input) */
__u64 features; /* Requested features (input/output) */
__u64 ioctls; /* Available ioctl() operations (output) */
};
Library
Standard C library (libc, -lc)
Overview
Enable operation of the userfaultfd and perform API handshake.
The api field denotes the API version requested by the application. The kernel verifies that it can support the requested API version, and sets the features and ioctls fields to bit masks representing all the available features and the generic ioctl(2) operations available.
Before Linux 4.11, the features field must be initialized to zero before the call to UFFDIO_API, and zero (i.e., no feature bits) is placed in the features field by the kernel upon return from ioctl(2).
Since Linux 4.11, userfaultfd supports features that need to be enabled explicitly. To enable any of the features, one needs to set the corresponding feature bits in features when issuing the UFFDIO_API ioctl.
For historical reasons, a temporary userfaultfd is needed to probe what userfaultfd features the kernel supports. The application needs to create a temporary userfaultfd, issue an UFFDIO_API ioctl with features set to zero. After the UFFDIO_API ioctl returns successfully, features should contain all the userfaultfd features that the kernel supports. The temporary userfaultfd can be safely closed after the probe.
If the application sets unsupported feature bits, the kernel will zero out the returned uffdio_api structure and return EINVAL.
The following feature bits may be set:
UFFD_FEATURE_EVENT_FORK(since Linux 4.11)-
When this feature is enabled, the userfaultfd objects associated with a parent process are duplicated into the child process during fork(2) and a
UFFD_EVENT_FORKevent is delivered to the userfaultfd monitor
UFFD_FEATURE_EVENT_REMAP(since Linux 4.11)-
If this feature is enabled, when the faulting process invokes mremap(2), the userfaultfd monitor will receive an event of type
UFFD_EVENT_REMAP.
UFFD_FEATURE_EVENT_REMOVE(since Linux 4.11)-
If this feature is enabled, when the faulting process calls madvise(2) with the
MADV_DONTNEEDorMADV_REMOVEadvice value to free a virtual memory area the userfaultfd monitor will receive an event of typeUFFD_EVENT_REMOVE.
UFFD_FEATURE_MISSING_HUGETLBFS(since Linux 4.11)-
If this feature bit is set, the kernel supports registering userfaultfd ranges on hugetlbfs virtual memory areas
UFFD_FEATURE_MISSING_SHMEM(since Linux 4.11)-
If this feature bit is set, the kernel supports registering userfaultfd ranges on shared memory areas. This includes all kernel shared memory APIs: System V shared memory, tmpfs(5), shared mappings of /dev/zero, mmap(2) with the
MAP_SHAREDflag set, memfd_create(2), and so on.
UFFD_FEATURE_SIGBUS(since Linux 4.14)-
If this feature bit is set, no page-fault events (
UFFD_EVENT_PAGEFAULT) will be delivered. Instead, a SIGBUS signal will be sent to the faulting process. Applications using this feature will not require the use of a userfaultfd monitor for processing memory accesses to the regions registered with userfaultfd.
UFFD_FEATURE_THREAD_ID(since Linux 4.14)-
If this feature bit is set, uffd_msg.pagefault.feat.ptid will be set to the faulted thread ID for each page-fault message.
UFFD_FEATURE_PAGEFAULT_FLAG_WP(since Linux 5.10)-
If this feature bit is set, userfaultfd supports write-protect faults for anonymous memory. (Note that shmem / hugetlbfs support is indicated by a separate feature.)
UFFD_FEATURE_MINOR_HUGETLBFS(since Linux 5.13)-
If this feature bit is set, the kernel supports registering userfaultfd ranges in minor mode on hugetlbfs-backed memory areas.
UFFD_FEATURE_MINOR_SHMEM(since Linux 5.14)-
If this feature bit is set, the kernel supports registering userfaultfd ranges in minor mode on shmem-backed memory areas.
UFFD_FEATURE_EXACT_ADDRESS(since Linux 5.18)-
If this feature bit is set, uffd_msg.pagefault.address will be set to the exact page-fault address that was reported by the hardware, and will not mask the offset within the page. Note that old Linux versions might indicate the exact address as well, even though the feature bit is not set.
UFFD_FEATURE_WP_HUGETLBFS_SHMEM(since Linux 5.19)-
If this feature bit is set, userfaultfd supports write-protect faults for hugetlbfs and shmem / tmpfs memory.
UFFD_FEATURE_WP_UNPOPULATED(since Linux 6.4)-
If this feature bit is set, the kernel will handle anonymous memory the same way as file memory, by allowing the user to write-protect unpopulated page table entries.
UFFD_FEATURE_POISON(since Linux 6.6)-
If this feature bit is set, the kernel supports resolving faults with the
UFFDIO_POISONioctl.
UFFD_FEATURE_WP_ASYNC(since Linux 6.7)-
If this feature bit is set, the write protection faults would be asynchronously resolved by the kernel.
UFFD_FEATURE_MOVE(since Linux 6.8)-
If this feature bit is set, the kernel supports resolving faults with the
UFFDIO_MOVEioctl.
UFFD_FEATURE_RWP(since Linux 7.2)-
If this feature bit is set, the kernel supports read-write-protection tracking, and the
UFFDIO_REGISTER_MODE_RWPregistration mode and theUFFDIO_RWPROTECTioctl become available.
UFFD_FEATURE_RWP_ASYNC(since Linux 7.2)-
If this feature bit is set, the kernel will resolve read-write-protect faults in place without delivering a notification, automatically restoring page permissions and letting the faulted thread continue. This bit requires
UFFD_FEATURE_RWPto be set in the sameUFFDIO_APIcall. The async mode can also be toggled at runtime using the UFFDIO_SET_MODE(2const) ioctl.
The returned argp->ioctls field can contain the following bits:
- 1 << _UFFDIO_API
-
The
UFFDIO_APIoperation is supported.
- 1 << _UFFDIO_REGISTER
-
The
UFFDIO_REGISTERoperation is supported.
- 1 << _UFFDIO_UNREGISTER
-
The
UFFDIO_UNREGISTERoperation is supported.
- 1 << _UFFDIO_SET_MODE (since Linux 7.2)
-
The
UFFDIO_SET_MODEoperation is supported. This is a file-descriptor-level ioctl and is reported once per userfaultfd, independent of any registered range.
Return value
On success, 0 is returned.
On error, -1 is returned and errno is set to indicate the error.
Errors
- EFAULT
-
argp refers to an address that is outside the calling process's accessible address space.
- EINVAL
-
The API version requested in the api field is not supported by this kernel, or the features field passed to the kernel includes feature bits that are not supported by the current kernel version.
- EINVAL
-
A previous
UFFDIO_APIcall already enabled one or more features for this userfaultfd. CallingUFFDIO_APItwice, the first time with no features set, is explicitly allowed as per the two-step feature detection handshake.
- EPERM
-
The
UFFD_FEATURE_EVENT_FORKfeature was enabled, but the calling process doesn't have theCAP_SYS_PTRACEcapability.
Caveats
If an error occurs, the kernel may zero the provided uffdio_api structure. The caller should treat its contents as unspecified, and reinitialize it before re-attempting another UFFDIO_API call.
Examples
See userfaultfd(2).
Releases
2 releases since Debian 13 (Trixie); the text changed 1 time.