mirror of
https://github.com/lemonade-sdk/mac_linuxgpu.git
synced 2026-11-22 23:57:31 -10:00
MMIO token numbers are renewed between sessions
Every DriverKit MMIO mapping takes a token number (1..511) that doubles as its synthetic pointer. To keep a stale pointer from reaching a different mapping, an unmapped number was never issued again, so each session consumed about four for good: the register token, upstream's register ioremap and the kernel doorbell kmap among them. After roughly 120 session open/close cycles none were left; the doorbell kmap failed and the probe returned -ENOMEM (242: "kernel bo map failed", quarantine, restart required). The numbers are now renewed when the last mapping is unmapped. The session's register token lives from dext_open to dext_close, so an empty table means no session, no upstream module and no synthetic pointer remain. Within a session an unmapped number is still never reissued. test-session-cycles runs 300 session open/probe/close cycles through the real upstream probe and the production PCI, MMIO-token and heap code against a mock IOPCIDevice, and checks after every close that no MMIO mapping survives and that heap, DART-mapped bytes and mappings, page descriptors and threads are at their baseline. Before this change it fails at cycle 255 with the probe returning -ENOMEM. The DriverKit token bounds test also covers renewal and in-session non-reuse.
This commit is contained in:
1 parent
ff324ff9a0
commit
0ee07c4401
8 files changed
+433
-4
No files matched your search
@@ -790,6 +790,10 @@ test-fence-lifetime:
|
||||
test-upstream-pci-failure: lib
|
||||
bash scripts/test-upstream-pci-failure.sh
|
||||
|
||||
# Session open/probe/close cycles return every resource to its baseline.
|
||||
test-session-cycles: lib
|
||||
bash scripts/test-session-cycles.sh
|
||||
|
||||
test-bitmap-contracts:
|
||||
bash scripts/test-bitmap-contracts.sh
|
||||
|
||||
@@ -949,7 +953,7 @@ test-ttm-lru-cleanup:
|
||||
# Userspace-only crash regressions: mocked DriverKit/PCI boundaries, real shim
|
||||
# implementations and upstream error cleanup. No installed driver is opened.
|
||||
CRASH_PATH_TESTS := test-dext-alloc test-debugfs-lifecycle test-kmem-cache-lifecycle \
|
||||
test-probe-cleanup test-upstream-pci-failure test-page-alloc-dk test-iokit-dma \
|
||||
test-probe-cleanup test-upstream-pci-failure test-session-cycles test-page-alloc-dk test-iokit-dma \
|
||||
test-pci-dext test-pci-lifecycle test-platform-devres test-dext-sync \
|
||||
test-dext-threads test-task-kthread test-rwsem test-rcu-dk \
|
||||
test-mutex-completion test-wait-event test-drm-lifecycle test-workqueue-lifetime \
|
||||
@@ -1039,7 +1043,7 @@ test-crash-paths: export UBSAN_OPTIONS = halt_on_error=1:abort_on_error=0
|
||||
test-crash-paths: verify-source $(CRASH_PATH_TESTS)
|
||||
@echo "Offline crash-path regressions passed; hardware safety is not established by these checks."
|
||||
|
||||
test: test-kmem-cache-lifecycle test-probe-cleanup test-iokit-dma test-upstream-pci-failure test-workqueue-lifetime test-atomic-semantics test-aql-storage-bounds
|
||||
test: test-kmem-cache-lifecycle test-probe-cleanup test-iokit-dma test-upstream-pci-failure test-session-cycles test-workqueue-lifetime test-atomic-semantics test-aql-storage-bounds
|
||||
test: test-ww-mutex test-compute-dispatch test-compute-lifetime test-irq-lifetime test-xarray-safety test-mm-boundaries test-page-boundaries test-dext-compute-production
|
||||
test: test-drm-exec-lifetime test-fence-lifetime
|
||||
test: test-gpu-buddy test-partial-ttm-cleanup
|
||||
@@ -1058,7 +1062,7 @@ test: test-queue-partition
|
||||
|
||||
.PHONY: test-ww-mutex test-compute-dispatch test-compute-lifetime test-irq-lifetime test-xarray-safety test-mm-boundaries test-page-boundaries test-dext-compute-production
|
||||
|
||||
.PHONY: test-crash-paths test-kmem-cache-lifecycle test-probe-cleanup test-iokit-dma test-upstream-pci-failure test-workqueue-lifetime test-atomic-semantics test-aql-storage-bounds
|
||||
.PHONY: test-crash-paths test-kmem-cache-lifecycle test-probe-cleanup test-iokit-dma test-upstream-pci-failure test-session-cycles test-workqueue-lifetime test-atomic-semantics test-aql-storage-bounds
|
||||
|
||||
.PHONY: test-ttm-device-pool test-page-alloc-dk all test dext clean config-check hsa hsa-test hsa-ios test-dext-alloc verify-source verify-linux test-dext-time test-dext-stdio test-pci-dext test-pci-lifecycle test-platform-devres test-dext-sync test-dma-mask test-rwsem test-debugfs-lifecycle test-task-kthread test-rbtree test-dext-threads test-mutex-completion test-rcu-dk test-bootstrap test-driver-bootstrap-integration test-pseudo-fs test-jiffies test-spinlock test-wait-event test-chrdev test-class-device test-platform-policy test-xarray-limit test-drm-lifecycle
|
||||
|
||||
|
||||
@@ -262,6 +262,8 @@ extern struct page *alloc_pages(gfp_t gfp, unsigned int order);
|
||||
/* linuxu test hook: grow the page pool/arena (must run before the
|
||||
* first alloc_pages); see linuxu/src/mm/page.c. */
|
||||
extern int linuxu_page_pool_extend(unsigned long pages);
|
||||
/* Page descriptors held by allocations (until their backing is released). */
|
||||
extern unsigned long linuxu_page_descriptors(void);
|
||||
static inline struct page *alloc_page(gfp_t gfp) { return alloc_pages(gfp, 0); }
|
||||
static inline struct page *alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long arg)
|
||||
{
|
||||
|
||||
@@ -137,6 +137,9 @@ int rt_pci_has_retained_probe(void);
|
||||
void *rt_ioremap(struct rt_device *dev, uint64_t phys, uint64_t size);
|
||||
void *rt_ioremap_active(uint64_t phys, uint64_t size);
|
||||
void rt_mmio_free(void *cookie);
|
||||
/* DriverKit build: MMIO mappings (tokens) currently live. Zero between
|
||||
* sessions; at zero the token numbers are renewed. */
|
||||
unsigned int rt_mmio_live_tokens(void);
|
||||
|
||||
/*
|
||||
* The readl/writel/readq/writeq/readb/writeb signatures the driver's
|
||||
|
||||
@@ -64,6 +64,7 @@ static unsigned long page_max; /* pages at most: RAM, or the test hook's */
|
||||
static pthread_cond_t page_changed = PTHREAD_COND_INITIALIZER;
|
||||
static pthread_mutex_t page_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int page_pool_ready;
|
||||
static unsigned long page_descriptors; /* slots owned by an allocation */
|
||||
|
||||
static struct page_chunk *page_chunk_of(unsigned long pfn)
|
||||
{
|
||||
@@ -101,6 +102,14 @@ static unsigned long page_ram_pages(void)
|
||||
return (unsigned long)(bytes >> LINUXU_PAGE_SHIFT);
|
||||
}
|
||||
|
||||
unsigned long linuxu_page_descriptors(void)
|
||||
{
|
||||
pthread_mutex_lock(&page_lock);
|
||||
unsigned long held = page_descriptors;
|
||||
pthread_mutex_unlock(&page_lock);
|
||||
return held;
|
||||
}
|
||||
|
||||
/* Test hook: before the first allocation, hold the pool to @pages (tests
|
||||
* keep their memory small this way). Later, only a cap no larger than the
|
||||
* one in force is accepted. */
|
||||
@@ -446,6 +455,7 @@ static struct page *alloc_pages_backing(gfp_t gfp, unsigned int order, bool cpu_
|
||||
for (n = 0; n < need; n++) {
|
||||
struct page *page = PG(first + n);
|
||||
PG_ALLOC(first + n) = allocation;
|
||||
page_descriptors++;
|
||||
PG_ORDER(first + n) = n ? -2 : (signed char)order;
|
||||
page->flags = 0;
|
||||
page->mapping = NULL;
|
||||
@@ -515,6 +525,7 @@ static void page_release_backing(struct linuxu_page_allocation *allocation)
|
||||
if (PG_ALLOC(index) == allocation) {
|
||||
__atomic_store_n(&PG_ALLOC(index), NULL, __ATOMIC_RELEASE);
|
||||
PG_ORDER(index) = -1;
|
||||
page_descriptors--;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&page_lock);
|
||||
|
||||
@@ -39,11 +39,23 @@ static struct mmio_slot mmio_slots[RT_MMIO_NUM_SLOTS];
|
||||
static pthread_mutex_t mmio_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
#ifdef LINUXU_DEXT_DK
|
||||
/* Synthetic pointers must never alias a different BAR after unmap. Keep
|
||||
* retired token numbers recognizable so stale doorbell atomics fail closed. */
|
||||
* retired token numbers recognizable so stale doorbell atomics fail closed.
|
||||
* The numbers are renewed when the last mapping is unmapped: the session's
|
||||
* register token lives from dext_open to dext_close, so an empty table means
|
||||
* no session, no upstream module and no synthetic pointer remain. */
|
||||
static uint8_t mmio_issued[RT_MMIO_DK_MAX_SLOTS];
|
||||
static unsigned int mmio_live;
|
||||
#endif
|
||||
|
||||
#ifdef LINUXU_DEXT_DK
|
||||
unsigned int rt_mmio_live_tokens(void)
|
||||
{
|
||||
pthread_mutex_lock(&mmio_lock);
|
||||
unsigned int live = mmio_live;
|
||||
pthread_mutex_unlock(&mmio_lock);
|
||||
return live;
|
||||
}
|
||||
|
||||
extern int dext_bar_info(uint8_t bar, uint8_t *mem_index, uint64_t *size);
|
||||
extern int dext_mem_read8(uint32_t token, uint64_t off, uint8_t *value);
|
||||
extern int dext_mem_read16(uint32_t token, uint64_t off, uint16_t *value);
|
||||
@@ -159,6 +171,7 @@ uint32_t rt_mmio_mint_token(struct pci_dev *dev, uint8_t mem_index,
|
||||
mmio_slots[token].used = 1;
|
||||
#ifdef LINUXU_DEXT_DK
|
||||
mmio_issued[token] = 1;
|
||||
mmio_live++;
|
||||
#endif
|
||||
mmio_slots[token].dev = dev;
|
||||
mmio_slots[token].mem_index = mem_index;
|
||||
@@ -189,6 +202,9 @@ uint32_t rt_mmio_mint_token(struct pci_dev *dev, uint8_t mem_index,
|
||||
mmio_slots[token].shadow_len =
|
||||
len ? len : RT_MMIO_SLOT_GRANULE;
|
||||
if (!mmio_slots[token].shadow) {
|
||||
#ifdef LINUXU_DEXT_DK
|
||||
mmio_live--;
|
||||
#endif
|
||||
memset(&mmio_slots[token], 0, sizeof(mmio_slots[token]));
|
||||
pthread_mutex_unlock(&mmio_lock);
|
||||
return RT_MMIO_TOKEN_INVALID;
|
||||
@@ -208,6 +224,10 @@ void rt_mmio_free_token(uint32_t token)
|
||||
{
|
||||
pthread_mutex_lock(&mmio_lock);
|
||||
if (token && token < RT_MMIO_NUM_SLOTS) {
|
||||
#ifdef LINUXU_DEXT_DK
|
||||
if (mmio_slots[token].used && !--mmio_live)
|
||||
memset(mmio_issued, 0, sizeof(mmio_issued));
|
||||
#endif
|
||||
free(mmio_slots[token].shadow);
|
||||
memset(&mmio_slots[token], 0,
|
||||
sizeof(mmio_slots[0]));
|
||||
|
||||
@@ -32,5 +32,29 @@ int main(void)
|
||||
rt_mmio_free_token(token);
|
||||
rt_mmio_memcpy_toio(mapping, bytes, 0, sizeof(bytes), NULL);
|
||||
assert(writes == 8 && faults == 3);
|
||||
|
||||
/* While a session holds its register token, an unmapped token number is
|
||||
* not issued again: a stale pointer cannot reach a different mapping. */
|
||||
uint32_t session = rt_mmio_mint_token(&dev, 5, 0, 32, 0);
|
||||
uint32_t first = rt_mmio_mint_token(&dev, 2, 0, 32, 0);
|
||||
assert(session && first && first != session);
|
||||
rt_mmio_free_token(first);
|
||||
for (unsigned i = 0; i < 64; ++i) {
|
||||
uint32_t next = rt_mmio_mint_token(&dev, 2, 0, 32, 0);
|
||||
assert(next && next != first && next != session);
|
||||
rt_mmio_free_token(next);
|
||||
}
|
||||
/* Once the last mapping is gone the numbers are renewed, so sessions
|
||||
* never run out of them (build 242 failed its probe after ~120). */
|
||||
rt_mmio_free_token(session);
|
||||
for (unsigned cycle = 0; cycle < 4 * RT_MMIO_DK_MAX_SLOTS; ++cycle) {
|
||||
session = rt_mmio_mint_token(&dev, 5, 0, 32, 0);
|
||||
uint32_t registers = rt_mmio_mint_token(&dev, 5, 0, 32, 0);
|
||||
uint32_t doorbells = rt_mmio_mint_token(&dev, 2, 0, 32, 0);
|
||||
assert(session && registers && doorbells);
|
||||
rt_mmio_free_token(doorbells);
|
||||
rt_mmio_free_token(registers);
|
||||
rt_mmio_free_token(session);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
/* Session open/probe/close cycles against the real upstream amdgpu probe and
|
||||
* the production DriverKit PCI, MMIO-token and heap code (mock IOPCIDevice).
|
||||
*
|
||||
* Each cycle is what one driver session does: open the PCI session and mint
|
||||
* its register token (dext_open), create the runtime device, run upstream's
|
||||
* probe (it maps BAR5 and fails later, as an unanswered device does), remove
|
||||
* it, free the runtime device and close the PCI session (dext_close).
|
||||
* Build 242 lost MMIO token numbers on every cycle and failed the probe
|
||||
* with -ENOMEM after about 120 sessions. Every resource a session takes
|
||||
* must be back at its baseline after each close. */
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <mach/mach.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "mock_memory_sysctl.h"
|
||||
|
||||
#include <linux/pci.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/mm.h>
|
||||
#include <rt/bootstrap.h>
|
||||
#include <rt/dart.h>
|
||||
#include <rt/dext_dma.h>
|
||||
#include <rt/dext_pci.h>
|
||||
#include <rt/rt.h>
|
||||
#include "dext_heap_backend.h"
|
||||
|
||||
/* The cycle has no endpoint capable of a function reset, and the device
|
||||
* never leaves the bus. */
|
||||
int dext_pci_function_reset(void) { return -95; }
|
||||
int dext_pci_removed(void) { return 0; }
|
||||
|
||||
extern uint32_t rt_mmio_mint_token(struct pci_dev *dev, uint8_t mem_index,
|
||||
uint64_t base, uint64_t size, int host_shadow);
|
||||
extern void rt_mmio_free_token(uint32_t token);
|
||||
|
||||
#define BAR0_BYTES (16ULL << 30)
|
||||
#define BAR5_BYTES (512UL << 10)
|
||||
|
||||
static uint16_t pci_command;
|
||||
static unsigned long mmio_reads, mmio_writes;
|
||||
static unsigned int bar0_maps;
|
||||
static unsigned long bar5_mappings;
|
||||
|
||||
int dext_pci_snapshot(struct dext_pci_snapshot *snapshot)
|
||||
{
|
||||
memset(snapshot, 0, sizeof(*snapshot));
|
||||
snapshot->vendor = 0x1002;
|
||||
snapshot->device = 0x7551;
|
||||
snapshot->subsystem_vendor = 0x1da2;
|
||||
snapshot->subsystem_device = 0xe499;
|
||||
snapshot->revision = 0xc0;
|
||||
snapshot->class_code = 0x030000;
|
||||
snapshot->bus = 5;
|
||||
snapshot->bar[0] = (struct dext_pci_bar) {
|
||||
.base = 0x40000000000ULL, .size = BAR0_BYTES, .present = 1,
|
||||
};
|
||||
snapshot->bar[5] = (struct dext_pci_bar) {
|
||||
.base = 0x40410000000ULL, .size = BAR5_BYTES, .present = 1,
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dext_pci_config_read32(uint64_t offset, uint32_t *value)
|
||||
{
|
||||
*value = offset == 0 ? 0x75511002 : 0;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_config_read16(uint64_t offset, uint16_t *value)
|
||||
{
|
||||
*value = offset == PCI_COMMAND ? pci_command : 0;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_config_read8(uint64_t offset, uint8_t *value)
|
||||
{
|
||||
(void)offset;
|
||||
*value = 0;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_config_write32(uint64_t offset, uint32_t value)
|
||||
{
|
||||
(void)offset; (void)value;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_config_write16(uint64_t offset, uint16_t value)
|
||||
{
|
||||
if (offset == PCI_COMMAND)
|
||||
pci_command = value;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_config_write8(uint64_t offset, uint8_t value)
|
||||
{
|
||||
(void)offset; (void)value;
|
||||
return 0;
|
||||
}
|
||||
int dext_pci_irq_status(unsigned int *armed, unsigned int *type)
|
||||
{
|
||||
*armed = 0;
|
||||
*type = DEXT_PCI_IRQ_NONE;
|
||||
return 0;
|
||||
}
|
||||
int dext_bar_info(uint8_t bar, uint8_t *index, uint64_t *size)
|
||||
{
|
||||
if (bar != 0 && bar != 5)
|
||||
return -1;
|
||||
if (bar == 5)
|
||||
bar5_mappings++; /* each pci_iomap/ioremap of the registers */
|
||||
*index = bar;
|
||||
*size = bar ? BAR5_BYTES : BAR0_BYTES;
|
||||
return 0;
|
||||
}
|
||||
void *dext_bar0_cpu_map(uint64_t offset, uint64_t size)
|
||||
{
|
||||
(void)offset; (void)size;
|
||||
bar0_maps++;
|
||||
return NULL;
|
||||
}
|
||||
int dext_bar0_cpu_unmap(const void *address)
|
||||
{
|
||||
(void)address;
|
||||
return 0;
|
||||
}
|
||||
int dext_bar0_cpu_contains(const void *address, size_t size)
|
||||
{
|
||||
(void)address; (void)size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Registers read as zero: the device answers nothing upstream can use. */
|
||||
#define MMIO_READ(bits) \
|
||||
int dext_mem_read##bits(uint32_t token, uint64_t offset, uint##bits##_t *value) \
|
||||
{ \
|
||||
(void)token; (void)offset; \
|
||||
mmio_reads++; \
|
||||
*value = 0; \
|
||||
return 0; \
|
||||
}
|
||||
#define MMIO_WRITE(bits) \
|
||||
int dext_mem_write##bits(uint32_t token, uint64_t offset, uint##bits##_t value) \
|
||||
{ \
|
||||
(void)token; (void)offset; (void)value; \
|
||||
mmio_writes++; \
|
||||
return 0; \
|
||||
}
|
||||
MMIO_READ(8) MMIO_READ(16) MMIO_READ(64)
|
||||
|
||||
/* Except the firmware's "IFWI init complete" flag (MP0 C2PMSG_33 bit 31),
|
||||
* which upstream's discovery otherwise polls for two seconds. */
|
||||
int dext_mem_read32(uint32_t token, uint64_t offset, uint32_t *value)
|
||||
{
|
||||
(void)token;
|
||||
mmio_reads++;
|
||||
*value = offset == 0x16061 * 4 ? 0x80000000u : 0;
|
||||
return 0;
|
||||
}
|
||||
MMIO_WRITE(8) MMIO_WRITE(16) MMIO_WRITE(32) MMIO_WRITE(64)
|
||||
|
||||
void dext_pci_transport_record_fault(int fault, uint64_t offset)
|
||||
{
|
||||
fprintf(stderr, "unexpected transport fault %d at 0x%llx\n", fault,
|
||||
(unsigned long long)offset);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* dext_open's register token: BAR5, BAR-relative, no host shadow, minted
|
||||
* against the IOPCIDevice before the runtime device exists. */
|
||||
static int iopci_device;
|
||||
static uint32_t session_open(void)
|
||||
{
|
||||
return rt_mmio_mint_token((struct pci_dev *)&iopci_device, 5, 0, BAR5_BYTES, 0);
|
||||
}
|
||||
|
||||
static unsigned int thread_count(void)
|
||||
{
|
||||
thread_act_array_t threads;
|
||||
mach_msg_type_number_t count;
|
||||
assert(task_threads(mach_task_self(), &threads, &count) == KERN_SUCCESS);
|
||||
for (mach_msg_type_number_t i = 0; i < count; i++)
|
||||
mach_port_deallocate(mach_task_self(), threads[i]);
|
||||
vm_deallocate(mach_task_self(), (vm_address_t)threads,
|
||||
count * sizeof(*threads));
|
||||
return count;
|
||||
}
|
||||
|
||||
struct footprint {
|
||||
size_t heap_allocations, heap_bytes;
|
||||
uint64_t dma_bytes; /* DART-mapped bytes (IOVA space held) */
|
||||
int dma_mappings;
|
||||
unsigned long pages; /* page descriptors held */
|
||||
unsigned int threads;
|
||||
};
|
||||
|
||||
static struct footprint footprint(void)
|
||||
{
|
||||
return (struct footprint) {
|
||||
.heap_allocations = dext_heap_test_live_allocations(),
|
||||
.heap_bytes = dext_heap_test_live_bytes(),
|
||||
.dma_bytes = linuxu_dart_used(),
|
||||
.dma_mappings = linuxu_dart_table_count(),
|
||||
.pages = linuxu_page_descriptors(),
|
||||
.threads = thread_count(),
|
||||
};
|
||||
}
|
||||
|
||||
/* One session. Returns upstream's probe result. */
|
||||
static int session_cycle(unsigned int cycle)
|
||||
{
|
||||
uint32_t token = session_open();
|
||||
if (token == RT_MMIO_TOKEN_INVALID) {
|
||||
fprintf(stderr, "cycle %u: the register token could not be minted\n", cycle);
|
||||
exit(1);
|
||||
}
|
||||
struct rt_device *device = rt_device_alloc();
|
||||
if (!device) {
|
||||
fprintf(stderr, "cycle %u: runtime device allocation failed\n", cycle);
|
||||
exit(1);
|
||||
}
|
||||
struct pci_dev *pdev = rt_device_get_pdev(device);
|
||||
const unsigned long mappings_before = bar5_mappings;
|
||||
assert(linuxu_driver_bootstrap() == 0);
|
||||
const int result = rt_pci_probe_result(pdev);
|
||||
/* Upstream mapped the registers (amdgpu_device_init's ioremap) and
|
||||
* unmapped them on its error path. */
|
||||
if (bar5_mappings == mappings_before) {
|
||||
fprintf(stderr, "cycle %u: probe %d before mapping the registers\n",
|
||||
cycle, result);
|
||||
exit(1);
|
||||
}
|
||||
assert(!pdev->dev.driver && !pci_get_drvdata(pdev));
|
||||
assert(!pdev->dev.devres && pdev->enable_cnt == 0);
|
||||
linuxu_driver_shutdown();
|
||||
rt_device_free(device);
|
||||
assert(rt_device_active_pdev() == NULL);
|
||||
rt_mmio_free_token(token);
|
||||
/* Every MMIO mapping of the session is gone, upstream's included. */
|
||||
if (rt_mmio_live_tokens()) {
|
||||
fprintf(stderr, "cycle %u: %u MMIO mappings outlived the session\n",
|
||||
cycle, rt_mmio_live_tokens());
|
||||
exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned int cycles = argc > 1 ? (unsigned int)strtoul(argv[1], NULL, 10) : 300;
|
||||
assert(cycles >= 2);
|
||||
|
||||
/* The first session initializes lazily created process-wide state. */
|
||||
const int expected = session_cycle(0);
|
||||
assert(expected < 0 && expected != -EAGAIN);
|
||||
const struct footprint baseline = footprint();
|
||||
for (unsigned int cycle = 1; cycle < cycles; cycle++) {
|
||||
const int result = session_cycle(cycle);
|
||||
const struct footprint now = footprint();
|
||||
if (result != expected) {
|
||||
fprintf(stderr, "cycle %u: probe returned %d, the first session returned %d\n",
|
||||
cycle, result, expected);
|
||||
return 1;
|
||||
}
|
||||
if (now.heap_allocations != baseline.heap_allocations ||
|
||||
now.heap_bytes != baseline.heap_bytes) {
|
||||
fprintf(stderr, "cycle %u: heap %zu allocations / %zu bytes, baseline %zu / %zu\n",
|
||||
cycle, now.heap_allocations, now.heap_bytes,
|
||||
baseline.heap_allocations, baseline.heap_bytes);
|
||||
return 1;
|
||||
}
|
||||
if (now.dma_bytes != baseline.dma_bytes ||
|
||||
now.dma_mappings != baseline.dma_mappings) {
|
||||
fprintf(stderr, "cycle %u: %d DMA mappings / %llu bytes, baseline %d / %llu\n",
|
||||
cycle, now.dma_mappings, (unsigned long long)now.dma_bytes,
|
||||
baseline.dma_mappings, (unsigned long long)baseline.dma_bytes);
|
||||
return 1;
|
||||
}
|
||||
if (now.pages != baseline.pages) {
|
||||
fprintf(stderr, "cycle %u: %lu page descriptors held, baseline %lu\n",
|
||||
cycle, now.pages, baseline.pages);
|
||||
return 1;
|
||||
}
|
||||
if (now.threads != baseline.threads) {
|
||||
fprintf(stderr, "cycle %u: %u threads, baseline %u\n",
|
||||
cycle, now.threads, baseline.threads);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
printf("%u session open/probe/close cycles: probe %d each time after mapping the registers, "
|
||||
"heap %zu allocations / %zu bytes, %d DMA mappings / %llu bytes, %lu page descriptors "
|
||||
"and %u threads at baseline\n",
|
||||
cycles, expected, baseline.heap_allocations, baseline.heap_bytes,
|
||||
baseline.dma_mappings, (unsigned long long)baseline.dma_bytes, baseline.pages,
|
||||
baseline.threads);
|
||||
return 0;
|
||||
}
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env bash
|
||||
# Session open/probe/close cycles (linuxu/tests/test_session_cycles.c): the
|
||||
# real upstream amdgpu probe, the production DriverKit PCI and MMIO-token
|
||||
# code and the production heap adapter, against a mock IOPCIDevice. Every
|
||||
# resource a session takes must return to its baseline after each close.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
make lib >/dev/null
|
||||
work_dir=$(mktemp -d "${TMPDIR:-/tmp}/linuxgpu-session-cycles.XXXXXX")
|
||||
trap 'rm -rf "$work_dir"' EXIT
|
||||
cat > "$work_dir/flags.mk" <<'MAKE'
|
||||
.PHONY: print-includes print-probe-objects
|
||||
print-includes:
|
||||
@printf '%s\n' $(INCPATHS)
|
||||
print-probe-objects:
|
||||
@printf '%s\n' $(DRIVER_OBJS) $(LINUXU_OBJS)
|
||||
MAKE
|
||||
include_string=$(make -s -f Makefile -f "$work_dir/flags.mk" print-includes | tr '\n' ' ')
|
||||
read -r -a include_flags <<< "$include_string"
|
||||
common=(-std=gnu11 -D__KERNEL__ -DCONFIG_DRM_FBDEV_OVERALLOC=0
|
||||
-include linux/autoconf.h -w -Wno-incompatible-function-pointer-types
|
||||
-ffunction-sections -fdata-sections)
|
||||
sanitizers=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer)
|
||||
heap_aliases=(-Dmalloc=dext_test_malloc -Dcalloc=dext_test_calloc
|
||||
-Drealloc=dext_test_realloc -Daligned_alloc=dext_test_aligned_alloc
|
||||
-Dfree=dext_test_free -Dstrdup=dext_test_strdup -Dstrndup=dext_test_strndup)
|
||||
objects=()
|
||||
while IFS= read -r object; do
|
||||
case "$object" in
|
||||
build/gen/*) source="$object" ;;
|
||||
build/linuxu/*) source="linuxu/src/${object#build/linuxu/}" ;;
|
||||
build/driver/drm-core/*) source="third_party/linux/drivers/gpu/drm/${object#build/driver/drm-core/}" ;;
|
||||
build/driver/ttm/*|build/driver/scheduler/*) source="third_party/linux/drivers/gpu/drm/${object#build/driver/}" ;;
|
||||
build/driver/*) source="third_party/linux/drivers/gpu/drm/amd/${object#build/driver/}" ;;
|
||||
*) echo "unknown probe object: $object" >&2; exit 1 ;;
|
||||
esac
|
||||
source="${source%.o}.c"
|
||||
extra=(-DLINUXU_RT_HOST_SHADOW=1)
|
||||
rebuild=0
|
||||
case "$source" in
|
||||
# The session's PCI device, MMIO tokens and register access are the
|
||||
# DriverKit build's.
|
||||
linuxu/src/amdgpu-rt/device.c|linuxu/src/pci/pci_stub.c|linuxu/src/pci/pdev_mmio.c|third_party/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c|third_party/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_reg_access.c)
|
||||
extra=(-DLINUXU_DEXT_DK=1); rebuild=1 ;;
|
||||
linuxu/src/kmem/kmemcheck.c|third_party/linux/drivers/gpu/drm/drm_managed.c|third_party/linux/drivers/gpu/drm/drm_debugfs.c)
|
||||
rebuild=1 ;;
|
||||
esac
|
||||
# Every source that allocates from the C heap uses the production adapter.
|
||||
if [[ -f "$source" ]] && grep -Eq '\b(malloc|free|calloc|realloc|strdup|strndup|aligned_alloc)[[:space:]]*\(' "$source"; then
|
||||
rebuild=1
|
||||
fi
|
||||
if (( rebuild )); then
|
||||
replacement="$work_dir/${object//\//_}"
|
||||
clang "${common[@]}" "${sanitizers[@]}" "${heap_aliases[@]}" "${extra[@]}" \
|
||||
"${include_flags[@]}" -c "$source" -o "$replacement"
|
||||
object="$replacement"
|
||||
fi
|
||||
objects+=("$object")
|
||||
done < <(make -s -f Makefile -f "$work_dir/flags.mk" print-probe-objects)
|
||||
ar rcs "$work_dir/libmacamgdu-cycles.a" "${objects[@]}"
|
||||
clang "${common[@]}" "${sanitizers[@]}" "${heap_aliases[@]}" -DLINUXU_DEXT_DK=1 \
|
||||
"${include_flags[@]}" -c linuxu/src/shims/dext_alloc.c -o "$work_dir/dext_alloc.o"
|
||||
clang "${sanitizers[@]}" -c linuxu/tests/dext_heap_backend.c -o "$work_dir/heap_backend.o"
|
||||
mkdir -p build/tests
|
||||
clang "${common[@]}" "${sanitizers[@]}" "${heap_aliases[@]}" -DLINUXU_DEXT_DK=1 "${include_flags[@]}" \
|
||||
linuxu/tests/test_session_cycles.c "$work_dir/dext_alloc.o" "$work_dir/heap_backend.o" \
|
||||
"$work_dir/libmacamgdu-cycles.a" \
|
||||
-Wl,-dead_strip -lpthread -o build/tests/test_session_cycles
|
||||
UBSAN_OPTIONS=halt_on_error=1:abort_on_error=0 build/tests/test_session_cycles "${SESSION_CYCLES:-300}"
|
||||
Reference in new issue
Block a user