|
3 | 3 | * This software may be used and distributed according to the terms of the |
4 | 4 | * GNU General Public License version 2. |
5 | 5 | * |
6 | | - * This header defines the 32-bit dispatch queue (DSQ) ID encoding |
| 6 | + * This header defines the 64-bit dispatch queue (DSQ) ID encoding |
7 | 7 | * scheme for scx_mitosis, using type fields to distinguish between |
8 | 8 | * per-CPU and cell+L3 domain queues. It includes helper functions to |
9 | 9 | * construct, validate, and parse these DSQ IDs for queue management. |
|
37 | 37 | * Only the low 32 bits are used. |
38 | 38 | * |
39 | 39 | * [63 .. 32] [31..0] |
40 | | - * [ 0s or unused ] [ VAL ] |
| 40 | + * [ 0][ unused ] [ VAL ] |
41 | 41 | * |
42 | 42 | * Mitosis uses VAL as follows: |
43 | 43 | * |
44 | | - * [31..24] [23..0] |
| 44 | + * [31..28] [27..0] |
45 | 45 | * [QTYPE ] [DATA ] |
46 | 46 | * |
47 | | - * QTYPE encodes the queue type (exactly one bit set): |
| 47 | + * QTYPE encodes the queue type: |
48 | 48 | * |
49 | 49 | * QTYPE = 0x1 -> Per-CPU Q |
50 | | - * [31 .. 24] [23 .. 16] [15 .. 0] |
51 | | - * [00000001] [00000000] [ CPU# ] |
| 50 | + * [31..28] [27 .. .. 0] |
| 51 | + * [ 0001 ] [ CPU# ] |
52 | 52 | * [Q-TYPE:1] |
53 | 53 | * |
54 | 54 | * QTYPE = 0x2 -> Cell+L3 Q |
55 | | - * [31 .. 24] [23 .. 16] [15 .. 0] |
56 | | - * [00000010] [ CELL# ] [ L3ID ] |
| 55 | + * [31..28] [27 .. 16] [15 .. 0] |
| 56 | + * [ 0010 ] [ CELL# ] [ L3ID ] |
57 | 57 | * [Q-TYPE:2] |
58 | 58 | * |
59 | 59 | */ |
| 60 | +/* |
| 61 | + * The use of these bitfields depends on compiler defined byte AND bit ordering. |
| 62 | + * Make sure we're only building with Clang/LLVM and that we're little-endian. |
| 63 | + */ |
| 64 | +#ifndef __clang__ |
| 65 | +#error "This code must be compiled with Clang/LLVM (eBPF: clang -target bpf)." |
| 66 | +#endif |
| 67 | + |
| 68 | +#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ |
| 69 | +#error "dsq64 bitfield layout assumes little-endian (bpfel)." |
| 70 | +#endif |
| 71 | + |
| 72 | +/* ---- Bitfield widths (bits) ---- */ |
| 73 | +#define CPU_B 28 |
| 74 | +#define L3_B 16 |
| 75 | +#define CELL_B 12 |
| 76 | +#define TYPE_B 4 |
| 77 | +#define DATA_B 28 |
| 78 | +#define RSVD_B 32 |
| 79 | + |
| 80 | +/* Sum checks (in bits) */ |
| 81 | +_Static_assert(CPU_B + TYPE_B == 32, "CPU layout low half must be 32 bits"); |
| 82 | +_Static_assert(L3_B + CELL_B + TYPE_B == 32, "CELL+L3 layout low half must be 32 bits"); |
| 83 | +_Static_assert(DATA_B + TYPE_B == 32, "Common layout low half must be 32 bits"); |
| 84 | + |
| 85 | +typedef union { |
| 86 | + u64 raw; |
60 | 87 |
|
61 | | -#define DSQ_ERROR 0xFFFFFFFF; /* Error value for DSQ functions */ |
| 88 | + /* Per-CPU user DSQ */ |
| 89 | + struct { u64 cpu: CPU_B; u64 type: TYPE_B; u64 rsvd: RSVD_B; } cpu_dsq; |
| 90 | + |
| 91 | + /* Cell+L3 user DSQ */ |
| 92 | + struct { u64 l3: L3_B; u64 cell: CELL_B; u64 type: TYPE_B; u64 rsvd: RSVD_B; } cell_l3_dsq; |
| 93 | + |
| 94 | + /* Generic user view */ |
| 95 | + struct { u64 data: DATA_B; u64 type: TYPE_B; u64 rsvd: RSVD_B; } user_dsq; |
| 96 | + |
| 97 | + /* Built-in DSQ view */ |
| 98 | + struct { u64 value:32; u64 rsvd:30; u64 local_on:1; u64 builtin:1; } builtin_dsq; |
| 99 | + |
| 100 | + /* NOTE: Considered packed and aligned attributes, but that's redundant */ |
| 101 | +} dsq_id_t; |
| 102 | + |
| 103 | +/* |
| 104 | + * Invalid DSQ ID Sentinel: |
| 105 | + * invalid bc bit 63 clear (it's a user DSQ) && dsq_type == 0 (no type) |
| 106 | + * Good for catching uninitialized DSQ IDs. |
| 107 | +*/ |
| 108 | +#define DSQ_INVALID ((u64) 0) |
| 109 | + |
| 110 | +_Static_assert(sizeof(((dsq_id_t){0}).cpu_dsq) == sizeof(u64), "cpu view must be 8 bytes"); |
| 111 | +_Static_assert(sizeof(((dsq_id_t){0}).cell_l3_dsq) == sizeof(u64), "cell+l3 view must be 8 bytes"); |
| 112 | +_Static_assert(sizeof(((dsq_id_t){0}).user_dsq) == sizeof(u64), "user common view must be 8 bytes"); |
| 113 | +_Static_assert(sizeof(((dsq_id_t){0}).builtin_dsq) == sizeof(u64), "builtin view must be 8 bytes"); |
| 114 | + |
| 115 | +/* Compile-time checks (in bytes) */ |
| 116 | +_Static_assert(sizeof(dsq_id_t) == sizeof(u64), "dsq_id_t must be 8 bytes (64 bits)"); |
| 117 | +_Static_assert(_Alignof(dsq_id_t) == sizeof(u64), "dsq_id_t must be 8-byte aligned"); |
62 | 118 |
|
63 | 119 | /* DSQ type enumeration */ |
64 | 120 | enum dsq_type { |
65 | | - DSQ_UNKNOWN, |
| 121 | + DSQ_TYPE_NONE, |
66 | 122 | DSQ_TYPE_CPU, |
67 | 123 | DSQ_TYPE_CELL_L3, |
68 | 124 | }; |
69 | 125 |
|
70 | | -/* DSQ ID structure using unions for type-safe access */ |
71 | | -struct dsq_cpu { |
72 | | - u32 cpu : 16; |
73 | | - u32 unused : 8; |
74 | | - u32 type : 8; |
75 | | -} __attribute__((packed)); |
76 | | - |
77 | | -struct dsq_cell_l3 { |
78 | | - u32 l3 : 16; |
79 | | - u32 cell : 8; |
80 | | - u32 type : 8; |
81 | | -} __attribute__((packed)); |
82 | | - |
83 | | -union dsq_id { |
84 | | - u32 raw; |
85 | | - struct dsq_cpu cpu; |
86 | | - struct dsq_cell_l3 cell_l3; |
87 | | - struct { |
88 | | - u32 data : 24; |
89 | | - u32 type : 8; |
90 | | - } common; |
91 | | -} __attribute__((packed)); |
92 | | - |
93 | | -/* Static assertions to ensure correct sizes */ |
94 | | -/* Verify that all DSQ structures are exactly 32 bits */ |
95 | | -_Static_assert(sizeof(struct dsq_cpu) == 4, "dsq_cpu must be 32 bits"); |
96 | | -_Static_assert(sizeof(struct dsq_cell_l3) == 4, "dsq_cell_l3 must be 32 bits"); |
97 | | -_Static_assert(sizeof(union dsq_id) == 4, "dsq_id union must be 32 bits"); |
98 | | - |
99 | | -/* Inline helper functions for DSQ ID manipulation */ |
| 126 | +/* Range guards */ |
| 127 | +_Static_assert(MAX_CPUS <= (1u << CPU_B), "MAX_CPUS must fit in field"); |
| 128 | +_Static_assert(MAX_L3S <= (1u << L3_B), "MAX_L3S must fit in field"); |
| 129 | +_Static_assert(MAX_CELLS <= (1u << CELL_B), "MAX_CELLS must fit in field"); |
| 130 | +_Static_assert(DSQ_TYPE_CELL_L3 < (1u << TYPE_B), "DSQ_TYPE_CELL_L3 must fit in field"); |
| 131 | + |
| 132 | +/* |
| 133 | + * While I considered error propagation, I decided to bail to force errors early. |
| 134 | +*/ |
| 135 | + |
| 136 | +static inline bool is_user_dsq(dsq_id_t dsq_id){ |
| 137 | + return !dsq_id.builtin_dsq.builtin && dsq_id.user_dsq.type != DSQ_TYPE_NONE; |
| 138 | +} |
100 | 139 |
|
101 | 140 | // Is this a per CPU DSQ? |
102 | | -static inline bool is_cpu_dsq(u32 dsq_id) |
| 141 | +static inline bool is_cpu_dsq(dsq_id_t dsq_id) |
103 | 142 | { |
104 | | - union dsq_id id = { .raw = dsq_id }; |
105 | | - return id.common.type == DSQ_TYPE_CPU; |
| 143 | + return is_user_dsq(dsq_id) && dsq_id.user_dsq.type == DSQ_TYPE_CPU; |
106 | 144 | } |
107 | 145 |
|
108 | 146 | // If this is a per cpu dsq, return the cpu |
109 | | -static inline u32 get_cpu_from_dsq(u32 dsq_id) |
| 147 | +static inline u32 get_cpu_from_dsq(u64 id) |
110 | 148 | { |
111 | | - union dsq_id id = { .raw = dsq_id }; |
112 | | - if (id.common.type != DSQ_TYPE_CPU) |
113 | | - return DSQ_ERROR; |
114 | | - return id.cpu.cpu; |
| 149 | + dsq_id_t dsq_id = (dsq_id_t) {.raw = id}; |
| 150 | + if (!is_cpu_dsq(dsq_id)) |
| 151 | + scx_bpf_error("trying to get cpu from non-cpu dsq\n"); |
| 152 | + |
| 153 | + return dsq_id.cpu_dsq.cpu; |
115 | 154 | } |
116 | 155 |
|
117 | 156 | /* Helper functions to construct DSQ IDs */ |
118 | | -static inline u32 get_cpu_dsq_id(u32 cpu) |
| 157 | +static inline u64 get_cpu_dsq_id(u32 cpu) |
119 | 158 | { |
| 159 | + // Check for valid CPU range, 0 indexed so >=. |
120 | 160 | if (cpu >= MAX_CPUS) |
121 | | - return DSQ_ERROR; |
122 | | - union dsq_id id = { .cpu = { .cpu = cpu, .unused = 0, .type = DSQ_TYPE_CPU } }; |
123 | | - return id.raw; |
| 161 | + scx_bpf_error("invalid cpu %u\n", cpu); |
| 162 | + dsq_id_t dsq_id = { .cpu_dsq = { .cpu = cpu, .type = DSQ_TYPE_CPU } }; |
| 163 | + |
| 164 | + return dsq_id.raw; |
124 | 165 | } |
125 | 166 |
|
126 | | -static inline u32 get_cell_l3_dsq_id(u32 cell, u32 l3) |
| 167 | +static inline u64 get_cell_l3_dsq_id(u32 cell, u32 l3) |
127 | 168 | { |
128 | 169 | if (cell >= MAX_CELLS || l3 >= MAX_L3S) |
129 | | - return DSQ_ERROR; |
130 | | - union dsq_id id = { .cell_l3 = {.l3 = l3, .cell = cell, .type = DSQ_TYPE_CELL_L3 } }; |
131 | | - return id.raw; |
| 170 | + scx_bpf_error("cell %u or l3 %u too large\n", cell, l3); |
| 171 | + dsq_id_t dsq_id = { .cell_l3_dsq = { .l3 = l3, .cell = cell, .type = DSQ_TYPE_CELL_L3 } }; |
| 172 | + |
| 173 | + return dsq_id.raw; |
132 | 174 | } |
0 commit comments