Subversion Repositories HelenOS-historic

Rev

Rev 771 | Rev 773 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 771 Rev 772
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/*
29
/*
30
 * The SLAB allocator is closely modelled after Opensolaris SLAB allocator
30
 * The SLAB allocator is closely modelled after Opensolaris SLAB allocator
31
 * http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/
31
 * http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/
32
 *
32
 *
33
 * with the following exceptions:
33
 * with the following exceptions:
34
 *   - empty SLABS are deallocated immediately
34
 *   - empty SLABS are deallocated immediately
35
 *     (in Linux they are kept in linked list, in Solaris ???)
35
 *     (in Linux they are kept in linked list, in Solaris ???)
36
 *   - empty magazines are deallocated when not needed
36
 *   - empty magazines are deallocated when not needed
37
 *     (in Solaris they are held in linked list in slab cache)
37
 *     (in Solaris they are held in linked list in slab cache)
38
 *
38
 *
39
 *   Following features are not currently supported but would be easy to do:
39
 *   Following features are not currently supported but would be easy to do:
40
 *   - cache coloring
40
 *   - cache coloring
41
 *   - dynamic magazine growing (different magazine sizes are already
41
 *   - dynamic magazine growing (different magazine sizes are already
42
 *     supported, but we would need to adjust allocating strategy)
42
 *     supported, but we would need to adjust allocating strategy)
43
 *
43
 *
44
 * The SLAB allocator supports per-CPU caches ('magazines') to facilitate
44
 * The SLAB allocator supports per-CPU caches ('magazines') to facilitate
45
 * good SMP scaling.
45
 * good SMP scaling.
46
 *
46
 *
47
 * When a new object is being allocated, it is first checked, if it is
47
 * When a new object is being allocated, it is first checked, if it is
48
 * available in CPU-bound magazine. If it is not found there, it is
48
 * available in CPU-bound magazine. If it is not found there, it is
49
 * allocated from CPU-shared SLAB - if partial full is found, it is used,
49
 * allocated from CPU-shared SLAB - if partial full is found, it is used,
50
 * otherwise a new one is allocated.
50
 * otherwise a new one is allocated.
51
 *
51
 *
52
 * When an object is being deallocated, it is put to CPU-bound magazine.
52
 * When an object is being deallocated, it is put to CPU-bound magazine.
53
 * If there is no such magazine, new one is allocated (if it fails,
53
 * If there is no such magazine, new one is allocated (if it fails,
54
 * the object is deallocated into SLAB). If the magazine is full, it is
54
 * the object is deallocated into SLAB). If the magazine is full, it is
55
 * put into cpu-shared list of magazines and new one is allocated.
55
 * put into cpu-shared list of magazines and new one is allocated.
56
 *
56
 *
57
 * The CPU-bound magazine is actually a pair of magazine to avoid
57
 * The CPU-bound magazine is actually a pair of magazine to avoid
58
 * thrashing when somebody is allocating/deallocating 1 item at the magazine
58
 * thrashing when somebody is allocating/deallocating 1 item at the magazine
59
 * size boundary. LIFO order is enforced, which should avoid fragmentation
59
 * size boundary. LIFO order is enforced, which should avoid fragmentation
60
 * as much as possible.
60
 * as much as possible.
61
 *  
61
 *  
62
 * Every cache contains list of full slabs and list of partialy full slabs.
62
 * Every cache contains list of full slabs and list of partialy full slabs.
63
 * Empty SLABS are immediately freed (thrashing will be avoided because
63
 * Empty SLABS are immediately freed (thrashing will be avoided because
64
 * of magazines).
64
 * of magazines).
65
 *
65
 *
66
 * The SLAB information structure is kept inside the data area, if possible.
66
 * The SLAB information structure is kept inside the data area, if possible.
67
 * The cache can be marked that it should not use magazines. This is used
67
 * The cache can be marked that it should not use magazines. This is used
68
 * only for SLAB related caches to avoid deadlocks and infinite recursion
68
 * only for SLAB related caches to avoid deadlocks and infinite recursion
69
 * (the SLAB allocator uses itself for allocating all it's control structures).
69
 * (the SLAB allocator uses itself for allocating all it's control structures).
70
 *
70
 *
71
 * The SLAB allocator allocates lot of space and does not free it. When
71
 * The SLAB allocator allocates lot of space and does not free it. When
72
 * frame allocator fails to allocate the frame, it calls slab_reclaim().
72
 * frame allocator fails to allocate the frame, it calls slab_reclaim().
73
 * It tries 'light reclaim' first, then brutal reclaim. The light reclaim
73
 * It tries 'light reclaim' first, then brutal reclaim. The light reclaim
74
 * releases slabs from cpu-shared magazine-list, until at least 1 slab
74
 * releases slabs from cpu-shared magazine-list, until at least 1 slab
75
 * is deallocated in each cache (this algorithm should probably change).
75
 * is deallocated in each cache (this algorithm should probably change).
76
 * The brutal reclaim removes all cached objects, even from CPU-bound
76
 * The brutal reclaim removes all cached objects, even from CPU-bound
77
 * magazines.
77
 * magazines.
78
 *
78
 *
79
 *
79
 *
80
 */
80
 */
81
 
81
 
82
 
82
 
83
#include <synch/spinlock.h>
83
#include <synch/spinlock.h>
84
#include <mm/slab.h>
84
#include <mm/slab.h>
85
#include <list.h>
85
#include <list.h>
86
#include <memstr.h>
86
#include <memstr.h>
87
#include <align.h>
87
#include <align.h>
88
#include <mm/heap.h>
88
#include <mm/heap.h>
89
#include <mm/frame.h>
89
#include <mm/frame.h>
90
#include <config.h>
90
#include <config.h>
91
#include <print.h>
91
#include <print.h>
92
#include <arch.h>
92
#include <arch.h>
93
#include <panic.h>
93
#include <panic.h>
94
#include <debug.h>
94
#include <debug.h>
95
#include <bitops.h>
95
#include <bitops.h>
96
 
96
 
97
SPINLOCK_INITIALIZE(slab_cache_lock);
97
SPINLOCK_INITIALIZE(slab_cache_lock);
98
static LIST_INITIALIZE(slab_cache_list);
98
static LIST_INITIALIZE(slab_cache_list);
99
 
99
 
100
/** Magazine cache */
100
/** Magazine cache */
101
static slab_cache_t mag_cache;
101
static slab_cache_t mag_cache;
102
/** Cache for cache descriptors */
102
/** Cache for cache descriptors */
103
static slab_cache_t slab_cache_cache;
103
static slab_cache_t slab_cache_cache;
104
 
104
 
