aboutsummaryrefslogtreecommitdiffstats
path: root/xen/common/page_alloc.c
blob: 323ecd244e1e810ad576f1e2984d1859fc248de4 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/******************************************************************************
 * page_alloc.c
 * 
 * Simple buddy heap allocator for Xen.
 * 
 * Copyright (c) 2002-2004 K A Fraser
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/types.h>
#include <xen/lib.h>
#include <asm/page.h>
#include <xen/spinlock.h>
#include <xen/slab.h>
#include <xen/irq.h>

extern char opt_badpage[];

/*********************
 * ALLOCATION BITMAP
 *  One bit per page of memory. Bit set => page is allocated.
 */

static unsigned long *alloc_bitmap;
#define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)

#define allocated_in_map(_pn) \
(alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & (1<<((_pn)&(PAGES_PER_MAPWORD-1))))


/*
 * Hint regarding bitwise arithmetic in map_{alloc,free}:
 *  -(1<<n)  sets all bits >= n. 
 *  (1<<n)-1 sets all bits <  n.
 * Variable names in map_{alloc,free}:
 *  *_idx == Index into `alloc_bitmap' array.
 *  *_off == Bit offset within an element of the `alloc_bitmap' array.
 */

static void map_alloc(unsigned long first_page, unsigned long nr_pages)
{
    unsigned long start_off, end_off, curr_idx, end_idx;

#ifndef NDEBUG
    unsigned long i;
    /* Check that the block isn't already allocated. */
    for ( i = 0; i < nr_pages; i++ )
        ASSERT(!allocated_in_map(first_page + i));
#endif

    curr_idx  = first_page / PAGES_PER_MAPWORD;
    start_off = first_page & (PAGES_PER_MAPWORD-1);
    end_idx   = (first_page + nr_pages) / PAGES_PER_MAPWORD;
    end_off   = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);

    if ( curr_idx == end_idx )
    {
        alloc_bitmap[curr_idx] |= ((1<<end_off)-1) & -(1<<start_off);
    }
    else 
    {
        alloc_bitmap[curr_idx] |= -(1<<start_off);
        while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0L;
        alloc_bitmap[curr_idx] |= (1<<end_off)-1;
    }
}


static void map_free(unsigned long first_page, unsigned long nr_pages)
{
    unsigned long start_off, end_off, curr_idx, end_idx;

#ifndef NDEBUG
    unsigned long i;
    /* Check that the block isn't already freed. */
    for ( i = 0; i < nr_pages; i++ )
        ASSERT(allocated_in_map(first_page + i));
#endif

    curr_idx = first_page / PAGES_PER_MAPWORD;
    start_off = first_page & (PAGES_PER_MAPWORD-1);
    end_idx   = (first_page + nr_pages) / PAGES_PER_MAPWORD;
    end_off   = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);

    if ( curr_idx == end_idx )
    {
        alloc_bitmap[curr_idx] &= -(1<<end_off) | ((1<<start_off)-1);
    }
    else 
    {
        alloc_bitmap[curr_idx] &= (1<<start_off)-1;
        while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
        alloc_bitmap[curr_idx] &= -(1<<end_off);
    }
}



/*************************
 * BINARY BUDDY ALLOCATOR
 */

#define MEMZONE_XEN 0
#define MEMZONE_DOM 1
#define NR_ZONES    2

/* Up to 2^10 pages can be allocated at once. */
#define MIN_ORDER  0
#define MAX_ORDER 10
#define NR_ORDERS (MAX_ORDER - MIN_ORDER + 1)
static struct list_head heap[NR_ZONES][NR_ORDERS];

static unsigned long avail[NR_ZONES];

#define round_pgdown(_p)  ((_p)&PAGE_MASK)
#define round_pgup(_p)    (((_p)+(PAGE_SIZE-1))&PAGE_MASK)

static spinlock_t heap_lock = SPIN_LOCK_UNLOCKED;


