XenevaOS
Loading...
Searching...
No Matches
slab.h
Go to the documentation of this file.
1#ifndef __LINUX_SLAB_H__
2#define __LINUX_SLAB_H__
3
4#include <Mm/kmalloc.h>
5#include <Mm/vmmngr.h> /* PAGE_SIZE -- tty_buffer.c computes TTY_BUFFER_PAGE from it */
6#include <linux/kernel.h> /* gfp_t, uintptr_t, E2BIG */
7#include <linux/gfp_types.h> /* __GFP_ZERO -- the one gfp bit this
8 * header does not drop, read by
9 * dcl_kmalloc_flags() below */
10#include <linux/overflow.h> /* struct_size/flex_array_size -- mainline's
11 * <linux/slab.h> takes it at :18, and
12 * lib/dma-resv.c's dma_resv_list_alloc()
13 * sizes its fence array with struct_size().
14 * DCL's copy of this header had no such
15 * include, so overflow.h was reachable from
16 * nowhere in the tree and struct_size simply
17 * was not a macro in that translation unit. */
18#include <linux/sprintf.h> /* _vsnprintf, for kasprintf below */
19#include <linux/string.h> /* memcpy -- kmemdup()'s copy (added below) */
20/*
21 * asm/cache.h for ARCH_DMA_MINALIGN, and the reason is the same one the
22 * overflow.h comment above gives: mainline reaches it as
23 * asm/cache.h -> linux/cache.h, and in this tree linux/cache.h is included by
24 * exactly four headers (debug_locks.h, mmzone.h, dma-mapping.h, itself), none
25 * of which drm_managed.c includes. So the definition sat in asm/cache.h with
26 * no includer reaching it, and drm_managed.c:56's
27 * `u8 __aligned(ARCH_DMA_MINALIGN) data[]` failed with the identifier
28 * undeclared. slab.h is the path rather than the new home -- the definition
29 * stays in asm/cache.h where mainline puts it -- and it is a reasonable one
30 * because ARCH_DMA_MINALIGN is a statement about what an allocator may
31 * promise, which is what this header already owns in ARCH_KMALLOC_MINALIGN.
32 */
33#include <asm/cache.h> /* ARCH_DMA_MINALIGN */
34/*
35 * kdev_t.h for old_encode_dev(), which is not an allocator dependency: the
36 * two debug messages drm_ioctl.c:884/:923 build from a dev_t call it, and
37 * their translation unit reaches neither fs.h nor cdev.h, the two headers
38 * that carry kdev_t.h in mainline and in this tree. It does reach this one
39 * (drm_crtc.h -> hdmi.h -> device.h -> devres.h), so the include is made
40 * here rather than restating a definition that already exists in exactly
41 * one place. Where the pinned fs.h has already spelled MAJOR/MINOR/MKDEV,
42 * kdev_t.h's spellings of the same three re-expand to the same values
43 * (MINORBITS is 20 and fs.h shifts by 20), so that case is
44 * -Wmacro-redefined and nothing more.
45 */
46#include <linux/kdev_t.h>
47
48#define KMALLOC_SHIFT_HIGH 22
49#define KMALLOC_MIN_SIZE 8
50
51/*
52 * ARCH_KMALLOC_MINALIGN -- the alignment every kmalloc() return already has,
53 * from mainline's <linux/slab.h>. 8, the alignment of the widest basic type,
54 * because Xeneva's allocator hands out TLSF blocks that are at least 8-byte
55 * aligned (Mm/kmalloc.h); nothing here can promise a cache line.
56 *
57 * One caller: drm_format_helper.c:190 rounds its scratch-buffer length up to
58 * it before aligning the per-plane offsets inside that buffer, so the number
59 * is load-bearing for the *layout* of the buffer rather than only for whether
60 * an allocation is legal. Under-promising it (say 1) would leave a plane
61 * starting at an unaligned offset; over-promising it (a cache line) would
62 * waste up to 56 bytes per scratch buffer and would be a claim the allocator
63 * does not actually make.
64 */
65#ifndef ARCH_KMALLOC_MINALIGN
66#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
67#endif
68
69struct kmem_cache;
70
71/*
72 * mainline: kmalloc(size, gfp). Xeneva's allocator takes the size only and
73 * DCL drops the flags -- GFP_KERNEL is 0 here -- with one exception:
74 * __GFP_ZERO selects the zeroing path. It cannot be dropped, because
75 * vendored code reaches the zeroing through the flag and nowhere else:
76 * drmm_kzalloc() calls drmm_kmalloc(dev, size, gfp | __GFP_ZERO), and
77 * drm_managed.c's alloc_dr() builds the whole drmres block -- header plus
78 * payload -- with one kmalloc_node_track_caller(tot_size, gfp, nid). Ignore
79 * the flag and drmm_kzalloc() hands back whatever TLSF held last: vkms's
80 * plane came back with a garbage properties.count, and the first
81 * drm_object_attach_property() turned it into a wild store.
82 * dcl_kmalloc_flags() below is the dispatch that reads it.
83 *
84 * Zero-size requests are the one disagreement vendored code trips over:
85 * mainline's kmalloc(0) -- and kzalloc(0), kcalloc(0, ...) -- answer with
86 * ZERO_SIZE_PTR, which is non-NULL, and callers are written for that.
87 * drm_atomic_commit_init() is the example that found this:
88 *
89 * state->colorops = kzalloc_objs(*state->colorops, num_colorop);
90 * if (!state->colorops)
91 * goto fail;
92 *
93 * and a driver with no color pipelines reports num_colorop == 0 -- vkms
94 * does -- so a NULL here becomes -ENOMEM on every atomic commit before the
95 * ioctl has looked at anything the caller sent. The native allocators
96 * refuse zero (Mm/kmalloc.c: both return NULL for an empty request, kmalloc
97 * on `!size`, kcalloc on either factor), so the request is rounded up to
98 * one byte rather than special-cased: the pointer stays real, zeroed where
99 * the name promises it, and freeable -- none of which ZERO_SIZE_PTR would
100 * be here, since kfree() expects a TLSF block.
101 *
102 * The helper below is expanded at a point where the macro does not exist
103 * yet, so its body resolves to the native one-argument kmalloc() from
104 * Mm/kmalloc.h; no recursion, and native DCL files that include
105 * Mm/kmalloc.h directly keep calling the plain function.
106 */
107static inline void* dcl_kmalloc(unsigned int size) {
108 return kmalloc(size ? size : 1);
109}
110
111/* defined with the rest of the kzalloc family further down */
112static inline void* dcl_kzalloc(unsigned int size);
113
114/* __GFP_ZERO -> the zeroing route, everything else -> a plain block. This is
115 * the one GFP bit that has to be read rather than dropped; the block above
116 * records the drmm_kzalloc() path that made that true. */
117static inline void* dcl_kmalloc_flags(unsigned int size, gfp_t flags) {
118 if (flags & __GFP_ZERO)
119 return dcl_kzalloc(size);
120 return dcl_kmalloc(size);
121}
122
123#define kmalloc(size, flags) dcl_kmalloc_flags(size, flags)
124
125/*
126 * kcalloc(n, size, flags) -- the same shape as kmalloc above, except DCL's
127 * native kcalloc() (Mm/kmalloc.h) takes two arguments and mainline's takes
128 * three, and the two cannot both be named `kcalloc`.
129 *
130 * The trailing `...` rather than a named third parameter is the point: this
131 * one macro has to accept both spellings. BaseHdr's own dcl_kzalloc() above
132 * calls `kcalloc(1, size)` and every native caller of Mm/kmalloc.h still
133 * does, while Vendored/lib/string_helpers.c:768 writes
134 * `kcalloc(n, sizeof(char *), gfp)` with a flag word DCL drops anyway. A
135 * named third parameter would break the two-argument callers, which is the
136 * opposite trade to the one that works.
137 *
138 * Like dcl_kmalloc, dcl_kcalloc is defined before the macro exists so that
139 * its body resolves to the native two-argument function rather than to
140 * itself; the comment at the top of this header is the same reasoning.
141 *
142 * An empty product (either factor 0) takes the same one-byte route the
143 * kmalloc block above explains: mainline answers kcalloc(0, ...) with
144 * ZERO_SIZE_PTR, the native allocator answers NULL, and vendored callers
145 * check for the former.
146 */
147static inline void* dcl_kcalloc(size_t n, size_t size) {
148 if (!n || !size)
149 return kcalloc(1, 1);
150 return kcalloc(n, size);
151}
152
153#define kcalloc(n, size, ...) dcl_kcalloc((n), (size))
154
155/*
156 * krealloc(ptr, size, flags) -- the realloc shape of the same problem.
157 * DCL's native krealloc() (Mm/kmalloc.h) takes two arguments, mainline's
158 * takes three, and the third is dropped for the same reason the others are:
159 * TLSF ignores gfp flags entirely.
160 *
161 * The variadic third parameter again, so that the native two-argument
162 * spelling keeps working -- it is the only spelling anywhere in the tree
163 * today besides the vendored three-argument ones (drm_atomic.c:1005,
164 * drm_edid.c:2146, drm_format_helper.c:86 and others), so both have to
165 * compile in the same header.
166 *
167 * The size is taken as size_t and narrowed to the native unsigned int.
168 * Every vendored caller passes a value it just computed as
169 * `count * sizeof(*item)`, so the narrowing is a truncation risk that
170 * already exists on the native side (kmalloc() takes unsigned int too) and
171 * is not widened here: making just this one entry point 64-bit would let a
172 * caller believe the allocation was sized faithfully when the allocator
173 * underneath still truncates. Fixing that means changing the native
174 * allocator's signature, which is a separate decision.
175 */
176static inline void* dcl_krealloc(void* ptr, size_t size) {
177 return krealloc(ptr, (unsigned int)size);
178}
179
180#define krealloc(ptr, size, ...) dcl_krealloc((ptr), (size))
181
182/*
183 * krealloc_array(ptr, n, size, flags) -- mainline's: an overflow-checked
184 * n * size, then krealloc(). The check is the whole point of the helper --
185 * drm_atomic.c:1300 and dma-resv.c:591 both grow an array whose length
186 * comes from connector/fence counts, and a wrapped product would allocate
187 * a small block and then be filled as a large one. Mainline routes it
188 * through array_size(); DCL has that in <linux/overflow.h>, included above,
189 * so the multiplication is exactly mainline's and not a re-derivation.
190 */
191#define krealloc_array(ptr, n, size, flags) \
192 dcl_krealloc((ptr), array_size((n), (size)))
193
194/*
195 * kmalloc_size_roundup(size) -- mainline's is an extern (slab.h:1463) with
196 * its body in mm/slab_common.c, and the one caller here is
197 * dma-resv.c:104:
198 *
199 * size = kmalloc_size_roundup(struct_size(list, table, max_fences));
200 * list = kmalloc(size, GFP_KERNEL);
201 * list->max_fences = (size - offsetof(typeof(*list), table)) /
202 * sizeof(*list->table);
203 *
204 * so `size` is used twice: as the request, and then -- by subtracting the
205 * header -- as the basis for how many fence pointers fit. The contract
206 * mainline is satisfying is "the allocator will actually use at least this
207 * many bytes", which is what makes the second line safe.
208 *
209 * DCL returns the request unchanged rather than pretending to a bucket
210 * table: TLSF rounds internally and exposes no way to ask a size what it
211 * became, and inventing a rounding here would be a number that looks like
212 * allocator knowledge this tree does not have. Unchanged satisfies "at
213 * least this many" exactly, and the capacity arithmetic that follows comes
214 * out at exactly the struct_size requested -- the extra room TLSF may hand
215 * back is simply not claimed. Under-reporting the capacity wastes a few
216 * slots; over-reporting it would write past the block.
217 */
218static inline size_t kmalloc_size_roundup(size_t size)
219{
220 return size;
221}
222
223/*
224 * kzalloc(size, flags) -- allocate and zero.
225 *
226 * DCL needs this as its own thing because the native allocator does not zero.
227 * kmalloc() hands back whatever the TLSF free list last held; only kcalloc()
228 * (KernelAA64/Mm/kmalloc.c) memsets, and it does so because *it* is the one
229 * promising zeroed memory. Going through kcalloc(1, size) rather than
230 * kmalloc()+memset() means the zeroing is one native call with one size
231 * computation instead of an allocate-then-sweep, and it is the same code
232 * path that already carries the "this must be zero" contract.
233 *
234 * This matters more than it looks: virtio-rng.c does
235 *
236 * vi = kzalloc_obj(struct virtrng_info);
237 *
238 * and then reads vi->data_avail, vi->data_idx, vi->hwrng_register_done and
239 * vi->hwrng_removed before ever writing them. With a non-zeroing kmalloc()
240 * that is a garbage entropy-buffer length, a random memcpy bound, and a
241 * driver that may skip or repeat its own cleanup -- all invisible until the
242 * numbers happen to be large.
243 *
244 * The flags argument is accepted and dropped: zeroing here is unconditional
245 * rather than opt-in, so the caller cannot ask for less than the name
246 * promises. kmalloc() is the one that has to read __GFP_ZERO, and its block
247 * above records why.
248 *
249 * size 0 gets one byte rather than the native NULL, for the contract reason
250 * the kmalloc block above records -- kzalloc_objs() over a zero count is how
251 * that path was found, and it arrives here.
252 */
253static inline void* dcl_kzalloc(unsigned int size) {
254 return kcalloc(1, size ? size : 1);
255}
256
257#define kzalloc(size, flags) dcl_kzalloc(size)
258
259/*
260 * kzalloc_obj(TYPE, [flags]) -- mainline's "one object of this type, zeroed":
261 *
262 * vi = kzalloc_obj(struct virtrng_info);
263 *
264 * mainline's slab.h spells it `__alloc_objs(kzalloc, default_gfp(__VA_ARGS__),
265 * typeof(TYPE), 1)`, i.e. sizeof(typeof(TYPE)) bytes with room for exactly one.
266 *
267 * The trailing flags are accepted and dropped, matching kmalloc_obj() below
268 * and for the same reason. This used to be a single named parameter on the
269 * argument that a silent no-op flag is worse than a syntax error: a caller
270 * writing `kzalloc_obj(T, GFP_KERNEL)` would be told at once that the flag
271 * went nowhere. That trade no longer holds, because the caller that turned
272 * up -- drm_modeset_lock.c:151, `kzalloc_obj(*ctx, GFP_KERNEL | __GFP_NOFAIL)`
273 * -- lives in Vendored/, which has to stay byte-identical to v7.2 and so
274 * cannot be rewritten to the one-argument spelling. The error would be
275 * correct and unactionable, which is worse than either alternative.
276 *
277 * Note the argument is a dereference at that call site, `*ctx` rather than a
278 * type name; sizeof() takes both, so nothing special is needed for it.
279 *
280 * __GFP_NOFAIL in particular deserves its mention: it means the allocation
281 * may not fail, which DCL's TLSF heap cannot promise for any gfp value. The
282 * call site is guarded by an earlier size check that makes the failure
283 * unreachable in practice, so the flag is satisfied by construction rather
284 * than by the allocator. Nothing else in gfp_t is honoured either.
285 */
286DEFINE_FREE(kfree, void*, kfree(_T))
287
288#define kzalloc_obj(type, ...) dcl_kzalloc((unsigned int)sizeof(type))
289
290/*
291 * kmalloc_obj(type, ...) -- the uninitialised twin of kzalloc_obj, and the
292 * two shapes the drivers actually write:
293 *
294 * p->em485 = kmalloc_obj(struct uart_8250_em485, GFP_ATOMIC);
295 * (8250_port.c:560)
296 * port = kmalloc_obj(struct port);
297 * (virtio_console.c:1328, :1974)
298 *
299 * kzalloc_obj() takes the type alone; kmalloc_obj() historically took flags
300 * as a second parameter, which is why the trailing parameter is `...` and not
301 * a named one: mainline's spelling varies by call site, and a macro that
302 * insists on exactly two rejects the one-argument caller with "too few
303 * arguments provided to function-like macro invocation" -- an error about the
304 * macro's arity that says nothing about which side is wrong.
305 *
306 * The flags are swallowed rather than honoured for the reason the block below
307 * gives at length: GFP_ATOMIC means "must not sleep", DCL's allocator does
308 * not block on anything -- it is a bump over the TLSF heap -- so the guarantee
309 * GFP_ATOMIC exists to make is satisfied whether or not the flag is looked at.
310 * Nothing else in gfp_t is honoured either (see the flags comment above
311 * kmalloc_flex: an unrecognised gfp bit should be a compile error there
312 * rather than a silently different allocation -- and with `...` it is now
313 * silently accepted, which is the one thing this change trades away).
314 */
315#define kmalloc_obj(type, ...) dcl_kmalloc((unsigned int)sizeof(type))
316
317/*
318 * kmalloc_flex(p, member, count, flags) -- allocate "struct + count of the
319 * flexible array".
320 *
321 * The call site is tty_buffer.c:187:
322 *
323 * p = kmalloc_flex(*p, data, 2 * size, GFP_ATOMIC | __GFP_NOWARN);
324 *
325 * -- the first argument is a *dereference*, because mainline's macro (slab.h
326 * v7.2) is `__alloc_flex(kmalloc, gfp, typeof(VAR_OR_TYPE), FAM, COUNT)`,
327 * i.e. `typeof(*p)` -- the struct, and the size follows from that type. The
328 * spelling below has to reproduce that, and the trap is where the parameter
329 * sits. Writing `sizeof(*(p))` substitutes `*p` into `*(p)`, giving
330 * `sizeof(*(*p))` -- a dereference of a struct -- which is precisely the
331 * "indirection requires pointer operand" error clang reported on the first
332 * build. So the parameter appears bare: `typeof(p)` with the argument tokens
333 * `*p` becomes `typeof(*p)`, the struct itself -- and a struct is not a
334 * pointer, so `(typeof(p))0` fails again ("used type 'typeof (*p)' (aka
335 * 'struct tty_buffer') where arithmetic or pointer type is required"). The
336 * null pointer comes from `typeof(&p)`: `&*p` is `struct tty_buffer *`, and
337 * `((typeof(&p))0)->data` is then an ordinary member access through a null
338 * pointer, unevaluated, giving sizeof(u8) == 1.
339 *
340 * The count is in *elements of the flexible member*, and the element size
341 * comes from the member rather than a hardcoded 1, so a change to `data[]`'s
342 * type cannot silently mis-size every allocation.
343 *
344/*
345 * The flags argument is accepted and ignored, and it is now `...` rather than
346 * a named fourth parameter: virtio_console.c:411 writes the three-argument
347 * form -- `kmalloc_flex(*buf, sg, pages)` -- because mainline's macro dropped
348 * its gfp argument, while tty_buffer.c:187 still writes the four-argument
349 * `kmalloc_flex(*p, data, 2 * size, GFP_ATOMIC | __GFP_NOWARN)`. A macro that
350 * insists on one count of parameters rejects the other caller with "too few
351 * arguments provided to function-like macro invocation", an error about
352 * arity rather than about anything either caller did wrong. Both forms
353 * substitute the same expansion; the swalled arguments are the GFP flags,
354 * which are documentation in DCL and not policy (see the note above).
355 *
356 * This is where GFP_ATOMIC | __GFP_NOWARN from tty_buffer.c lands, and both
357 * are advisory here. A failed allocation still returns NULL -- tty_buffer_alloc()
358 * checks for it -- so the "don't warn on failure" half of __GFP_NOWARN is moot:
359 * DCL's allocator does not warn.
360 *
361 * __GFP_NOWARN exists as a name only so that expression parses. GFP flags are
362 * documentation in DCL, not policy, except for __GFP_ZERO: <linux/gfp_types.h>
363 * comes in at the top of this header, so its real bit is defined by the time
364 * this fallback is reached (and skipped) -- dcl_kmalloc_flags() is the reader.
365 */
366#ifndef __GFP_NOWARN
367#define __GFP_NOWARN 0
368#endif
369#ifndef __GFP_ZERO
370#define __GFP_ZERO 0
371#endif
372#ifndef __GFP_RETRY_MAYFAIL
373#define __GFP_RETRY_MAYFAIL 0
374#endif
375
376#define kmalloc_flex(p, member, count, ...) \
377 dcl_kmalloc((unsigned int)(sizeof(typeof(p)) + \
378 sizeof(*((typeof(&p))0)->member) * (size_t)(count)))
379
380/*
381 * kzalloc_flex(p, member, count) -- kmalloc_flex() zeroed. Mainline spells
382 * both as the same __alloc_flex() over a different allocator (slab.h:1148 and
383 * :1156), and this pair is the same split: dcl_kzalloc() is dcl_kmalloc()
384 * with the zeroing, so the only difference is which one the expansion calls.
385 * The `...` is mainline's gfp slot, kept for the same reason kmalloc_flex
386 * keeps it -- a caller that writes the four-argument form should still
387 * compile, and the flags are documentation here.
388 *
389 * One caller: dma-fence-array.c:182, `kzalloc_flex(*array, callbacks,
390 * num_fences)` at the tail of dma_fence_array_alloc(), sizing the
391 * callbacks[] flexible array struct dma_fence_array ends in. The
392 * `(typeof(&p))0` spelling is what makes that call work: `p` is the
393 * *expression* `*array`, so &p is the address of a local pointer and the
394 * member lookup has the struct type to resolve against.
395 */
396#define kzalloc_flex(p, member, count, ...) \
397 dcl_kzalloc((unsigned int)(sizeof(typeof(p)) + \
398 sizeof(*((typeof(&p))0)->member) * (size_t)(count)))
399
400
401/*
402 * kzalloc_objs(type, count) -- an array of `count` objects, zeroed.
403 *
404 * serial_core.c wants it twice: kzalloc_objs(struct uart_state, drv->nr) at
405 * :2737 to hold a driver's port states, and
406 * kzalloc_objs(*uport->tty_groups, num_groups) at :3109 for the attribute
407 * groups the tty device exposes. Both are `struct`-or-expression first, count
408 * second, which is why this is not kzalloc_obj() with an extra argument
409 * bolted on: kmalloc_obj takes flags there, kzalloc_obj takes nothing, and a
410 * single name with a different second parameter would be a trap. Separate
411 * name, and the plural is mainline's own distinction (kcalloc_array ->
412 * kcalloc).
413 *
414 * The cast to unsigned int matches what the rest of this header hands
415 * dcl_kzalloc; a count that overflows 32 bits of byte length would wrap here
416 * rather than fail, and the only counts in reach are the number of UART ports
417 * (small) and their attribute groups (smaller).
418 */
419#define kzalloc_objs(type, count) \
420 dcl_kzalloc((unsigned int)(sizeof(type) * (unsigned int)(count)))
421
422/*
423 * kmalloc_objs(type, count) -- the uninitialised twin, same two-argument
424 * shape as kzalloc_objs() above.
425 *
426 * virtio_console.c is the first caller: :1820 `vqs = kmalloc_objs(struct
427 * virtqueue *, nr_queues);` and its three siblings at :1821-1823, each
428 * allocating an array of pointers. The first argument is a *type* here --
429 * `struct virtqueue *` -- and sizeof() of a pointer type is exactly what an
430 * array of those pointers needs, so the expansion is the same
431 * sizeof * count as kzalloc_objs without the zeroing.
432 *
433 * Named separately rather than as kzalloc_objs with a flag bolted on, for the
434 * reason that block gives: two callers of two names cannot disagree about
435 * whether the memory comes back zeroed, whereas one name with a third
436 * parameter could be read either way at the call site.
437 */
438#define kmalloc_objs(type, count) \
439 dcl_kmalloc((unsigned int)(sizeof(type) * (unsigned int)(count)))
440
441/*
442 * kvmalloc_objs(type, count) -- kmalloc_objects again, under the name mainline
443 * uses when the size is not known to be small enough for kmalloc().
444 *
445 * mainline's kvmalloc() tries kmalloc() and falls back to vmalloc() above a
446 * page or so; DCL has no vmalloc, so the fallback is not a fallback but a
447 * different allocator with a different failure mode, and taking it here would
448 * mean deciding a threshold nothing has measured. It expands to the same
449 * dcl_kmalloc() as kmalloc_objs, which means the answer to "what happens with
450 * a large npages" is "the same thing that happens with a large count today".
451 *
452 * One caller: drm_gem.c:687's `pages = kvmalloc_objs(struct page *, npages)`,
453 * the array drm_gem_get_pages builds so it can walk a GEM object's pages one
454 * at a time. Undefined, that line reads as a call to a function of unknown
455 * type whose first argument is a type name, and clang reports "expected
456 * expression" at the comma inside `struct page *` -- the message names the
457 * comma, not the macro.
458 */
459#define kvmalloc_objs(type, count) \
460 dcl_kmalloc((unsigned int)(sizeof(type) * (unsigned int)(count)))
461
462/*
463 * kvzalloc_objs(type, count) -- the zeroing spelling, added with the
464 * virtio-gpu port: virtgpu_submit.c:109 and :200 size their syncobj arrays
465 * with it. mainline's v-named pair differs only in the fallback both DCL
466 * spellings share, so this expands to kzalloc_objs() and the overflow note
467 * there applies here too.
468 */
469#define kvzalloc_objs(type, count) \
470 kzalloc_objs(type, count)
471
472/*
473 * The rest of the kvmalloc family and the two string helpers, as functions
474 * rather than more macros: a vendored caller names each of them as a call --
475 * vkms_composer.c:568/:574 (kvmalloc), drm_property.c:565 (kvzalloc),
476 * drm_gem.c:728 (kvfree), drm_managed.c:134 (ksize), vkms_config.c:19 and
477 * drm_managed.c:154/:239 (kstrdup_const/kfree_const). Nothing declared them,
478 * so those call sites compiled as implicit declarations returning int, which
479 * truncates a 64-bit pointer to its low half -- the definitions in
480 * DCL/linux_alloc_shim.c fix the link, but the declarations here are what
481 * make the calls type-correct.
482 *
483 * mainline puts kstrdup_const()/kfree_const() in <linux/string.h>; DCL's
484 * string.h is memcpy-and-friends only, and this header already owns the
485 * string half of the allocation surface (kstrdup(), kmemdup(),
486 * memdup_user_nul()), so they land beside those.
487 */
488void *kvmalloc(size_t size, gfp_t flags);
489void *kvzalloc(size_t size, gfp_t flags);
490void kvfree(const void *addr);
491size_t ksize(const void *objp);
492const char *kstrdup_const(const char *s, gfp_t gfp);
493void kfree_const(const void *x);
494
495/*
496 * kmalloc_array(n, size, flags) -- mainline's overflow-checked array
497 * allocation (v7.2 slab.h:1202). The check is the whole point, as with
498 * krealloc_array() above: drm_plane.c:402/:427 sizes a format or modifier
499 * table from a drm ioctl count, and a wrapped product would allocate a small
500 * block and then be filled as a large one. array_size() would do the same
501 * check, but mainline spells this one with check_mul_overflow() and the NULL
502 * answer on overflow, which is what the callers test for.
503 */
504static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
505{
506 size_t bytes;
507
508 if (unlikely(check_mul_overflow(n, size, &bytes)))
509 return NULL;
510 return dcl_kmalloc_flags((unsigned int)bytes, flags);
511}
512
513/*
514 * kvmalloc_array(n, size, flags) -- the v-named spelling of the same
515 * overflow-checked array allocation, added with the virtio-gpu port: the
516 * submit path sizes its syncobj arrays with it. mainline picks kmalloc and
517 * falls back to vmalloc above a threshold; DCL's kvmalloc family has no
518 * vmalloc to fall back to -- see the note over kvmalloc_objs() -- so the
519 * body is kmalloc_array()'s under the name its caller names.
520 */
521static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
522{
523 size_t bytes;
524
525 if (unlikely(check_mul_overflow(n, size, &bytes)))
526 return NULL;
527 return dcl_kmalloc_flags((unsigned int)bytes, flags);
528}
529
530/*
531 * kmalloc_node_track_caller(size, flags, node) -- kmalloc() with a NUMA node
532 * and caller tracking, both of which this tree absorbs. One TLSF heap means
533 * there are no nodes to choose between, and the tracking is slab leak
534 * accounting, which DCL does not do (its allocator keeps its own statistics).
535 * The caller is drm_managed.c:97, which builds a drmres header plus its
536 * flexible data[] in one block and would otherwise get no prototype at all.
537 */
538static inline void *kmalloc_node_track_caller(size_t size, gfp_t flags, int node)
539{
540 (void)node;
541 return dcl_kmalloc_flags((unsigned int)size, flags);
542}
543
544/*
545 * memdup_array_user()/vmemdup_array_user() -- mainline's string.h inlines
546 * over memdup_user()/vmemdup_user(), declared here for the same reachability
547 * reason as the two string helpers above (drm_lease.c:512, drm_gem.c:849).
548 * Bodies in DCL/linux_alloc_shim.c.
549 */
550void *memdup_array_user(const void __user *src, size_t n, size_t size);
551void *vmemdup_array_user(const void __user *src, size_t n, size_t size);
552
553/*
554 * memdup_user()/vmemdup_user() -- the pair those inlines are *over*,
555 * declared here for the same reachability reason as they are. mainline
556 * declares the pair in <linux/string.h>; DCL's string.h is
557 * memcpy-and-friends only, and this header already owns the string half of
558 * the allocation surface. The virtio-gpu ioctl blob paths and the submit
559 * queue name them directly, and neither had a definition. Bodies in
560 * DCL/linux_alloc_shim.c beside the array forms; IS_ERR() answers because
561 * the callers check with IS_ERR().
562 */
563void *memdup_user(const void __user *src, size_t len);
564void *vmemdup_user(const void __user *src, size_t len);
565
566/*
567 * kmemdup(src, len, gfp) -- copy `len` bytes into a fresh allocation.
568 *
569 * mainline declares it in <linux/slab.h> too (kernel: mm/util.c), so this is
570 * its home and not an import. One caller, virtio_console.c:1118:
571 *
572 * data = kmemdup(buf, count, GFP_ATOMIC);
573 * if (!data)
574 * return -ENOMEM;
575 *
576 * which needs exactly the contract mainline gives: NULL on failure, and the
577 * failure *checked for*, which is why the body must not silently fall back to
578 * anything. The gfp flag is swallowed like every other one in this header --
579 * see the note above kmalloc_obj -- and memcpy comes from <linux/string.h>,
580 * which this header now includes for it (kernel.h names memcpy but only
581 * commentates on where mainline gets it from).
582 *
583 * size_t length, unsigned int size: the cast matches what the rest of this
584 * header hands the allocator, and a length beyond 4 GB would wrap here rather
585 * than fail. The only caller's count is a virtio-console control message.
586 */
587/*
588 * struct kmem_cache -- mainline's per-cache descriptor, cut to the one
589 * field its callers read here. DCL allocates from one TLSF heap whether
590 * or not the caller named a cache, so the descriptor carries only the
591 * object size kmem_cache_alloc_lru() needs to hand back. The only cache
592 * in reach of this tree is radix-tree.h's extern radix_tree_node_cachep,
593 * whose size is set in DCL/drm_stubs.c.
594 */
596 unsigned int size;
597};
598
599/*
600 * kmem_cache_alloc_lru(cache, lru, gfp) -- allocate from `cache`. The lru
601 * node mainline tracks the allocation against is dropped with every other
602 * gfp flag here; see the flags note at the top of this header.
603 */
604static inline void* kmem_cache_alloc_lru(struct kmem_cache* cache,
605 const void* lru, gfp_t gfp)
606{
607 (void)lru;
608 return kmalloc(cache->size, gfp);
609}
610
611/*
612 * DCL delta: the cache lifecycle virtio-gpu's vbuf allocator drives --
613 * virtgpu_vq.c:75 creates the cache, :101/:208/:628 allocate from it,
614 * :208/:89 free and :83 destroys it. A DCL cache is the size in its
615 * descriptor and nothing else: every allocation comes from the one TLSF
616 * heap whatever name it was asked for, so create() allocates the descriptor
617 * out of that same heap, destroy() returns it, and the two allocation
618 * spellings bottom out in kmalloc/kzalloc by way of the descriptor's size.
619 *
620 * `align`, the flags word and the constructor have nothing to act on and are
621 * accepted because mainline's signature is what the vendored caller compiles
622 * against. The one consequence a reader should see: it is a real allocation,
623 * so create() can fail, and the caller checks -- which is the same failure
624 * path mainline's create() has.
625 */
626static inline struct kmem_cache* kmem_cache_create(const char* name,
627 unsigned int size,
628 unsigned int align,
629 unsigned int flags,
630 void (*ctor)(void*))
631{
632 struct kmem_cache* cache;
633
634 (void)name;
635 (void)align;
636 (void)flags;
637 (void)ctor;
638 cache = dcl_kmalloc(sizeof(*cache));
639 if (!cache)
640 return NULL;
641 cache->size = size;
642 return cache;
643}
644
645static inline void kmem_cache_destroy(struct kmem_cache* cache)
646{
647 kfree(cache);
648}
649
650static inline void* kmem_cache_zalloc(struct kmem_cache* cache, gfp_t gfp)
651{
652 (void)gfp;
653 return dcl_kzalloc(cache->size);
654}
655
656static inline void kmem_cache_free(struct kmem_cache* cache, void* obj)
657{
658 (void)cache;
659 kfree(obj);
660}
661
662static inline void* kmemdup(const void* src, size_t len, gfp_t gfp)
663{
664 void* p = dcl_kmalloc((unsigned int)len);
665
666 (void)gfp;
667 if (p && len)
668 memcpy(p, src, len);
669 return p;
670}
671
672/*
673 * kstrdup(s, gfp) -- copy a NUL-terminated string into a fresh allocation.
674 *
675 * The string twin of kmemdup() above, and it sits beside it for that reason:
676 * mainline keeps both in mm/util.c under one "duplicate this" heading, and
677 * the two differ only in how the length is found. Callers are drm_print.c's
678 * printer setup, which test the result against NULL the way mainline
679 * documents, so the failure must be a real NULL and not a fallback.
680 *
681 * The terminator is included in the copy, which is why the length is
682 * strlen + 1 and why memcpy is safe here: `src` is a C string by contract,
683 * so the run it scans cannot pass the end of what it points at.
684 */
685static inline char* kstrdup(const char* s, gfp_t gfp)
686{
687 size_t len = strlen(s) + 1;
688 char* p = (char*)dcl_kmalloc((unsigned int)len);
689
690 (void)gfp;
691 if (p)
692 memcpy(p, s, len);
693 return p;
694}
695
696/*
697 * memdup_user_nul(src, len) -- copy `len` bytes out of a user pointer and
698 * append a terminator, so a driver gets a kernel string of exactly the
699 * length the user asked for. Callers are lib/string_helpers.c:179 and
700 * drm_ioctl.c:590, and both test with IS_ERR() rather than NULL -- which is
701 * the part worth getting right, because mainline distinguishes "no memory"
702 * (-ENOMEM) from "the user pointer was bad" (-EFAULT), and a NULL here would
703 * read as neither.
704 *
705 * The copy is memcpy rather than copy_from_user, and that is not a shortcut:
706 * <linux/mm_shim.c>'s opening comment records that Xeneva runs a syscall with
707 * the calling process's page tables live and the devfs bridge already hands
708 * raw user pointers to ->read/->write, so copy_from_user *is* a memcpy there
709 * too. The two ERR_PTR arms are kept because they are the contract, not
710 * because of a boundary.
711 *
712 * The len >= INT_MAX arm is mainline's guard against `len + 1` overflowing
713 * the allocation size; it is unreachable for a drm ioctl name and cheap to
714 * keep.
715 */
716static inline void* memdup_user_nul(const void* src, size_t len)
717{
718 char* p;
719
720 if (len >= (size_t)0x7fffffff)
721 return ERR_PTR(-EINVAL);
722
723 p = (char*)dcl_kmalloc((unsigned int)(len + 1));
724 if (!p)
725 return ERR_PTR(-ENOMEM);
726
727 memcpy(p, src, len);
728 p[len] = '\0';
729 return p;
730}
731
732/*
733 * get_zeroed_page() / free_page() -- the tty xmit buffer's allocator
734 * (serial_core.c:255 and :296, one page per port).
735 *
736 * TWO THINGS ABOUT THE SIGNATURE, both because this is aarch64-unknown-windows:
737 *
738 * 1. The parameter and return are `uintptr_t`, not mainline's `unsigned
739 * long`. This target is LLP64: unsigned long is 4 bytes, a pointer is 8,
740 * and get_zeroed_page hands back a heap address the TLSF allocator carved
741 * out of `0xFFFFC00000000000`. Declaring it `unsigned long` would narrow
742 * on the way out, so every page handed to a port would be an address in
743 * the low 4 GB that nothing maps -- and free_page() would then be handed
744 * back a number that could not identify it. This is the partner half of
745 * a two-part fix: the two `unsigned long` lines in serial_core.c that
746 * carry the address have to be uintptr_t too, and doing only one half of
747 * that pair would achieve nothing.
748 *
749 * 2. It is one page from the ordinary allocator, not from a page allocator:
750 * Xeneva's pmmngr manages physical pages for the initial identity map and
751 * has no per-page free list for the kernel heap, while kcalloc() already
752 * zeroes and kfree() already understands the block. The page needs to be
753 * zeroed because kfifo_init() is handed it as a fresh ring and reads
754 * nothing that was not written. PAGE_SIZE comes from <Mm/vmmngr.h>,
755 * included at the top of this header.
756 */
757static inline uintptr_t get_zeroed_page(gfp_t gfp)
758{
759 (void)gfp;
760 return (uintptr_t)dcl_kzalloc(PAGE_SIZE);
761}
762
763static inline void free_page(uintptr_t addr)
764{
765 if (addr)
766 kfree((void*)addr);
767}
768
769/*
770 * kasprintf(gfp, fmt, ...) -- allocate and format, mainline's
771 * include/linux/kernel.h spelling; it is here because this is the header
772 * that owns allocation and serial_core.c includes it directly for kzalloc_objs.
773 *
774 * The window is fixed at DCL_KASPRINTF_MAX rather than mainline's
775 * measure-then-allocate pair. There is no measuring call to hand the format
776 * to twice: _vsnprintf() would report a length, but the sizing pass upstream
777 * does needs vsnprintf(NULL, 0, ...) to be legal, and DCL's formatter lives
778 * in Xeneva's _print.c with no NULL-buffer contract to rely on -- guessing
779 * that it has one would be a heap overrun waiting on an assumption. So the
780 * buffer is allocated first at a size no caller can exceed and the copy is
781 * bounded, which fails as a truncated name instead of as a write past the
782 * end.
783 *
784 * The single caller is serial_core.c:3087, kasprintf(GFP_KERNEL, "%s%u",
785 * drv->dev_name, name_base + line) -- a driver name and a port number, an
786 * order of magnitude inside 128. If a ported file ever formats a longer
787 * thing here, the number to raise is DCL_KASPRINTF_MAX and the check is
788 * that kasprintf's caller still tests for NULL.
789 */
790#define DCL_KASPRINTF_MAX 128
791
792static inline char* kasprintf(gfp_t gfp, const char* fmt, ...)
793{
794 char* buf;
795 va_list ap;
796 int n;
797
798 (void)gfp; /* GFP is advisory in DCL, as above */
799
800 buf = (char*)dcl_kmalloc(DCL_KASPRINTF_MAX);
801 if (!buf)
802 return NULL; /* serial_core.c:3089 tests this */
803
804 va_start(ap, fmt);
805 n = _vsnprintf(buf, DCL_KASPRINTF_MAX, fmt, ap);
806 va_end(ap);
807
808 if (n < 0)
809 {
810 kfree(buf);
811 return NULL;
812 }
813 buf[n < DCL_KASPRINTF_MAX ? n : DCL_KASPRINTF_MAX - 1] = '\0';
814 return buf;
815}
816
817/*
818 * kvasprintf(gfp, fmt, ap) -- kasprintf() with the caller's va_list, which is
819 * how mainline splits the pair (sprintf.h:18). Declared here beside its
820 * varargs twin, whose fixed window and bounded copy its body in
821 * DCL/linux_alloc_shim.c repeats; the three callers -- drm_crtc.c:286,
822 * drm_plane.c:439, drm_encoder.c:120 -- format a driver-supplied name and
823 * kfree() the result later.
824 */
825char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap);
826
827#endif
#define PAGE_SIZE
Definition vmmngr.h:92
unsigned int uintptr_t
Definition stdint.h:94
uint8_t flags
Definition virtio.h:0
int _vsnprintf(char *output, size_t sz, const char *format, va_list ap)
Definition _print.c:814
#define EINVAL
Definition acclib.h:198
#define ENOMEM
Definition acclib.h:192
char * va_list
Definition acmsvcex.h:186
#define va_end(ap)
Definition acmsvcex.h:198
#define va_start(ap, A)
Definition acmsvcex.h:199
#define NULL
Definition actypes.h:561
#define DEFINE_FREE(_name, _type, _free)
Definition cleanup.h:120
#define unlikely(x)
Definition compiler.h:30
#define __user
Definition compiler.h:63
size_t size
Definition drm_bridge.h:1299
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
const char * fmt
Definition drm_file.h:466
#define ERR_PTR(err)
Definition err.h:51
size_t len
Definition initial.c:23
void kfree(void)
void strlen(void)
void memcpy(void)
void int node
Definition kthread.h:31
unsigned int gfp_t
Definition kernel.h:56
#define check_mul_overflow(a, b, d)
Definition overflow.h:153
void * kvmalloc(size_t size, gfp_t flags)
#define DCL_KASPRINTF_MAX
Definition slab.h:790
#define kmalloc(size, flags)
Definition slab.h:123
void * memdup_array_user(const void __user *src, size_t n, size_t size)
#define __GFP_ZERO
Definition slab.h:370
void * vmemdup_user(const void __user *src, size_t len)
void * memdup_user(const void __user *src, size_t len)
#define kcalloc(n, size,...)
Definition slab.h:153
const char * kstrdup_const(const char *s, gfp_t gfp)
char * kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
void kfree_const(const void *x)
size_t ksize(const void *objp)
void * vmemdup_array_user(const void __user *src, size_t n, size_t size)
void * kvzalloc(size_t size, gfp_t flags)
void kvfree(const void *addr)
#define krealloc(ptr, size,...)
Definition slab.h:180
Definition slab.h:595
unsigned int size
Definition slab.h:596