105
/** Cache for external slab descriptors
105
/** Cache for external slab descriptors
106
 * This time we want per-cpu cache, so do not make it static
106
 * This time we want per-cpu cache, so do not make it static
107
 * - using SLAB for internal SLAB structures will not deadlock,
107
 * - using SLAB for internal SLAB structures will not deadlock,
108
 *   as all slab structures are 'small' - control structures of
108
 *   as all slab structures are 'small' - control structures of
109
 *   their caches do not require further allocation
109
 *   their caches do not require further allocation
110
 */
110
 */
111
static slab_cache_t *slab_extern_cache;
111
static slab_cache_t *slab_extern_cache;
112
/** Caches for malloc */
112
/** Caches for malloc */
113
static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1];
113
static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1];
114
char *malloc_names[] =  {
114
char *malloc_names[] =  {
115
    "malloc-8","malloc-16","malloc-32","malloc-64","malloc-128",
115
    "malloc-8","malloc-16","malloc-32","malloc-64","malloc-128",
116
    "malloc-256","malloc-512","malloc-1K","malloc-2K",
116
    "malloc-256","malloc-512","malloc-1K","malloc-2K",
117
    "malloc-4K","malloc-8K","malloc-16K","malloc-32K",
117
    "malloc-4K","malloc-8K","malloc-16K","malloc-32K",
118
    "malloc-64K","malloc-128K"
118
    "malloc-64K","malloc-128K"
119
};
119
};
120
 
120
 
121
/** Slab descriptor */
121
/** Slab descriptor */
122
typedef struct {
122
typedef struct {
123
    slab_cache_t *cache; /**< Pointer to parent cache */
123
    slab_cache_t *cache; /**< Pointer to parent cache */
124
    link_t link;       /* List of full/partial slabs */
124
    link_t link;       /* List of full/partial slabs */
125
    void *start;       /**< Start address of first available item */
125
    void *start;       /**< Start address of first available item */
126
    count_t available; /**< Count of available items in this slab */
126
    count_t available; /**< Count of available items in this slab */
127
    index_t nextavail; /**< The index of next available item */
127
    index_t nextavail; /**< The index of next available item */
128
}slab_t;
128
}slab_t;
129
 
129
 
130
/**************************************/
130
/**************************************/
131
/* SLAB allocation functions          */
131
/* SLAB allocation functions          */
132
 
132
 
133
/**
133
/**
134
 * Allocate frames for slab space and initialize
134
 * Allocate frames for slab space and initialize
135
 *
135
 *
136
 */
136
 */
137
static slab_t * slab_space_alloc(slab_cache_t *cache, int flags)
137
static slab_t * slab_space_alloc(slab_cache_t *cache, int flags)
138
{
138
{
139
    void *data;
139
    void *data;
140
    slab_t *slab;
140
    slab_t *slab;
141
    size_t fsize;
141
    size_t fsize;
142
    int i;
142
    int i;
143
    zone_t *zone = NULL;
143
    zone_t *zone = NULL;
144
    int status;
144
    int status;
145
    frame_t *frame;
145
    frame_t *frame;
146
 
146
 
147
    data = (void *)frame_alloc(FRAME_KA | flags, cache->order, &status, &zone);
147
    data = (void *)frame_alloc(FRAME_KA | flags, cache->order, &status, &zone);
148
    if (status != FRAME_OK) {
148
    if (status != FRAME_OK) {
149
        return NULL;
149
        return NULL;
150
    }
150
    }
151
    if (! (cache->flags & SLAB_CACHE_SLINSIDE)) {
151
    if (! (cache->flags & SLAB_CACHE_SLINSIDE)) {
152
        slab = slab_alloc(slab_extern_cache, flags);
152
        slab = slab_alloc(slab_extern_cache, flags);
153
        if (!slab) {
153
        if (!slab) {
154
            frame_free((__address)data);
154
            frame_free((__address)data);
155
            return NULL;
155
            return NULL;
156
        }
156
        }
157
    } else {
157
    } else {
158
        fsize = (PAGE_SIZE << cache->order);
158
        fsize = (PAGE_SIZE << cache->order);
159
        slab = data + fsize - sizeof(*slab);
159
        slab = data + fsize - sizeof(*slab);
160
    }
160
    }
161
       
161
       
162
    /* Fill in slab structures */
162
    /* Fill in slab structures */
163
    /* TODO: some better way of accessing the frame */
163
    /* TODO: some better way of accessing the frame */
164
    for (i=0; i < (1 << cache->order); i++) {
164
    for (i=0; i < (1 << cache->order); i++) {
165
        frame = ADDR2FRAME(zone, KA2PA((__address)(data+i*PAGE_SIZE)));
165
        frame = ADDR2FRAME(zone, KA2PA((__address)(data+i*PAGE_SIZE)));
166
        frame->parent = slab;
166
        frame->parent = slab;
167
    }
167
    }
168
 
168
 
169
    slab->start = data;
169
    slab->start = data;
170
    slab->available = cache->objects;
170
    slab->available = cache->objects;
171
    slab->nextavail = 0;
171
    slab->nextavail = 0;
172
    slab->cache = cache;
172
    slab->cache = cache;
173
 
173
 
174
    for (i=0; i<cache->objects;i++)
174
    for (i=0; i<cache->objects;i++)
175
        *((int *) (slab->start + i*cache->size)) = i+1;
175
        *((int *) (slab->start + i*cache->size)) = i+1;
176
 
176
 
177
    atomic_inc(&cache->allocated_slabs);
177
    atomic_inc(&cache->allocated_slabs);
178
    return slab;
178
    return slab;
179
}
179
}
180
 
180
 
181
/**
181
/**
182
 * Deallocate space associated with SLAB
182
 * Deallocate space associated with SLAB
183
 *
183
 *
184
 * @return number of freed frames
184
 * @return number of freed frames
185
 */
185
 */
186
static count_t slab_space_free(slab_cache_t *cache, slab_t *slab)
186
static count_t slab_space_free(slab_cache_t *cache, slab_t *slab)
187
{
187
{
188
    frame_free((__address)slab->start);
188
    frame_free((__address)slab->start);
189
    if (! (cache->flags & SLAB_CACHE_SLINSIDE))
189
    if (! (cache->flags & SLAB_CACHE_SLINSIDE))
190
        slab_free(slab_extern_cache, slab);
190
        slab_free(slab_extern_cache, slab);
191
 
191
 
192
    atomic_dec(&cache->allocated_slabs);
192
    atomic_dec(&cache->allocated_slabs);
193
   
193
   
194
    return 1 << cache->order;
194
    return 1 << cache->order;
195
}
195
}
196
 
196
 