/* Initialise allocator to handle up to @max_pages. */
unsigned long init_heap_allocator(
    unsigned long bitmap_start, unsigned long max_pages)
{
    int i, j;
    unsigned long bitmap_size, bad_pfn;
    char *p;

    memset(avail, 0, sizeof(avail));

    for ( i = 0; i < NR_ZONES; i++ )
        for ( j = 0; j < NR_ORDERS; j++ )
            INIT_LIST_HEAD(&heap[i][j]);

    bitmap_start = round_pgup(bitmap_start);

    /* Allocate space for the allocation bitmap. */
    bitmap_size  = max_pages / 8;
    bitmap_size  = round_pgup(bitmap_size);
    alloc_bitmap = (unsigned long *)phys_to_virt(bitmap_start);

    /* All allocated by default. */
    memset(alloc_bitmap, ~0, bitmap_size);

    /*
     * Process the bad-page list. Marking the page free in the bitmap will
     * indicate to init_heap_pages() that it should not be placed on the 
     * buddy lists.
     */
    p = opt_badpage;
    while ( *p != '\0' )
    {
        bad_pfn = simple_strtoul(p, &p, 0);

        if ( *p == ',' )
            p++;
        else if ( *p != '\0' )
            break;

        if ( (bad_pfn < max_pages) && allocated_in_map(bad_pfn) )
        {
            printk("Marking page %08lx as bad\n", bad_pfn);
            map_free(bad_pfn, 1);
        }
    }

    return bitmap_start + bitmap_size;
}

/* Hand the specified arbitrary page range to the specified heap zone. */
void init_heap_pages(int zone, struct pfn_info *pg, unsigned long nr_pages)
{
    unsigned long i, pfn = page_to_pfn(pg);

    /* Process each page in turn, skipping bad pages. */
    for ( i = 0; i < nr_pages; i++ )
    {
        if ( likely(allocated_in_map(pfn+i)) ) /* bad page? */
            free_heap_pages(zone, pg+i, 0);
    }
}


/* Allocate 2^@order contiguous pages. */
struct pfn_info *alloc_heap_pages(int zone, int order)
{
    int i;
    struct pfn_info *pg;
    unsigned long flags;

    if ( unlikely(order < MIN_ORDER) || unlikely(order > MAX_ORDER) )
        return NULL;

    spin_lock_irqsave(&heap_lock, flags);

    /* Find smallest order which can satisfy the request. */
    for ( i = order; i < NR_ORDERS; i++ )
	if ( !list_empty(&heap[zone][i]) )
	    break;

    if ( i == NR_ORDERS ) 
        goto no_memory;
 
    pg = list_entry(heap[zone][i].next, struct pfn_info, list);
    list_del(&pg->list);

    /* We may have to halve the chunk a number of times. */
    while ( i != order )
    {
        PFN_ORDER(pg) = --i;
        list_add_tail(&pg->list, &heap[zone][i]);
        pg += 1 << i;
    }
    
    map_alloc(page_to_pfn(pg), 1 << order);
    avail[zone] -= 1 << order;

    spin_unlock_irqrestore(&heap_lock, flags);

    return pg;

 no_memory:
    spin_unlock_irqrestore(&heap_lock, flags);
    return NULL;
}


/* Free 2^@order set of pages. */
void free_heap_pages(int zone, struct pfn_info *pg, int order)
{
    unsigned long mask;
    unsigned long flags;

    spin_lock_irqsave(&heap_lock, flags);

    map_free(page_to_pfn(pg), 1 << order);
    avail[zone] += 1 << order;
    
    /* Merge chunks as far as possible. */
    while ( order < MAX_ORDER )
    {
        mask = 1 << order;

        if ( (page_to_pfn(pg) & mask) )
        {
            /* Merge with predecessor block? */
            if ( allocated_in_map(page_to_pfn(pg)-mask) ||
                 (PFN_ORDER(pg-mask) != order) )
                break;
            list_del(&(pg-mask)->list);
            pg -= mask;
        }
        else
        {
            /* Merge with successor block? */
            if ( allocated_in_map(page_to_pfn(pg)+mask) ||
                 (PFN_ORDER(pg+mask) != order) )
                break;
            list_del(&(pg+mask)->list);
        }
        
        order++;
    }

    PFN_ORDER(pg) = order;
    list_add_tail(&pg->list, &heap[zone][order]);

    spin_unlock_irqrestore(&heap_lock, flags);
}



/*************************
 * XEN-HEAP SUB-ALLOCATOR
 */

void init_xenheap_pages(unsigned long ps, unsigned long pe)
{
    ps = round_pgup(ps);
    pe = round_pgdown(pe);
    memguard_guard_range(__va(ps), pe - ps);
    init_heap_pages(MEMZONE_XEN, phys_to_page(ps), (pe - ps) >> PAGE_SHIFT);
}

unsigned long alloc_xenheap_pages(int order)
{
    struct pfn_info *pg;
    int attempts = 0;

 retry:
    if ( unlikely((pg = alloc_heap_pages(MEMZONE_XEN, order)) == NULL) )
        goto no_memory;
    memguard_unguard_range(page_to_virt(pg), 1 << (order + PAGE_SHIFT));
    return (unsigned long)page_to_virt(pg);

 no_memory:
    if ( attempts++ < 8 )
    {
        xmem_cache_reap();
        goto retry;
    }

    printk("Cannot handle page request order %d!\n", order);
    dump_slabinfo();
    return 0;
}

