1#include "mem/slab/tests/test_internal.h"
2#include <acpi/lapic.h>
3#include <irq/ipi.h>
4#include <irq/irq.h>
5
6#define VFREE_IRQ_TEST_SPIN_MASK 0x3f
7static void **vfree_irq_allocs = NULL;
8static size_t vfree_irq_total_allocs = 0;
9static atomic_size_t vfree_irq_test_consumed = 0;
10
11static enum irq_result vfree_irq_test_irq(struct irq_context *ictx, void *ctx) {
12 cc_ignore(ctx, ictx);
13 size_t total = vfree_irq_total_allocs;
14 int midrange = total / 10;
15 int delta = (prng_next() % (midrange * 2)) - midrange;
16 int possible = midrange + delta;
17 if (possible < 1)
18 possible = 1;
19
20 if (possible + atomic_load(&vfree_irq_test_consumed) > total)
21 possible = total - atomic_load(&vfree_irq_test_consumed);
22
23 for (int i = 0; i < possible; i++) {
24 size_t idx = atomic_inc(&vfree_irq_test_consumed);
25 if (idx < total) {
26 vfree_defer_irq(ptr: vfree_irq_allocs[idx]);
27 int spins = prng_next() & VFREE_IRQ_TEST_SPIN_MASK;
28
29 while (spins) {
30 cpu_pause();
31 spins--;
32 }
33 }
34 }
35
36 return IRQ_HANDLED;
37}
38
39TEST_DEFINE_INTEGRATION(slab, vfree_defer_irq, TEST_INTENSITY(256, 2048, 16384),
40 .min_cores = 4) {
41 size_t total = ctx->intensity_val ? ctx->intensity_val : 2048;
42 vfree_irq_total_allocs = total;
43 vfree_irq_allocs = vnews(vfree_irq_allocs, total);
44 TEST_ASSERT_NONNULL(vfree_irq_allocs);
45
46 atomic_store(&vfree_irq_test_consumed, 0);
47
48 irq_t irq = irq_alloc_entry();
49 irq_register(name: "vfree_defer_irq_test", vector: irq, handler: vfree_irq_test_irq, NULL,
50 flags: IRQ_FLAG_NONE);
51 irq_set_chip(vector: irq, chip: lapic_get_chip(), NULL);
52
53 for (size_t i = 0; i < total; i++) {
54 vfree_irq_allocs[i] = valloc(64);
55 TEST_ASSERT_NONNULL(vfree_irq_allocs[i]);
56 }
57
58 while (atomic_load(&vfree_irq_test_consumed) < total) {
59 ipi_send(cpu: 3, vector: irq);
60 int spins = prng_next() & VFREE_IRQ_TEST_SPIN_MASK;
61
62 while (spins) {
63 cpu_pause();
64 spins--;
65 }
66 }
67
68 vfree(vfree_irq_allocs);
69 vfree_irq_allocs = NULL;
70
71 return TEST_SUCCESS;
72}
73