mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Merge pull request #388 from ChrisMythos/pr/sysmem-scatterlist-dma-memtypes
rmapi_node: scatterlist DMA mapping and UC/WC memtypes for RM API system memory
This commit is contained in:
@ -21,6 +21,7 @@ Where stock NVIDIA `580.178.04` avoids `#if LINUX_VERSION_CODE` guards altogethe
|
||||
| `fb_is_primary_device()` → `video_is_primary_device()` in `<asm/video.h>` | declaration in `asm/video.h` (**6.9**); before it `vga_default_device()` | `ga10x_adapter.c` (via `NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE` from `kernel-open/Kbuild`) |
|
||||
| `pci_resize_resource(..., exclude_bars)` | declaration in `pci.h` (6.19, backported to 6.18 stable), else **6.19** / `UTS_UBUNTU_RELEASE_ABI` | `pci_adapter.c` ×2 (via `NV_RS_PCI_RESIZE_EXCLUDE_BARS` from `kernel-open/Kbuild`) |
|
||||
| `drm_driver.date` (field removed) | **6.14** | `nvidia-drm/nvidia_rs_drm.c` |
|
||||
| `pin_user_pages(..., vmas)` → without `vmas` | declaration in `mm.h` (**6.5**), as stock's `NV_PIN_USER_PAGES_HAS_ARGS_VMAS` | `rmapi_node.c` (via `NV_RS_PIN_USER_PAGES_HAS_VMAS` from `kernel-open/compat_probes.mk`) |
|
||||
|
||||
- Host-fixture builds (`NV_RS_*_HOST_FIXTURE`) do not pull `<linux/version.h>`. `tests/irq-mock/irq_mock.h` and `tests/rom-fw-mock/rom_fw_mock.h` define `LINUX_VERSION_CODE 0` and `KERNEL_VERSION(a,b,c)` so the guards preprocess cleanly on the host-fixture side.
|
||||
|
||||
|
||||
@ -22,6 +22,10 @@ endif
|
||||
ifeq ($(shell grep -qs 'vma_start_write' $(NV_RS_MM_HEADERS) && echo y),y)
|
||||
ccflags-y += -DNV_RS_HAVE_VMA_START_WRITE
|
||||
endif
|
||||
# pin_user_pages() lost its `vmas` argument in 6.5 (stock conftest: NV_PIN_USER_PAGES_HAS_ARGS_VMAS)
|
||||
ifeq ($(shell grep -A2 'long pin_user_pages.unsigned long start' $(srctree)/include/linux/mm.h 2>/dev/null | grep -q 'vm_area_struct' && echo y),y)
|
||||
ccflags-y += -DNV_RS_PIN_USER_PAGES_HAS_VMAS
|
||||
endif
|
||||
|
||||
# video_is_primary_device() moved to <asm/video.h> in 6.9 (fb_is_primary_device() before)
|
||||
ifeq ($(shell grep -qs 'video_is_primary_device' $(srctree)/arch/$(SRCARCH)/include/asm/video.h $(srctree)/include/asm-generic/video.h && echo y),y)
|
||||
|
||||
@ -277,6 +277,8 @@ int nv_rs_pci_open(struct pci_dev *pdev, struct nv_rs_pci **out)
|
||||
error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
|
||||
if (error) goto fail_mapping;
|
||||
p->dma_bits = 32;
|
||||
/* as the original probe (620.73.04 nv-pci.c:1277): DMA segments of a scatterlist may be as long as the mapping needs */
|
||||
dma_set_max_seg_size(&pdev->dev, U32_MAX);
|
||||
mutex_init(&p->access);
|
||||
raw_spin_lock_init(&p->timer_irq);
|
||||
raw_spin_lock_init(&p->fifo_irq);
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
#include <linux/poll.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/rcupdate.h>
|
||||
#include <linux/scatterlist.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/sched/signal.h>
|
||||
#include <linux/sched/task.h>
|
||||
@ -40,6 +41,7 @@
|
||||
#ifdef CONFIG_X86
|
||||
#include <asm/cpufeature.h>
|
||||
#include <asm/processor.h>
|
||||
#include <asm/set_memory.h>
|
||||
#include <asm/tsc.h>
|
||||
#endif
|
||||
#ifdef CONFIG_COMPAT
|
||||
@ -535,14 +537,20 @@ static struct nv_rs_rmapi_file *nv_rs_rmapi_context(struct file *file, int gpu)
|
||||
/* System memory of the RM API (class 0x3e): the operating-system side the Rust provider
|
||||
* src/nvidia/kernel/rmapi_memory.rs calls. Every function below runs under
|
||||
* nv_rs_rmapi_lock, from an ioctl or a release of one of this driver's RM API nodes; none
|
||||
* touches a device. The pages are ordinary kernel pages with the kernel's cached
|
||||
* attribute: the original also changes the page attributes for the uncached and
|
||||
* write-combined cache types and maps the pages for the GPU's DMA, neither is done here.
|
||||
* touches a device. The pages are ordinary kernel pages. On x86 an allocation of the
|
||||
* uncached or write-combined cache type gets an uncached kernel mapping and user mappings
|
||||
* of its type, as the original's (nv_set_memory_type, nv_encode_caching); elsewhere it
|
||||
* stays cached. Pinned user pages (0x71) whose range lies wholly in uncached user mappings of
|
||||
* such allocations get uncached kernel and user mappings, those wholly in write-combined ones
|
||||
* write-combined mappings; any other range that lies partly in them is refused
|
||||
* (NV_ERR_INVALID_ADDRESS).
|
||||
*
|
||||
* An allocation lives while it has users: the RM object (until nv_rs_rmapi_os_free_pages)
|
||||
* and every file that carries a mapping context of it (until the file is released; the
|
||||
* original defers its nv_free_pages the same way). User mappings do not count: the page
|
||||
* references vm_insert_page took keep their pages after the record is gone. */
|
||||
* original defers its nv_free_pages the same way), and every pinned-user-pages record whose
|
||||
* range was pinned through a user mapping of it (until that record is destroyed). User
|
||||
* mappings do not count: the page references vm_insert_page took keep their pages after the
|
||||
* record is gone. */
|
||||
#define NV_RS_RMAPI_MEMORY_MAX_PAGES ((256ull << 20) >> PAGE_SHIFT)
|
||||
static u64 nv_rs_rmapi_memory_max_pages(void)
|
||||
{
|
||||
@ -570,6 +578,7 @@ static u64 nv_rs_rmapi_total_max_pages(void)
|
||||
u64 ram = (u64)totalram_pages();
|
||||
return ram - ram / 8;
|
||||
}
|
||||
#define NV_RS_STATUS_INSUFFICIENT_RESOURCES 0x1a
|
||||
#define NV_RS_STATUS_INVALID_ADDRESS 0x1e
|
||||
#define NV_RS_STATUS_INVALID_ARGUMENT 0x1f
|
||||
#define NV_RS_STATUS_NO_MEMORY 0x51
|
||||
@ -582,6 +591,17 @@ bool nv_rs_alloc_reg_can_admit(u64 count, bool user_pinned, u64 pool_pages,
|
||||
bool nv_rs_alloc_reg_insert(void *alloc_ptr, u64 count, bool user_pinned, u64 *out_token);
|
||||
void *nv_rs_alloc_reg_find(u64 token);
|
||||
void *nv_rs_alloc_reg_retain(u64 token);
|
||||
bool nv_rs_alloc_reg_retain_ptr(void *alloc_ptr);
|
||||
/* The cache type of a range pinned for class 0x71 (PinnedRangeCache in src/nvidia/src/rmapi/alloc_registry.rs):
|
||||
* NV_MEMORY_* 0, 1 or 2, or NV_RS_PINNED_RANGE_REFUSED. */
|
||||
#define NV_RS_PINNED_RANGE_REFUSED 0xffffffffu
|
||||
u64 nv_rs_pinned_area_bytes(u64 start, u64 end, u64 vm_start, u64 vm_end);
|
||||
u32 nv_rs_pinned_range_cache(u64 range_bytes, u64 uncached_bytes, u64 write_combined_bytes);
|
||||
/* The most allocations a range pinned for class 0x71 may lie in user mappings of, and the most it holds
|
||||
* (PinnedSources in alloc_registry.rs); past it the range is refused with NV_ERR_INSUFFICIENT_RESOURCES. */
|
||||
#define NV_RS_PINNED_SOURCES_MAX 1024u
|
||||
bool nv_rs_pinned_sources_add(void **owners, u64 *owner_count, void **sources, u64 *source_count, u64 capacity,
|
||||
void *owner, void *const *held, u64 held_count);
|
||||
bool nv_rs_alloc_reg_mark_freed_and_put(u64 token, void **out_ptr);
|
||||
bool nv_rs_alloc_reg_put_ptr(void *alloc_ptr);
|
||||
void nv_rs_alloc_reg_for_each(void (*cb)(void *alloc_ptr, void *ctx), void *ctx);
|
||||
@ -597,6 +617,11 @@ struct nv_rs_rmapi_dma_map {
|
||||
struct device *dev;
|
||||
u32 gpu_id;
|
||||
u64 count;
|
||||
/* The whole allocation mapped in one scatterlist (`sg_mapped`), as the original maps every allocation of more
|
||||
* than one page (nv_dma_map_pages -> nv_dma_map_scatterlist). Otherwise each page is mapped on its own as it is
|
||||
* first asked for. */
|
||||
struct sg_table sgt;
|
||||
bool sg_mapped;
|
||||
u64 encoded[]; /* 0 = unmapped; otherwise (u64)dma_addr + 1 */
|
||||
};
|
||||
struct nv_rs_rmapi_alloc {
|
||||
@ -604,9 +629,24 @@ struct nv_rs_rmapi_alloc {
|
||||
u32 cache;
|
||||
bool user_pinned;
|
||||
bool writable;
|
||||
/* `cache` is uncached or write-combined and the kernel mapping of the pages was made uncached
|
||||
* (nv_rs_sysmem_make_uncached): user mappings carry `cache`, the kernel mapping (vmap) is uncached as the
|
||||
* original's for either type, and the pages get their write-back attribute back before they are freed or
|
||||
* pooled. For pinned user pages (`user_pinned`, nv_rs_rmapi_os_lock_user_pages): every page of the range lies in
|
||||
* a user mapping of such allocations, all of one type, which is `cache`; kernel and user mappings follow it and
|
||||
* the pages' attribute stays their allocation's. */
|
||||
bool kernel_uc;
|
||||
/* Pinned user pages: the allocations of this node whose user mappings the range was pinned through, each once
|
||||
* however many areas of the range map it and holding one use until this record is destroyed, so none restores
|
||||
* write-back, pools or frees its pages while this record still pins and maps them. Never another pinned record
|
||||
* (nv_rs_rmapi_pin_scan). */
|
||||
struct nv_rs_rmapi_alloc **sources;
|
||||
u64 source_count;
|
||||
void *kernel;
|
||||
struct page *pages[];
|
||||
};
|
||||
/* The user mappings of system memory (nv_rs_rmapi_mmap); vm_private_data is the allocation. */
|
||||
static const struct vm_operations_struct nv_rs_rmapi_vm_operations;
|
||||
static unsigned long nv_rs_rmapi_gpu_cache_flushes;
|
||||
static atomic64_t nv_rs_rmapi_kvalloc_live = ATOMIC64_INIT(0);
|
||||
|
||||
@ -1487,6 +1527,31 @@ static void nv_rs_rmapi_trim_page_pools_on_client_close(void)
|
||||
nv_rs_rmapi_trim_page_pools((unsigned long)retain_pages);
|
||||
}
|
||||
|
||||
/* The page-attribute changes of the original's nv_set_pages_array_type (580.178.04 nv-vm.c:106-128), which it has
|
||||
* on x86 only (conftest-guarded set_pages_array_*). nv_rs_sysmem_make_uncached and nv_rs_sysmem_may_release
|
||||
* (src/nvidia/kernel/rmapi_memory.rs) call them and decide what a failure means. set_pages_array_* take an int count:
|
||||
* a larger allocation is never changed, so it has nothing to restore. */
|
||||
int nv_rs_sysmem_make_uncached(struct page **pages, u64 count);
|
||||
bool nv_rs_sysmem_may_release(struct page **pages, u64 count, bool kernel_uc);
|
||||
bool nv_rs_rmapi_os_pages_uc(struct page **pages, u64 count);
|
||||
bool nv_rs_rmapi_os_pages_wb(struct page **pages, u64 count);
|
||||
bool nv_rs_rmapi_os_pages_uc(struct page **pages, u64 count)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
return count <= INT_MAX && set_pages_array_uc(pages, (int)count) == 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
bool nv_rs_rmapi_os_pages_wb(struct page **pages, u64 count)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
return count > INT_MAX || set_pages_array_wb(pages, (int)count) == 0;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void *nv_rs_rmapi_kvzalloc(size_t size);
|
||||
void nv_rs_rmapi_kvfree(void *ptr);
|
||||
|
||||
@ -1600,16 +1665,63 @@ void nv_rs_rmapi_gpu_negotiate_dma(u32 gpu_id, unsigned int bits)
|
||||
static void nv_rs_rmapi_dma_map_destroy(struct nv_rs_rmapi_dma_map *dm)
|
||||
{
|
||||
u64 i;
|
||||
for (i = 0; i < dm->count; i++) {
|
||||
if (dm->encoded[i]) {
|
||||
dma_unmap_page(dm->dev, (dma_addr_t)(dm->encoded[i] - 1),
|
||||
PAGE_SIZE, DMA_BIDIRECTIONAL);
|
||||
if (dm->sg_mapped) {
|
||||
dma_unmap_sgtable(dm->dev, &dm->sgt, DMA_BIDIRECTIONAL, 0);
|
||||
sg_free_table(&dm->sgt);
|
||||
} else {
|
||||
for (i = 0; i < dm->count; i++) {
|
||||
if (dm->encoded[i]) {
|
||||
dma_unmap_page(dm->dev, (dma_addr_t)(dm->encoded[i] - 1),
|
||||
PAGE_SIZE, DMA_BIDIRECTIONAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
put_device(dm->dev);
|
||||
kvfree(dm);
|
||||
}
|
||||
|
||||
/* NV_DMA_MAX_SEGMENT_SIZE (620.73.04 nv-linux.h:1059) */
|
||||
#define NV_RS_DMA_MAX_SEGMENT_SIZE 0x80000000u
|
||||
|
||||
/* nv_dma_map_scatterlist (620.73.04 nv-dma.c:356-399): every page of the allocation in one scatterlist
|
||||
* (NV_ALLOC_DMA_SUBMAP_SCATTERLIST, nv-linux.h:1060-1068), mapped with one dma_map_sg, and each page's DMA address
|
||||
* read back from the DMA segments, which the mapping may have merged (nv_load_dma_map_scatterlist, nv-dma.c:327-354).
|
||||
* Behind an IOMMU the allocation gets one IOVA range instead of one per page. False leaves nothing mapped and the
|
||||
* caller maps page by page. Called with nv_rs_rmapi_alloc_lock held, on a record nobody else can see yet. */
|
||||
static bool nv_rs_rmapi_dma_map_whole(struct nv_rs_rmapi_dma_map *dm, struct page **pages)
|
||||
{
|
||||
struct scatterlist *sg;
|
||||
unsigned int i;
|
||||
u64 off, page = 0;
|
||||
|
||||
/* nv_dma_map_pages (nv-dma.c:486-559) maps a single page without a scatterlist */
|
||||
if (dm->count < 2 || dm->count > UINT_MAX)
|
||||
return false;
|
||||
if (sg_alloc_table_from_pages_segment(&dm->sgt, pages, (unsigned int)dm->count, 0,
|
||||
(unsigned long)dm->count << PAGE_SHIFT,
|
||||
NV_RS_DMA_MAX_SEGMENT_SIZE, GFP_KERNEL))
|
||||
return false;
|
||||
if (dma_map_sgtable(dm->dev, &dm->sgt, DMA_BIDIRECTIONAL, 0)) {
|
||||
sg_free_table(&dm->sgt);
|
||||
return false;
|
||||
}
|
||||
for_each_sgtable_dma_sg(&dm->sgt, sg, i) {
|
||||
for (off = 0; off < sg_dma_len(sg) && page < dm->count; off += PAGE_SIZE)
|
||||
dm->encoded[page++] = (u64)sg_dma_address(sg) + off + 1;
|
||||
}
|
||||
if (page != dm->count) {
|
||||
/* page-aligned segments cover every page; a table that does not is never used */
|
||||
dma_unmap_sgtable(dm->dev, &dm->sgt, DMA_BIDIRECTIONAL, 0);
|
||||
sg_free_table(&dm->sgt);
|
||||
memset(dm->encoded, 0, dm->count * sizeof(dm->encoded[0]));
|
||||
return false;
|
||||
}
|
||||
dm->sg_mapped = true;
|
||||
pr_debug("nvidia_rs: DMA map of %llu pages: %u CPU segments, %u DMA segments\n",
|
||||
dm->count, dm->sgt.orig_nents, dm->sgt.nents);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void nv_rs_rmapi_alloc_unmap_dma(struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
struct nv_rs_rmapi_dma_map *dm;
|
||||
@ -1645,13 +1757,24 @@ static struct nv_rs_rmapi_alloc *nv_rs_rmapi_alloc_find(u64 token)
|
||||
return nv_rs_alloc_reg_find(token);
|
||||
}
|
||||
|
||||
/* The kernel mapping of an allocation of this node is the page's own linear address for one page, as
|
||||
* nv_alloc_kernel_mapping returns at->page_table[pageIndex].virt_addr for it (580.178.04 nv.c:3730-3737), and a vmap
|
||||
* otherwise. Pinned user pages are always vmapped, as the original vmaps user memory of any size (isUserAllocatedMem,
|
||||
* nv.c:3722-3732): their kernel mapping carries the record's cache type, not the linear map's attribute. */
|
||||
static bool nv_rs_rmapi_kernel_is_vmap(const struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
return alloc->count > 1 || alloc->user_pinned;
|
||||
}
|
||||
|
||||
static void nv_rs_rmapi_alloc_unmap(struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
if (alloc->kernel && alloc->count > 1)
|
||||
if (alloc->kernel && nv_rs_rmapi_kernel_is_vmap(alloc))
|
||||
vunmap(alloc->kernel);
|
||||
alloc->kernel = NULL;
|
||||
}
|
||||
|
||||
static void nv_rs_rmapi_put_sources(struct nv_rs_rmapi_alloc **sources, u64 count);
|
||||
|
||||
/* Releases DMA maps, kernel mapping, OS page references, and the record itself
|
||||
* after Rust's AllocLedger has dropped its final reference. */
|
||||
static void nv_rs_rmapi_alloc_destroy(struct nv_rs_rmapi_alloc *alloc)
|
||||
@ -1661,6 +1784,14 @@ static void nv_rs_rmapi_alloc_destroy(struct nv_rs_rmapi_alloc *alloc)
|
||||
nv_rs_rmapi_alloc_unmap(alloc);
|
||||
if (alloc->user_pinned) {
|
||||
unpin_user_pages_dirty_lock(alloc->pages, alloc->count, alloc->writable);
|
||||
/* Only now may the allocations the range was pinned through restore write-back, pool or free their pages.
|
||||
* They are never pinned records themselves, so this does not recurse further. */
|
||||
nv_rs_rmapi_put_sources(alloc->sources, alloc->source_count);
|
||||
} else if (!nv_rs_sysmem_may_release(alloc->pages, alloc->count, alloc->kernel_uc)) {
|
||||
/* nv_free_system_pages restores write-back first (580.178.04 nv-vm.c:619-620). Pages that may still be
|
||||
* uncached are leaked: never pooled, never handed back to the page allocator. */
|
||||
pr_warn_ratelimited("nvidia_rs: leaking %llu pages whose cached attribute could not be restored\n",
|
||||
alloc->count);
|
||||
} else {
|
||||
int retain_mb = READ_ONCE(system_memory_pool_retain_mb);
|
||||
for (index = 0; index < alloc->count; index++) {
|
||||
@ -1683,15 +1814,136 @@ static void nv_rs_rmapi_alloc_put(struct nv_rs_rmapi_alloc *alloc)
|
||||
nv_rs_rmapi_alloc_destroy(alloc);
|
||||
}
|
||||
|
||||
/* NV_PIN_USER_PAGES (580.178.04 nv-mm.h:53-59): pin_user_pages lost its vmas argument in 6.5. */
|
||||
#if defined(NV_RS_PIN_USER_PAGES_HAS_VMAS)
|
||||
#define nv_rs_pin_user_pages(start, nr_pages, gup_flags, pages) \
|
||||
pin_user_pages(start, nr_pages, gup_flags, pages, NULL)
|
||||
#else
|
||||
#define nv_rs_pin_user_pages pin_user_pages
|
||||
#endif
|
||||
|
||||
/* Drops the uses a pinned record holds of the allocations its range was pinned through (nv_rs_rmapi_pin_retain) and
|
||||
* frees the list. */
|
||||
static void nv_rs_rmapi_put_sources(struct nv_rs_rmapi_alloc **sources, u64 count)
|
||||
{
|
||||
while (count)
|
||||
nv_rs_rmapi_alloc_put(sources[--count]);
|
||||
kvfree(sources);
|
||||
}
|
||||
|
||||
/* The allocation of this node a user area maps: nv_rs_rmapi_mmap sets vm_private_data, as the original sets
|
||||
* NV_VMA_PRIVATE(vma) = at (580.178.04 nv-mmap.c:706). NULL for any other area. */
|
||||
static struct nv_rs_rmapi_alloc *nv_rs_rmapi_area_alloc(const struct vm_area_struct *vma)
|
||||
{
|
||||
return vma->vm_ops == &nv_rs_rmapi_vm_operations ? vma->vm_private_data : NULL;
|
||||
}
|
||||
|
||||
/* The NV_MEMORY_* type nv_rs_rmapi_mmap gives the user mappings of `alloc`: its cache type, write-combined (2) or
|
||||
* uncached (1), when the kernel mapping of its pages was made uncached; cached (0) otherwise. */
|
||||
static u32 nv_rs_rmapi_user_cache(const struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
if (!alloc->kernel_uc)
|
||||
return 0;
|
||||
return alloc->cache == 2 ? 2 : 1;
|
||||
}
|
||||
|
||||
/* The areas of [start, end) of `mm`, under the mmap lock the range was pinned under: they are the areas the pages were
|
||||
* pinned through, and each area this node mapped holds its file, whose mapping context holds the allocation.
|
||||
* `uncached` / `write_combined`: the bytes of the range inside uncached / write-combined user mappings of this node
|
||||
* (nv_rs_rmapi_user_cache), summed over every area. `set`: NULL when no area maps an allocation of this node; else room
|
||||
* for 2 * NV_RS_PINNED_SOURCES_MAX entries whose first `source_count` are the allocations a record pinned through these
|
||||
* areas holds, each once (nv_rs_pinned_sources_add; the second half keeps the allocations seen). An allocation counts
|
||||
* once however many of its areas the range covers (a mapping split by mprotect, a second mmap next to it), and a
|
||||
* mapping of pinned user pages counts for the allocations that record holds, so a record never holds another record
|
||||
* and teardown never recurses past one level. Returns 0, NV_RS_STATUS_INSUFFICIENT_RESOURCES when the range lies in
|
||||
* mappings of more than NV_RS_PINNED_SOURCES_MAX allocations or would hold more, or NV_RS_STATUS_NO_MEMORY. */
|
||||
static u32 nv_rs_rmapi_pin_scan(struct mm_struct *mm, unsigned long start, unsigned long end, u64 *uncached,
|
||||
u64 *write_combined, void ***set, u64 *source_count)
|
||||
{
|
||||
struct vm_area_struct *vma;
|
||||
u64 owner_count = 0;
|
||||
u32 status = 0;
|
||||
|
||||
*uncached = 0;
|
||||
*write_combined = 0;
|
||||
*set = NULL;
|
||||
*source_count = 0;
|
||||
for (vma = find_vma(mm, start); vma && vma->vm_start < end; vma = find_vma(mm, vma->vm_end)) {
|
||||
struct nv_rs_rmapi_alloc *owner = nv_rs_rmapi_area_alloc(vma);
|
||||
u32 type;
|
||||
if (!owner)
|
||||
continue;
|
||||
/* the areas do not overlap, so neither sum exceeds the length of the range */
|
||||
type = nv_rs_rmapi_user_cache(owner);
|
||||
if (type == 2)
|
||||
*write_combined += nv_rs_pinned_area_bytes(start, end, vma->vm_start, vma->vm_end);
|
||||
else if (type == 1)
|
||||
*uncached += nv_rs_pinned_area_bytes(start, end, vma->vm_start, vma->vm_end);
|
||||
if (status)
|
||||
continue;
|
||||
if (!*set) {
|
||||
*set = kvcalloc(2 * NV_RS_PINNED_SOURCES_MAX, sizeof(**set), GFP_KERNEL);
|
||||
if (!*set) {
|
||||
status = NV_RS_STATUS_NO_MEMORY;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!nv_rs_pinned_sources_add(*set + NV_RS_PINNED_SOURCES_MAX, &owner_count, *set, source_count,
|
||||
NV_RS_PINNED_SOURCES_MAX, owner,
|
||||
owner->user_pinned ? (void *const *)owner->sources : (void *const *)&owner,
|
||||
owner->user_pinned ? owner->source_count : 1))
|
||||
status = NV_RS_STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Takes one use of each of the `count` allocations nv_rs_rmapi_pin_scan gathered in `set`, into `sources` (room for
|
||||
* `count`), under the same mmap lock, whose areas keep them alive until then. Returns how many it took; fewer than
|
||||
* `count` only if a use count is saturated. */
|
||||
static u64 nv_rs_rmapi_pin_retain(void *const *set, struct nv_rs_rmapi_alloc **sources, u64 count)
|
||||
{
|
||||
u64 taken;
|
||||
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
for (taken = 0; taken < count && nv_rs_alloc_reg_retain_ptr(set[taken]); taken++)
|
||||
sources[taken] = set[taken];
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
return taken;
|
||||
}
|
||||
|
||||
/* Pins `count` user-space 4 KiB pages starting at `address` for class 0x71
|
||||
* (NV01_MEMORY_SYSTEM_OS_DESCRIPTOR) so UVM/GPU page tables can reference their physical
|
||||
* addresses directly. */
|
||||
* addresses directly.
|
||||
*
|
||||
* As os_lock_user_pages (580.178.04 os-mlock.c:216-309) the pages are pinned under the mmap lock, and a range that
|
||||
* cannot be pinned is refused with NV_ERR_INVALID_ADDRESS. The original looks at nothing else and maps every pinned
|
||||
* range uncached (nv_register_user_pages, nv.c:3390): write-back pages get uncached mappings, and once the
|
||||
* allocation a range was pinned through is freed (nv_free_system_pages restores write-back, nv-vm.c:619-620;
|
||||
* 620.73.04 hands freed pages to its pool, nv-vm.c:962-963) the pinned record maps pages that are write-back again
|
||||
* or belong to another allocation. Here:
|
||||
* - a range that lies wholly in uncached user mappings of this node's allocations whose kernel mapping was made
|
||||
* uncached gets uncached kernel and user mappings; one that lies wholly in write-combined user mappings of them gets
|
||||
* write-combined kernel (vmap) and user mappings, the type of the user mappings it was pinned through; any other
|
||||
* range cached ones (PinnedRangeCache). The pages' attribute in the linear map stays their allocation's (UC- for
|
||||
* both types, as the original's nv_set_memory_type makes it, nv-vm.c:583-584);
|
||||
* - a range that lies partly in such mappings and partly in other memory, or in both uncached and write-combined
|
||||
* ones, is refused with NV_ERR_INVALID_ADDRESS: no one mapping type fits all its pages;
|
||||
* - the record holds every allocation of this node it was pinned through until it is destroyed, so none restores
|
||||
* write-back, pools or frees its pages first. It holds each of them once, however many areas of the range map it
|
||||
* and through however many mappings of pinned records, and takes one use of each; a range that lies in user
|
||||
* mappings of more than NV_RS_PINNED_SOURCES_MAX allocations, or would hold more, is refused with
|
||||
* NV_ERR_INSUFFICIENT_RESOURCES (0x1a; the original has no such list, so no such refusal). */
|
||||
u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *token)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc;
|
||||
u64 pinned = 0;
|
||||
struct nv_rs_rmapi_alloc **sources = NULL;
|
||||
void **set = NULL;
|
||||
struct mm_struct *mm = current->mm;
|
||||
unsigned long start = (unsigned long)address, end;
|
||||
u64 pinned = 0, uncached = 0, write_combined = 0, source_count = 0, taken = 0;
|
||||
u64 pool;
|
||||
u64 tok = 0;
|
||||
u32 cache = NV_RS_PINNED_RANGE_REFUSED, status = NV_RS_STATUS_INVALID_ADDRESS;
|
||||
bool ok;
|
||||
unsigned int gup_flags = writable ? FOLL_WRITE : 0;
|
||||
#ifdef FOLL_LONGTERM
|
||||
@ -1699,7 +1951,7 @@ u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *t
|
||||
gup_flags |= FOLL_LONGTERM;
|
||||
#endif
|
||||
|
||||
if (!count || !token || (address & (PAGE_SIZE - 1)))
|
||||
if (!count || !token || !mm || (address & (PAGE_SIZE - 1)))
|
||||
return NV_RS_STATUS_INVALID_ARGUMENT;
|
||||
pool = nv_rs_sysmem_pool_len();
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
@ -1713,29 +1965,56 @@ u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *t
|
||||
if (!alloc)
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
|
||||
/* The mmap lock also keeps the areas the pages were pinned through for the scan below. */
|
||||
mmap_read_lock(mm);
|
||||
while (pinned < count) {
|
||||
u64 remain = count - pinned;
|
||||
int chunk = remain > 1024 ? 1024 : (int)remain;
|
||||
int got = pin_user_pages_fast((unsigned long)(address + (pinned << PAGE_SHIFT)),
|
||||
chunk, gup_flags, &alloc->pages[pinned]);
|
||||
if (got <= 0) {
|
||||
if (pinned > 0)
|
||||
unpin_user_pages(alloc->pages, pinned);
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_INVALID_ADDRESS;
|
||||
}
|
||||
unsigned long chunk = remain > 1024 ? 1024 : (unsigned long)remain;
|
||||
long got = nv_rs_pin_user_pages((unsigned long)(address + (pinned << PAGE_SHIFT)),
|
||||
chunk, gup_flags, &alloc->pages[pinned]);
|
||||
if (got <= 0)
|
||||
break;
|
||||
pinned += (u64)got;
|
||||
}
|
||||
if (pinned == count) {
|
||||
/* every page is mapped, so [start, end) is a valid user range */
|
||||
end = start + (unsigned long)(count << PAGE_SHIFT);
|
||||
status = nv_rs_rmapi_pin_scan(mm, start, end, &uncached, &write_combined, &set, &source_count);
|
||||
cache = nv_rs_pinned_range_cache(count << PAGE_SHIFT, uncached, write_combined);
|
||||
if (cache == NV_RS_PINNED_RANGE_REFUSED)
|
||||
status = NV_RS_STATUS_INVALID_ADDRESS;
|
||||
if (!status && source_count) {
|
||||
sources = kvmalloc_array(source_count, sizeof(*sources), GFP_KERNEL);
|
||||
if (!sources)
|
||||
status = NV_RS_STATUS_NO_MEMORY;
|
||||
else if ((taken = nv_rs_rmapi_pin_retain(set, sources, source_count)) != source_count)
|
||||
status = NV_RS_STATUS_INVALID_ADDRESS;
|
||||
}
|
||||
}
|
||||
mmap_read_unlock(mm);
|
||||
kvfree(set);
|
||||
if (status) {
|
||||
if (pinned > 0)
|
||||
unpin_user_pages(alloc->pages, pinned);
|
||||
nv_rs_rmapi_put_sources(sources, taken);
|
||||
kvfree(alloc);
|
||||
return status;
|
||||
}
|
||||
|
||||
alloc->count = count;
|
||||
alloc->cache = 0;
|
||||
alloc->cache = cache;
|
||||
/* uncached or write-combined: every page lies in user mappings of that type (nv_rs_rmapi_user_cache) */
|
||||
alloc->kernel_uc = cache != 0;
|
||||
alloc->user_pinned = true;
|
||||
alloc->writable = writable;
|
||||
alloc->sources = sources;
|
||||
alloc->source_count = source_count;
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
ok = nv_rs_alloc_reg_insert(alloc, count, true, &tok);
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
if (!ok) {
|
||||
unpin_user_pages(alloc->pages, count);
|
||||
nv_rs_rmapi_put_sources(sources, source_count);
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1792,12 +2071,27 @@ u32 nv_rs_rmapi_os_alloc_pages(u64 count, u64 page, bool contiguous, u32 cache,
|
||||
}
|
||||
alloc->count = count;
|
||||
alloc->cache = cache;
|
||||
/* nv_alloc_system_pages: not cached -> nv_set_memory_type(at, NV_MEMORY_UNCACHED) (580.178.04 nv-vm.c:583-584).
|
||||
* If that fails the allocation stays cached everywhere, user mappings included. */
|
||||
if (cache != 0) {
|
||||
int uc = nv_rs_sysmem_make_uncached(alloc->pages, count);
|
||||
if (uc < 0) {
|
||||
pr_warn_ratelimited("nvidia_rs: leaking %llu pages whose cached attribute could not be restored\n",
|
||||
count);
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
alloc->kernel_uc = uc == 0;
|
||||
}
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
ok = nv_rs_alloc_reg_insert(alloc, count, false, &tok);
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
if (!ok) {
|
||||
for (index = 0; index < count; index++)
|
||||
__free_page(alloc->pages[index]);
|
||||
/* as on free: pages that may still be uncached are leaked */
|
||||
if (nv_rs_sysmem_may_release(alloc->pages, count, alloc->kernel_uc)) {
|
||||
for (index = 0; index < count; index++)
|
||||
__free_page(alloc->pages[index]);
|
||||
}
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1823,8 +2117,31 @@ void nv_rs_rmapi_os_free_pages(u64 token)
|
||||
nv_rs_rmapi_alloc_destroy(ptr);
|
||||
}
|
||||
|
||||
/* nv_vmap (580.178.04 nv-vm.c:634-656), which nv_alloc_kernel_mapping asks for a cached mapping only for
|
||||
* NV_MEMORY_CACHED (nv.c:3762-3763): PAGE_KERNEL_NOCACHE for pages whose kernel mapping was made uncached, the
|
||||
* write-combined allocation's own vmap included (nv_vmap has no write-combined protection). Pinned user pages whose
|
||||
* range lies wholly in write-combined user mappings are mapped write-combined instead, the type of those mappings;
|
||||
* the original maps every pinned range PAGE_KERNEL_NOCACHE (nv_register_user_pages, nv.c:3390). */
|
||||
static pgprot_t nv_rs_rmapi_kernel_prot(const struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
if (alloc->kernel_uc)
|
||||
return alloc->user_pinned && alloc->cache == 2 ? pgprot_writecombine(PAGE_KERNEL) : PAGE_KERNEL_NOCACHE;
|
||||
#endif
|
||||
return PAGE_KERNEL;
|
||||
}
|
||||
|
||||
/* The kernel mapping nv_alloc_kernel_mapping makes of the whole allocation (nv_rs_rmapi_kernel_is_vmap). Called with
|
||||
* nv_rs_rmapi_alloc_lock held. */
|
||||
static void *nv_rs_rmapi_kernel_map_pages(struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
if (!nv_rs_rmapi_kernel_is_vmap(alloc))
|
||||
return page_address(alloc->pages[0]);
|
||||
return vmap(alloc->pages, alloc->count, VM_MAP, nv_rs_rmapi_kernel_prot(alloc));
|
||||
}
|
||||
|
||||
/* nv_alloc_kernel_mapping of the whole allocation: the page's own address for one page,
|
||||
* vmap for more. */
|
||||
* vmap for more and for pinned user pages. */
|
||||
u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc;
|
||||
@ -1835,8 +2152,7 @@ u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
||||
if (!alloc || size > alloc->count << PAGE_SHIFT) {
|
||||
rc = NV_RS_STATUS_INVALID_ARGUMENT;
|
||||
} else if (!alloc->kernel) {
|
||||
alloc->kernel = alloc->count == 1 ? page_address(alloc->pages[0])
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP, PAGE_KERNEL);
|
||||
alloc->kernel = nv_rs_rmapi_kernel_map_pages(alloc);
|
||||
if (!alloc->kernel)
|
||||
rc = NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1844,6 +2160,47 @@ u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
||||
return rc;
|
||||
}
|
||||
|
||||
#ifdef NV_RS_PCI_TEST_EDU
|
||||
/* EDU guest test only (tests/vm/edu_adapter_test.c): the kernel mapping of an allocation, NULL before one is made,
|
||||
* so the test can read the cache mode of its page-table entries. */
|
||||
void *nv_rs_rmapi_os_kernel_address(u64 token);
|
||||
void *nv_rs_rmapi_os_kernel_address(u64 token)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc;
|
||||
void *kernel = NULL;
|
||||
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
alloc = nv_rs_rmapi_alloc_find(token);
|
||||
if (alloc)
|
||||
kernel = alloc->kernel;
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
return kernel;
|
||||
}
|
||||
|
||||
/* EDU guest test only: how many entries the list of allocations the pinned record `token` holds has (`sources`,
|
||||
* nv_rs_rmapi_pin_scan), and in `*of_source` how many of them are the allocation `source`. U64_MAX for a token that
|
||||
* is no pinned record. */
|
||||
u64 nv_rs_rmapi_os_pinned_sources(u64 token, u64 source, u64 *of_source);
|
||||
u64 nv_rs_rmapi_os_pinned_sources(u64 token, u64 source, u64 *of_source)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc, *held;
|
||||
u64 count = U64_MAX, index;
|
||||
|
||||
*of_source = 0;
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
alloc = nv_rs_rmapi_alloc_find(token);
|
||||
held = nv_rs_rmapi_alloc_find(source);
|
||||
if (alloc && alloc->user_pinned) {
|
||||
count = alloc->source_count;
|
||||
for (index = 0; index < count; index++)
|
||||
if (held && alloc->sources[index] == held)
|
||||
(*of_source)++;
|
||||
}
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
return count;
|
||||
}
|
||||
#endif
|
||||
|
||||
void nv_rs_rmapi_os_kernel_unmap(u64 token)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc;
|
||||
@ -1874,9 +2231,7 @@ u32 nv_rs_rmapi_os_kernel_write(u64 token, u64 offset, const u8 *bytes, size_t l
|
||||
rc = NV_RS_STATUS_INVALID_ARGUMENT;
|
||||
} else {
|
||||
if (!alloc->kernel) {
|
||||
alloc->kernel = alloc->count == 1
|
||||
? page_address(alloc->pages[0])
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP, PAGE_KERNEL);
|
||||
alloc->kernel = nv_rs_rmapi_kernel_map_pages(alloc);
|
||||
if (!alloc->kernel)
|
||||
rc = NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1904,7 +2259,8 @@ u32 nv_rs_rmapi_os_page_phys(u64 token, u64 page_index, u64 *phys)
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Resolves (and lazily IOMMU/DMA-maps) one 4 KiB page of a system-memory allocation for `gpu_id`. */
|
||||
/* Resolves one 4 KiB page of a system-memory allocation for `gpu_id`. The first request for a GPU DMA-maps the whole
|
||||
* allocation (nv_rs_rmapi_dma_map_whole); when that fails, each page is mapped as it is first asked for. */
|
||||
u32 nv_rs_rmapi_os_page_dma(u32 gpu_id, u64 token, u64 page_index, u64 *dma_out)
|
||||
{
|
||||
struct nv_rs_rmapi_alloc *alloc;
|
||||
@ -1950,10 +2306,11 @@ u32 nv_rs_rmapi_os_page_dma(u32 gpu_id, u64 token, u64 page_index, u64 *dma_out)
|
||||
dm->dev = dev;
|
||||
dm->gpu_id = gpu_id;
|
||||
dm->count = alloc->count;
|
||||
(void)nv_rs_rmapi_dma_map_whole(dm, alloc->pages);
|
||||
if (!nv_rs_dma_reg_insert(alloc, gpu_id, dev, dm)) {
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
put_device(dev);
|
||||
kvfree(dm);
|
||||
/* drops the mapping and the device reference */
|
||||
nv_rs_rmapi_dma_map_destroy(dm);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
}
|
||||
@ -2993,10 +3350,12 @@ static const struct vm_operations_struct nv_rs_rmapi_vm_operations = {
|
||||
* whose page references keep them alive while mapped.
|
||||
*
|
||||
* Differences from the original: a private mapping is refused with EINVAL (the original
|
||||
* turns it into a shared one by setting VM_SHARED afterwards); the pages are always mapped
|
||||
* cached (the original applies the allocation's cache type to the protection, after
|
||||
* changing the pages' kernel attributes at allocation); a read-only context is applied
|
||||
* before the pages are inserted, not after.
|
||||
* turns it into a shared one by setting VM_SHARED afterwards); the allocation's cache type
|
||||
* is applied only when its kernel mapping was made uncached (for pinned user pages: when
|
||||
* their range maps such allocations, all of one type, which the record's mappings take),
|
||||
* otherwise the pages are mapped cached; pinned user pages of write-combined mappings are
|
||||
* mapped write-combined, not uncached; a read-only context is applied before the pages
|
||||
* are inserted, not after.
|
||||
*
|
||||
* The context is immutable once valid and holds a use of the allocation until the file is
|
||||
* released, so nothing is locked here. */
|
||||
@ -3138,8 +3497,23 @@ static int nv_rs_rmapi_mmap(struct file *file, struct vm_area_struct *vma)
|
||||
nv_rs_vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
|
||||
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
|
||||
}
|
||||
/* nvidia_mmap: nv_encode_caching(at->cache_type) before nvidia_mmap_sysmem (580.178.04 nv-mmap.c:328-394,
|
||||
* 699-708; 620.73.04 nv-mmap.c:346-363 uses pgprot_writecombine / pgprot_noncached as here). The type is the
|
||||
* one nv_rs_rmapi_pin_scan counts this area as. */
|
||||
switch (nv_rs_rmapi_user_cache(alloc)) {
|
||||
case 2:
|
||||
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
|
||||
break;
|
||||
case 1:
|
||||
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
nv_rs_vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
|
||||
vma->vm_ops = &nv_rs_rmapi_vm_operations;
|
||||
/* NV_VMA_PRIVATE(vma) = at (580.178.04 nv-mmap.c:706); read by nv_rs_rmapi_area_alloc */
|
||||
vma->vm_private_data = alloc;
|
||||
for (index = mapping.page_index; index < mapping.page_index + pages; index++) {
|
||||
/* On failure the caller removes the area and with it the pages inserted so far. */
|
||||
if (vm_insert_page(vma, address, alloc->pages[index]))
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -158,6 +158,9 @@ static void pci_iounmap(struct pci_dev*p,void*mem){assert(mem==mmio&&p->mappings
|
||||
static int dma_set_mask_and_coherent(struct device*d,u64 mask){if(fail_at==5)return -EIO;d->mask=mask;d->stream=mask;return 0;}
|
||||
/* `mask` is the coherent mask, `stream` the streaming one. */
|
||||
static int dma_set_mask(struct device*d,u64 mask){if(fail_at==6)return -EIO;d->stream=mask;return 0;}
|
||||
#define U32_MAX UINT32_MAX
|
||||
/* The device's max DMA segment size (the mock maps no scatterlists). */
|
||||
static void dma_set_max_seg_size(struct device*d,unsigned int size){(void)d;(void)size;}
|
||||
static u64 dma_get_mask(struct device*d){return d->stream;}
|
||||
static bool pci_dev_is_disconnected(struct pci_dev*p){return p->disconnected;}
|
||||
static void pci_set_master(struct pci_dev*p){p->master=true;}
|
||||
|
||||
@ -13,8 +13,10 @@ use core::ptr;
|
||||
use core::sync::atomic::{AtomicU64, Ordering};
|
||||
use nv_rs_abi::NvStatus;
|
||||
use nv_rs_core::rmapi::alloc_registry::{
|
||||
AllocLedger, AllocRecord, DmaMapLedger, DmaMapRecord, FileMappingLedger, FileMappingRecord,
|
||||
GpuSlotLedger, SysmemPagePool, UvmCpuDmaLedger, UvmCpuDmaRecord, UvmCpuDmaSlot,
|
||||
pinned_area_bytes, sysmem_make_uncached, sysmem_may_release, AllocLedger, AllocRecord,
|
||||
DmaMapLedger, DmaMapRecord, FileMappingLedger, FileMappingRecord, GpuSlotLedger,
|
||||
PinnedRangeCache, PinnedSources, SysmemPageAttrs, SysmemPagePool, SysmemUncached,
|
||||
UvmCpuDmaLedger, UvmCpuDmaRecord, UvmCpuDmaSlot,
|
||||
};
|
||||
use nv_rs_core::rmapi::memory::{MappingContext, OsMemory, OsToken};
|
||||
use nv_rs_core::rmapi::status;
|
||||
@ -93,6 +95,8 @@ unsafe extern "C" {
|
||||
fn nv_rs_rmapi_os_page_dma(gpu_id: u32, token: u64, page_index: u64, dma_out: *mut u64) -> u32;
|
||||
fn nv_rs_rmapi_os_flush_gpu_cache();
|
||||
fn nv_rs_rmapi_os_ctl_fd_owner(descriptor: i32) -> u64;
|
||||
fn nv_rs_rmapi_os_pages_uc(pages: *mut c_void, count: u64) -> bool;
|
||||
fn nv_rs_rmapi_os_pages_wb(pages: *mut c_void, count: u64) -> bool;
|
||||
}
|
||||
|
||||
/// The kernel's memory services for the RM API nodes. Use only under the node mutex.
|
||||
@ -101,7 +105,9 @@ pub struct KernelOs;
|
||||
impl OsMemory for KernelOs {
|
||||
/// One zeroed kernel page per 4 KiB page. Big and huge granules and contiguous runs of
|
||||
/// more than one page are NOT_SUPPORTED; past the node's page budget NO_MEMORY (0x51).
|
||||
/// The cache type is recorded only: the pages keep the kernel's cached attribute.
|
||||
/// On x86 an allocation of cache type 1 (uncached) or 2 (write-combined) gets an uncached
|
||||
/// kernel mapping (`nv_rs_sysmem_make_uncached`) and user mappings of its type; elsewhere,
|
||||
/// or when that change fails and is undone, it stays cached.
|
||||
fn alloc_pages(
|
||||
&mut self,
|
||||
_gpu: u32,
|
||||
@ -409,6 +415,15 @@ pub unsafe extern "C" fn nv_rs_alloc_reg_retain(token: u64) -> *mut c_void {
|
||||
ledger.retain_live(slots.as_mut_slice(), token) as *mut c_void
|
||||
}
|
||||
|
||||
/// A use of the allocation at `alloc_ptr`, freed by its RM object or not, for the pinned range (class 0x71) that was
|
||||
/// pinned through a user mapping of it (`AllocLedger::retain_ptr`); dropped with `nv_rs_alloc_reg_put_ptr`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_alloc_reg_retain_ptr(alloc_ptr: *mut c_void) -> bool {
|
||||
let ledger = unsafe { &*ptr::addr_of!(ALLOC_LEDGER) };
|
||||
let slots = unsafe { &mut *ptr::addr_of_mut!(ALLOC_SLOTS) };
|
||||
ledger.retain_ptr(slots.as_mut_slice(), alloc_ptr as usize)
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_alloc_reg_mark_freed_and_put(
|
||||
token: u64,
|
||||
@ -795,6 +810,127 @@ pub unsafe extern "C" fn nv_rs_sysmem_pool_release_storage() {
|
||||
SYSMEM_POOL_LEN.store(0, Ordering::Release);
|
||||
}
|
||||
|
||||
/// The `struct page *` array of one allocation in `rmapi_node.c`, whose kernel mapping
|
||||
/// `nv_rs_rmapi_os_pages_uc` / `nv_rs_rmapi_os_pages_wb` change.
|
||||
struct OsPages {
|
||||
pages: *mut c_void,
|
||||
count: u64,
|
||||
}
|
||||
|
||||
impl SysmemPageAttrs for OsPages {
|
||||
fn set_uncached(&mut self) -> bool {
|
||||
// SAFETY: `pages` is the caller's array of `count` pages, which it owns for the call.
|
||||
unsafe { nv_rs_rmapi_os_pages_uc(self.pages, self.count) }
|
||||
}
|
||||
fn set_write_back(&mut self) -> bool {
|
||||
// SAFETY: as in `set_uncached`.
|
||||
unsafe { nv_rs_rmapi_os_pages_wb(self.pages, self.count) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes the kernel mapping of a new allocation that is not cached uncached
|
||||
/// (`sysmem_make_uncached`): 0 uncached, 1 cached everywhere (a failed change was undone),
|
||||
/// -1 the pages could not be restored and the caller leaks them.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_sysmem_make_uncached(pages: *mut c_void, count: u64) -> i32 {
|
||||
match sysmem_make_uncached(&mut OsPages { pages, count }) {
|
||||
SysmemUncached::Uncached => 0,
|
||||
SysmemUncached::Cached => 1,
|
||||
SysmemUncached::Leak => -1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Before an allocation's pages go back to the pool or the page allocator
|
||||
/// (`sysmem_may_release`): `false` when they must be leaked instead.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_sysmem_may_release(
|
||||
pages: *mut c_void,
|
||||
count: u64,
|
||||
kernel_uc: bool,
|
||||
) -> bool {
|
||||
sysmem_may_release(kernel_uc, &mut OsPages { pages, count })
|
||||
}
|
||||
|
||||
/// The bytes of the range `[start, end)` being pinned for class 0x71 inside the user area `[vm_start, vm_end)`
|
||||
/// (`pinned_area_bytes`).
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_pinned_area_bytes(start: u64, end: u64, vm_start: u64, vm_end: u64) -> u64 {
|
||||
pinned_area_bytes(start, end, vm_start, vm_end)
|
||||
}
|
||||
|
||||
/// The cache type of a range pinned for class 0x71 from its length and the bytes of it inside uncached and
|
||||
/// write-combined user mappings of this node's allocations (`PinnedRangeCache::of`): `NV_MEMORY_*` 0 cached,
|
||||
/// 1 uncached, 2 write-combined; `u32::MAX` refused (NV_ERR_INVALID_ADDRESS).
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_pinned_range_cache(
|
||||
range_bytes: u64,
|
||||
uncached_bytes: u64,
|
||||
write_combined_bytes: u64,
|
||||
) -> u32 {
|
||||
PinnedRangeCache::of(range_bytes, uncached_bytes, write_combined_bytes)
|
||||
.memory_type()
|
||||
.unwrap_or(u32::MAX)
|
||||
}
|
||||
|
||||
/// One user area of a range being pinned for class 0x71, which maps the allocation `owner` (`nv_rs_rmapi_pin_scan`):
|
||||
/// adds it, and what a record pinned through it holds for it (`held[..held_count]`: `owner` itself, or the allocations
|
||||
/// a pinned record holds), to the sorted sets `owners[..*owner_count]` and `sources[..*source_count]`, each with room
|
||||
/// for `capacity` (`PinnedSources::add_area`), so the record holds each allocation once. `false`: the range would
|
||||
/// exceed that room and is refused (NV_ERR_INSUFFICIENT_RESOURCES).
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub unsafe extern "C" fn nv_rs_pinned_sources_add(
|
||||
owners: *mut *mut c_void,
|
||||
owner_count: *mut u64,
|
||||
sources: *mut *mut c_void,
|
||||
source_count: *mut u64,
|
||||
capacity: u64,
|
||||
owner: *mut c_void,
|
||||
held: *const *mut c_void,
|
||||
held_count: u64,
|
||||
) -> bool {
|
||||
if owners.is_null()
|
||||
|| owner_count.is_null()
|
||||
|| sources.is_null()
|
||||
|| source_count.is_null()
|
||||
|| (held.is_null() && held_count != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let (Ok(capacity), Ok(held_count)) = (usize::try_from(capacity), usize::try_from(held_count))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
// SAFETY: the caller gives two arrays of `capacity` pointers (pointer-sized, as `usize` is), the counts it keeps
|
||||
// for them, and `held_count` pointers at `held`, all its own for the call; the sets and `held` do not overlap.
|
||||
let (owners, sources, held, owner_count, source_count) = unsafe {
|
||||
(
|
||||
core::slice::from_raw_parts_mut(owners.cast::<usize>(), capacity),
|
||||
core::slice::from_raw_parts_mut(sources.cast::<usize>(), capacity),
|
||||
if held_count == 0 {
|
||||
&[][..]
|
||||
} else {
|
||||
core::slice::from_raw_parts(held.cast::<usize>(), held_count)
|
||||
},
|
||||
&mut *owner_count,
|
||||
&mut *source_count,
|
||||
)
|
||||
};
|
||||
let (Ok(owners_now), Ok(sources_now)) = (
|
||||
usize::try_from(*owner_count),
|
||||
usize::try_from(*source_count),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
let Some(mut set) = PinnedSources::new(owners, owners_now, sources, sources_now) else {
|
||||
return false;
|
||||
};
|
||||
let added = set.add_area(owner as usize, held).is_ok();
|
||||
*owner_count = set.owner_count() as u64;
|
||||
*source_count = set.sources().len() as u64;
|
||||
added
|
||||
}
|
||||
|
||||
static mut UVM_CPU_DMA_LEDGER: UvmCpuDmaLedger = UvmCpuDmaLedger::new();
|
||||
static mut UVM_CPU_DMA_SLOTS: KernelVec<UvmCpuDmaSlot> = KernelVec::new();
|
||||
|
||||
|
||||
@ -163,6 +163,27 @@ impl AllocLedger {
|
||||
0
|
||||
}
|
||||
|
||||
/// Retains a use of the allocation whose C pointer is `ptr`, freed by its RM object or not, for a range of user
|
||||
/// memory pinned (class 0x71) through a user mapping of it: the pinned record keeps the allocation, and with it
|
||||
/// the pages' cache attribute and their place outside the page pool, until the record is destroyed
|
||||
/// ([`AllocLedger::put_ptr`]). The caller knows the record exists because the user mapping holds its file, whose
|
||||
/// mapping context holds a use. `false` if no record has `ptr` or its count is saturated.
|
||||
pub fn retain_ptr(&self, slots: &mut [Option<AllocRecord>], ptr: usize) -> bool {
|
||||
if ptr == 0 {
|
||||
return false;
|
||||
}
|
||||
for rec in slots.iter_mut().flatten() {
|
||||
if rec.ptr == ptr {
|
||||
let Some(next) = rec.users.checked_add(1) else {
|
||||
return false;
|
||||
};
|
||||
rec.users = next;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Marks the live allocation named by `token` as `freed` and drops the RM-object
|
||||
/// reference. A second call for the same `token` is a safe no-op (`ptr = 0, destroy = false`).
|
||||
pub fn mark_freed_and_put(
|
||||
@ -379,6 +400,59 @@ mod tests {
|
||||
assert_eq!(ledger.user_pinned_pages(), 0);
|
||||
}
|
||||
|
||||
/// CodeRabbit on #388: a range pinned (class 0x71) through a user mapping of an allocation holds a use of it, so
|
||||
/// releasing the allocation's RM object and then its mapping file does not destroy it (restore write-back, pool or
|
||||
/// free its pages) while the pinned record still maps and pins them. It goes when the pinned record does.
|
||||
#[test]
|
||||
fn pinned_record_keeps_the_allocation_it_was_pinned_through() {
|
||||
let mut ledger = AllocLedger::new();
|
||||
let mut slots = [None; 4];
|
||||
let (source, pinned) = (0x1000, 0x2000);
|
||||
|
||||
// class 0x3e allocation (RM object), mapped through a file context
|
||||
let source_token = ledger.insert(&mut slots, source, 16, false).unwrap();
|
||||
assert_eq!(ledger.retain_live(&mut slots, source_token), source);
|
||||
// class 0x71 record pinned through that mapping: it holds the allocation
|
||||
let pinned_token = ledger.insert(&mut slots, pinned, 16, true).unwrap();
|
||||
assert!(ledger.retain_ptr(&mut slots, source));
|
||||
|
||||
// RM object freed, then the mapping's file released: the allocation stays
|
||||
assert!(!ledger.mark_freed_and_put(&mut slots, source_token).destroy);
|
||||
assert!(!ledger.put_ptr(&mut slots, source));
|
||||
assert_eq!(ledger.total_pages(), 32);
|
||||
assert_eq!(ledger.find_live(&slots, source_token), 0);
|
||||
|
||||
// the pinned record goes (unpin first), then it drops its use: the allocation is destroyed now
|
||||
let released = ledger.mark_freed_and_put(&mut slots, pinned_token);
|
||||
assert_eq!(
|
||||
released,
|
||||
ReleaseResult {
|
||||
ptr: pinned,
|
||||
destroy: true
|
||||
}
|
||||
);
|
||||
assert!(ledger.put_ptr(&mut slots, source));
|
||||
assert_eq!(ledger.total_pages(), 0);
|
||||
assert_eq!(ledger.user_pinned_pages(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retain_ptr_needs_a_record_and_a_free_count() {
|
||||
let mut ledger = AllocLedger::new();
|
||||
let mut slots = [None; 2];
|
||||
assert!(!ledger.retain_ptr(&mut slots, 0));
|
||||
assert!(!ledger.retain_ptr(&mut slots, 0x1000));
|
||||
ledger.insert(&mut slots, 0x1000, 1, false).unwrap();
|
||||
if let Some(rec) = slots.iter_mut().flatten().next() {
|
||||
rec.users = u32::MAX;
|
||||
}
|
||||
assert!(!ledger.retain_ptr(&mut slots, 0x1000));
|
||||
assert_eq!(
|
||||
slots.iter().flatten().next().map(|rec| rec.users),
|
||||
Some(u32::MAX)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn budget_and_drain_enforce_limits() {
|
||||
let mut ledger = AllocLedger::new();
|
||||
@ -1146,6 +1220,584 @@ impl SysmemPagePool {
|
||||
}
|
||||
}
|
||||
|
||||
/// The two attribute changes `rmapi_node.c` makes to the kernel mapping of one allocation's pages
|
||||
/// (`set_pages_array_uc` / `set_pages_array_wb` on x86). Each returns `false` when it failed.
|
||||
pub trait SysmemPageAttrs {
|
||||
fn set_uncached(&mut self) -> bool;
|
||||
fn set_write_back(&mut self) -> bool;
|
||||
}
|
||||
|
||||
/// What became of the kernel mapping of a new allocation RM does not want cached.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SysmemUncached {
|
||||
/// The kernel mapping is uncached; user and kernel mappings carry the allocation's cache type.
|
||||
Uncached,
|
||||
/// The change failed and was undone: the allocation stays cached everywhere.
|
||||
Cached,
|
||||
/// The change failed and could not be undone, so part of the pages may still be uncached:
|
||||
/// they are never mapped cached, pooled or freed.
|
||||
Leak,
|
||||
}
|
||||
|
||||
/// `nv_set_memory_type(at, NV_MEMORY_UNCACHED)`, which the original's `nv_alloc_system_pages`
|
||||
/// applies to every allocation that is not `NV_MEMORY_CACHED` (580.178.04 nv-vm.c:583-584,
|
||||
/// nv-vm.c:106-128). The original ignores the result. Here a failure is undone, because
|
||||
/// `set_pages_array_uc` can fail after it changed the attribute of some pages (it releases only
|
||||
/// the memtype reservations), and a cached user mapping must not alias an uncached page.
|
||||
pub fn sysmem_make_uncached(attrs: &mut impl SysmemPageAttrs) -> SysmemUncached {
|
||||
if attrs.set_uncached() {
|
||||
SysmemUncached::Uncached
|
||||
} else if attrs.set_write_back() {
|
||||
SysmemUncached::Cached
|
||||
} else {
|
||||
SysmemUncached::Leak
|
||||
}
|
||||
}
|
||||
|
||||
/// `nv_set_memory_type(at, NV_MEMORY_WRITEBACK)` before the original frees the pages of an
|
||||
/// allocation that is not cached (580.178.04 nv-vm.c:619-620). `true`: the pages may go back to
|
||||
/// the page pool or the page allocator. `false`: their attribute could not be restored and they
|
||||
/// are leaked.
|
||||
pub fn sysmem_may_release(kernel_uc: bool, attrs: &mut impl SysmemPageAttrs) -> bool {
|
||||
!kernel_uc || attrs.set_write_back()
|
||||
}
|
||||
|
||||
/// The memory type of the kernel and user mappings of a range of user memory pinned for class 0x71
|
||||
/// (`nv_rs_rmapi_os_lock_user_pages`), from the user mappings of this driver's system memory the range lies in.
|
||||
///
|
||||
/// The original pins the range (os_lock_user_pages, 580.178.04 os-mlock.c:216-309) without looking at what maps it,
|
||||
/// and gives every pinned range `NV_MEMORY_UNCACHED` (nv_register_user_pages, nv.c:3390), so its kernel mapping is
|
||||
/// `PAGE_KERNEL_NOCACHE` (nv_alloc_kernel_mapping, nv.c:3762-3763) even over write-back pages. Here a range gets the
|
||||
/// memory type of the user mappings it was pinned through, and a range whose pages are not all mapped with one type
|
||||
/// is refused.
|
||||
///
|
||||
/// The pages' attribute in the kernel's linear mapping stays their allocation's: uncached (UC-) for an allocation of
|
||||
/// the uncached or the write-combined type, as the original's `nv_set_memory_type(at, NV_MEMORY_UNCACHED)` makes it
|
||||
/// for every type but `NV_MEMORY_CACHED` (580.178.04 nv-vm.c:583-584; 620.73.04 nv-vm.c:1056-1058). The pinned record
|
||||
/// changes only its own mappings.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PinnedRangeCache {
|
||||
/// No byte lies in a user mapping of an allocation whose kernel mapping was made uncached: cached mappings.
|
||||
Cached,
|
||||
/// Every byte lies in uncached (UC-) user mappings: uncached kernel and user mappings.
|
||||
Uncached,
|
||||
/// Every byte lies in write-combined user mappings: write-combined kernel and user mappings, the type of the user
|
||||
/// mappings the range was pinned through.
|
||||
WriteCombined,
|
||||
/// Any other mix of write-back, uncached and write-combined memory: one mapping type for the range would alias
|
||||
/// some of its pages with a second type. The import is refused with NV_ERR_INVALID_ADDRESS, the status of a range
|
||||
/// os_lock_user_pages cannot pin (os-mlock.c:298-304).
|
||||
Mixed,
|
||||
}
|
||||
|
||||
impl PinnedRangeCache {
|
||||
/// From the length of the pinned range and the bytes of it inside uncached (`uncached_bytes`) and write-combined
|
||||
/// (`write_combined_bytes`) user mappings of this driver's allocations ([`pinned_area_bytes`] summed over the
|
||||
/// areas of the range, which do not overlap).
|
||||
pub const fn of(range_bytes: u64, uncached_bytes: u64, write_combined_bytes: u64) -> Self {
|
||||
match (uncached_bytes, write_combined_bytes) {
|
||||
(0, 0) => Self::Cached,
|
||||
(uncached, 0) if uncached == range_bytes => Self::Uncached,
|
||||
(0, write_combined) if write_combined == range_bytes => Self::WriteCombined,
|
||||
_ => Self::Mixed,
|
||||
}
|
||||
}
|
||||
|
||||
/// The `NV_MEMORY_*` cache type the pinned record keeps (`os/nv_memory_type.h:30-32`: `CACHED` 0, `UNCACHED` 1,
|
||||
/// `WRITECOMBINED` 2); `None` for a refused range.
|
||||
pub const fn memory_type(self) -> Option<u32> {
|
||||
match self {
|
||||
Self::Cached => Some(0),
|
||||
Self::Uncached => Some(1),
|
||||
Self::WriteCombined => Some(2),
|
||||
Self::Mixed => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The bytes of the pinned range `[start, end)` inside the user area `[vm_start, vm_end)`.
|
||||
pub const fn pinned_area_bytes(start: u64, end: u64, vm_start: u64, vm_end: u64) -> u64 {
|
||||
let low = if start > vm_start { start } else { vm_start };
|
||||
let high = if end < vm_end { end } else { vm_end };
|
||||
high.saturating_sub(low)
|
||||
}
|
||||
|
||||
/// The allocations of this driver a range of user memory pinned for class 0x71 holds (`nv_rs_rmapi_os_lock_user_pages`),
|
||||
/// gathered from the user areas of the range that map one (`nv_rs_rmapi_pin_scan`). The record takes one use of each
|
||||
/// and drops it when it is destroyed.
|
||||
///
|
||||
/// The original keeps no such list: its record holds only the page references (os_lock_user_pages, 580.178.04
|
||||
/// os-mlock.c:216-309; 620.73.04 os-mlock.c:231-322). Here the record holds the allocations it was pinned through, so
|
||||
/// none restores write-back, pools or frees its pages while the record maps them, and it holds each of them once:
|
||||
///
|
||||
/// - an allocation whose user mapping is split into several areas (`mprotect` of part of it, a second `mmap` of it
|
||||
/// next to it) counts once, however many of its areas the range covers;
|
||||
/// - a user mapping of pinned user pages counts for the allocations that record holds, never for the record itself, so
|
||||
/// a record never holds another record and teardown never recurses. An allocation reached through several such
|
||||
/// mappings, or through one of them and a mapping of its own, counts once.
|
||||
///
|
||||
/// Counting every area of every mapping instead held an allocation once per area: a range across two areas of one
|
||||
/// mapping held it twice, and a record pinned through two areas of a mapping of that record held it four times, the
|
||||
/// list doubling with every level while the pinned pages stayed few.
|
||||
///
|
||||
/// Both sets are sorted and live in the caller's storage, whose length is the bound: a range that lies in user mappings
|
||||
/// of more allocations than that, or would hold more than that, is refused ([`PinnedSourcesFull`]). The list a record
|
||||
/// keeps, the uses it takes and the work to gather them stay bounded by it.
|
||||
pub struct PinnedSources<'a> {
|
||||
/// The allocations whose user mappings the range lies in, pinned records included: an allocation seen again is
|
||||
/// not looked at twice.
|
||||
owners: &'a mut [usize],
|
||||
owner_count: usize,
|
||||
/// What the record holds: allocations, never pinned records.
|
||||
sources: &'a mut [usize],
|
||||
source_count: usize,
|
||||
}
|
||||
|
||||
/// A pinned range would lie in user mappings of more allocations, or hold more, than [`PinnedSources`] has room for.
|
||||
/// `nv_rs_rmapi_os_lock_user_pages` refuses it with NV_ERR_INSUFFICIENT_RESOURCES (0x1a, "Ran out of a critical
|
||||
/// resource, other than memory", nvstatuscodes.h:55 in 580.178.04 and 620.73.04).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct PinnedSourcesFull;
|
||||
|
||||
impl<'a> PinnedSources<'a> {
|
||||
/// The sets `owners[..owner_count]` and `sources[..source_count]` as earlier calls left them (both empty at
|
||||
/// first). `None` if a count exceeds its storage.
|
||||
pub fn new(
|
||||
owners: &'a mut [usize],
|
||||
owner_count: usize,
|
||||
sources: &'a mut [usize],
|
||||
source_count: usize,
|
||||
) -> Option<Self> {
|
||||
if owner_count > owners.len() || source_count > sources.len() {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
owners,
|
||||
owner_count,
|
||||
sources,
|
||||
source_count,
|
||||
})
|
||||
}
|
||||
|
||||
/// One user area of the range, which maps the allocation `owner`. `held`: what a record pinned through the area
|
||||
/// holds for it, `[owner]` for an allocation, or the allocations a pinned record holds for a mapping of that
|
||||
/// record. An owner already seen adds nothing.
|
||||
pub fn add_area(&mut self, owner: usize, held: &[usize]) -> Result<(), PinnedSourcesFull> {
|
||||
if !sorted_insert(self.owners, &mut self.owner_count, owner)? {
|
||||
return Ok(());
|
||||
}
|
||||
for &source in held {
|
||||
sorted_insert(self.sources, &mut self.source_count, source)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// How many allocations the range lies in user mappings of.
|
||||
pub fn owner_count(&self) -> usize {
|
||||
self.owner_count
|
||||
}
|
||||
|
||||
/// What the record holds, each allocation once.
|
||||
pub fn sources(&self) -> &[usize] {
|
||||
&self.sources[..self.source_count]
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts `value` into the sorted set `set[..*len]`: `Ok(true)` if it was added, `Ok(false)` if it was there already.
|
||||
fn sorted_insert(
|
||||
set: &mut [usize],
|
||||
len: &mut usize,
|
||||
value: usize,
|
||||
) -> Result<bool, PinnedSourcesFull> {
|
||||
let position = match set[..*len].binary_search(&value) {
|
||||
Ok(_) => return Ok(false),
|
||||
Err(position) => position,
|
||||
};
|
||||
if *len == set.len() {
|
||||
return Err(PinnedSourcesFull);
|
||||
}
|
||||
let mut index = *len;
|
||||
while index > position {
|
||||
// `black_box` keeps LLVM from turning the shift into a `memmove` call (tools/check_kernel_module.py).
|
||||
set[index] = core::hint::black_box(set[index - 1]);
|
||||
index -= 1;
|
||||
}
|
||||
set[position] = value;
|
||||
*len += 1;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pinned_range_tests {
|
||||
use super::*;
|
||||
|
||||
const PAGE: u64 = 4096;
|
||||
|
||||
/// What a user area of the range maps: memory that is no user mapping of an allocation of this node with an
|
||||
/// uncached kernel mapping (anonymous memory, a cached allocation), or an uncached or write-combined user mapping
|
||||
/// of one.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Area {
|
||||
WriteBack,
|
||||
Uncached,
|
||||
WriteCombined,
|
||||
}
|
||||
use Area::{Uncached as Uc, WriteBack as Wb, WriteCombined as Wc};
|
||||
|
||||
/// The decision for the range of `pages` from `start`, over `areas` laid end to end from `start`, each
|
||||
/// (pages, what it maps), summed per type as `nv_rs_rmapi_pin_scan` sums them.
|
||||
fn decide(start: u64, pages: u64, areas: &[(u64, Area)]) -> PinnedRangeCache {
|
||||
let end = start + pages * PAGE;
|
||||
let (mut vm_start, mut uncached, mut write_combined) = (start, 0, 0);
|
||||
for &(area_pages, area) in areas {
|
||||
let vm_end = vm_start + area_pages * PAGE;
|
||||
let bytes = pinned_area_bytes(start, end, vm_start, vm_end);
|
||||
match area {
|
||||
Wb => {}
|
||||
Uc => uncached += bytes,
|
||||
Wc => write_combined += bytes,
|
||||
}
|
||||
vm_start = vm_end;
|
||||
}
|
||||
PinnedRangeCache::of(pages * PAGE, uncached, write_combined)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_inside_one_uncached_mapping_is_mapped_uncached() {
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 16, &[(16, Uc)]),
|
||||
PinnedRangeCache::Uncached
|
||||
);
|
||||
// a range inside a larger mapping counts only its own pages
|
||||
assert_eq!(
|
||||
pinned_area_bytes(
|
||||
0x10_0000 + PAGE,
|
||||
0x10_0000 + 3 * PAGE,
|
||||
0x10_0000,
|
||||
0x10_0000 + 16 * PAGE
|
||||
),
|
||||
2 * PAGE
|
||||
);
|
||||
// two uncached allocations side by side
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 32, &[(16, Uc), (16, Uc)]),
|
||||
PinnedRangeCache::Uncached
|
||||
);
|
||||
}
|
||||
|
||||
/// CodeRabbit on #388: a range pinned through write-combined user mappings gets write-combined mappings, the type
|
||||
/// of those source mappings, not uncached ones.
|
||||
#[test]
|
||||
fn range_inside_write_combined_mappings_is_mapped_write_combined() {
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 16, &[(16, Wc)]),
|
||||
PinnedRangeCache::WriteCombined
|
||||
);
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 1, &[(1, Wc)]),
|
||||
PinnedRangeCache::WriteCombined
|
||||
);
|
||||
// two write-combined allocations side by side
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 32, &[(16, Wc), (16, Wc)]),
|
||||
PinnedRangeCache::WriteCombined
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_of_write_back_memory_stays_cached() {
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 16, &[(8, Wb), (8, Wb)]),
|
||||
PinnedRangeCache::Cached
|
||||
);
|
||||
}
|
||||
|
||||
/// CodeRabbit on #388: a range across an uncached mapping and ordinary write-back memory must not get one
|
||||
/// mapping type for the whole range. Either order, one page of overlap or many.
|
||||
#[test]
|
||||
fn range_across_uncached_and_write_back_memory_is_refused() {
|
||||
for areas in [
|
||||
&[(16, Wb), (16, Uc)][..],
|
||||
&[(16, Uc), (16, Wb)][..],
|
||||
&[(31, Wb), (1, Uc)][..],
|
||||
&[(1, Uc), (30, Wb), (1, Uc)][..],
|
||||
] {
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 32, areas),
|
||||
PinnedRangeCache::Mixed,
|
||||
"{areas:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// CodeRabbit on #388: write-combined memory shares a pinned range with neither write-back nor uncached memory.
|
||||
/// Either order, one page of overlap or many, and all three types in one range.
|
||||
#[test]
|
||||
fn range_across_write_combined_and_other_memory_is_refused() {
|
||||
for areas in [
|
||||
&[(16, Wb), (16, Wc)][..],
|
||||
&[(16, Wc), (16, Wb)][..],
|
||||
&[(31, Wb), (1, Wc)][..],
|
||||
&[(16, Uc), (16, Wc)][..],
|
||||
&[(16, Wc), (16, Uc)][..],
|
||||
&[(31, Wc), (1, Uc)][..],
|
||||
&[(1, Uc), (30, Wc), (1, Uc)][..],
|
||||
&[(10, Wb), (11, Uc), (11, Wc)][..],
|
||||
] {
|
||||
assert_eq!(
|
||||
decide(0x10_0000, 32, areas),
|
||||
PinnedRangeCache::Mixed,
|
||||
"{areas:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn areas_outside_the_range_do_not_count() {
|
||||
let (start, end) = (0x10_0000, 0x10_0000 + 4 * PAGE);
|
||||
assert_eq!(pinned_area_bytes(start, end, 0, start), 0);
|
||||
assert_eq!(pinned_area_bytes(start, end, end, end + PAGE), 0);
|
||||
assert_eq!(
|
||||
pinned_area_bytes(start, end, start - PAGE, start + PAGE),
|
||||
PAGE
|
||||
);
|
||||
assert_eq!(pinned_area_bytes(start, end, end - PAGE, u64::MAX), PAGE);
|
||||
// an area of the other type just past the range does not make it mixed
|
||||
assert_eq!(
|
||||
decide(start, 4, &[(4, Uc), (4, Wc)]),
|
||||
PinnedRangeCache::Uncached
|
||||
);
|
||||
assert_eq!(
|
||||
decide(start, 4, &[(4, Wc), (4, Uc)]),
|
||||
PinnedRangeCache::WriteCombined
|
||||
);
|
||||
// more bytes of one type than the range has cannot be mapped consistently either
|
||||
assert_eq!(
|
||||
PinnedRangeCache::of(4 * PAGE, 5 * PAGE, 0),
|
||||
PinnedRangeCache::Mixed
|
||||
);
|
||||
assert_eq!(
|
||||
PinnedRangeCache::of(4 * PAGE, 0, 5 * PAGE),
|
||||
PinnedRangeCache::Mixed
|
||||
);
|
||||
}
|
||||
|
||||
/// The cache type the record keeps and `nv_rs_rmapi_os_lock_user_pages` maps with: the `NV_MEMORY_*` values.
|
||||
#[test]
|
||||
fn memory_types_are_the_nv_memory_values() {
|
||||
assert_eq!(PinnedRangeCache::Cached.memory_type(), Some(0));
|
||||
assert_eq!(PinnedRangeCache::Uncached.memory_type(), Some(1));
|
||||
assert_eq!(PinnedRangeCache::WriteCombined.memory_type(), Some(2));
|
||||
assert_eq!(PinnedRangeCache::Mixed.memory_type(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pinned_sources_tests {
|
||||
use super::*;
|
||||
extern crate std;
|
||||
use std::vec::Vec;
|
||||
|
||||
/// What `nv_rs_rmapi_pin_scan` gathers for a range over `areas`, each (the allocation the area maps, what a record
|
||||
/// pinned through it holds), with one `nv_rs_pinned_sources_add` call per area as the C side makes them: a new
|
||||
/// [`PinnedSources`] over the same storage each time. Room for `ROOM` allocations in each set. Returns the sources
|
||||
/// storage, how many it holds and how many allocations the range lies in mappings of.
|
||||
fn gather<const ROOM: usize>(
|
||||
areas: &[(usize, &[usize])],
|
||||
) -> Result<([usize; ROOM], usize, usize), PinnedSourcesFull> {
|
||||
let (mut owners, mut sources) = ([0usize; ROOM], [0usize; ROOM]);
|
||||
let (mut owner_count, mut source_count) = (0, 0);
|
||||
for &(owner, held) in areas {
|
||||
let mut set =
|
||||
PinnedSources::new(&mut owners, owner_count, &mut sources, source_count).unwrap();
|
||||
set.add_area(owner, held)?;
|
||||
owner_count = set.owner_count();
|
||||
source_count = set.sources().len();
|
||||
}
|
||||
Ok((sources, source_count, owner_count))
|
||||
}
|
||||
|
||||
/// CodeRabbit on #388: an allocation whose user mapping was split in two areas (mprotect of half of it), pinned
|
||||
/// across both, is held once; a user mapping of that record split the same way and pinned across both areas again
|
||||
/// holds it once, at any depth. Counting every area held it 2, 4, ... 2^depth times.
|
||||
#[test]
|
||||
fn split_mapping_pinned_twice_holds_each_allocation_once() {
|
||||
const A: usize = 0xffff_8880_0001_0000;
|
||||
let (sources, count, owners) = gather::<4>(&[(A, &[A]), (A, &[A])]).unwrap();
|
||||
assert_eq!((&sources[..count], owners), (&[A][..], 1));
|
||||
// each level: a record pinned through two areas of a mapping of the record before it
|
||||
let mut held = [A];
|
||||
for depth in 1..=40usize {
|
||||
let record = 0xffff_8880_1000_0000 + depth * 0x1000;
|
||||
let (sources, count, owners) =
|
||||
gather::<4>(&[(record, &held[..]), (record, &held[..])]).unwrap();
|
||||
assert_eq!((&sources[..count], owners), (&[A][..], 1), "depth {depth}");
|
||||
held = [sources[0]];
|
||||
}
|
||||
}
|
||||
|
||||
/// Two records holding the same allocation, mapped side by side, and the allocation's own mapping between their
|
||||
/// areas: each allocation once, whatever the order of the areas.
|
||||
#[test]
|
||||
fn allocation_reached_through_several_mappings_is_held_once() {
|
||||
const A: usize = 0x1000;
|
||||
const B: usize = 0x2000;
|
||||
const C: usize = 0x3000;
|
||||
const R1: usize = 0x10_0000;
|
||||
const R2: usize = 0x20_0000;
|
||||
let (sources, count, owners) = gather::<8>(&[
|
||||
(R1, &[A, C]),
|
||||
(B, &[B]),
|
||||
(R2, &[C, A, B]),
|
||||
(A, &[A]),
|
||||
(R1, &[A, C]),
|
||||
(B, &[B]),
|
||||
])
|
||||
.unwrap();
|
||||
// sorted, each once; four allocations mapped (A, B, R1, R2)
|
||||
assert_eq!((&sources[..count], owners), (&[A, B, C][..], 4));
|
||||
}
|
||||
|
||||
/// A record pinned through anonymous memory holds nothing; a range pinned through a mapping of it holds nothing
|
||||
/// either, as before.
|
||||
#[test]
|
||||
fn mapping_of_record_holding_nothing_adds_nothing() {
|
||||
let (_, count, owners) = gather::<4>(&[(0x10_0000, &[]), (0x10_0000, &[])]).unwrap();
|
||||
assert_eq!((count, owners), (0, 1));
|
||||
}
|
||||
|
||||
/// The bound: a range that would hold more allocations than there is room for, or lies in mappings of more
|
||||
/// allocations than that, is refused; one that fills the room exactly is not, and an allocation already held
|
||||
/// still fits a full set.
|
||||
#[test]
|
||||
fn range_past_the_bound_is_refused() {
|
||||
let four: [(usize, &[usize]); 4] = [
|
||||
(0x1000, &[0x1000]),
|
||||
(0x2000, &[0x2000]),
|
||||
(0x3000, &[0x3000]),
|
||||
(0x4000, &[0x4000]),
|
||||
];
|
||||
let (sources, count, _) = gather::<4>(&four).unwrap();
|
||||
assert_eq!(&sources[..count], &[0x1000, 0x2000, 0x3000, 0x4000]);
|
||||
let mut again: Vec<(usize, &[usize])> = four.to_vec();
|
||||
again.push((0x2000, &[0x2000]));
|
||||
assert!(gather::<4>(&again).is_ok());
|
||||
again.push((0x5000, &[0x5000]));
|
||||
assert_eq!(gather::<4>(&again), Err(PinnedSourcesFull));
|
||||
// one record holding more than the room
|
||||
assert_eq!(
|
||||
gather::<4>(&[(0x10_0000, &[1, 2, 3, 4, 5])]),
|
||||
Err(PinnedSourcesFull)
|
||||
);
|
||||
// mappings of more records than the room, all holding the same allocation
|
||||
let records: [(usize, &[usize]); 5] = [
|
||||
(0x10_0000, &[0x1000]),
|
||||
(0x20_0000, &[0x1000]),
|
||||
(0x30_0000, &[0x1000]),
|
||||
(0x40_0000, &[0x1000]),
|
||||
(0x50_0000, &[0x1000]),
|
||||
];
|
||||
assert!(gather::<4>(&records[..4]).is_ok());
|
||||
assert_eq!(gather::<4>(&records), Err(PinnedSourcesFull));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counts_past_the_storage_are_rejected() {
|
||||
let (mut owners, mut sources) = ([0usize; 2], [0usize; 2]);
|
||||
assert!(PinnedSources::new(&mut owners, 3, &mut sources, 0).is_none());
|
||||
assert!(PinnedSources::new(&mut owners, 0, &mut sources, 3).is_none());
|
||||
assert!(PinnedSources::new(&mut owners, 2, &mut sources, 2).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sysmem_memtype_tests {
|
||||
use super::*;
|
||||
|
||||
/// Page attributes as the x86 calls leave them: `set_uncached` may fail after it changed
|
||||
/// some pages, which then stay uncached until a successful `set_write_back`.
|
||||
struct Pages {
|
||||
uncached: [bool; 4],
|
||||
uc_fails_after: Option<usize>,
|
||||
wb_fails: bool,
|
||||
uc_calls: u32,
|
||||
wb_calls: u32,
|
||||
}
|
||||
|
||||
impl Pages {
|
||||
fn new(uc_fails_after: Option<usize>, wb_fails: bool) -> Self {
|
||||
Self {
|
||||
uncached: [false; 4],
|
||||
uc_fails_after,
|
||||
wb_fails,
|
||||
uc_calls: 0,
|
||||
wb_calls: 0,
|
||||
}
|
||||
}
|
||||
fn any_uncached(&self) -> bool {
|
||||
self.uncached.iter().any(|&page| page)
|
||||
}
|
||||
}
|
||||
|
||||
impl SysmemPageAttrs for Pages {
|
||||
fn set_uncached(&mut self) -> bool {
|
||||
self.uc_calls += 1;
|
||||
let changed = self.uc_fails_after.unwrap_or(self.uncached.len());
|
||||
for page in self.uncached.iter_mut().take(changed) {
|
||||
*page = true;
|
||||
}
|
||||
self.uc_fails_after.is_none()
|
||||
}
|
||||
fn set_write_back(&mut self) -> bool {
|
||||
self.wb_calls += 1;
|
||||
if self.wb_fails {
|
||||
return false;
|
||||
}
|
||||
self.uncached = [false; 4];
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conversion_that_succeeds_is_kept() {
|
||||
let mut pages = Pages::new(None, false);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Uncached);
|
||||
assert_eq!((pages.uc_calls, pages.wb_calls), (1, 0));
|
||||
assert!(pages.uncached.iter().all(|&page| page));
|
||||
// free: write-back first, then the pages may be released
|
||||
assert!(sysmem_may_release(true, &mut pages));
|
||||
assert_eq!(pages.wb_calls, 1);
|
||||
assert!(!pages.any_uncached());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_conversion_is_undone_before_the_cached_fallback() {
|
||||
// two of four pages changed before the failure
|
||||
let mut pages = Pages::new(Some(2), false);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Cached);
|
||||
assert_eq!((pages.uc_calls, pages.wb_calls), (1, 1));
|
||||
assert!(!pages.any_uncached());
|
||||
// a cached allocation is released without another attribute change
|
||||
assert!(sysmem_may_release(false, &mut pages));
|
||||
assert_eq!(pages.wb_calls, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_conversion_that_cannot_be_undone_leaks_the_pages() {
|
||||
let mut pages = Pages::new(Some(2), true);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Leak);
|
||||
assert!(pages.any_uncached());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pages_whose_write_back_fails_on_free_are_leaked() {
|
||||
let mut pages = Pages::new(None, true);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Uncached);
|
||||
assert!(!sysmem_may_release(true, &mut pages));
|
||||
assert!(pages.any_uncached());
|
||||
}
|
||||
}
|
||||
|
||||
/// Number of hash buckets in [`UvmCpuDmaLedger`], matching the 10-bit hash table
|
||||
/// (`DEFINE_HASHTABLE(nv_rs_uvm_cpu_dma_ht, 10)`) previously maintained in C.
|
||||
pub const UVM_DMA_BUCKETS: usize = 1024;
|
||||
|
||||
@ -7,9 +7,13 @@
|
||||
#include <linux/jiffies.h>
|
||||
#include <linux/iommu.h>
|
||||
#include <linux/mm.h>
|
||||
#include <linux/mman.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/kthread.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/file.h>
|
||||
#include <linux/fdtable.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include "rmapi_node.h"
|
||||
#include "pci_adapter.h"
|
||||
@ -361,6 +365,275 @@ static int rmapi_read32(void *cookie,u64 offset,u32 *value)
|
||||
if(offset==0x20400){*value=30<<8;return 0;}
|
||||
return -ERANGE;
|
||||
}
|
||||
/* System memory of the RM API (rmapi_node.c) DMA-mapped for the listed GPU: an allocation of more than one page is
|
||||
* mapped in one scatterlist, as the original does (nv_dma_map_scatterlist), so behind a translating IOMMU its pages
|
||||
* get one IOVA range, in order; without one each page's DMA address is its own physical address. An allocation that
|
||||
* is not cached (cache type 1 or 2) has an uncached (UC-) kernel mapping until it is freed (nv_set_memory_type). */
|
||||
u32 nv_rs_rmapi_os_alloc_pages(u64 count,u64 page,bool contiguous,u32 cache,u64 *token);
|
||||
void nv_rs_rmapi_os_free_pages(u64 token);
|
||||
u32 nv_rs_rmapi_os_page_phys(u64 token,u64 page_index,u64 *phys);
|
||||
u32 nv_rs_rmapi_os_page_dma(u32 gpu_id,u64 token,u64 page_index,u64 *dma_out);
|
||||
u32 nv_rs_rmapi_os_kernel_write(u64 token,u64 offset,const u8 *bytes,size_t length);
|
||||
/* The cache mode of the kernel page-table entry of `address` (the linear map or a vmap), read as rom_fw_adapter.c
|
||||
* verify_pages does. */
|
||||
static bool rmapi_pte_cache_is(unsigned long address,enum page_cache_mode mode)
|
||||
{
|
||||
unsigned int level;pteval_t value;unsigned long expected=cachemode2protval(mode)&_PAGE_CACHE_MASK;
|
||||
pte_t *entry=lookup_address(address,&level);
|
||||
if(!entry)return false;
|
||||
value=pte_val(*entry);
|
||||
if(level>PG_LEVEL_4K)value=protval_large_2_4k(value);
|
||||
return (value&_PAGE_CACHE_MASK)==expected;
|
||||
}
|
||||
/* The cache mode of the kernel's linear mapping of the page at `phys`. */
|
||||
static bool rmapi_page_cache_is(u64 phys,bool uncached)
|
||||
{
|
||||
return rmapi_pte_cache_is((unsigned long)page_address(pfn_to_page(phys>>PAGE_SHIFT)),uncached?_PAGE_CACHE_MODE_UC_MINUS:_PAGE_CACHE_MODE_WB);
|
||||
}
|
||||
static int rmapi_sysmem_check(struct pci_dev *pdev,u32 cache)
|
||||
{
|
||||
struct iommu_domain *domain=iommu_get_domain_for_dev(&pdev->dev);
|
||||
bool translated=domain&&(domain->type==IOMMU_DOMAIN_DMA||domain->type==IOMMU_DOMAIN_DMA_FQ);
|
||||
u32 gpu_id=((u32)(pci_domain_nr(pdev->bus)&0xffff)<<16)|((u32)pdev->bus->number<<8)|PCI_SLOT(pdev->devfn);
|
||||
u64 token=0,first=0,dma=0,phys[16]={0};unsigned int i;int e=0;u8 byte=0x5a;
|
||||
if(nv_rs_rmapi_os_alloc_pages(16,PAGE_SIZE,false,cache,&token))e=-ENOMEM;
|
||||
/* the kernel mapping of the whole allocation (vmap; PAGE_KERNEL_NOCACHE when it is not cached) */
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(token,15*PAGE_SIZE+7,&byte,1))e=-EIO;
|
||||
for(i=0;i<16&&!e;i++){
|
||||
if(nv_rs_rmapi_os_page_dma(gpu_id,token,i,&dma)||nv_rs_rmapi_os_page_phys(token,i,&phys[i]))e=-EIO;
|
||||
else if(!i)first=dma;
|
||||
if(!e&&(translated?dma!=first+(u64)i*PAGE_SIZE:dma!=phys[i]))e=-ERANGE;
|
||||
if(!e&&!rmapi_page_cache_is(phys[i],cache!=0))e=-EPROTO;
|
||||
}
|
||||
if(token)nv_rs_rmapi_os_free_pages(token);
|
||||
/* freed pages are write-back again (only the direct map's entry is read) */
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],false))e=-EPROTO;
|
||||
if(e)pr_err("NV_RS_VM_FAIL rmapi system memory (cache type %u): %d\n",cache,e);
|
||||
return e;
|
||||
}
|
||||
/* Pinned user pages (class 0x71) of anonymous memory, pinned under the mmap lock as os_lock_user_pages does: the
|
||||
* kernel mapping of the pinned range writes the process's own pages; a range that is not mapped is refused with
|
||||
* NV_ERR_INVALID_ADDRESS (0x1e). Module init runs in the insmod process, whose address space this borrows. */
|
||||
u32 nv_rs_rmapi_os_lock_user_pages(u64 address,u64 count,bool writable,u64 *token);
|
||||
void nv_rs_rmapi_os_unlock_user_pages(u64 token);
|
||||
static int rmapi_user_pages_check(void)
|
||||
{
|
||||
unsigned long user=vm_mmap(NULL,0,16*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,0);
|
||||
u64 token=0;u8 byte=0x3c;int e=0;
|
||||
if(IS_ERR_VALUE(user)){pr_err("NV_RS_VM_FAIL rmapi pinned user pages: %ld\n",(long)user);return (int)(long)user;}
|
||||
if(nv_rs_rmapi_os_lock_user_pages(user,16,true,&token))e=-ENOMEM;
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(token,9*PAGE_SIZE+1,&byte,1))e=-EIO;
|
||||
byte=0;
|
||||
if(!e&&(copy_from_user(&byte,(const void __user *)(user+9*PAGE_SIZE+1),1)||byte!=0x3c))e=-EPROTO;
|
||||
if(token)nv_rs_rmapi_os_unlock_user_pages(token);
|
||||
token=0;
|
||||
vm_munmap(user,16*PAGE_SIZE);
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(user,16,true,&token)!=0x1e)e=-EFAULT;
|
||||
if(token)nv_rs_rmapi_os_unlock_user_pages(token);
|
||||
if(e)pr_err("NV_RS_VM_FAIL rmapi pinned user pages: %d\n",e);
|
||||
return e;
|
||||
}
|
||||
/* Pinned user pages through user mappings of this node's system memory, for a source allocation of cache type `cache`
|
||||
* (1 uncached, 2 write-combined). The source is mapped from the control node into the insmod process, as a client
|
||||
* maps one (mapping context, then mmap), over pages 16-31 of a 48-page anonymous mapping, and an allocation of the
|
||||
* other of the two types over pages 32-47:
|
||||
* - both allocations have a UC- linear map (set_pages_array_uc) and the source's own kernel mapping (vmap) is UC
|
||||
* (PAGE_KERNEL_NOCACHE) for either type, as the original's (nv_set_memory_type(at, NV_MEMORY_UNCACHED), nv_vmap);
|
||||
* each user mapping has its allocation's type: UC- (pgprot_noncached) or WC (nv_encode_caching);
|
||||
* - ranges across the anonymous memory and the source, and across the source and the other allocation, are refused
|
||||
* with NV_ERR_INVALID_ADDRESS (0x1e): one mapping of them would alias some of their pages with a second type;
|
||||
* - the source's range is pinned. A user mapping of the record has the source user mapping's type; the record's
|
||||
* kernel mapping is WC for a write-combined source and UC (PAGE_KERNEL_NOCACHE) for an uncached one. A byte written
|
||||
* through the kernel mapping is read back through both user mappings, and a range pinned through the record's user
|
||||
* mapping gets the same types;
|
||||
* - with the RM objects freed, the mappings removed and their files released, the pinned record still holds the
|
||||
* source: its pages stay UC- in the linear map, and become write-back only when the record goes. The other
|
||||
* allocation, which no refused range kept, is write-back as soon as its file is released. */
|
||||
u32 nv_rs_rmapi_os_add_context(s32 descriptor,u32 protection,u64 token,u64 page_index,u32 caching);
|
||||
u32 nv_rs_rmapi_os_kernel_map(u64 token,u64 size);
|
||||
void *nv_rs_rmapi_os_kernel_address(u64 token);
|
||||
/* The cache mode of the user area at `address` of the insmod process: the protection nv_rs_rmapi_mmap gave the area,
|
||||
* which vm_insert_page maps every page of it with. */
|
||||
static bool rmapi_user_cache_is(unsigned long address,enum page_cache_mode mode)
|
||||
{
|
||||
struct vm_area_struct *vma;bool same=false;
|
||||
mmap_read_lock(current->mm);
|
||||
vma=find_vma(current->mm,address);
|
||||
if(vma&&vma->vm_start<=address)same=(pgprot_val(vma->vm_page_prot)&_PAGE_CACHE_MASK)==(cachemode2protval(mode)&_PAGE_CACHE_MASK);
|
||||
mmap_read_unlock(current->mm);
|
||||
return same;
|
||||
}
|
||||
/* Every page of the kernel mapping of `token` has `mode`. */
|
||||
static bool rmapi_kernel_map_cache_is(u64 token,enum page_cache_mode mode)
|
||||
{
|
||||
unsigned long kernel=(unsigned long)nv_rs_rmapi_os_kernel_address(token);unsigned int i;
|
||||
for(i=0;i<16;i++)if(!kernel||!rmapi_pte_cache_is(kernel+i*PAGE_SIZE,mode))return false;
|
||||
return true;
|
||||
}
|
||||
/* A file of the control node and a descriptor of it in the insmod process, which holds a reference of its own. */
|
||||
static int rmapi_ctl_open(struct file **node,int *fd)
|
||||
{
|
||||
struct file *opened=filp_open("/dev/nvidia-rs-rmapi-ctl",O_RDWR,0);
|
||||
if(IS_ERR(opened))return (int)PTR_ERR(opened);
|
||||
*node=opened;
|
||||
*fd=get_unused_fd_flags(O_CLOEXEC);
|
||||
if(*fd<0)return *fd;
|
||||
fd_install(*fd,get_file(opened));
|
||||
return 0;
|
||||
}
|
||||
static int rmapi_user_pages_memtype_check(u32 cache)
|
||||
{
|
||||
/* user mappings (pgprot_writecombine / pgprot_noncached) and the record's kernel mapping */
|
||||
enum page_cache_mode mode=cache==2?_PAGE_CACHE_MODE_WC:_PAGE_CACHE_MODE_UC_MINUS;
|
||||
enum page_cache_mode other_mode=cache==2?_PAGE_CACHE_MODE_UC_MINUS:_PAGE_CACHE_MODE_WC;
|
||||
enum page_cache_mode kernel_mode=cache==2?_PAGE_CACHE_MODE_WC:_PAGE_CACHE_MODE_UC;
|
||||
u32 other_cache=cache==2?1:2;
|
||||
struct file *node[3]={NULL,NULL,NULL};int fd[3]={-1,-1,-1};
|
||||
unsigned long base=0,record=0,mapped;u64 source=0,other=0,pin=0,again=0,phys[16]={0},other_phys[16]={0};
|
||||
int e=0;unsigned int i;u8 byte=0xa5;
|
||||
for(i=0;i<3&&!e;i++)e=rmapi_ctl_open(&node[i],&fd[i]);
|
||||
if(!e&&(nv_rs_rmapi_os_alloc_pages(16,PAGE_SIZE,false,cache,&source)||nv_rs_rmapi_os_alloc_pages(16,PAGE_SIZE,false,other_cache,&other)))e=-ENOMEM;
|
||||
for(i=0;i<16&&!e;i++)if(nv_rs_rmapi_os_page_phys(source,i,&phys[i])||nv_rs_rmapi_os_page_phys(other,i,&other_phys[i])||!rmapi_page_cache_is(phys[i],true)||!rmapi_page_cache_is(other_phys[i],true))e=-EPROTO;
|
||||
if(!e&&nv_rs_rmapi_os_kernel_map(source,16*PAGE_SIZE))e=-ENOMEM;
|
||||
if(!e&&!rmapi_kernel_map_cache_is(source,_PAGE_CACHE_MODE_UC))e=-EBADMSG;
|
||||
/* read-write contexts of the whole allocations (as RmMapMemory hands them to the control file) */
|
||||
if(!e&&(nv_rs_rmapi_os_add_context(fd[0],3,source,0,cache)||nv_rs_rmapi_os_add_context(fd[1],3,other,0,other_cache)))e=-EINVAL;
|
||||
if(!e){base=vm_mmap(NULL,0,48*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,0);if(IS_ERR_VALUE(base)){e=(int)(long)base;base=0;}}
|
||||
if(!e){mapped=vm_mmap(node[0],base+16*PAGE_SIZE,16*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_FIXED,0);if(mapped!=base+16*PAGE_SIZE)e=IS_ERR_VALUE(mapped)?(int)(long)mapped:-EFAULT;}
|
||||
if(!e){mapped=vm_mmap(node[1],base+32*PAGE_SIZE,16*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_FIXED,0);if(mapped!=base+32*PAGE_SIZE)e=IS_ERR_VALUE(mapped)?(int)(long)mapped:-EFAULT;}
|
||||
if(!e&&(!rmapi_user_cache_is(base+16*PAGE_SIZE,mode)||!rmapi_user_cache_is(base+32*PAGE_SIZE,other_mode)))e=-EMEDIUMTYPE;
|
||||
/* anonymous memory and the source; the source and the other type; all three */
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base+15*PAGE_SIZE,2,true,&pin)!=0x1e)e=-EBADE;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base+31*PAGE_SIZE,2,true,&pin)!=0x1e)e=-EBADE;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base+16*PAGE_SIZE,32,true,&pin)!=0x1e)e=-EBADE;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base,48,true,&pin)!=0x1e)e=-EBADE;
|
||||
if(pin){nv_rs_rmapi_os_unlock_user_pages(pin);pin=0;}
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base+16*PAGE_SIZE,16,true,&pin))e=-ENOMEM;
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(pin,3*PAGE_SIZE+5,&byte,1))e=-EIO;
|
||||
if(!e&&!rmapi_kernel_map_cache_is(pin,kernel_mode))e=-EBADSLT;
|
||||
/* a user mapping of the pinned record, and a range pinned through it */
|
||||
if(!e&&nv_rs_rmapi_os_add_context(fd[2],3,pin,0,cache))e=-EINVAL;
|
||||
if(!e){record=vm_mmap(node[2],0,16*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,0);if(IS_ERR_VALUE(record)){e=(int)(long)record;record=0;}}
|
||||
if(!e&&!rmapi_user_cache_is(record,mode))e=-EBADRQC;
|
||||
byte=0;
|
||||
if(!e&&(copy_from_user(&byte,(const void __user *)(base+19*PAGE_SIZE+5),1)||byte!=0xa5))e=-EPROTO;
|
||||
byte=0;
|
||||
if(!e&&(copy_from_user(&byte,(const void __user *)(record+3*PAGE_SIZE+5),1)||byte!=0xa5))e=-EPROTO;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(record,16,true,&again))e=-ENOMEM;
|
||||
if(!e&&nv_rs_rmapi_os_kernel_map(again,16*PAGE_SIZE))e=-ENOMEM;
|
||||
if(!e&&!rmapi_kernel_map_cache_is(again,kernel_mode))e=-EBADSLT;
|
||||
if(again)nv_rs_rmapi_os_unlock_user_pages(again);
|
||||
/* the RM objects, the mappings and the last references to their files go first */
|
||||
if(record)vm_munmap(record,16*PAGE_SIZE);
|
||||
if(source)nv_rs_rmapi_os_free_pages(source);
|
||||
if(other)nv_rs_rmapi_os_free_pages(other);
|
||||
if(base)vm_munmap(base,48*PAGE_SIZE);
|
||||
for(i=0;i<3;i++)if(fd[i]>=0)close_fd(fd[i]);
|
||||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
|
||||
for(i=0;i<3;i++)if(node[i])__fput_sync(node[i]);
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(other_phys[i],false))e=-EUCLEAN;
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],true))e=-ESTALE;
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(pin,15*PAGE_SIZE,&byte,1))e=-EIO;
|
||||
if(pin)nv_rs_rmapi_os_unlock_user_pages(pin);
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],false))e=-EPROTO;
|
||||
#else
|
||||
/* Before 6.6, __fput_sync() BUG()s outside a kthread, and fput() from insmod defers the final release to the return
|
||||
* to user space, after this check. Only the half before the release runs there. */
|
||||
for(i=0;i<3;i++)if(node[i])fput(node[i]);
|
||||
if(pin)nv_rs_rmapi_os_unlock_user_pages(pin);
|
||||
if(!e)pr_info("NV_RS_VM_NOTE rmapi pinned mapping (cache type %u): lifetime half skipped before 6.6\n",cache);
|
||||
#endif
|
||||
if(e)pr_err("NV_RS_VM_FAIL rmapi pinned mapping (cache type %u): %d\n",cache,e);
|
||||
return e;
|
||||
}
|
||||
/* CodeRabbit on #388: a pinned record holds each allocation once, however many areas of the range map it. An uncached
|
||||
* source allocation of 16 pages is mapped over pages 0-15 of a 32-page area of the insmod process, and that mapping is
|
||||
* split in two at page 8 into the areas mprotect of its second half leaves (no module can call mprotect): a second
|
||||
* context of the source from its page 8 is mapped over pages 8-15 (MAP_FIXED), so two adjacent areas map the source's
|
||||
* pages 0-7 and 8-15 as before.
|
||||
* - the range over both areas is pinned (record 1) and holds the source once;
|
||||
* - record 1 is mapped over pages 16-31 and split at page 24 the same way, and the range over both of its areas is
|
||||
* pinned (record 2): it holds the source once, the allocation record 1 holds, once;
|
||||
* - all 32 pages, two areas of the source and two of record 1, are pinned (record 3): the source once.
|
||||
* Counting every area held it 2, 4 and 6 times. A byte written through record 3 reads back through both mappings.
|
||||
* With records 2 and 3 gone, the source's RM object freed, the mappings removed and their files released, record 1
|
||||
* alone keeps the source (its pages stay UC-); when record 1 goes the last use is dropped and they are write-back. */
|
||||
u64 nv_rs_rmapi_os_pinned_sources(u64 token,u64 source,u64 *of_source);
|
||||
/* Maps the mapping context of `node` at `address` for `pages` pages, over what is there. */
|
||||
static int rmapi_map_fixed(struct file *node,unsigned long address,unsigned long pages)
|
||||
{
|
||||
unsigned long mapped=vm_mmap(node,address,pages*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_FIXED,0);
|
||||
if(mapped==address)return 0;
|
||||
return IS_ERR_VALUE(mapped)?(int)(long)mapped:-EFAULT;
|
||||
}
|
||||
/* Two areas of the insmod process meet at `address` and map the same allocation of the RM API node. */
|
||||
static bool rmapi_split_at(unsigned long address)
|
||||
{
|
||||
struct vm_area_struct *low,*high;bool split=false;
|
||||
mmap_read_lock(current->mm);
|
||||
low=find_vma(current->mm,address-1);
|
||||
high=find_vma(current->mm,address);
|
||||
if(low&&high&&low!=high&&low->vm_end==address&&high->vm_start==address)
|
||||
split=low->vm_ops==high->vm_ops&&low->vm_private_data&&low->vm_private_data==high->vm_private_data;
|
||||
mmap_read_unlock(current->mm);
|
||||
return split;
|
||||
}
|
||||
/* The pinned record `pin` (record `number`) holds one entry, the allocation `source`. */
|
||||
static int rmapi_holds_once(u64 pin,u64 source,unsigned int number)
|
||||
{
|
||||
u64 of=0,held=nv_rs_rmapi_os_pinned_sources(pin,source,&of);
|
||||
if(held==1&&of==1)return 0;
|
||||
pr_err("NV_RS_VM_FAIL rmapi pinned split mapping: record %u holds %llu entries, %llu of them the source\n",number,held,of);
|
||||
return -EBADE;
|
||||
}
|
||||
static int rmapi_user_pages_split_check(void)
|
||||
{
|
||||
struct file *node[4]={NULL,NULL,NULL,NULL};int fd[4]={-1,-1,-1,-1};
|
||||
unsigned long base=0;u64 source=0,pin[3]={0,0,0},phys[16]={0};
|
||||
int e=0;unsigned int i;u8 byte=0x6b;
|
||||
for(i=0;i<4&&!e;i++)e=rmapi_ctl_open(&node[i],&fd[i]);
|
||||
if(!e&&nv_rs_rmapi_os_alloc_pages(16,PAGE_SIZE,false,1,&source))e=-ENOMEM;
|
||||
for(i=0;i<16&&!e;i++)if(nv_rs_rmapi_os_page_phys(source,i,&phys[i]))e=-EPROTO;
|
||||
if(!e&&(nv_rs_rmapi_os_add_context(fd[0],3,source,0,1)||nv_rs_rmapi_os_add_context(fd[1],3,source,8,1)))e=-EINVAL;
|
||||
if(!e){base=vm_mmap(NULL,0,32*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,0);if(IS_ERR_VALUE(base)){e=(int)(long)base;base=0;}}
|
||||
if(!e)e=rmapi_map_fixed(node[0],base,16);
|
||||
if(!e)e=rmapi_map_fixed(node[1],base+8*PAGE_SIZE,8);
|
||||
if(!e&&!rmapi_split_at(base+8*PAGE_SIZE))e=-ENOTUNIQ;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base,16,true,&pin[0]))e=-ENOMEM;
|
||||
if(!e)e=rmapi_holds_once(pin[0],source,1);
|
||||
/* record 1's mapping, split the same way, pinned across both of its areas */
|
||||
if(!e&&(nv_rs_rmapi_os_add_context(fd[2],3,pin[0],0,1)||nv_rs_rmapi_os_add_context(fd[3],3,pin[0],8,1)))e=-EINVAL;
|
||||
if(!e)e=rmapi_map_fixed(node[2],base+16*PAGE_SIZE,16);
|
||||
if(!e)e=rmapi_map_fixed(node[3],base+24*PAGE_SIZE,8);
|
||||
if(!e&&!rmapi_split_at(base+24*PAGE_SIZE))e=-ENOTUNIQ;
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base+16*PAGE_SIZE,16,true,&pin[1]))e=-ENOMEM;
|
||||
if(!e)e=rmapi_holds_once(pin[1],source,2);
|
||||
if(!e&&nv_rs_rmapi_os_lock_user_pages(base,32,true,&pin[2]))e=-ENOMEM;
|
||||
if(!e)e=rmapi_holds_once(pin[2],source,3);
|
||||
/* page 20 of the range is record 1's page 4, the source's page 4 */
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(pin[2],20*PAGE_SIZE+3,&byte,1))e=-EIO;
|
||||
byte=0;
|
||||
if(!e&&(copy_from_user(&byte,(const void __user *)(base+4*PAGE_SIZE+3),1)||byte!=0x6b))e=-EPROTO;
|
||||
byte=0;
|
||||
if(!e&&(copy_from_user(&byte,(const void __user *)(base+20*PAGE_SIZE+3),1)||byte!=0x6b))e=-EPROTO;
|
||||
for(i=2;i>0;i--)if(pin[i])nv_rs_rmapi_os_unlock_user_pages(pin[i]);
|
||||
if(source)nv_rs_rmapi_os_free_pages(source);
|
||||
if(base)vm_munmap(base,32*PAGE_SIZE);
|
||||
for(i=0;i<4;i++)if(fd[i]>=0)close_fd(fd[i]);
|
||||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
|
||||
for(i=0;i<4;i++)if(node[i])__fput_sync(node[i]);
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],true))e=-ESTALE;
|
||||
if(pin[0])nv_rs_rmapi_os_unlock_user_pages(pin[0]);
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],false))e=-EUCLEAN;
|
||||
#else
|
||||
/* as in rmapi_user_pages_memtype_check: the files' last release comes after this check before 6.6 */
|
||||
for(i=0;i<4;i++)if(node[i])fput(node[i]);
|
||||
if(pin[0])nv_rs_rmapi_os_unlock_user_pages(pin[0]);
|
||||
if(!e)pr_info("NV_RS_VM_NOTE rmapi pinned split mapping: lifetime half skipped before 6.6\n");
|
||||
#endif
|
||||
if(e)pr_err("NV_RS_VM_FAIL rmapi pinned split mapping: %d\n",e);
|
||||
return e;
|
||||
}
|
||||
static int rmapi_start(void)
|
||||
{
|
||||
int e=nv_rs_rmapi_node_init();
|
||||
@ -370,6 +643,12 @@ static int rmapi_start(void)
|
||||
e=nv_rs_rmapi_node_gpu_add(rmapi_gpu,0x140000a1);
|
||||
if(e!=0){pci_dev_put(rmapi_gpu);rmapi_gpu=NULL;nv_rs_rmapi_node_exit();return e<0?e:-EINVAL;}
|
||||
e=nv_rs_rmapi_node_gpu_reader(rmapi_gpu,rmapi_read32,NULL,NULL);
|
||||
if(!e)e=rmapi_sysmem_check(rmapi_gpu,0);
|
||||
if(!e)e=rmapi_sysmem_check(rmapi_gpu,2);
|
||||
if(!e)e=rmapi_user_pages_check();
|
||||
if(!e)e=rmapi_user_pages_memtype_check(1);
|
||||
if(!e)e=rmapi_user_pages_memtype_check(2);
|
||||
if(!e)e=rmapi_user_pages_split_check();
|
||||
if(e){nv_rs_rmapi_node_gpu_remove(rmapi_gpu);pci_dev_put(rmapi_gpu);rmapi_gpu=NULL;nv_rs_rmapi_node_exit();return e;}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ fi
|
||||
"$RUSTC" $RUSTFLAGS --crate-name nv_rs_core --crate-type=rlib src/nvidia/src/lib.rs --extern nv_rs_abi="$OUT/libnv_rs_abi.rlib" -o "$OUT/libnv_rs_core.rlib"
|
||||
"$RUSTC" $RUSTFLAGS --crate-name vm_rust_backing --crate-type=staticlib -C lto=fat tests/vm/rust_backing.rs --extern nv_rs_abi="$OUT/libnv_rs_abi.rlib" --extern nv_rs_core="$OUT/libnv_rs_core.rlib" -L dependency="$OUT" -o "$OUT/libvm_rust_backing.a"
|
||||
python3 "$ROOT/tools/prepare_kernel_archive.py" "$OUT/libvm_rust_backing.a" "$OUT/libvm_rust_backing.kernel.a"
|
||||
ld -r --gc-sections --whole-archive "$OUT/libvm_rust_backing.kernel.a" --no-whole-archive -u vm_rust_backing -u vm_rust_backing_size -u vm_rust_backing_align -u vm_gpu_lease_size -u vm_gpu_lease_acquire -u vm_gpu_lease_ready -u vm_gpu_lease_drop -u vm_gpu_registers_size -u vm_gpu_registers_acquire -u vm_gpu_registers_read -u vm_gpu_registers_write -u vm_gpu_registers_drop -u vm_gpu_locked_size -u vm_gpu_locked_acquire -u vm_cold_msi_config -u vm_gpu_locked_read -u vm_gpu_locked_write -u vm_gpu_locked_drop -u vm_gpu_waited_acquire -u vm_native_memory -u vm_gpu_registry_probe -u vm_gpu_registry_removed -u vm_nano_timer -u vm_os_event -u vm_device_software -u vm_first_open_cpu_state -u vm_software_manager_init -u vm_software_manager_shutdown -u vm_nano_size -u vm_nano_hold -u vm_nano_removed -u vm_host_services -u vm_host_irq_services -u vm_thread_state -u nv_rs_gpu_state_size -u nv_rs_gpu_state_align -u nv_rs_gpu_prepare_owned -u nv_rs_gpu_set_probe_identity -u nv_rs_gpu_first_open -u nv_rs_gpu_registry_publish -u nv_rs_gpu_registry_retire -u nv_rs_gpu_mark_removed -u nv_rs_gpu_release_driver -u nv_rs_gpu_retry_cleanup -u nv_rs_gpu_retry_gp100_hbm_restore -u nv_rs_gpu_close -u nv_rs_sysmem_state_size -u nv_rs_sysmem_state_align -u nv_rs_sysmem_prepare -u nv_rs_sysmem_allocate -u nv_rs_sysmem_close -u nv_rs_rmapi_state_size -u nv_rs_rmapi_state_align -u nv_rs_rmapi_init -u nv_rs_rmapi_gpu_add -u nv_rs_rmapi_gpu_remove -u nv_rs_rmapi_gpu_minor -u nv_rs_rmapi_gpu_open -u nv_rs_rmapi_attach_register_count -u nv_rs_rmapi_attach_register -u nv_rs_rmapi_gpu_needs_registers -u nv_rs_rmapi_gpu_attach_registers -u nv_rs_rmapi_set_host_bridge -u nv_rs_rmapi_set_cpu_info -u nv_rs_rmapi_set_force_p2p -u nv_rs_rmapi_set_gsp_abi -u nv_rs_rmapi_set_driver_version -u nv_rs_rmapi_set_strict_version_check -u nv_rs_rmapi_get_driver_version -u nv_rs_rmapi_gpu_release -u nv_rs_rmapi_register_fd -u nv_rs_rmapi_prepare -u nv_rs_rmapi_nested_plan -u nv_rs_rmapi_execute -u nv_rs_rmapi_copy_fault -u nv_rs_rmapi_close -u nv_rs_rmapi_rc_recover_fault -u nv_rs_gpu_nice_set_policy -u nv_rs_gpu_nice_get_policy -u nv_rs_gpu_nice_refresh_fallbacks -u nv_rs_gpu_nice_get_row -u nv_rs_gpu_nice_active_count -u nv_rs_gpu_nice_has_tracked -u nv_rs_gpu_nice_list_pids -u nv_rs_gpu_nice_query_pid -u nv_rs_gpu_nice_preview_nice -u nv_rs_gpu_nice_set_pid -u nv_rs_gpu_nice_sync_pid -u nv_rs_gpu_nice_drain -u nv_rs_gpu_nice_desktop_comm -u nv_rs_gpu_nice_copy_custom_comms -u nv_rs_alloc_reg_can_admit -u nv_rs_alloc_reg_insert -u nv_rs_alloc_reg_find -u nv_rs_alloc_reg_retain -u nv_rs_alloc_reg_mark_freed_and_put -u nv_rs_alloc_reg_put_ptr -u nv_rs_alloc_reg_for_each -u nv_rs_alloc_reg_total_pages -u nv_rs_alloc_reg_pop_any -u nv_rs_alloc_reg_release_storage -u nv_rs_dma_reg_find -u nv_rs_dma_reg_insert -u nv_rs_dma_reg_pop_for_alloc -u nv_rs_dma_reg_pop_for_dev -u nv_rs_file_map_insert_alloc -u nv_rs_file_map_insert_io -u nv_rs_file_map_get -u nv_rs_file_map_remove -u nv_rs_gpu_slot_activate -u nv_rs_gpu_slot_deactivate -u nv_rs_gpu_slot_is_live_gen -u nv_rs_gpu_slot_set_numa -u nv_rs_gpu_slot_validate_bar0 -u nv_rs_gpu_slot_validate_bar1 -u nv_rs_modeset_register_gpu -u nv_rs_modeset_unregister_gpu -u nv_rs_sysmem_pool_push -u nv_rs_sysmem_pool_pop -u nv_rs_sysmem_pool_pop_for_trim -u nv_rs_sysmem_pool_len -u nv_rs_sysmem_pool_release_storage -o "$OUT/rust_backing.o_binary"
|
||||
ld -r --gc-sections --whole-archive "$OUT/libvm_rust_backing.kernel.a" --no-whole-archive -u vm_rust_backing -u vm_rust_backing_size -u vm_rust_backing_align -u vm_gpu_lease_size -u vm_gpu_lease_acquire -u vm_gpu_lease_ready -u vm_gpu_lease_drop -u vm_gpu_registers_size -u vm_gpu_registers_acquire -u vm_gpu_registers_read -u vm_gpu_registers_write -u vm_gpu_registers_drop -u vm_gpu_locked_size -u vm_gpu_locked_acquire -u vm_cold_msi_config -u vm_gpu_locked_read -u vm_gpu_locked_write -u vm_gpu_locked_drop -u vm_gpu_waited_acquire -u vm_native_memory -u vm_gpu_registry_probe -u vm_gpu_registry_removed -u vm_nano_timer -u vm_os_event -u vm_device_software -u vm_first_open_cpu_state -u vm_software_manager_init -u vm_software_manager_shutdown -u vm_nano_size -u vm_nano_hold -u vm_nano_removed -u vm_host_services -u vm_host_irq_services -u vm_thread_state -u nv_rs_gpu_state_size -u nv_rs_gpu_state_align -u nv_rs_gpu_prepare_owned -u nv_rs_gpu_set_probe_identity -u nv_rs_gpu_first_open -u nv_rs_gpu_registry_publish -u nv_rs_gpu_registry_retire -u nv_rs_gpu_mark_removed -u nv_rs_gpu_release_driver -u nv_rs_gpu_retry_cleanup -u nv_rs_gpu_retry_gp100_hbm_restore -u nv_rs_gpu_close -u nv_rs_sysmem_state_size -u nv_rs_sysmem_state_align -u nv_rs_sysmem_prepare -u nv_rs_sysmem_allocate -u nv_rs_sysmem_close -u nv_rs_rmapi_state_size -u nv_rs_rmapi_state_align -u nv_rs_rmapi_init -u nv_rs_rmapi_gpu_add -u nv_rs_rmapi_gpu_remove -u nv_rs_rmapi_gpu_minor -u nv_rs_rmapi_gpu_open -u nv_rs_rmapi_attach_register_count -u nv_rs_rmapi_attach_register -u nv_rs_rmapi_gpu_needs_registers -u nv_rs_rmapi_gpu_attach_registers -u nv_rs_rmapi_set_host_bridge -u nv_rs_rmapi_set_cpu_info -u nv_rs_rmapi_set_force_p2p -u nv_rs_rmapi_set_gsp_abi -u nv_rs_rmapi_set_driver_version -u nv_rs_rmapi_set_strict_version_check -u nv_rs_rmapi_get_driver_version -u nv_rs_rmapi_gpu_release -u nv_rs_rmapi_register_fd -u nv_rs_rmapi_prepare -u nv_rs_rmapi_nested_plan -u nv_rs_rmapi_execute -u nv_rs_rmapi_copy_fault -u nv_rs_rmapi_close -u nv_rs_rmapi_rc_recover_fault -u nv_rs_gpu_nice_set_policy -u nv_rs_gpu_nice_get_policy -u nv_rs_gpu_nice_refresh_fallbacks -u nv_rs_gpu_nice_get_row -u nv_rs_gpu_nice_active_count -u nv_rs_gpu_nice_has_tracked -u nv_rs_gpu_nice_list_pids -u nv_rs_gpu_nice_query_pid -u nv_rs_gpu_nice_preview_nice -u nv_rs_gpu_nice_set_pid -u nv_rs_gpu_nice_sync_pid -u nv_rs_gpu_nice_drain -u nv_rs_gpu_nice_desktop_comm -u nv_rs_gpu_nice_copy_custom_comms -u nv_rs_alloc_reg_can_admit -u nv_rs_alloc_reg_insert -u nv_rs_alloc_reg_find -u nv_rs_alloc_reg_retain -u nv_rs_alloc_reg_retain_ptr -u nv_rs_alloc_reg_mark_freed_and_put -u nv_rs_alloc_reg_put_ptr -u nv_rs_alloc_reg_for_each -u nv_rs_alloc_reg_total_pages -u nv_rs_alloc_reg_pop_any -u nv_rs_alloc_reg_release_storage -u nv_rs_dma_reg_find -u nv_rs_dma_reg_insert -u nv_rs_dma_reg_pop_for_alloc -u nv_rs_dma_reg_pop_for_dev -u nv_rs_file_map_insert_alloc -u nv_rs_file_map_insert_io -u nv_rs_file_map_get -u nv_rs_file_map_remove -u nv_rs_gpu_slot_activate -u nv_rs_gpu_slot_deactivate -u nv_rs_gpu_slot_is_live_gen -u nv_rs_gpu_slot_set_numa -u nv_rs_gpu_slot_validate_bar0 -u nv_rs_gpu_slot_validate_bar1 -u nv_rs_modeset_register_gpu -u nv_rs_modeset_unregister_gpu -u nv_rs_sysmem_pool_push -u nv_rs_sysmem_pool_pop -u nv_rs_sysmem_pool_pop_for_trim -u nv_rs_sysmem_pool_len -u nv_rs_sysmem_pool_release_storage -u nv_rs_sysmem_make_uncached -u nv_rs_sysmem_may_release -u nv_rs_pinned_area_bytes -u nv_rs_pinned_range_cache -u nv_rs_pinned_sources_add -o "$OUT/rust_backing.o_binary"
|
||||
# --strip-debug: with -C lto=fat the object carries core's DWARF, which pahole cannot encode
|
||||
# when the kernel builds module BTF (same failure #42 fixed for the production module).
|
||||
objcopy --strip-debug --remove-section=.llvmbc --remove-section=.llvmcmd --remove-section=.eh_frame --remove-section=.eh_frame_hdr --remove-section=.gcc_except_table "$OUT/rust_backing.o_binary"
|
||||
|
||||
Reference in New Issue
Block a user