Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cxl: docs/allocation/dax

Small example of accessing CXL memory capacity via DAX device

Signed-off-by: Gregory Price <gourry@gourry.net>
Link: https://patch.msgid.link/20250512162134.3596150-14-gourry@gourry.net
Signed-off-by: Dave Jiang <dave.jiang@intel.com>

authored by

Gregory Price and committed by
Dave Jiang
78ab6751 641fdea6

+65
+60
Documentation/driver-api/cxl/allocation/dax.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0 2 + 3 + =========== 4 + DAX Devices 5 + =========== 6 + CXL capacity exposed as a DAX device can be accessed directly via mmap. 7 + Users may wish to use this interface mechanism to write their own userland 8 + CXL allocator, or to managed shared or persistent memory regions across multiple 9 + hosts. 10 + 11 + If the capacity is shared across hosts or persistent, appropriate flushing 12 + mechanisms must be employed unless the region supports Snoop Back-Invalidate. 13 + 14 + Note that mappings must be aligned (size and base) to the dax device's base 15 + alignment, which is typically 2MB - but maybe be configured larger. 16 + 17 + :: 18 + 19 + #include <stdio.h> 20 + #include <stdlib.h> 21 + #include <stdint.h> 22 + #include <sys/mman.h> 23 + #include <fcntl.h> 24 + #include <unistd.h> 25 + 26 + #define DEVICE_PATH "/dev/dax0.0" // Replace DAX device path 27 + #define DEVICE_SIZE (4ULL * 1024 * 1024 * 1024) // 4GB 28 + 29 + int main() { 30 + int fd; 31 + void* mapped_addr; 32 + 33 + /* Open the DAX device */ 34 + fd = open(DEVICE_PATH, O_RDWR); 35 + if (fd < 0) { 36 + perror("open"); 37 + return -1; 38 + } 39 + 40 + /* Map the device into memory */ 41 + mapped_addr = mmap(NULL, DEVICE_SIZE, PROT_READ | PROT_WRITE, 42 + MAP_SHARED, fd, 0); 43 + if (mapped_addr == MAP_FAILED) { 44 + perror("mmap"); 45 + close(fd); 46 + return -1; 47 + } 48 + 49 + printf("Mapped address: %p\n", mapped_addr); 50 + 51 + /* You can now access the device through the mapped address */ 52 + uint64_t* ptr = (uint64_t*)mapped_addr; 53 + *ptr = 0x1234567890abcdef; // Write a value to the device 54 + printf("Value at address %p: 0x%016llx\n", ptr, *ptr); 55 + 56 + /* Clean up */ 57 + munmap(mapped_addr, DEVICE_SIZE); 58 + close(fd); 59 + return 0; 60 + }
+5
Documentation/driver-api/cxl/index.rst
··· 40 40 linux/memory-hotplug 41 41 linux/access-coordinates 42 42 43 + .. toctree:: 44 + :maxdepth: 2 45 + :caption: Memory Allocation 46 + 47 + allocation/dax 43 48 44 49 .. only:: subproject and html