diff --git a/drivers/accel/ane/ane_fw_transport.h b/drivers/accel/ane/ane_fw_transport.h index e0e1a0edd1211..c5ff95e755a81 100644 --- a/drivers/accel/ane/ane_fw_transport.h +++ b/drivers/accel/ane/ane_fw_transport.h @@ -723,7 +723,9 @@ ane_fw_transport_free(struct ane_fw_transport *t, void *slot, u64 dva, u64 bytes * firmware frees it after UNLOAD (often only during a later program), but the * backing stays mapped until reset: without reuse the 12 MiB quota ran out at * the sixth program of a boot. Only memory the firmware itself gave back by - * FREE is served again, and only for the same tag and size. + * FREE, or that the host reserved for the request + * (ane_fw_transport_shared_reserve()), is served again, and only for the + * same tag and size. */ static inline unsigned int ane_fw_shared_reusable(const struct ane_fw_transport *t, size_t size, u64 tag) @@ -737,11 +739,80 @@ ane_fw_shared_reusable(const struct ane_fw_transport *t, size_t size, u64 tag) return t->shared_count; } +/* + * A new shared allocation of size bytes under tag, within the host's caps + * (ANE_FW_SHARED_MAX entries, ANE_FW_SHARED_TOTAL bytes): its index, live. + */ +static inline int ane_fw_shared_add(struct ane_fw_transport *t, size_t size, u64 tag, + unsigned int *index) +{ + struct ane_fw_surface s = {}; + unsigned int i; + int ret; + + if (t->shared_count == ANE_FW_SHARED_MAX || size > ANE_FW_SHARED_TOTAL - + t->shared_bytes) + return -ENOSPC; + ret = t->ops->alloc(t->ctx, size, tag, &s); + if (ret) + return ret; + if (!ane_fw_surface_valid(&s) || !IS_ALIGNED(s.dva, SZ_16K) || + s.size != size || ane_fw_surfaces_overlap(&s, &t->ipc) || + ane_fw_surfaces_overlap(&s, &t->pool)) + return -EPROTO; + for (i = 0; i < t->shared_count; i++) + if (ane_fw_surfaces_overlap(&s, &t->shared[i])) + return -EPROTO; + *index = t->shared_count; + t->shared_tags[t->shared_count] = tag; + t->shared_retired[t->shared_count] = false; + t->shared[t->shared_count++] = s; + t->shared_bytes += size; + return 0; +} + +/* + * Make sure a later SHAREDMALLOC request of size bytes under tag is served + * from memory the host already has: a retired allocation of that tag and + * size, or a new one made now and kept retired for it. The firmware waits + * for the answer to every request, with its shared-memory lock held, and a + * request the host cannot serve is never answered: the command behind it + * stalls and only a power cycle recovers the engine. A request the host + * knows is coming is therefore taken before the command that causes it, so + * running out of memory or quota (-ENOMEM, -ENOSPC) refuses that command + * before the firmware sees it. + */ +static inline int ane_fw_transport_shared_reserve(struct ane_fw_transport *t, size_t size, + u32 tag) +{ + unsigned int i; + int ret; + + if (t->error) + return t->error; + if (!size || !IS_ALIGNED(size, SZ_16K)) + return -EINVAL; + if (ane_fw_shared_reusable(t, size, tag) < t->shared_count) + return 0; + ret = ane_fw_shared_add(t, size, tag, &i); + /* + * An invalid surface from the backend is a host error, as it is for a + * request (ane_fw_transport_shared()): its mapping is not recorded, so + * retrying would map another one at every LOAD. + */ + if (ret == -EPROTO) + return ane_fw_transport_fail(t, ret); + if (ret) + return ret; + t->shared_retired[i] = true; + ane_fw_shared_note(t, "reserve", i); + return 0; +} + static inline int ane_fw_transport_irq(struct ane_fw_transport *t, u32 *pending); static inline int ane_fw_transport_shared(struct ane_fw_transport *t) { - struct ane_fw_surface s = {}; u64 word0, word1, word2; size_t size; unsigned int i; @@ -801,33 +872,13 @@ static inline int ane_fw_transport_shared(struct ane_fw_transport *t) ane_fw_shared_note(t, "reuse", i); return 0; } - if (t->shared_count == ANE_FW_SHARED_MAX || size > ANE_FW_SHARED_TOTAL - - t->shared_bytes) { - ret = -ENOSPC; - goto refused; - } - ret = t->ops->alloc(t->ctx, size, word2, &s); + ret = ane_fw_shared_add(t, size, word2, &i); if (ret) goto refused; - if (!ane_fw_surface_valid(&s) || !IS_ALIGNED(s.dva, SZ_16K) || - s.size != size || ane_fw_surfaces_overlap(&s, &t->ipc) || - ane_fw_surfaces_overlap(&s, &t->pool)) { - ret = -EPROTO; - goto refused; - } - for (i = 0; i < t->shared_count; i++) { - if (ane_fw_surfaces_overlap(&s, &t->shared[i])) { - ret = -EPROTO; - goto refused; - } - } - t->shared_tags[t->shared_count] = word2; - t->shared[t->shared_count++] = s; - t->shared_bytes += size; t->ops->store64(t->ctx, p + 8, 0); - t->ops->store64(t->ctx, p + 16, t->shared_count); + t->ops->store64(t->ctx, p + 16, i + 1); t->ops->write_barrier(t->ctx); - t->ops->store64(t->ctx, p, s.dva | 1); + t->ops->store64(t->ctx, p, t->shared[i].dva | 1); t->ops->write_barrier(t->ctx); ane_fw_transport_doorbell(t, ANE_FW_SHAREDMALLOC); ane_fw_transport_advance(t, ANE_FW_SHAREDMALLOC); diff --git a/drivers/accel/ane/ane_fw_transport_test.c b/drivers/accel/ane/ane_fw_transport_test.c index c38415ada6c6b..207a7e27f8fb5 100644 --- a/drivers/accel/ane/ane_fw_transport_test.c +++ b/drivers/accel/ane/ane_fw_transport_test.c @@ -1186,6 +1186,87 @@ static void ane_transport_pool_no_free_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ane_fw_transport_shared(&f->t), -ENOSPC); } +/* + * A reserved allocation is kept retired and serves the request it was made + * for, with no allocation then; a full quota or no memory refuses the + * reservation without failing the transport or touching the ring. + */ +static void ane_transport_shared_reserve_test(struct kunit *test) +{ + struct ane_transport_test *f = test->priv; + unsigned int i; + u64 dva; + + KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0); + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, 1, ANE_FW_SHARED_EXPP), + -EINVAL); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + 0); + KUNIT_EXPECT_EQ(test, f->allocs, 1U); + KUNIT_EXPECT_TRUE(test, f->t.shared_retired[0]); + dva = f->t.shared[0].dva; + /* Already there: no second allocation. */ + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + 0); + KUNIT_EXPECT_EQ(test, f->allocs, 1U); + ane_test_shared_request(f, 0, SZ_2M, ANE_FW_SHARED_EXPP); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + KUNIT_EXPECT_EQ(test, f->allocs, 1U); + KUNIT_EXPECT_FALSE(test, f->t.shared_retired[0]); + KUNIT_EXPECT_EQ(test, ane_test_load(f, ane_test_slot(f, ANE_FW_SHAREDMALLOC) - + ANE_FW_SLOT_SIZE), dva | 1); + + /* No memory: refused, and nothing recorded. */ + f->alloc_error = -ENOMEM; + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + -ENOMEM); + KUNIT_EXPECT_EQ(test, f->t.shared_count, 1U); + KUNIT_EXPECT_EQ(test, f->t.error, 0); + f->alloc_error = 0; + f->allocs = 1; + + /* Fill the quota with live pools; one more is refused cleanly. */ + for (i = 1; i < ANE_FW_SHARED_TOTAL / SZ_2M; i++) { + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, + ANE_FW_SHARED_EXPP), 0); + ane_test_shared_request(f, 0, SZ_2M, ANE_FW_SHARED_EXPP); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + } + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + -ENOSPC); + KUNIT_EXPECT_EQ(test, f->t.error, 0); + KUNIT_EXPECT_EQ(test, f->refusals, 0U); + + /* A FREE gives one back, which serves the next reservation. */ + f->t.native_free = true; + ane_test_shared_request(f, dva, 0, 0); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + 0); + KUNIT_EXPECT_EQ(test, f->allocs, (unsigned int)(ANE_FW_SHARED_TOTAL / SZ_2M)); + KUNIT_EXPECT_EQ(test, f->t.error, 0); +} + +/* + * A backend surface that fails validation fails the transport, as it does for + * a request, so a later LOAD does not map another one. + */ +static void ane_transport_shared_reserve_invalid_test(struct kunit *test) +{ + struct ane_transport_test *f = test->priv; + + KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0); + f->alloc_bad = 1; + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + -EPROTO); + KUNIT_EXPECT_EQ(test, f->t.error, -EPROTO); + KUNIT_EXPECT_EQ(test, f->t.shared_count, 0U); + KUNIT_EXPECT_EQ(test, f->t.shared_bytes, 0UL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP), + -EPROTO); + KUNIT_EXPECT_EQ(test, f->allocs, 1U); +} + /* A FREE posted after UNLOAD is answered by the settle loop. */ static void ane_transport_settle_test(struct kunit *test) { @@ -1550,6 +1631,8 @@ static struct kunit_case ane_transport_cases[] = { KUNIT_CASE(ane_transport_pool_reuse_test), KUNIT_CASE(ane_transport_pool_reuse_native_test), KUNIT_CASE(ane_transport_pool_no_free_test), + KUNIT_CASE(ane_transport_shared_reserve_test), + KUNIT_CASE(ane_transport_shared_reserve_invalid_test), KUNIT_CASE(ane_transport_settle_test), KUNIT_CASE(ane_transport_carry_shared_test), KUNIT_CASE(ane_transport_mode_stop_test), diff --git a/drivers/accel/ane/ane_h16_program.c b/drivers/accel/ane/ane_h16_program.c index e7d9c67bc89a9..ea16d1235aac7 100644 --- a/drivers/accel/ane/ane_h16_program.c +++ b/drivers/accel/ane/ane_h16_program.c @@ -631,6 +631,36 @@ static int h16_pool_room(struct ane_h16_session *s, const size_t *lengths, return 0; } +/* + * Every CREATE makes the firmware ask for a 2 MiB EXPP process pool by + * SHAREDMALLOC and wait for it before it answers; a pool it could not get + * leaves the CREATE stalled and the engine quarantined, and with it every + * other program of the session. Take the pool before LOAD instead + * (ane_fw_transport_shared_reserve()), so that without memory or quota the + * LOAD fails cleanly, before any traffic. A FREE of a torn-down program's + * pool may still be posted (it can come after the post-UNLOAD settle): + * serve it and try once more before giving up. + */ +static int h16_process_pool(struct ane_h16_session *s) +{ + int ret = ane_fw_transport_shared_reserve(s->transport, SZ_2M, ANE_FW_SHARED_EXPP); + + if (ret == -ENOSPC) { + ret = ane_fw_transport_settle(s->transport, s->settle_ns, + max(s->settle_ns, ANE_H16_SETTLE_LIMIT_NS)); + if (ret < 0) + return h16_fail(s, ret); + ret = ane_fw_transport_shared_reserve(s->transport, SZ_2M, + ANE_FW_SHARED_EXPP); + } + if (ret && s->transport->error) + return h16_fail(s, ret); + if (ret) + s->ops->log(s->ctx, "LOAD: no process pool", (u64)-ret, + s->transport->shared_bytes); + return ret; +} + /* * Outside a request batch, the commands published since mark were all * acknowledged and the firmware keeps no pointer into them: give their @@ -1101,6 +1131,9 @@ int ane_h16_session_load(struct ane_h16_session *s, if (ret) return ret; ret = h16_load_room(s); + if (ret) + return ret; + ret = h16_process_pool(s); if (ret) return ret; p = kzalloc_obj(*p); @@ -2074,6 +2107,9 @@ static int h16_replay(struct ane_h16_session *s, struct ane_h16_program *p) unsigned int i; int ret; + ret = h16_process_pool(s); + if (ret) + return ret; command = kzalloc(ANE_H16_CREATE_SIZE, GFP_KERNEL); if (!command) return -ENOMEM; diff --git a/drivers/accel/ane/ane_h16_program_test.c b/drivers/accel/ane/ane_h16_program_test.c index a72ee6616f8d7..109bb5b711293 100644 --- a/drivers/accel/ane/ane_h16_program_test.c +++ b/drivers/accel/ane/ane_h16_program_test.c @@ -16,6 +16,8 @@ #define FW_IPC_DVA 0x100003d0000ULL #define FW_POOL_DVA 0x10000384000ULL #define FW_MAP_DVA 0x10001f00000ULL +/* Shared (SHAREDMALLOC) allocations, far above the program mappings. */ +#define FW_SHARED_DVA 0x10010000000ULL #define FW_RING_SLOTS 8 #define FW_REPORTS 0x8000 @@ -87,6 +89,22 @@ struct prog_fw { */ u64 live, window; unsigned int unmaps, map_fail_at; + /* + * SHAREDMALLOC: with expp, CREATE asks for a 2 MiB EXPP pool and is + * answered only once the host served it, and UNLOAD frees the pool + * again, as the H16/H17 firmware does. irq holds the pending + * notification bits; allocs counts ops->alloc calls, alloc_fail + * makes them fail with -ENOMEM. + */ + bool expp, alloc_fail; + u32 irq; + u64 shared_dva; + unsigned int allocs; + /* The CREATE waiting for its pool, by IO slot; its program ID. */ + u8 *create_slot; + u64 create_length, create_dva; + u32 create_program; + u64 expp_dva[32]; }; static u64 fw_load(void *ctx, const void *p) @@ -105,7 +123,9 @@ static void fw_barrier(void *ctx) static u32 fw_read32(void *ctx, u32 reg) { - return 0; + struct prog_fw *f = ctx; + + return reg == f->soc.irq_status ? f->irq : 0; } static u8 *fw_slot(struct prog_fw *f, unsigned int ch) @@ -118,6 +138,17 @@ static void fw_advance(struct prog_fw *f, unsigned int ch) f->head[ch] = (f->head[ch] + 1) % f->t.channels[ch].slots; } +/* A SHAREDMALLOC request in the firmware's next slot, notified. */ +static void fw_shared_post(struct prog_fw *f, u64 word0, u64 bytes, u64 tag) +{ + u8 *slot = fw_slot(f, ANE_FW_SHAREDMALLOC); + + put_unaligned_le64(bytes, slot + 8); + put_unaligned_le64(tag, slot + 16); + put_unaligned_le64(word0, slot); + f->irq |= BIT(ANE_FW_SHAREDMALLOC); +} + static void fw_report(struct prog_fw *f, u64 uuid, u32 program, u32 process, u32 state) { u32 offset = FW_REPORTS + (f->reports++ % 16) * 0x60; @@ -232,8 +263,16 @@ static void fw_command(struct prog_fw *f, unsigned int ch) cmd + ANE_H16_LOAD_ID_OFFSET); break; case ANE_H16_UNLOAD_PROGRAM: - if (get_unaligned_le32(cmd + 8) - f->program < 32) - f->programs_live &= ~BIT(get_unaligned_le32(cmd + 8) - f->program); + if (get_unaligned_le32(cmd + 8) - f->program < 32) { + u32 i = get_unaligned_le32(cmd + 8) - f->program; + + f->programs_live &= ~BIT(i); + /* The pool comes back by FREE, after the UNLOAD reply. */ + if (f->expp && f->expp_dva[i]) { + fw_shared_post(f, f->expp_dva[i], 0, 0); + f->expp_dva[i] = 0; + } + } break; case ANE_H16_TERMINATE_PROCESS: if (get_unaligned_le32(cmd + 0xc) - f->process < 32) @@ -244,6 +283,16 @@ static void fw_command(struct prog_fw *f, unsigned int ch) put_unaligned_le32(fw_take_id(&f->processes_live, f->process), cmd + 0xc); if (f->mode == FW_CREATE_BODY) cmd[0x100] = 1; + if (f->expp) { + /* No reply until the host served the process pool. */ + f->create_slot = slot; + f->create_length = length; + f->create_dva = dva; + f->create_program = get_unaligned_le32(cmd + 8); + fw_shared_post(f, 0, SZ_2M, ANE_FW_SHARED_EXPP); + fw_advance(f, ch); + return; + } break; case ANE_H16_EXECUTE: f->exec_length = min_t(size_t, length, sizeof(f->exec)); @@ -258,12 +307,35 @@ static void fw_command(struct prog_fw *f, unsigned int ch) fw_execute(f, cmd); } +/* The host answered the SHAREDMALLOC slot; a CREATE waiting for it completes. */ +static void fw_shared_answered(struct prog_fw *f) +{ + u8 *slot = fw_slot(f, ANE_FW_SHAREDMALLOC); + u64 word0 = get_unaligned_le64(slot); + + if (!(word0 & 1)) + return; + fw_advance(f, ANE_FW_SHAREDMALLOC); + if (word0 == 1 || !f->create_slot) + return; + if (f->create_program - f->program < 32) + f->expp_dva[f->create_program - f->program] = word0 & ~1ULL; + put_unaligned_le64(f->create_length, f->create_slot + 8); + put_unaligned_le64(0, f->create_slot + 16); + put_unaligned_le64(f->create_dva | 1, f->create_slot); + f->create_slot = NULL; +} + static void fw_write32(void *ctx, u32 reg, u32 value) { struct prog_fw *f = ctx; + if (reg == f->soc.irq_ack) + f->irq &= ~value; if (reg != f->soc.doorbell) return; + if (value & BIT(ANE_FW_SHAREDMALLOC)) + fw_shared_answered(f); /* Commands take doorbell_ns to answer; returning report slots is free. */ if (value & (BIT(ANE_FW_IO) | BIT(ANE_FW_DATA_CHAIN))) f->now += f->doorbell_ns; @@ -294,9 +366,22 @@ static void fw_relax(void *ctx, u64 waited_ns) } } +static void *fw_zalloc(struct kunit *test, size_t size); + static int fw_alloc(void *ctx, size_t size, u32 tag, struct ane_fw_surface *s) { - return -ENOMEM; + struct prog_fw *f = ctx; + + f->allocs++; + if (f->alloc_fail) + return -ENOMEM; + *s = (struct ane_fw_surface) { + .cpu = fw_zalloc(f->test, size), .dva = f->shared_dva, .size = size, + }; + if (!s->cpu) + return -ENOMEM; + f->shared_dva += size; + return 0; } static void fw_refused(void *ctx, u64 w0, u64 w1, u64 w2, int error) @@ -481,6 +566,7 @@ static struct prog_fw *fw_create_family(struct kunit *test, unsigned int family) put_unaligned_le64(f->ipc.dva + 0x800 + i * 0x200, p + 0x50); } f->next_dva = FW_MAP_DVA; + f->shared_dva = FW_SHARED_DVA; f->program = 0x11; f->process = 0; if (ane_fw_transport_init(&f->t, &f->soc, &fw_ops, f, f->ipc, f->pool, SZ_16K, @@ -3355,6 +3441,133 @@ static void program_unmap_refused_load_test(struct kunit *test) KUNIT_EXPECT_EQ(test, fw_cycle(f), 0); } +/* As the firmware does at boot: a small shared allocation of its own. */ +static void fw_boot_allocations(struct kunit *test, struct prog_fw *f) +{ + fw_shared_post(f, 0, SZ_16K, 0x43434d44); + KUNIT_ASSERT_EQ(test, ane_fw_transport_irq(&f->t, &(u32){ 0 }), 0); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); +} + +/* + * Every live program holds a 2 MiB EXPP process pool, which the firmware + * asks for during CREATE and waits for. Six programs at once on the Neo + * went past the 12 MiB shared quota: the pool was refused, the CREATE + * stalled, and the quarantine took the five other programs with it. Now + * the LOAD past the quota fails with -ENOSPC before any traffic, the + * engine stays up, the other programs keep running, and a closed + * program's pool serves the next LOAD. + */ +static void program_process_pool_quota_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p[ANE_FW_SHARED_TOTAL / SZ_2M + 1] = {}, *q = NULL; + unsigned int i, n = 0, count, allocs; + int ret; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_hung; + f->t.native_free = true; + f->expp = true; + fw_boot_allocations(test, f); + while (n < ARRAY_SIZE(p)) { + count = f->count; + ret = fw_load_program(f, &p[n]); + if (ret) + break; + KUNIT_EXPECT_EQ(test, f->count, count + 2); + n++; + } + /* 16 KiB of the quota is the firmware's: room for one pool less. */ + KUNIT_EXPECT_EQ(test, n, (unsigned int)(ANE_FW_SHARED_TOTAL / SZ_2M - 1)); + KUNIT_EXPECT_EQ(test, ret, -ENOSPC); + KUNIT_EXPECT_PTR_EQ(test, p[n], NULL); + KUNIT_EXPECT_EQ(test, f->count, count); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + KUNIT_EXPECT_EQ(test, f->hung, 0U); + KUNIT_EXPECT_EQ(test, f->t.error, 0); + for (i = 0; i < n; i++) + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, p[i]), 0); + + /* The FREE after UNLOAD makes the pool serve the next LOAD. */ + allocs = f->allocs; + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p[0]), 0); + ane_h16_session_put(&f->s, p[0]); + KUNIT_ASSERT_EQ(test, fw_load_program(f, &q), 0); + KUNIT_EXPECT_EQ(test, f->allocs, allocs); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, q), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, q), 0); + ane_h16_session_put(&f->s, q); + for (i = 1; i < n; i++) { + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p[i]), 0); + ane_h16_session_put(&f->s, p[i]); + } + /* Load and close cycles take no new pool. */ + for (i = 0; i < 20; i++) + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + KUNIT_EXPECT_EQ(test, f->allocs, allocs); + KUNIT_EXPECT_EQ(test, f->hung, 0U); +} + +/* No memory for the pool: -ENOMEM before any traffic, and the next LOAD works. */ +static void program_process_pool_nomem_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *a = NULL, *b = NULL; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_hung; + f->t.native_free = true; + f->expp = true; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &a), 0); + KUNIT_EXPECT_EQ(test, f->count, 2U); + f->alloc_fail = true; + KUNIT_EXPECT_EQ(test, fw_load_program(f, &b), -ENOMEM); + KUNIT_EXPECT_PTR_EQ(test, b, NULL); + KUNIT_EXPECT_EQ(test, f->count, 2U); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + KUNIT_EXPECT_EQ(test, f->hung, 0U); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, a), 0); + f->alloc_fail = false; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &b), 0); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, b), 0); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, a), 0); + KUNIT_EXPECT_EQ(test, f->t.shared_count, 2U); + KUNIT_EXPECT_EQ(test, f->hung, 0U); +} + +/* + * A FREE the firmware posts only after the post-UNLOAD settle: at the quota + * the LOAD serves it and takes that pool instead of failing. + */ +static void program_process_pool_late_free_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p[ANE_FW_SHARED_TOTAL / SZ_2M] = {}, *q = NULL; + unsigned int i, allocs; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_hung; + f->t.native_free = true; + f->expp = true; + for (i = 0; i < ARRAY_SIZE(p); i++) + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p[i]), 0); + KUNIT_EXPECT_EQ(test, f->t.shared_bytes, (size_t)ANE_FW_SHARED_TOTAL); + /* Torn down, with the FREE held back past the settle. */ + f->expp = false; + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p[0]), 0); + ane_h16_session_put(&f->s, p[0]); + f->expp = true; + fw_shared_post(f, f->expp_dva[0], 0, 0); + f->expp_dva[0] = 0; + allocs = f->allocs; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &q), 0); + KUNIT_EXPECT_EQ(test, f->allocs, allocs); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, q), 0); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + KUNIT_EXPECT_EQ(test, f->hung, 0U); +} + static struct kunit_case program_cases[] = { KUNIT_CASE_SLOW(program_pool_rewind_test), KUNIT_CASE_SLOW(program_pool_resident_test), @@ -3415,6 +3628,9 @@ static struct kunit_case program_cases[] = { KUNIT_CASE(program_h17_sections_test), KUNIT_CASE(program_h17_mobilenet_test), KUNIT_CASE(program_h17_switch_test), + KUNIT_CASE(program_process_pool_quota_test), + KUNIT_CASE(program_process_pool_nomem_test), + KUNIT_CASE(program_process_pool_late_free_test), {} };