197
/** Map object to slab structure */
197
/** Map object to slab structure */
198
static slab_t * obj2slab(void *obj)
198
static slab_t * obj2slab(void *obj)
199
{
199
{
200
    frame_t *frame;
200
    frame_t *frame;
201
 
201
 
202
    frame = frame_addr2frame((__address)obj);
202
    frame = frame_addr2frame((__address)obj);
203
    return (slab_t *)frame->parent;
203
    return (slab_t *)frame->parent;
204
}
204
}
205
 
205
 
206
/**************************************/
206
/**************************************/
207
/* SLAB functions */
207
/* SLAB functions */
208
 
208
 
209
 
209
 
210
/**
210
/**
211
 * Return object to slab and call a destructor
211
 * Return object to slab and call a destructor
212
 *
212
 *
213
 * Assume the cache->lock is held;
213
 * Assume the cache->lock is held;
214
 *
214
 *
215
 * @param slab If the caller knows directly slab of the object, otherwise NULL
215
 * @param slab If the caller knows directly slab of the object, otherwise NULL
216
 *
216
 *
217
 * @return Number of freed pages
217
 * @return Number of freed pages
218
 */
218
 */
219
static count_t slab_obj_destroy(slab_cache_t *cache, void *obj,
219
static count_t slab_obj_destroy(slab_cache_t *cache, void *obj,
220
                slab_t *slab)
220
                slab_t *slab)
221
{
221
{
222
    count_t frames = 0;
222
    count_t frames = 0;
223
 
223
 
224
    if (!slab)
224
    if (!slab)
225
        slab = obj2slab(obj);
225
        slab = obj2slab(obj);
226
 
226
 
227
    ASSERT(slab->cache == cache);
227
    ASSERT(slab->cache == cache);
228
 
228
 
229
    *((int *)obj) = slab->nextavail;
229
    *((int *)obj) = slab->nextavail;
230
    slab->nextavail = (obj - slab->start)/cache->size;
230
    slab->nextavail = (obj - slab->start)/cache->size;
231
    slab->available++;
231
    slab->available++;
232
 
232
 
233
    /* Move it to correct list */
233
    /* Move it to correct list */
234
    if (slab->available == 1) {
234
    if (slab->available == 1) {
235
        /* It was in full, move to partial */
235
        /* It was in full, move to partial */
236
        list_remove(&slab->link);
236
        list_remove(&slab->link);
237
        list_prepend(&slab->link, &cache->partial_slabs);
237
        list_prepend(&slab->link, &cache->partial_slabs);
238
    }
238
    }
239
    if (slab->available == cache->objects) {
239
    if (slab->available == cache->objects) {
240
        /* Free associated memory */
240
        /* Free associated memory */
241
        list_remove(&slab->link);
241
        list_remove(&slab->link);
242
        /* Avoid deadlock */
242
        /* Avoid deadlock */
243
        spinlock_unlock(&cache->lock);
243
        spinlock_unlock(&cache->lock);
244
        frames = slab_space_free(cache, slab);
244
        frames = slab_space_free(cache, slab);
245
        spinlock_lock(&cache->lock);
245
        spinlock_lock(&cache->lock);
246
    }
246
    }
247
 
247
 
248
    return frames;
248
    return frames;
249
}
249
}
250
 
250
 
251
/**
251
/**
252
 * Take new object from slab or create new if needed
252
 * Take new object from slab or create new if needed
253
 *
253
 *
254
 * Assume cache->lock is held.
254
 * Assume cache->lock is held.
255
 *
255
 *
256
 * @return Object address or null
256
 * @return Object address or null
257
 */
257
 */
258
static void * slab_obj_create(slab_cache_t *cache, int flags)
258
static void * slab_obj_create(slab_cache_t *cache, int flags)
259
{
259
{
260
    slab_t *slab;
260
    slab_t *slab;
261
    void *obj;
261
    void *obj;
262
 
262
 
263
    if (list_empty(&cache->partial_slabs)) {
263
    if (list_empty(&cache->partial_slabs)) {
264
        /* Allow recursion and reclaiming
264
        /* Allow recursion and reclaiming
265
         * - this should work, as the SLAB control structures
265
         * - this should work, as the SLAB control structures
266
         *   are small and do not need to allocte with anything
266
         *   are small and do not need to allocte with anything
267
         *   other ten frame_alloc when they are allocating,
267
         *   other ten frame_alloc when they are allocating,
268
         *   that's why we should get recursion at most 1-level deep
268
         *   that's why we should get recursion at most 1-level deep
269
         */
269
         */
270
        spinlock_unlock(&cache->lock);
270
        spinlock_unlock(&cache->lock);
271
        slab = slab_space_alloc(cache, flags);
271
        slab = slab_space_alloc(cache, flags);
272
        spinlock_lock(&cache->lock);
272
        spinlock_lock(&cache->lock);
273
        if (!slab) {
273
        if (!slab) {
274
            return NULL;
274
            return NULL;
275
        }
275
        }
276
    } else {
276
    } else {
277
        slab = list_get_instance(cache->partial_slabs.next,
277
        slab = list_get_instance(cache->partial_slabs.next,
278
                     slab_t,
278
                     slab_t,
279
                     link);
279
                     link);
280
        list_remove(&slab->link);
280
        list_remove(&slab->link);
281
    }
281
    }
282
    obj = slab->start + slab->nextavail * cache->size;
282
    obj = slab->start + slab->nextavail * cache->size;
283
    slab->nextavail = *((int *)obj);
283
    slab->nextavail = *((int *)obj);
284
    slab->available--;
284
    slab->available--;
285
    if (! slab->available)
285
    if (! slab->available)
286
        list_prepend(&slab->link, &cache->full_slabs);
286
        list_prepend(&slab->link, &cache->full_slabs);
287
    else
287
    else
288
        list_prepend(&slab->link, &cache->partial_slabs);
288
        list_prepend(&slab->link, &cache->partial_slabs);
289
    return obj;
289
    return obj;
290
}
290
}
291
 
291
 
292
/**************************************/
292
/**************************************/
293
/* CPU-Cache slab functions */
293
/* CPU-Cache slab functions */
294
 
294
 
295
/**
295
/**
296
 * Free all objects in magazine and free memory associated with magazine
296
 * Free all objects in magazine and free memory associated with magazine
297
 *
297
 *
298
 * Assume mag_cache[cpu].lock is locked
298
 * Assume mag_cache[cpu].lock is locked
299
 *
299
 *
300
 * @return Number of freed pages
300
 * @return Number of freed pages
301
 */
301
 */
302
static count_t magazine_destroy(slab_cache_t *cache,
302
static count_t magazine_destroy(slab_cache_t *cache,
303
                slab_magazine_t *mag)
303
                slab_magazine_t *mag)
