summaryrefslogtreecommitdiff
path: root/src/kernel/mm/slab.cc
blob: 3bb6b1be94ab71b03f72a2d520f1dcc974057e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2026 Richard QIn
 */
#include "kernel/slab.h"
#include "kernel/pmm.h"
#include "kernel/spinlock.h"
#include "kernel/riscv.h"
#include "drivers/uart.h"
#include "lib/string.h"

#define PG_slab 0x04

// Supported object size (must be a multiple of 8 and <= 2048)
const uint64 kmalloc_sizes[] = {
    16, 32, 64, 128, 256, 512, 1024, 2048};
const int NUM_CACHES = 8;

struct SlabHeader
{
    struct SlabHeader *next; // Next slab in the linked list
    uint16 free_idx;         // Index of the head of the free list (0xFFFF means none)
    uint16 used_count;       // Number of allocated objects
    // Followed immediately by object data...
    // The free list of objects is embedded in the first 2 bytes of the object itself (stores the index of the next object)
    uint32 padding;
};

struct KmemCache
{
    Spinlock lock;
    uint64 obj_size;
    uint64 obj_count_per_slab;
    struct SlabHeader *slab_list; // Slab list with free objects
    const char *name;
};

struct KmemCache caches[NUM_CACHES];

// Virtual address -> struct Page*
static struct Page *virt_to_page(void *ptr)
{
    uint64 pa = (uint64)ptr;
    return PMM::pa_to_page(pa);
}

// Slab Implementation

namespace Slab
{

    void init()
    {
        for (int i = 0; i < NUM_CACHES; i++)
        {
            caches[i].lock.init("slab_lock");
            caches[i].obj_size = kmalloc_sizes[i];
            caches[i].name = "kmalloc";

            // Calculate how many objects can fit on a page
            // Excluding the overhead of the SlabHeader
            uint64 available = PGSIZE - sizeof(struct SlabHeader);
            caches[i].obj_count_per_slab = available / caches[i].obj_size;
            caches[i].slab_list = nullptr;
        }
        Drivers::uart_puts("[Slab] Initialized kmalloc caches.\n");
    }

    // Allocate a new slab page for the cache
    // Must hold cache->lock
    static struct SlabHeader *grow_cache(struct KmemCache *c)
    {
        void *p = PMM::alloc_page();
        if (!p)
            return nullptr;

        // Mark this page as a Slab page
        struct Page *page = virt_to_page(p);
        if (page)
        {
            page->flags |= PG_slab;
            int cache_idx = c - caches;
            page->order = (uint8)cache_idx;
        }

        struct SlabHeader *slab = (struct SlabHeader *)p;
        slab->next = nullptr;
        slab->used_count = 0;
        slab->free_idx = 0;
        slab->padding = 0;

        // Initialize the free list of objects
        // The starting position of each object stores the index of the next object
        // Starting position of the object data area
        char *base = (char *)(slab + 1);

        for (uint i = 0; i < c->obj_count_per_slab - 1; i++)
        {
            // The content of the i-th object is i+1
            uint16 *ptr = (uint16 *)(base + i * c->obj_size);
            *ptr = i + 1;
        }
        // The last object points to 0xFFFF
        uint16 *ptr = (uint16 *)(base + (c->obj_count_per_slab - 1) * c->obj_size);
        *ptr = 0xFFFF;

        return slab;
    }

    void *kmalloc(uint64 size)
    {
        if (size == 0)
            return nullptr;

        // Large objects go directly through PMM
        if (size > 2048)
        {
            // calculate order
            int order = 0;
            uint64 req = size;
            uint64 pages = (size + PGSIZE - 1) / PGSIZE;
            (void)req;

            // calculate order: 2^order >= pages
            while ((1UL << order) < pages)
            {
                order++;
            }
            if (order > MAX_ORDER)
                return nullptr;

            void *p = PMM::alloc_pages(order);
            // Here, page->flags defaults to PG_used, not PG_slab
            // kfree needs to be able to recognize it
            return p;
        }

        int idx = 0;
        while (idx < NUM_CACHES && kmalloc_sizes[idx] < size)
        {
            idx++;
        }

        struct KmemCache *c = &caches[idx];
        c->lock.acquire();

        if (!c->slab_list)
        {
            c->slab_list = grow_cache(c);
            if (!c->slab_list)
            {
                c->lock.release();
                return nullptr;
            }
        }

        struct SlabHeader *slab = c->slab_list;

        int obj_idx = slab->free_idx;
        char *base = (char *)(slab + 1);
        void *ptr = base + obj_idx * c->obj_size;

        uint16 *link = (uint16 *)ptr;
        slab->free_idx = *link;
        slab->used_count++;

        if (slab->free_idx == 0xFFFF)
        {
            if (slab->next)
            {
                c->slab_list = slab->next;
                slab->next = nullptr;
            }
            else
            {
                c->slab_list = nullptr;
            }
        }

        c->lock.release();

        memset(ptr, 0, size);
        return ptr;
    }

    void kfree(void *ptr)
    {
        if (!ptr)
            return;

        struct Page *page = virt_to_page(ptr);
        if (!page)
            return;

        if (!page || !(page->flags & PG_slab))
        {
            PMM::free_pages(ptr, page->order);
            return;
        }

        int cache_idx = page->order;
        if (cache_idx >= NUM_CACHES)
        {
            Drivers::uart_puts("Slab: Corrupted page metadata\n");
            return;
        }

        struct KmemCache *c = &caches[cache_idx];
        c->lock.acquire();

        struct SlabHeader *slab = (struct SlabHeader *)PGROUNDDOWN((uint64)ptr);

        if ((uint64)ptr < (uint64)(slab + 1) || (uint64)ptr >= (uint64)slab + PGSIZE)
        {
            Drivers::uart_puts("Slab: kfree invalid pointer\n");
            c->lock.release();
            return;
        }

        char *base = (char *)(slab + 1);
        int obj_idx = ((uint64)ptr - (uint64)base) / c->obj_size;

        uint16 *link = (uint16 *)ptr;
        *link = slab->free_idx;
        slab->free_idx = (uint16)obj_idx;

        slab->used_count--;

        if (slab->used_count == 0)
        {
            struct SlabHeader **pp = &c->slab_list;
            // bool found = false;
            while (*pp)
            {
                if (*pp == slab)
                {
                    *pp = slab->next;
                    // found = true;
                    break;
                }
                pp = &(*pp)->next;
            }

            page->flags &= ~PG_slab;
            PMM::free_page(slab);
        }
        else
        {

            if (slab->used_count == c->obj_count_per_slab - 1)
            {
                slab->next = c->slab_list;
                c->slab_list = slab;
            }
        }

        c->lock.release();
    }
}