UFFDIO_API(2const) UFFDIO_API(2const)

UFFDIO_API - enable operation of the userfaultfd and perform API handshake

Standard C library (libc, -lc)

#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) */
};

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.

Since Linux 4.11, applications should use the features field to perform a two-step handshake. First, UFFDIO_API is called with the features field set to zero. The kernel responds by setting all supported feature bits.

Applications which do not require any specific features can begin using the userfaultfd immediately. Applications which do need specific features should call UFFDIO_API again with a subset of the reported feature bits set to enable those features.

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).

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:

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_FORK event is delivered to the userfaultfd monitor
If this feature is enabled, when the faulting process invokes mremap(2), the userfaultfd monitor will receive an event of type UFFD_EVENT_REMAP.
If this feature is enabled, when the faulting process calls madvise(2) with the MADV_DONTNEED or MADV_REMOVE advice value to free a virtual memory area the userfaultfd monitor will receive an event of type UFFD_EVENT_REMOVE.
If this feature is enabled, when the faulting process unmaps virtual memory either explicitly with munmap(2), or implicitly during either mmap(2) or mremap(2), the userfaultfd monitor will receive an event of type UFFD_EVENT_UNMAP.
If this feature bit is set, the kernel supports registering userfaultfd ranges on hugetlbfs virtual memory areas
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_SHARED flag set, memfd_create(2), and so on.
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.
If this feature bit is set, uffd_msg.pagefault.feat.ptid will be set to the faulted thread ID for each page-fault message.
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.)
If this feature bit is set, the kernel supports registering userfaultfd ranges in minor mode on hugetlbfs-backed memory areas.
If this feature bit is set, the kernel supports registering userfaultfd ranges in minor mode on shmem-backed memory areas.
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.
If this feature bit is set, userfaultfd supports write-protect faults for hugetlbfs and shmem / tmpfs memory.
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.
If this feature bit is set, the kernel supports resolving faults with the UFFDIO_POISON ioctl.
If this feature bit is set, the write protection faults would be asynchronously resolved by the kernel.

The returned argp->ioctls field can contain the following bits:

1 << _UFFDIO_API
The UFFDIO_API operation is supported.
1 << _UFFDIO_REGISTER
The UFFDIO_REGISTER operation is supported.
1 << _UFFDIO_UNREGISTER
The UFFDIO_UNREGISTER operation is supported.

On success, 0 is returned.

On error, -1 is returned and errno is set to indicate the error.

argp refers to an address that is outside the calling process's accessible address space.
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.
A previous UFFDIO_API call already enabled one or more features for this userfaultfd. Calling UFFDIO_API twice, the first time with no features set, is explicitly allowed as per the two-step feature detection handshake.
The UFFD_FEATURE_EVENT_FORK feature was enabled, but the calling process doesn't have the CAP_SYS_PTRACE capability.

Linux.

Linux 4.3.

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.

In order to detect available userfault features and enable some subset of those features the userfaultfd file descriptor must be closed after the first UFFDIO_API operation that queries features availability and reopened before the second UFFDIO_API operation that actually enables the desired features.

See userfaultfd(2).

ioctl(2), ioctl_userfaultfd(2), mmap(2), userfaultfd(2)

linux.git/Documentation/admin-guide/mm/userfaultfd.rst

2024-06-17 Linux man-pages 6.9.1