304
{
304
{
305
    int i;
305
    int i;
306
    count_t frames = 0;
306
    count_t frames = 0;
307
 
307
 
308
    for (i=0;i < mag->busy; i++) {
308
    for (i=0;i < mag->busy; i++) {
309
        frames += slab_obj_destroy(cache, mag->objs[i], NULL);
309
        frames += slab_obj_destroy(cache, mag->objs[i], NULL);
310
        atomic_dec(&cache->cached_objs);
310
        atomic_dec(&cache->cached_objs);
311
    }
311
    }
312
   
312
   
313
    slab_free(&mag_cache, mag);
313
    slab_free(&mag_cache, mag);
314
 
314
 
315
    return frames;
315
    return frames;
316
}
316
}
317
 
317
 
318
/**
318
/**
319
 * Find full magazine, set it as current and return it
319
 * Find full magazine, set it as current and return it
320
 *
320
 *
321
 * Assume cpu_magazine lock is held
321
 * Assume cpu_magazine lock is held
322
 */
322
 */
323
static slab_magazine_t * get_full_current_mag(slab_cache_t *cache)
323
static slab_magazine_t * get_full_current_mag(slab_cache_t *cache)
324
{
324
{
325
    slab_magazine_t *cmag, *lastmag, *newmag;
325
    slab_magazine_t *cmag, *lastmag, *newmag;
326
 
326
 
327
    cmag = cache->mag_cache[CPU->id].current;
327
    cmag = cache->mag_cache[CPU->id].current;
328
    lastmag = cache->mag_cache[CPU->id].last;
328
    lastmag = cache->mag_cache[CPU->id].last;
329
    if (cmag) { /* First try local CPU magazines */
329
    if (cmag) { /* First try local CPU magazines */
330
        if (cmag->busy)
330
        if (cmag->busy)
331
            return cmag;
331
            return cmag;
332
 
332
 
333
        if (lastmag && lastmag->busy) {
333
        if (lastmag && lastmag->busy) {
334
            cache->mag_cache[CPU->id].current = lastmag;
334
            cache->mag_cache[CPU->id].current = lastmag;
335
            cache->mag_cache[CPU->id].last = cmag;
335
            cache->mag_cache[CPU->id].last = cmag;
336
            return lastmag;
336
            return lastmag;
337
        }
337
        }
338
    }
338
    }
339
    /* Local magazines are empty, import one from magazine list */
339
    /* Local magazines are empty, import one from magazine list */
340
    spinlock_lock(&cache->lock);
340
    spinlock_lock(&cache->lock);
341
    if (list_empty(&cache->magazines)) {
341
    if (list_empty(&cache->magazines)) {
342
        spinlock_unlock(&cache->lock);
342
        spinlock_unlock(&cache->lock);
343
        return NULL;
343
        return NULL;
344
    }
344
    }
345
    newmag = list_get_instance(cache->magazines.next,
345
    newmag = list_get_instance(cache->magazines.next,
346
                   slab_magazine_t,
346
                   slab_magazine_t,
347
                   link);
347
                   link);
348
    list_remove(&newmag->link);
348
    list_remove(&newmag->link);
349
    spinlock_unlock(&cache->lock);
349
    spinlock_unlock(&cache->lock);
350
 
350
 
351
    if (lastmag)
351
    if (lastmag)
352
        slab_free(&mag_cache, lastmag);
352
        slab_free(&mag_cache, lastmag);
353
    cache->mag_cache[CPU->id].last = cmag;
353
    cache->mag_cache[CPU->id].last = cmag;
354
    cache->mag_cache[CPU->id].current = newmag;
354
    cache->mag_cache[CPU->id].current = newmag;
355
    return newmag;
355
    return newmag;
356
}
356
}
357
 
357
 
358
/**
358
/**
359
 * Try to find object in CPU-cache magazines
359
 * Try to find object in CPU-cache magazines
360
 *
360
 *
361
 * @return Pointer to object or NULL if not available
361
 * @return Pointer to object or NULL if not available
362
 */
362
 */
363
static void * magazine_obj_get(slab_cache_t *cache)
363
static void * magazine_obj_get(slab_cache_t *cache)
364
{
364
{
365
    slab_magazine_t *mag;
365
    slab_magazine_t *mag;
366
    void *obj;
366
    void *obj;
367
 
367
 
-
 
368
    if (!CPU)
-
 
369
        return NULL;
-
 
370
 
368
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
371
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
369
 
372
 
370
    mag = get_full_current_mag(cache);
373
    mag = get_full_current_mag(cache);
371
    if (!mag) {
374
    if (!mag) {
372
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
375
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
373
        return NULL;
376
        return NULL;
374
    }
377
    }
375
    obj = mag->objs[--mag->busy];
378
    obj = mag->objs[--mag->busy];
376
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
379
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
377
    atomic_dec(&cache->cached_objs);
380
    atomic_dec(&cache->cached_objs);
378
   
381
   
379
    return obj;
382
    return obj;
380
}
383
}
381
 
384
 
382
/**
385
/**
383
 * Assure that the current magazine is empty, return pointer to it, or NULL if
386
 * Assure that the current magazine is empty, return pointer to it, or NULL if
384
 * no empty magazine is available and cannot be allocated
387
 * no empty magazine is available and cannot be allocated
385
 *
388
 *
386
 * We have 2 magazines bound to processor.
389
 * We have 2 magazines bound to processor.
387
 * First try the current.
390
 * First try the current.
388
 *  If full, try the last.
391
 *  If full, try the last.
389
 *   If full, put to magazines list.
392
 *   If full, put to magazines list.
390
 *   allocate new, exchange last & current
393
 *   allocate new, exchange last & current
391
 *
394
 *
392
 */
395
 */
