kmem: a slab allocator, and what the fixed tables actually cost
Gate 0's last structural row. kernel/core/kmem.c: slabs are pages from
kalloc() with a header at the front, and free objects hold the free list in
their own first eight bytes.
That last detail is the whole design. **An object carries no header and
freeing performs no search**: the page base of any object *is* its slab, and
the slab names its cache. It is also exactly why an object cannot exceed a
page, which matters below.
Two interfaces. kmem_cache_create/alloc/free for a subsystem with one kind of
thing -- it gets its own line in /proc/slab, which is most of why naming it is
worth the argument -- and kmalloc/kmfree over power-of-two classes from 16 to
2048 for code with no single type to build a cache around. Each cache keeps
partial and full lists; a slab that empties goes straight back to kalloc()
rather than being kept, so /proc/slab never overstates what is held.
`make kmem-test` asserts the bookkeeping on the build machine. Same arrangement
as schfs-objtest, and for the same reason: slab lists, free-list threading, the
page-to-cache lookup and whether pages actually come back are all things that
can be wrong without a kernel to be wrong in. 26 checks, including one that a
permissive allocator cannot pass -- write a distinct pattern through 512
objects and read them all back, because two objects sharing storage shows up
there and nowhere else.
One bug the test found immediately: kmem_max_obj() returned the raw page
remainder, which kmem_cache_create() then rounded *up* past its own limit. The
largest legal size was reported as illegal.
**It does not do what the register said it would.** The claim was that every
fixed table exists to work around kalloc. Measured:
struct proc 10656 NPROC 128 larger than a page
struct socket 4928 NSOCK 32 larger than a page
struct buf 4272 NBUF 256 larger than a page
struct inode 280 NINODE 200 a cache, bounded on purpose
struct device 120 NDEVICE 32 3.8 KB, never hit
struct file 48 NFILE 512 24 KB, never hit
The three largest cannot go in a page-backed slab whatever it does. NINODE is
a cache and is meant to be bounded. NFILE and NDEVICE are small enough that
their tables are rounding errors and their ceilings have never been reached.
Freeing the big ones needs a contiguous multi-page allocator, which is a
different thing and is not written. That is now in the register and in
docs/architecture/kernel.md rather than left as a claim nobody checked.
What it does fix is real, and was measured rather than assumed:
- **A pipe is 640 bytes and came from a whole 4096-byte page.** Every `|` in
a shell command burned three and a half kilobytes it never touched. Six
pipes per page now, and /proc/slab shows the page going back when they
close.
- **procread() built its answer in a 2048-byte array on the kernel stack**,
under a comment that had warned about exactly that since it was written --
it stayed a local because a whole page for 2 KB felt worse, and kmalloc is
the answer that was missing. Its frame is 64 bytes now instead of ~2100.
- procfs's other generator took a page from kalloc for the same 2 KB.
Verified on a boot: /proc/slab tracks pipe and kmalloc-2048 through real
pipelines, allocs and frees balance, and $(), ps, time, readlink, sigtest and
rename are all still green with no traps.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
0b39f8bad1
commit
c98bdf0019
10 changed files with 651 additions and 16 deletions
|
|
@ -36,6 +36,7 @@
|
|||
#include "fsstat.h"
|
||||
#include "schfs.h"
|
||||
#include "sbuf.h"
|
||||
#include "kmem.h"
|
||||
#include "dbg.h"
|
||||
|
||||
#define PROC_BUFSZ 2048
|
||||
|
|
@ -344,6 +345,7 @@ gen_filesystems(struct sbuf *sb)
|
|||
#define PROC_KMSG 17
|
||||
#define PROC_RESOLV 18
|
||||
#define PROC_BOOTMODE 19
|
||||
#define PROC_SLAB 20
|
||||
|
||||
// Render global file `id` (a PROC_* constant) into `buf`. Returns its length.
|
||||
//
|
||||
|
|
@ -364,6 +366,9 @@ proc_gen_global(int id, char *buf, int cap)
|
|||
case PROC_LOADAVG: gen_loadavg(&sb); break;
|
||||
case PROC_MOUNTS: gen_mounts(&sb); break;
|
||||
case PROC_FSSTAT: gen_fsstat(&sb); break;
|
||||
// The slab allocator renders its own table, for the same reason SchFS and
|
||||
// PCI do: it owns the data and must not depend on this file's helpers.
|
||||
case PROC_SLAB: kmem_stats(&sb); break;
|
||||
case PROC_FILESYSTEMS: gen_filesystems(&sb); break;
|
||||
// SchFS renders straight into the buffer rather than through sbuf, because
|
||||
// it lives in kernel/fs/schfs and must not depend on this file's helpers.
|
||||
|
|
@ -404,15 +409,27 @@ procread(struct inode *ip, uint64 off, char *dst, int n)
|
|||
if (ip->minor == PROC_KMSG)
|
||||
return klog_read(off, dst, n);
|
||||
|
||||
char buf[PROC_BUFSZ];
|
||||
int len = proc_gen_global(ip->minor, buf, PROC_BUFSZ);
|
||||
// **Not a local.** This is PROC_BUFSZ bytes -- a quarter of the kernel
|
||||
// stack -- on a path that is several frames deep already, and the comment
|
||||
// above has warned about exactly that since it was written. It stayed a
|
||||
// local because a page from kalloc() for 2 KB felt worse; kmalloc is the
|
||||
// answer that was missing.
|
||||
char *buf = kmalloc(PROC_BUFSZ);
|
||||
int len;
|
||||
|
||||
if (len <= 0 || (int)off >= len)
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
len = proc_gen_global(ip->minor, buf, PROC_BUFSZ);
|
||||
|
||||
if (len <= 0 || (int)off >= len) {
|
||||
kmfree(buf);
|
||||
return 0; // EOF
|
||||
}
|
||||
int avail = len - (int)off;
|
||||
if (n > avail)
|
||||
n = avail;
|
||||
memmove(dst, buf + off, n);
|
||||
kmfree(buf);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue