aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#504 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/0a694a4bd3f71db2.bundle -o 0a694a4bd3f71db2.bundle git fetch 0a694a4bd3f71db2.bundle refs/pull/504/head
Provenance
github-import 1
aien-dev/aienux
504
3a3c4bd73e63f67428ec795c5bf4f424c70b8247
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":504,"url":"https://github.com/aien-dev/aienux/pull/504","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:37Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 02d8e533..eaa06564 100644 --- a/.crumb +++ b/.crumb @@ -33,8 +33,8 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "e4a6c8a97c0df7d9bda79a63342f74c0b06b18ebee1d0de31d4bf1099470bbe2", - "last_commit": "6b91f3f0", + "digest": "93e6e9eab515a36245e770ac5789a51b33daaa56ebddb6d6c7070fc65f047cd3", + "last_commit": "47973bb4", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, @@ -51,15 +51,15 @@ "purpose": "First-party host artifact preparation tools" } ], - "children_root": "sha256:617c10b4433852ffcb2c18c1105369d747910a51eee27f0aafd9afc5f1d9222b", - "compiled_at": "2026-11-06T00:33:49Z", + "children_root": "sha256:1032cc94531222dcaeafffcae91ddde712858e8a68df4845cfd719c62333798b", + "compiled_at": "2026-11-06T00:50:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:6e62fd06c16bd1d2449db58ca39b3d11aca9342348bd81a115fa00d384d9e7a0", + "digest": "sha256:c4c9b96af51c878bbb675600b396ddd610fe2ca282f86f6fcb4c4ed4a2991ed6", "evidence_files": 274, "evidence_root": "sha256:af014a4ed8f6fe759fc5809807fb47578d3410811b522d9f65d749382b029802", "files": 3777, - "generated_at_commit": "6b91f3f0e54cd3323f6f1459460b4bf0c09e1c80", - "source_tree": "sha256:e1fb5804ef0bb6e1a9de98c69db475893b367bae7a314bc131837ef043a99667" + "generated_at_commit": "47973bb401c35ff45002e8296a216a1558f663e0", + "source_tree": "sha256:1c3133c0831aeb85637ddb725ab4a6aba7bca9b3c7b594a69fd36bdd5e813d97" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index 598faafa..994e24c8 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "cd715446fbb6b337b4a64cb07273017cbd1a67eea0d1435d1ad25c96ff7cce16", - "last_commit": "6b91f3f0", + "digest": "4dc3dd1b9414534e1880c835a10a98b74c9440b4c59929b491b8b985f502206e", + "last_commit": "47973bb4", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -115,15 +115,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:01bdb8eb493ab4ad63a208ede8e69a0f611395240543791b90d5f881719e6645", - "compiled_at": "2026-11-06T00:33:49Z", + "children_root": "sha256:672b890a47c3ea517cd06d74b4feefce2f83dcfecb83a9d25d4b9a9d63e839a8", + "compiled_at": "2026-11-06T00:50:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:e4a6c8a97c0df7d9bda79a63342f74c0b06b18ebee1d0de31d4bf1099470bbe2", + "digest": "sha256:93e6e9eab515a36245e770ac5789a51b33daaa56ebddb6d6c7070fc65f047cd3", "evidence_files": 24, "evidence_root": "sha256:ecc7ccf2e924c221a5bac1d91e23b13d8f3c8d78c2a893a882ed7ca5e88d855d", "files": 777, - "generated_at_commit": "6b91f3f0e54cd3323f6f1459460b4bf0c09e1c80", - "source_tree": "sha256:eb6369205a9a161cec1c2f9d863cccc70b2d0c31609f0de40a5e11dbd09616a5" + "generated_at_commit": "47973bb401c35ff45002e8296a216a1558f663e0", + "source_tree": "sha256:2734116d52606f13968e529e2c832db22f98b81468b54ad8eba4bf37427797d1" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index a5916447..1ee1e2cf 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -58,19 +58,19 @@ }, { "digest": "82f5b707835d6baec04a4d7e87debf00054d9e16fd090292e0f121e56bf05b30", - "last_commit": "6b91f3f0", + "last_commit": "38d322ee", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "0b8f0332d884b0f6e39eecb4a487ca0be5435609af49dceb0d33cb0354947ae9", - "last_commit": "6b91f3f0", + "last_commit": "38d322ee", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "5157b513cd269418b046ab3661ca4e68c25bbe8bd709a8cc8f9be213173e9cef", - "last_commit": "5b1ba463", + "last_commit": "38d322ee", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -82,13 +82,13 @@ }, { "digest": "87e4d6c053c07b5933ce205d638c8611a390747d11452629f4767191f2a4d8ea", - "last_commit": "6b91f3f0", + "last_commit": "38d322ee", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "7f717340268adbe7ed0e7831a1905f23229bcce12b7690a406fcf29c9efe9f7a", - "last_commit": "6b91f3f0", + "digest": "f973f76cc4e15d69d53f58665ce12638eb309d2f7bbf32351cbf44d93354e242", + "last_commit": "47973bb4", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:6951316af457a69ea725d160b4f221c43879b68b1ab4630df394798b2d0c60d2", - "compiled_at": "2026-11-06T00:33:49Z", + "children_root": "sha256:2e64f2252781aeb26b438840dc0cf6e91f9c4fb1485cc2d6f40afcae25aa2842", + "compiled_at": "2026-11-06T00:50:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:cd715446fbb6b337b4a64cb07273017cbd1a67eea0d1435d1ad25c96ff7cce16", + "digest": "sha256:4dc3dd1b9414534e1880c835a10a98b74c9440b4c59929b491b8b985f502206e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 521, - "generated_at_commit": "6b91f3f0e54cd3323f6f1459460b4bf0c09e1c80", - "source_tree": "sha256:52f8fca126fc3db1f6bd44a0251941373cec1967bbb794cd02a7979a7286b735" + "generated_at_commit": "47973bb401c35ff45002e8296a216a1558f663e0", + "source_tree": "sha256:ef8ccbed3b99fd9dcc2c4d3a4248e133bd8362c449e1d2a74b8aeaac22daf0ec" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 00adc3a0..503ade1a 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -3028,7 +3028,8 @@ gpu-freestanding: echo "gpu-freestanding: PASS (gmmu gpu_mem gpu_chan built with KCC/KCFLAGS, linked -r, no undefined symbol except the PIE GOT)" # Each mutant removes one check; test_gpu_sim must FAIL for every one. GPU_SIM_MUTANTS := NO_OVERLAP NO_PA_RANGE NO_VA_BITS NO_POOL_COUNT NO_INVAL_POLL NO_TIMEOUT_LATCH NO_QUARANTINE \ - NO_POLLED_UNCACHED NO_GP_VA40 NO_RING_FULL NO_IDLE_QUARANTINE NO_MIXED_PAGES NO_INST_PDB NO_CHID_WIDTH NO_USERD_WIDTH NO_DISABLE + NO_POLLED_UNCACHED NO_GP_VA40 NO_RING_FULL NO_IDLE_QUARANTINE NO_MIXED_PAGES NO_INST_PDB NO_CHID_WIDTH NO_USERD_WIDTH NO_DISABLE \ + NO_PDE_CHECK NO_POOL_OVERLAP gpu-sim-mutants: @mkdir -p $(OUT)/host @fail=0; for m in $(GPU_SIM_MUTANTS); do \ diff --git a/native/kernel/gpu/gmmu.c b/native/kernel/gpu/gmmu.c index d1bd2aee..6c8af055 100644 --- a/native/kernel/gpu/gmmu.c +++ b/native/kernel/gpu/gmmu.c @@ -74,6 +74,38 @@ static int pt_alloc(struct ck_gmmu_as *as, uint64_t *pa) return CK_GMMU_ENOMEM; } +/* A table address read back from table memory must name a page of this + * address space's pool that is in use. Anything else (a stray or corrupted + * PDE) would make the walk read and write outside the page tables. */ +static int pt_live(const struct ck_gmmu_as *as, uint64_t pa) +{ +#ifdef CK_GPU_MUTANT_NO_PDE_CHECK + (void)as; + (void)pa; + return 1; +#else + for (uint32_t i = 0; i < as->pt_count; i++) + if (as->pt_pa[i] == pa) + return as->pt_used[i] != 0; + return 0; +#endif +} + +static int overlaps_pool(const struct ck_gmmu_as *as, uint64_t pa, uint64_t size) +{ +#ifdef CK_GPU_MUTANT_NO_POOL_OVERLAP + (void)as; + (void)pa; + (void)size; + return 0; +#else + for (uint32_t i = 0; i < as->pt_count; i++) + if (as->pt_pa[i] < pa + size && pa < as->pt_pa[i] + CK_GMMU_PT_BYTES) + return 1; + return 0; +#endif +} + static uint32_t pt_free_count(const struct ck_gmmu_as *as) { uint32_t n = 0; @@ -98,12 +130,14 @@ int ck_gmmu_as_init(struct ck_gmmu_as *as, const struct ck_gpu_window *win, uint return pt_alloc(as, &as->root_pa); } -enum walk_r { W_FREE, W_MAPPED, W_MISSING, W_CONFLICT }; +enum walk_r { W_FREE, W_MAPPED, W_MISSING, W_CONFLICT, W_CORRUPT }; /* Walk to the leaf slot for `va` at page size `page`. With create=0 nothing * is written; *miss_lv gets the first level whose table does not exist yet * (W_MISSING). With create=1 missing tables are allocated (the caller has - * already checked the pool). *leaf is the PTE slot on W_FREE / W_MAPPED. */ + * already checked the pool). *leaf is the PTE slot on W_FREE / W_MAPPED. + * W_CORRUPT: a PDE read back from table memory does not point at a live page + * of this pool; nothing was dereferenced through it. */ static enum walk_r walk(struct ck_gmmu_as *as, uint64_t va, uint64_t page, int create, uint32_t *miss_lv, volatile uint64_t **leaf) { @@ -125,6 +159,8 @@ static enum walk_r walk(struct ck_gmmu_as *as, uint64_t va, uint64_t page, int c v = *e; } tbl = v & PDE_ADDR_MASK; + if (!pt_live(as, tbl)) + return W_CORRUPT; } volatile uint64_t *d = ent(as, tbl, lv_index(LV_PD0, va), 16); uint64_t w0 = d[0], w1 = d[1]; @@ -167,6 +203,8 @@ static enum walk_r walk(struct ck_gmmu_as *as, uint64_t va, uint64_t page, int c d[0] = nd[0]; } else { pt = big ? (w0 & DUAL_BIG_ADDR_MASK) : (w1 & PTE_ADDR_MASK); + if (!pt_live(as, pt)) + return W_CORRUPT; } volatile uint64_t *p = ent(as, pt, lv_index(big ? LV_BIG : LV_SMALL, va), 8); *leaf = p; @@ -206,6 +244,8 @@ int ck_gmmu_map(struct ck_gmmu_as *as, uint64_t va, uint64_t pa, uint64_t size, if (pa >= (1ull << CK_GMMU_PA_BITS) || !pa_in_window(as->win, pa, size)) return CK_GMMU_ERANGE; #endif + if (overlaps_pool(as, pa, size)) + return CK_GMMU_ERANGE; /* a GPU mapping never covers its own page tables */ /* Dry run: every page must be free, and count the page-table pages the * map would create. Pages run in VA order, so a table instance at level @@ -218,6 +258,10 @@ int ck_gmmu_map(struct ck_gmmu_as *as, uint64_t va, uint64_t pa, uint64_t size, uint32_t miss = 0; volatile uint64_t *leaf = 0; enum walk_r w = walk(as, va + off, page, 0, &miss, &leaf); + if (w == W_CORRUPT) { + as->dead = 1; + return CK_GMMU_EDEAD; + } #ifndef CK_GPU_MUTANT_NO_OVERLAP if (w == W_MAPPED || w == W_CONFLICT) #else @@ -267,7 +311,12 @@ int ck_gmmu_unmap(struct ck_gmmu_as *as, uint64_t va, uint64_t size, uint64_t pa for (uint64_t off = 0; off < size; off += page) { uint32_t miss = 0; volatile uint64_t *leaf = 0; - if (walk(as, va + off, page, 0, &miss, &leaf) != W_MAPPED) + enum walk_r w = walk(as, va + off, page, 0, &miss, &leaf); + if (w == W_CORRUPT) { + as->dead = 1; + return CK_GMMU_EDEAD; + } + if (w != W_MAPPED) return CK_GMMU_ENOENT; } for (uint64_t off = 0; off < size; off += page) { diff --git a/native/kernel/gpu/gmmu.h b/native/kernel/gpu/gmmu.h index 6a5d0a26..ec850b66 100644 --- a/native/kernel/gpu/gmmu.h +++ b/native/kernel/gpu/gmmu.h @@ -106,12 +106,13 @@ enum ck_gmmu_status { CK_GMMU_OK = 0, CK_GMMU_EINVAL, /* bad argument: size, page size, flags */ CK_GMMU_EALIGN, /* VA, PA or size not aligned to the page size */ - CK_GMMU_ERANGE, /* VA beyond 57 bits, or PA outside the window */ + CK_GMMU_ERANGE, /* VA beyond 57 bits, PA outside the window, or PA over the page-table pool */ CK_GMMU_EBUSY, /* overlaps an existing mapping or a different page size */ CK_GMMU_ENOMEM, /* page-table pool exhausted */ CK_GMMU_ENOENT, /* unmap of a range that is not fully mapped */ CK_GMMU_ETIMEDOUT, /* invalidate never completed */ - CK_GMMU_EDEAD, /* the address space latched a failure; refuses everything */ + CK_GMMU_EDEAD, /* the address space latched a failure (invalidate timeout, or a PDE + * read back that names no live page of the pool); refuses everything */ }; #define CK_GMMU_MAX_PT 64u /* page-table pages per address space (bounded) */ @@ -122,7 +123,7 @@ struct ck_gmmu_as { uint64_t pt_pa[CK_GMMU_MAX_PT]; /* page-table pool, carved by the caller */ uint8_t pt_used[CK_GMMU_MAX_PT]; uint32_t pt_count; - uint32_t dead; /* latched after an invalidate timeout */ + uint32_t dead; /* latched after an invalidate timeout or a corrupt PDE */ }; /* pool_pa: CK_GMMU_MAX_PT * 4 KiB inside the window, 4 KiB aligned; pages diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index b88fb356..49e53f2c 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-11-06T00:33:49Z", + "compiled_at": "2026-11-06T00:50:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:7f717340268adbe7ed0e7831a1905f23229bcce12b7690a406fcf29c9efe9f7a", + "digest": "sha256:f973f76cc4e15d69d53f58665ce12638eb309d2f7bbf32351cbf44d93354e242", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 221, - "generated_at_commit": "6b91f3f0e54cd3323f6f1459460b4bf0c09e1c80", - "source_tree": "sha256:06613542d81428a195766f5b9c7a6975ea9721e91d41b2dd081ea0ab1377aa1c" + "generated_at_commit": "47973bb401c35ff45002e8296a216a1558f663e0", + "source_tree": "sha256:f93908806410e11a1b945b1ebae3cdf0485f49f2d172809e16fb7dbde6d23bd4" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_gpu_sim.c b/native/kernel/tests/test_gpu_sim.c index 561a593b..80c949c6 100644 --- a/native/kernel/tests/test_gpu_sim.c +++ b/native/kernel/tests/test_gpu_sim.c @@ -484,6 +484,59 @@ static void test_gp_va40(void) CHECK(ck_gpu_chan_teardown(&ch, 2, 0, 0) == CK_GCHAN_OK); } +/* Entry slot of `va` at level lv (0 = PD4 root .. 4 = PD0, 16-byte dual + * entries), found by following the PDEs gmmu wrote. */ +static uint64_t *pd_slot(uint64_t va, unsigned lv) +{ + static const uint8_t lo[5] = {56, 47, 38, 29, 21}, bits[5] = {1, 9, 9, 9, 8}; + uint64_t tbl = as.root_pa; + for (unsigned l = 0;; l++) { + uint64_t idx = (va >> lo[l]) & ((1ull << bits[l]) - 1); + uint64_t *p = (uint64_t *)(mem + (tbl - PA_BASE) + idx * (l == 4 ? 16u : 8u)); + if (l == lv) + return p; + tbl = *p & 0x000FFFFFFFFFF000ull; + } +} + +/* 11. #493 N2/N3: a PDE read back from table memory must name a live page + * of the pool (else latch EDEAD, touch nothing), and no mapping may cover + * the page-table pool itself. */ +static void test_pde_and_pool(void) +{ + uint64_t va = VA_BASE, junk = DATA_PA + 0x10000; + uint8_t before[4096]; + + /* Map path: a PD3 entry redirected into data memory (0xA5 garbage). */ + fresh(64); + CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK); + *pd_slot(va, 1) = ck_gmmu_pde(junk, CK_GMMU_PDE_AP_SYS_COH); + memcpy(before, mem + (junk - PA_BASE), sizeof before); + CHECK(ck_gmmu_map(&as, va + 0x1000, DATA_PA + 0x1000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EDEAD); + CHECK(as.dead == 1); + CHECK(memcmp(before, mem + (junk - PA_BASE), sizeof before) == 0); + CHECK(ck_gmmu_map(&as, va + 0x200000, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_EDEAD); /* latched */ + + /* Unmap path: the PD0 small-table half redirected into data memory. */ + fresh(64); + CHECK(ck_gmmu_map(&as, va, DATA_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK); + uint64_t nd[2]; + ck_gmmu_dual_pde(nd, 0, CK_GMMU_PDE_AP_INVALID, junk, CK_GMMU_PDE_AP_SYS_COH); + pd_slot(va, 4)[1] = nd[1]; + memcpy(before, mem + (junk - PA_BASE), sizeof before); + CHECK(ck_gmmu_unmap(&as, va, 0x1000, CK_GMMU_SZ_4K) == CK_GMMU_EDEAD); + CHECK(as.dead == 1); + CHECK(memcmp(before, mem + (junk - PA_BASE), sizeof before) == 0); /* no PTE cleared in data */ + + /* Pool overlap: 8 page-table pages at POOL_PA. */ + fresh(8); + CHECK(ck_gmmu_map(&as, va, POOL_PA, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* the root */ + CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x7000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* last page */ + CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x6000, 0x4000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_ERANGE); /* straddles end */ + CHECK(ck_gmmu_map(&as, va, POOL_PA + 0x8000, 0x1000, CK_GMMU_SZ_4K, 2, 0) == CK_GMMU_OK); /* first page after */ + CHECK(as.dead == 0); +} + int main(void) { mem = aligned_alloc(4096, MEM_BYTES); @@ -499,6 +552,7 @@ int main(void) test_reopen(); test_huge_levels(); test_gp_va40(); + test_pde_and_pool(); free(mem); return ck_t_verdict("test_gpu_sim"); } diff --git a/native/linux/aien-gpu/aien_gpu.c b/native/linux/aien-gpu/aien_gpu.c index 2aa7fa65..2f56aca2 100644 --- a/native/linux/aien-gpu/aien_gpu.c +++ b/native/linux/aien-gpu/aien_gpu.c @@ -17,8 +17,10 @@ * (gpu/contract/gb10_gsp.devc). Writes need a second switch, allow_boot=1, and CAP_SYS_RAWIO. The SEC2 * doorbell (QUEUE_HEAD(0)) can be rung once per load: a failed start-up is never retried without a reset. * remove sets gone under the device lock first. After that, register access, INFO and mmap return -ENODEV. - * A live arena VMA or an open fd keeps the arena (leak until reboot) instead of freeing it. sysfs unbind - * is not offered (suppress_bind_attrs). + * A live arena VMA or an open fd keeps the arena (leak until reboot) instead of freeing it. A probe after + * remove (PCI rescan) is refused with -EBUSY while such an fd, VMA or a rung doorbell survives; otherwise it + * clears gone and the info. The lock is static, never re-initialised. arena_mib must be 1..SIZE_MAX >> 20. + * sysfs unbind is not offered (suppress_bind_attrs). * The start-up logic itself (gpu/gsp_mmio.c, dev/gb10_gsp_boot.c) runs in user space (aien_gsp_boot) over these two * ioctls, so the driver core stays AGPL and outside this GPL module. GB10B has no FSP: SEC2 takes the start-up packet. * What comes next (not here): the RPC subset (docs/numeric/DRIVER_FLOOR.md), the channel from @@ -85,11 +87,12 @@ static struct { dma_addr_t heap_iova; size_t heap_bytes; struct aien_gpu_info info; - struct mutex lock; /* register access, the doorbell latch, and gone */ + struct mutex lock; /* register access, the doorbell latch, and gone; initialised once, never per probe */ atomic_t open; /* one opener at a time; stays set until the last file ref drops */ atomic_t maps; /* live arena VMAs (mmap counts; vm_ops open/close track splits) */ - bool gone; /* set at the start of remove; register, INFO and mmap then return -ENODEV */ -} g; + bool gone; /* set at the start of remove; register, INFO and mmap then return -ENODEV. + * A later probe clears it only when no fd, VMA or doorbell survives. */ +} g = { .lock = __MUTEX_INITIALIZER(g.lock) }; static bool aien_gpu_listed(const u32 *list, size_t n, u32 off) { @@ -244,6 +247,22 @@ static int aien_gpu_probe(struct pci_dev *pdev, const struct pci_device_id *id) dev_info(&pdev->dev, "aien-gpu: allow_bind=0, not binding\n"); return -ENODEV; } + if (!arena_mib || arena_mib > SIZE_MAX >> 20) { + dev_err(&pdev->dev, "aien-gpu: arena_mib %u out of range (1..SIZE_MAX >> 20)\n", arena_mib); + return -EINVAL; + } + /* Probe after remove (PCI remove + rescan). An fd or arena VMA from the previous binding, or an + * attempted GSP start-up (arena and heap leaked until reboot), keeps the device refused. Otherwise + * start from a clean state: the lock is static, so an old release() can never see it re-initialised. */ + mutex_lock(&g.lock); + if (g.gone && (atomic_read(&g.open) || atomic_read(&g.maps) || g.info.doorbells)) { + mutex_unlock(&g.lock); + dev_warn(&pdev->dev, "aien-gpu: previous binding still has an fd, a mapping or a GSP start-up; reboot first\n"); + return -EBUSY; + } + g.gone = false; + memset(&g.info, 0, sizeof(g.info)); + mutex_unlock(&g.lock); rc = pcim_enable_device(pdev); if (rc) return rc; @@ -254,7 +273,6 @@ static int aien_gpu_probe(struct pci_dev *pdev, const struct pci_device_id *id) g.info.dma_bits = AIEN_GPU_DMA_BITS; g.info.allow_boot = allow_boot; g.info.bar0_pa = pci_resource_start(pdev, 0); - mutex_init(&g.lock); g.bar0 = pcim_iomap(pdev, 0, 0); if (!g.bar0) return -ENOMEM;