393
static slab_magazine_t * make_empty_current_mag(slab_cache_t *cache)
396
static slab_magazine_t * make_empty_current_mag(slab_cache_t *cache)
394
{
397
{
395
    slab_magazine_t *cmag,*lastmag,*newmag;
398
    slab_magazine_t *cmag,*lastmag,*newmag;
396
 
399
 
397
    cmag = cache->mag_cache[CPU->id].current;
400
    cmag = cache->mag_cache[CPU->id].current;
398
    lastmag = cache->mag_cache[CPU->id].last;
401
    lastmag = cache->mag_cache[CPU->id].last;
399
 
402
 
400
    if (cmag) {
403
    if (cmag) {
401
        if (cmag->busy < cmag->size)
404
        if (cmag->busy < cmag->size)
402
            return cmag;
405
            return cmag;
403
        if (lastmag && lastmag->busy < lastmag->size) {
406
        if (lastmag && lastmag->busy < lastmag->size) {
404
            cache->mag_cache[CPU->id].last = cmag;
407
            cache->mag_cache[CPU->id].last = cmag;
405
            cache->mag_cache[CPU->id].current = lastmag;
408
            cache->mag_cache[CPU->id].current = lastmag;
406
            return lastmag;
409
            return lastmag;
407
        }
410
        }
408
    }
411
    }
409
    /* current | last are full | nonexistent, allocate new */
412
    /* current | last are full | nonexistent, allocate new */
410
    /* We do not want to sleep just because of caching */
413
    /* We do not want to sleep just because of caching */
411
    /* Especially we do not want reclaiming to start, as
414
    /* Especially we do not want reclaiming to start, as
412
     * this would deadlock */
415
     * this would deadlock */
413
    newmag = slab_alloc(&mag_cache, FRAME_ATOMIC | FRAME_NO_RECLAIM);
416
    newmag = slab_alloc(&mag_cache, FRAME_ATOMIC | FRAME_NO_RECLAIM);
414
    if (!newmag)
417
    if (!newmag)
415
        return NULL;
418
        return NULL;
416
    newmag->size = SLAB_MAG_SIZE;
419
    newmag->size = SLAB_MAG_SIZE;
417
    newmag->busy = 0;
420
    newmag->busy = 0;
418
 
421
 
419
    /* Flush last to magazine list */
422
    /* Flush last to magazine list */
420
    if (lastmag)
423
    if (lastmag)
421
        list_prepend(&lastmag->link, &cache->magazines);
424
        list_prepend(&lastmag->link, &cache->magazines);
422
    /* Move current as last, save new as current */
425
    /* Move current as last, save new as current */
423
    cache->mag_cache[CPU->id].last = cmag; 
426
    cache->mag_cache[CPU->id].last = cmag; 
424
    cache->mag_cache[CPU->id].current = newmag;
427
    cache->mag_cache[CPU->id].current = newmag;
425
 
428
 
426
    return newmag;
429
    return newmag;
427
}
430
}
428
 
431
 
429
/**
432
/**
430
 * Put object into CPU-cache magazine
433
 * Put object into CPU-cache magazine
431
 *
434
 *
432
 * @return 0 - success, -1 - could not get memory
435
 * @return 0 - success, -1 - could not get memory
433
 */
436
 */
434
static int magazine_obj_put(slab_cache_t *cache, void *obj)
437
static int magazine_obj_put(slab_cache_t *cache, void *obj)
435
{
438
{
436
    slab_magazine_t *mag;
439
    slab_magazine_t *mag;
437
 
440
 
-
 
441
    if (!CPU)
-
 
442
        return -1;
-
 
443
 
438
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
444
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
439
 
445
 
440
    mag = make_empty_current_mag(cache);
446
    mag = make_empty_current_mag(cache);
441
    if (!mag) {
447
    if (!mag) {
442
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
448
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
443
        return -1;
449
        return -1;
444
    }
450
    }
445
   
451
   
446
    mag->objs[mag->busy++] = obj;
452
    mag->objs[mag->busy++] = obj;
447
 
453
 
448
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
454
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
449
    atomic_inc(&cache->cached_objs);
455
    atomic_inc(&cache->cached_objs);
450
    return 0;
456
    return 0;
451
}
457
}
452
 
458
 
453
 
459
 
454
/**************************************/
460
/**************************************/
455
/* SLAB CACHE functions */
461
/* SLAB CACHE functions */
456
 
462
 
457
/** Return number of objects that fit in certain cache size */
463
/** Return number of objects that fit in certain cache size */
458
static int comp_objects(slab_cache_t *cache)
464
static int comp_objects(slab_cache_t *cache)
459
{
465
{
460
    if (cache->flags & SLAB_CACHE_SLINSIDE)
466
    if (cache->flags & SLAB_CACHE_SLINSIDE)
461
        return ((PAGE_SIZE << cache->order) - sizeof(slab_t)) / cache->size;
467
        return ((PAGE_SIZE << cache->order) - sizeof(slab_t)) / cache->size;
462
    else
468
    else
463
        return (PAGE_SIZE << cache->order) / cache->size;
469
        return (PAGE_SIZE << cache->order) / cache->size;
464
}
470
}
465
 
471
 
466
/** Return wasted space in slab */
472
/** Return wasted space in slab */
467
static int badness(slab_cache_t *cache)
473
static int badness(slab_cache_t *cache)
468
{
474
{
469
    int objects;
475
    int objects;
470
    int ssize;
476
    int ssize;
471
 
477
 
472
    objects = comp_objects(cache);
478
    objects = comp_objects(cache);
473
    ssize = PAGE_SIZE << cache->order;
479
    ssize = PAGE_SIZE << cache->order;
474
    if (cache->flags & SLAB_CACHE_SLINSIDE)
480
    if (cache->flags & SLAB_CACHE_SLINSIDE)
475
        ssize -= sizeof(slab_t);
481
        ssize -= sizeof(slab_t);
476
    return ssize - objects*cache->size;
482
    return ssize - objects*cache->size;
477
}
483
}
478
 
484
 
479
/** Initialize allocated memory as a slab cache */
485
/** Initialize allocated memory as a slab cache */
480
static void
486
static void
481
_slab_cache_create(slab_cache_t *cache,
487
_slab_cache_create(slab_cache_t *cache,
482
           char *name,
488
           char *name,
483
           size_t size,
489
           size_t size,
484
           size_t align,
490
           size_t align,
485
           int (*constructor)(void *obj, int kmflag),
491
           int (*constructor)(void *obj, int kmflag),
486
           void (*destructor)(void *obj),
492
           void (*destructor)(void *obj),
487
           int flags)
493
           int flags)