void free_xenheap_pages(unsigned long p, int order)
{
    memguard_guard_range((void *)p, 1 << (order + PAGE_SHIFT));    
    free_heap_pages(MEMZONE_XEN, virt_to_page(p), order);
}



/*************************
 * DOMAIN-HEAP SUB-ALLOCATOR
 */

void init_domheap_pages(unsigned long ps, unsigned long pe)
{
    ps = round_pgup(ps);
    pe = round_pgdown(pe);
    init_heap_pages(MEMZONE_DOM, phys_to_page(ps), (pe - ps) >> PAGE_SHIFT);
}

struct pfn_info *alloc_domheap_pages(struct domain *d, int order)
{
    struct pfn_info *pg;
    unsigned long mask, flushed_mask, pfn_stamp, cpu_stamp;
    int i;

    ASSERT(!in_irq());

    if ( unlikely((pg = alloc_heap_pages(MEMZONE_DOM, order)) == NULL) )
        return NULL;

    flushed_mask = 0;
    for ( i = 0; i < (1 << order); i++ )
    {
        pg[i].u.inuse.domain    = NULL;
        pg[i].u.inuse.type_info = 0;

        if ( (mask = (pg[i].u.free.cpu_mask & ~flushed_mask)) != 0 )
        {
            pfn_stamp = pg[i].tlbflush_timestamp;
            for ( i = 0; (mask != 0) && (i < smp_num_cpus); i++ )
            {
                if ( mask & (1<<i) )
                {
                    cpu_stamp = tlbflush_time[i];
                    if ( !NEED_FLUSH(cpu_stamp, pfn_stamp) )
                        mask &= ~(1<<i);
                }
            }
            
            if ( unlikely(mask != 0) )
            {
                flush_tlb_mask(mask);
                perfc_incrc(need_flush_tlb_flush);
                flushed_mask |= mask;
            }
        }
    }

    if ( d == NULL )
        return pg;

    spin_lock(&d->page_alloc_lock);

    if ( unlikely((d->tot_pages + (1 << order)) > d->max_pages) )
    {
        DPRINTK("Over-allocation for domain %u: %u > %u\n",
                d->domain, d->tot_pages + (1 << order), d->max_pages);
        spin_unlock(&d->page_alloc_lock);
        free_heap_pages(MEMZONE_DOM, pg, order);
        return NULL;
    }

    if ( unlikely(d->tot_pages == 0) )
        get_knownalive_domain(d);

    d->tot_pages += 1 << order;

    for ( i = 0; i < (1 << order); i++ )
    {
        pg[i].u.inuse.domain = d;
        wmb(); /* Domain pointer must be visible before updating refcnt. */
        pg[i].u.inuse.count_info = PGC_allocated | 1;
        list_add_tail(&pg[i].list, &d->page_list);
    }

    spin_unlock(&d->page_alloc_lock);
    
    return pg;
}

void free_domheap_pages(struct pfn_info *pg, int order)
{
    int            i, drop_dom_ref;
    struct domain *d = pg->u.inuse.domain;

    if ( unlikely(IS_XEN_HEAP_FRAME(pg)) )
    {
        spin_lock_recursive(&d->page_alloc_lock);
        d->xenheap_pages -= 1 << order;
        drop_dom_ref = (d->xenheap_pages == 0);
        spin_unlock_recursive(&d->page_alloc_lock);
    }
    else if ( likely(d != NULL) )
    {
        /* NB. May recursively lock from domain_relinquish_memory(). */
        spin_lock_recursive(&d->page_alloc_lock);

        for ( i = 0; i < (1 << order); i++ )
        {
            pg[i].tlbflush_timestamp = tlbflush_clock;
            pg[i].u.inuse.count_info = 0;
            pg[i].u.free.cpu_mask    = 1 << d->processor;
            list_del(&pg[i].list);
        }

        d->tot_pages -= 1 << order;
        drop_dom_ref = (d->tot_pages == 0);

        spin_unlock_recursive(&d->page_alloc_lock);

        free_heap_pages(MEMZONE_DOM, pg, order);
    }
    else
    {
        /* Freeing an anonymous domain-heap page. */
        free_heap_pages(MEMZONE_DOM, pg, order);
        drop_dom_ref = 0;
    }

    if ( drop_dom_ref )
        put_domain(d);
}

unsigned long avail_domheap_pages(void)
{
    return avail[MEMZONE_DOM];
}