488
{
494
{
489
    int i;
495
    int i;
490
    int pages;
496
    int pages;
491
 
497
 
492
    memsetb((__address)cache, sizeof(*cache), 0);
498
    memsetb((__address)cache, sizeof(*cache), 0);
493
    cache->name = name;
499
    cache->name = name;
494
 
500
 
495
    if (align < sizeof(__native))
501
    if (align < sizeof(__native))
496
        align = sizeof(__native);
502
        align = sizeof(__native);
497
    size = ALIGN_UP(size, align);
503
    size = ALIGN_UP(size, align);
498
       
504
       
499
    cache->size = size;
505
    cache->size = size;
500
 
506
 
501
    cache->constructor = constructor;
507
    cache->constructor = constructor;
502
    cache->destructor = destructor;
508
    cache->destructor = destructor;
503
    cache->flags = flags;
509
    cache->flags = flags;
504
 
510
 
505
    list_initialize(&cache->full_slabs);
511
    list_initialize(&cache->full_slabs);
506
    list_initialize(&cache->partial_slabs);
512
    list_initialize(&cache->partial_slabs);
507
    list_initialize(&cache->magazines);
513
    list_initialize(&cache->magazines);
508
    spinlock_initialize(&cache->lock, "cachelock");
514
    spinlock_initialize(&cache->lock, "cachelock");
509
    if (! (cache->flags & SLAB_CACHE_NOMAGAZINE)) {
515
    if (! (cache->flags & SLAB_CACHE_NOMAGAZINE)) {
510
        for (i=0; i< config.cpu_count; i++)
516
        for (i=0; i< config.cpu_count; i++) {
-
 
517
            memsetb((__address)&cache->mag_cache[i],
-
 
518
                sizeof(cache->mag_cache[i]), 0);
511
            spinlock_initialize(&cache->mag_cache[i].lock,
519
            spinlock_initialize(&cache->mag_cache[i].lock,
512
                        "cpucachelock");
520
                        "cpucachelock");
-
 
521
        }
513
    }
522
    }
514
 
523
 
515
    /* Compute slab sizes, object counts in slabs etc. */
524
    /* Compute slab sizes, object counts in slabs etc. */
516
    if (cache->size < SLAB_INSIDE_SIZE)
525
    if (cache->size < SLAB_INSIDE_SIZE)
517
        cache->flags |= SLAB_CACHE_SLINSIDE;
526
        cache->flags |= SLAB_CACHE_SLINSIDE;
518
 
527
 
519
    /* Minimum slab order */
528
    /* Minimum slab order */
520
    pages = ((cache->size-1) >> PAGE_WIDTH) + 1;
529
    pages = ((cache->size-1) >> PAGE_WIDTH) + 1;
521
    cache->order = fnzb(pages);
530
    cache->order = fnzb(pages);
522
 
531
 
523
    while (badness(cache) > SLAB_MAX_BADNESS(cache)) {
532
    while (badness(cache) > SLAB_MAX_BADNESS(cache)) {
524
        cache->order += 1;
533
        cache->order += 1;
525
    }
534
    }
526
    cache->objects = comp_objects(cache);
535
    cache->objects = comp_objects(cache);
527
    /* If info fits in, put it inside */
536
    /* If info fits in, put it inside */
528
    if (badness(cache) > sizeof(slab_t))
537
    if (badness(cache) > sizeof(slab_t))
529
        cache->flags |= SLAB_CACHE_SLINSIDE;
538
        cache->flags |= SLAB_CACHE_SLINSIDE;
530
 
539
 
531
    spinlock_lock(&slab_cache_lock);
540
    spinlock_lock(&slab_cache_lock);
532
 
541
 
533
    list_append(&cache->link, &slab_cache_list);
542
    list_append(&cache->link, &slab_cache_list);
534
 
543
 
535
    spinlock_unlock(&slab_cache_lock);
544
    spinlock_unlock(&slab_cache_lock);
536
}
545
}
537
 
546
 
538
/** Create slab cache  */
547
/** Create slab cache  */
539
slab_cache_t * slab_cache_create(char *name,
548
slab_cache_t * slab_cache_create(char *name,
540
                 size_t size,
549
                 size_t size,
541
                 size_t align,
550
                 size_t align,
542
                 int (*constructor)(void *obj, int kmflag),
551
                 int (*constructor)(void *obj, int kmflag),
543
                 void (*destructor)(void *obj),
552
                 void (*destructor)(void *obj),
544
                 int flags)
553
                 int flags)
545
{
554
{
546
    slab_cache_t *cache;
555
    slab_cache_t *cache;
547
 
556
 
548
    cache = slab_alloc(&slab_cache_cache, 0);
557
    cache = slab_alloc(&slab_cache_cache, 0);
549
    _slab_cache_create(cache, name, size, align, constructor, destructor,
558
    _slab_cache_create(cache, name, size, align, constructor, destructor,
550
               flags);
559
               flags);
551
    return cache;
560
    return cache;
552
}
561
}
553
 
562
 
554
/**
563
/**
555
 * Reclaim space occupied by objects that are already free
564
 * Reclaim space occupied by objects that are already free
556
 *
565
 *
557
 * @param flags If contains SLAB_RECLAIM_ALL, do aggressive freeing
566
 * @param flags If contains SLAB_RECLAIM_ALL, do aggressive freeing
558
 * @return Number of freed pages
567
 * @return Number of freed pages
559
 */
568
 */
560
static count_t _slab_reclaim(slab_cache_t *cache, int flags)
569
static count_t _slab_reclaim(slab_cache_t *cache, int flags)
561
{
570
{
562
    int i;
571
    int i;
563
    slab_magazine_t *mag;
572
    slab_magazine_t *mag;
564
    link_t *cur;
573
    link_t *cur;
565
    count_t frames = 0;
574
    count_t frames = 0;
566
   
575
   
567
    if (cache->flags & SLAB_CACHE_NOMAGAZINE)
576
    if (cache->flags & SLAB_CACHE_NOMAGAZINE)
568
        return 0; /* Nothing to do */
577
        return 0; /* Nothing to do */
569
   
578
   
570
    /* First lock all cpu caches, then the complete cache lock */
579
    /* First lock all cpu caches, then the complete cache lock */
571
    if (flags & SLAB_RECLAIM_ALL) {
580
    if (flags & SLAB_RECLAIM_ALL) {
572
        for (i=0; i < config.cpu_count; i++)
581
        for (i=0; i < config.cpu_count; i++)
573
            spinlock_lock(&cache->mag_cache[i].lock);
582
            spinlock_lock(&cache->mag_cache[i].lock);
574
    }
583
    }
575
    spinlock_lock(&cache->lock);
584
    spinlock_lock(&cache->lock);
576
   
585
   
577
    if (flags & SLAB_RECLAIM_ALL) {
586
    if (flags & SLAB_RECLAIM_ALL) {
578
        /* Aggressive memfree */
587
        /* Aggressive memfree */
579
        /* Destroy CPU magazines */
588
        /* Destroy CPU magazines */
580
        for (i=0; i<config.cpu_count; i++) {
589
        for (i=0; i<config.cpu_count; i++) {
581
            mag = cache->mag_cache[i].current;
590
            mag = cache->mag_cache[i].current;
582
            if (mag)
591
            if (mag)
583
                frames += magazine_destroy(cache, mag);
592
                frames += magazine_destroy(cache, mag);
584
            cache->mag_cache[i].current = NULL;
593
            cache->mag_cache[i].current = NULL;
585
           
594
           
586
            mag = cache->mag_cache[i].last;
595
            mag = cache->mag_cache[i].last;
587
            if (mag)
596
            if (mag)
588
                frames += magazine_destroy(cache, mag);
597
                frames += magazine_destroy(cache, mag);
589
            cache->mag_cache[i].last = NULL;
598
            cache->mag_cache[i].last = NULL;
590
        }
599
        }
591
    }
600
    }
592
    /* Destroy full magazines */
601
    /* Destroy full magazines */
593
    cur=cache->magazines.prev;
602
    cur=cache->magazines.prev;
594
 
603
 
595
    while (cur != &cache->magazines) {
604
    while (cur != &cache->magazines) {
596
        mag = list_get_instance(cur, slab_magazine_t, link);
605
        mag = list_get_instance(cur, slab_magazine_t, link);
597
       
606
       
598
        cur = cur->prev;
607
        cur = cur->prev;
599
        list_remove(&mag->link);
608
        list_remove(&mag->link);
600
        frames += magazine_destroy(cache,mag);
609
        frames += magazine_destroy(cache,mag);
601
        /* If we do not do full reclaim, break
610
        /* If we do not do full reclaim, break
602
         * as soon as something is freed */
611
         * as soon as something is freed */
603
        if (!(flags & SLAB_RECLAIM_ALL) && frames)
612
        if (!(flags & SLAB_RECLAIM_ALL) && frames)
604
            break;
613
            break;
605
    }
614
    }
606
   
615
   
607
    spinlock_unlock(&cache->lock);
616
    spinlock_unlock(&cache->lock);
608
    if (flags & SLAB_RECLAIM_ALL) {
617
    if (flags & SLAB_RECLAIM_ALL) {
609
        for (i=0; i < config.cpu_count; i++)
618
        for (i=0; i < config.cpu_count; i++)
610
            spinlock_unlock(&cache->mag_cache[i].lock);
619
            spinlock_unlock(&cache->mag_cache[i].lock);
611
    }
620
    }
612
   
621
   
613
    return frames;
622
    return frames;
614
}
623
}
615
 
624
 
616
/** Check that there are no slabs and remove cache from system  */
625
/** Check that there are no slabs and remove cache from system  */
617
void slab_cache_destroy(slab_cache_t *cache)
626
void slab_cache_destroy(slab_cache_t *cache)
618
{
627
{
619
    /* Do not lock anything, we assume the software is correct and
628
    /* Do not lock anything, we assume the software is correct and
620
     * does not touch the cache when it decides to destroy it */
629
     * does not touch the cache when it decides to destroy it */
621
   
630
   
622
    /* Destroy all magazines */
631
    /* Destroy all magazines */
623
    _slab_reclaim(cache, SLAB_RECLAIM_ALL);
632
    _slab_reclaim(cache, SLAB_RECLAIM_ALL);
624
 
633
 
625
    /* All slabs must be empty */
634
    /* All slabs must be empty */
626
    if (!list_empty(&cache->full_slabs) \
635
    if (!list_empty(&cache->full_slabs) \
627
        || !list_empty(&cache->partial_slabs))
636
        || !list_empty(&cache->partial_slabs))
628
        panic("Destroying cache that is not empty.");
637
        panic("Destroying cache that is not empty.");
629
 
638
 
630
    spinlock_lock(&slab_cache_lock);
639
    spinlock_lock(&slab_cache_lock);
631
    list_remove(&cache->link);
640
    list_remove(&cache->link);
632
    spinlock_unlock(&slab_cache_lock);
641
    spinlock_unlock(&slab_cache_lock);
633
 
642
 
634
    slab_free(&slab_cache_cache, cache);
643
    slab_free(&slab_cache_cache, cache);
635
}
644
}
636
 
645
 
637
/** Allocate new object from cache - if no flags given, always returns
646
/** Allocate new object from cache - if no flags given, always returns
638
    memory */
647
    memory */
639
void * slab_alloc(slab_cache_t *cache, int flags)
648
void * slab_alloc(slab_cache_t *cache, int flags)
640
{
649
{
641
    ipl_t ipl;
650
    ipl_t ipl;
642
    void *result = NULL;
651
    void *result = NULL;
643
 
652
 
644
    /* Disable interrupts to avoid deadlocks with interrupt handlers */
653
    /* Disable interrupts to avoid deadlocks with interrupt handlers */
645
    ipl = interrupts_disable();
654
    ipl = interrupts_disable();
646
 
655
 
647
    if (!(cache->flags & SLAB_CACHE_NOMAGAZINE) && CPU)
656
    if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
648
        result = magazine_obj_get(cache);
657
        result = magazine_obj_get(cache);
649
 
658
 
650
    if (!result) {
659
    if (!result) {
651
        spinlock_lock(&cache->lock);
660
        spinlock_lock(&cache->lock);
652
        result = slab_obj_create(cache, flags);
661
        result = slab_obj_create(cache, flags);
653
        spinlock_unlock(&cache->lock);
662
        spinlock_unlock(&cache->lock);
654
    }
663
    }
655
 
664
 
656
    interrupts_restore(ipl);
665
    interrupts_restore(ipl);
657
 
666
 
658
    if (result)
667
    if (result)
659
        atomic_inc(&cache->allocated_objs);
668
        atomic_inc(&cache->allocated_objs);
660
 
669
 
661
    return result;
670
    return result;
662
}
671
}
663
 
672
 
664
/** Return object to cache, use slab if known  */
673
/** Return object to cache, use slab if known  */
665
static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab)
674
static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab)
666
{
675
{
667
    ipl_t ipl;
676
    ipl_t ipl;
668
 
677
 
669
    ipl = interrupts_disable();
678
    ipl = interrupts_disable();
670
 
679
 
671
    if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \
680
    if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \
672
        || !CPU \
-
 
673
        || magazine_obj_put(cache, obj)) {
681
        || magazine_obj_put(cache, obj)) {
674
       
-
 
675
        spinlock_lock(&cache->lock);
682
        spinlock_lock(&cache->lock);
676
        slab_obj_destroy(cache, obj, slab);
683
        slab_obj_destroy(cache, obj, slab);
677
        spinlock_unlock(&cache->lock);
684
        spinlock_unlock(&cache->lock);
678
    }
685
    }
679
    interrupts_restore(ipl);
686
    interrupts_restore(ipl);
680
    atomic_dec(&cache->allocated_objs);
687
    atomic_dec(&cache->allocated_objs);
681
}
688
}
682
 
689
 
683
/** Return slab object to cache */
690
/** Return slab object to cache */
684
void slab_free(slab_cache_t *cache, void *obj)
691
void slab_free(slab_cache_t *cache, void *obj)
685
{
692
{
686
    _slab_free(cache,obj,NULL);
693
    _slab_free(cache,obj,NULL);
687
}
694
}
688
 
695
 
689
/* Go through all caches and reclaim what is possible */
696
/* Go through all caches and reclaim what is possible */
690
count_t slab_reclaim(int flags)
697
count_t slab_reclaim(int flags)
691
{
698
{
692
    slab_cache_t *cache;
699
    slab_cache_t *cache;
693
    link_t *cur;
700
    link_t *cur;
694
    count_t frames = 0;
701
    count_t frames = 0;
695
 
702
 
696
    spinlock_lock(&slab_cache_lock);
703
    spinlock_lock(&slab_cache_lock);
697
 
704
 
698
    for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) {
705
    for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) {
699
        cache = list_get_instance(cur, slab_cache_t, link);
706
        cache = list_get_instance(cur, slab_cache_t, link);
700
        frames += _slab_reclaim(cache, flags);
707
        frames += _slab_reclaim(cache, flags);
701
    }
708
    }
702
 
709
 
703
    spinlock_unlock(&slab_cache_lock);
710
    spinlock_unlock(&slab_cache_lock);
704
 
711
 
705
    return frames;
712
    return frames;
706
}
713
}
707
 
714
 
708
 
715
 
709
/* Print list of slabs */
716
/* Print list of slabs */
710
void slab_print_list(void)
717
void slab_print_list(void)
711
{
718
{
712
    slab_cache_t *cache;
719
    slab_cache_t *cache;
713
    link_t *cur;
720
    link_t *cur;
714
 
721
 
715
    spinlock_lock(&slab_cache_lock);
722
    spinlock_lock(&slab_cache_lock);
716
    printf("SLAB name\tOsize\tPages\tObj/pg\tSlabs\tCached\tAllocobjs\tCtl\n");
723
    printf("SLAB name\tOsize\tPages\tObj/pg\tSlabs\tCached\tAllocobjs\tCtl\n");
717
    for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) {
724
    for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) {
718
        cache = list_get_instance(cur, slab_cache_t, link);
725
        cache = list_get_instance(cur, slab_cache_t, link);
719
        printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\t\t%s\n", cache->name, cache->size,
726
        printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\t\t%s\n", cache->name, cache->size,
720
               (1 << cache->order), cache->objects,
727
               (1 << cache->order), cache->objects,
721
               atomic_get(&cache->allocated_slabs),
728
               atomic_get(&cache->allocated_slabs),
722
               atomic_get(&cache->cached_objs),
729
               atomic_get(&cache->cached_objs),
723
               atomic_get(&cache->allocated_objs),
730
               atomic_get(&cache->allocated_objs),
724
               cache->flags & SLAB_CACHE_SLINSIDE ? "In" : "Out");
731
               cache->flags & SLAB_CACHE_SLINSIDE ? "In" : "Out");
725
    }
732
    }
726
    spinlock_unlock(&slab_cache_lock);
733
    spinlock_unlock(&slab_cache_lock);
727
}
734
}
728
 
735
 
729
void slab_cache_init(void)
736
void slab_cache_init(void)
730
{
737
{
731
    int i, size;
738
    int i, size;
732
 
739
 
733
    /* Initialize magazine cache */
740
    /* Initialize magazine cache */
734
    _slab_cache_create(&mag_cache,
741
    _slab_cache_create(&mag_cache,
735
               "slab_magazine",
742
               "slab_magazine",
736
               sizeof(slab_magazine_t)+SLAB_MAG_SIZE*sizeof(void*),
743
               sizeof(slab_magazine_t)+SLAB_MAG_SIZE*sizeof(void*),
737
               sizeof(__address),
744
               sizeof(__address),
738
               NULL, NULL,
745
               NULL, NULL,
739
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
746
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
740
    /* Initialize slab_cache cache */
747
    /* Initialize slab_cache cache */
741
    _slab_cache_create(&slab_cache_cache,
748
    _slab_cache_create(&slab_cache_cache,
742
               "slab_cache",
749
               "slab_cache",
743
               sizeof(slab_cache_cache) + config.cpu_count*sizeof(slab_cache_cache.mag_cache[0]),
750
               sizeof(slab_cache_cache) + config.cpu_count*sizeof(slab_cache_cache.mag_cache[0]),
744
               sizeof(__address),
751
               sizeof(__address),
745
               NULL, NULL,
752
               NULL, NULL,
746
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
753
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
747
    /* Initialize external slab cache */
754
    /* Initialize external slab cache */
748
    slab_extern_cache = slab_cache_create("slab_extern",
755
    slab_extern_cache = slab_cache_create("slab_extern",
749
                          sizeof(slab_t),
756
                          sizeof(slab_t),
750
                          0, NULL, NULL,
757
                          0, NULL, NULL,
751
                          SLAB_CACHE_SLINSIDE);
758
                          SLAB_CACHE_SLINSIDE);
752
 
759
 
753
    /* Initialize structures for malloc */
760
    /* Initialize structures for malloc */
754
    for (i=0, size=(1<<SLAB_MIN_MALLOC_W);
761
    for (i=0, size=(1<<SLAB_MIN_MALLOC_W);
755
         i < (SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1);
762
         i < (SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1);
756
         i++, size <<= 1) {
763
         i++, size <<= 1) {
757
        malloc_caches[i] = slab_cache_create(malloc_names[i],
764
        malloc_caches[i] = slab_cache_create(malloc_names[i],
758
                             size, 0,
765
                             size, 0,
759
                             NULL,NULL,0);
766
                             NULL,NULL,0);
760
    }
767
    }
761
}
768
}
762
 
769
 
763
/**************************************/
770
/**************************************/
764
/* kalloc/kfree functions             */
771
/* kalloc/kfree functions             */
765
void * kalloc(unsigned int size, int flags)
772
void * kalloc(unsigned int size, int flags)
766
{
773
{
767
    int idx;
774
    int idx;
768
 
775
 
769
    ASSERT( size && size <= (1 << SLAB_MAX_MALLOC_W));
776
    ASSERT( size && size <= (1 << SLAB_MAX_MALLOC_W));
770
   
777
   
771
    if (size < (1 << SLAB_MIN_MALLOC_W))
778
    if (size < (1 << SLAB_MIN_MALLOC_W))
772
        size = (1 << SLAB_MIN_MALLOC_W);
779
        size = (1 << SLAB_MIN_MALLOC_W);
773
 
780
 
774
    idx = fnzb(size-1) - SLAB_MIN_MALLOC_W + 1;
781
    idx = fnzb(size-1) - SLAB_MIN_MALLOC_W + 1;
775
 
782
 
776
    return slab_alloc(malloc_caches[idx], flags);
783
    return slab_alloc(malloc_caches[idx], flags);
777
}
784
}
778
 
785
 
779
 
786
 
780
void kfree(void *obj)
787
void kfree(void *obj)
781
{
788
{
782
    slab_t *slab = obj2slab(obj);
789
    slab_t *slab = obj2slab(obj);
783
   
790
   
784
    _slab_free(slab->cache, obj, slab);
791
    _slab_free(slab->cache, obj, slab);
785
}
792
}
786
 
793