aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6.8.1-xen-sparse/drivers/xen/balloon/balloon.c
blob: 9d480c12cf62714a75b6b98fb01735dd291a339e (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/******************************************************************************
 * balloon.c
 *
 * Xen balloon driver - enables returning/claiming memory to/from Xen.
 *
 * Copyright (c) 2003, B Dragovic
 * 
 * This file may be distributed separately from the Linux kernel, or
 * incorporated into other software packages, subject to the following license:
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm-xen/xen_proc.h>

#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/smp_lock.h>
#include <linux/pagemap.h>
#include <linux/bootmem.h>
#include <linux/highmem.h>
#include <linux/vmalloc.h>

#include <asm-xen/hypervisor.h>
#include <asm-xen/ctrl_if.h>
#include <asm/pgalloc.h>
#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/tlb.h>

/* USER DEFINES -- THESE SHOULD BE COPIED TO USER-SPACE TOOLS */
#define USER_INFLATE_BALLOON  1   /* return mem to hypervisor */
#define USER_DEFLATE_BALLOON  2   /* claim mem from hypervisor */
typedef struct user_balloon_op {
    unsigned int  op;
    unsigned long size;
} user_balloon_op_t;
/* END OF USER DEFINE */

static struct proc_dir_entry *balloon_pde;

unsigned long credit;
static unsigned long current_pages, most_seen_pages;

/*
 * Dead entry written into balloon-owned entries in the PMT.
 * It is deliberately different to INVALID_P2M_ENTRY.
 */
#define DEAD 0xdead1234

static inline pte_t *get_ptep(unsigned long addr)
{
    pgd_t *pgd; pmd_t *pmd; pte_t *ptep;
    pgd = pgd_offset_k(addr);

    if ( pgd_none(*pgd) || pgd_bad(*pgd) ) BUG();

    pmd = pmd_offset(pgd, addr);
    if ( pmd_none(*pmd) || pmd_bad(*pmd) ) BUG();

    ptep = pte_offset_kernel(pmd, addr);

    return ptep;
}

/* Main function for relinquishing memory. */
static unsigned long inflate_balloon(unsigned long num_pages)
{
    unsigned long *parray;
    unsigned long *currp;
    unsigned long curraddr;
    unsigned long ret = 0;
    unsigned long i, j;

    parray = (unsigned long *)vmalloc(num_pages * sizeof(unsigned long));
    if ( parray == NULL )
    {
        printk(KERN_ERR "inflate_balloon: Unable to vmalloc parray\n");
        return -EFAULT;
    }

    currp = parray;

    for ( i = 0; i < num_pages; i++, currp++ )
    {
        struct page *page = alloc_page(GFP_HIGHUSER);
        unsigned long pfn = page - mem_map;

        /* If allocation fails then free all reserved pages. */
        if ( page == NULL )
        {
            printk(KERN_ERR "Unable to inflate balloon by %ld, only"
                   " %ld pages free.", num_pages, i);
            currp = parray;
            for ( j = 0; j < i; j++, currp++ )
                __free_page((struct page *) (mem_map + *currp));
            ret = -EFAULT;
            goto cleanup;
        }

        *currp = pfn;
    }


    for ( i = 0, currp = parray; i < num_pages; i++, currp++ )
    {
        unsigned long mfn = phys_to_machine_mapping[*currp];
        curraddr = (unsigned long)page_address(mem_map + *currp);
        /* Blow away page contents for security, and also p.t. ref if any. */
        if ( curraddr != 0 )
        {
            scrub_pages(curraddr, 1);
            queue_l1_entry_update(get_ptep(curraddr), 0);
        }
#ifdef CONFIG_XEN_SCRUB_PAGES
        else
        {
            void *p = kmap(&mem_map[*currp]);
            scrub_pages(p, 1);
            kunmap(&mem_map[*currp]);
        }
#endif
        phys_to_machine_mapping[*currp] = DEAD;
        *currp = mfn;
    }

    /* Flush updates through and flush the TLB. */
    xen_tlb_flush();

    ret = HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation, 
                                parray, num_pages, 0);
    if ( unlikely(ret != num_pages) )
    {
        printk(KERN_ERR "Unable to inflate balloon, error %lx\n", ret);
        goto cleanup;
    }

    credit += num_pages;
    ret = num_pages;

 cleanup:
    vfree(parray);

    return ret;
}

/*
 * Install new mem pages obtained by deflate_balloon. function walks 
 * phys->machine mapping table looking for DEAD entries and populates
 * them.
 */
static unsigned long process_returned_pages(unsigned long * parray, 
                                       unsigned long num)
{
    /* currently, this function is rather simplistic as 
     * it is assumed that domain reclaims only number of 
     * pages previously released. this is to change soon
     * and the code to extend page tables etc. will be 
     * incorporated here.
     */
     
    unsigned long tot_pages = most_seen_pages;   
    unsigned long * curr = parray;
    unsigned long num_installed;
    unsigned long i;

    num_installed = 0;
    for ( i = 0; (i < tot_pages) && (num_installed < num); i++ )
    {
        if ( phys_to_machine_mapping[i] == DEAD )
        {
            phys_to_machine_mapping[i] = *curr;
            queue_machphys_update(*curr, i);
            if (i<max_low_pfn)
              queue_l1_entry_update(
                get_ptep((unsigned long)__va(i << PAGE_SHIFT)),
                ((*curr) << PAGE_SHIFT) | pgprot_val(PAGE_KERNEL));

            __free_page(mem_map + i);

            curr++;
            num_installed++;
        }
    }

    return num_installed;
}

unsigned long deflate_balloon(unsigned long num_pages)
{
    unsigned long ret;
    unsigned long * parray;

    if ( num_pages > credit )
    {
        printk(KERN_ERR "deflate_balloon: %lu pages > %lu credit.\n",
               num_pages, credit);
        return -EAGAIN;
    }

    parray = (unsigned long *)vmalloc(num_pages * sizeof(unsigned long));
    if ( parray == NULL )
    {
        printk(KERN_ERR "deflate_balloon: Unable to vmalloc parray\n");
        return 0;
    }

    ret = HYPERVISOR_dom_mem_op(MEMOP_increase_reservation, 
                                parray, num_pages, 0);
    if ( unlikely(ret != num_pages) )
    {
        printk(KERN_ERR "deflate_balloon: xen increase_reservation err %lx\n",
               ret);
        goto cleanup;
    }

    if ( (ret = process_returned_pages(parray, num_pages)) < num_pages )
    {
        printk(KERN_WARNING
               "deflate_balloon: restored only %lx of %lx pages.\n",
           ret, num_pages);
        goto cleanup;
    }

    ret = num_pages;
    credit -= num_pages;

 cleanup:
    vfree(parray);

    return ret;
}

#define PAGE_TO_MB_SHIFT 8

/*
 * pagetable_extend() mimics pagetable_init() from arch/xen/mm/init.c 
 * The loops do go through all of low memory (ZONE_NORMAL).  The
 * old pages have _PAGE_PRESENT set and so get skipped.
 * If low memory is not full, the new pages are used to fill it, going
 * from cur_low_pfn to low_pfn.   high memory is not direct mapped so
 * no extension is needed for new high memory.
 */

static void pagetable_extend (int cur_low_pfn, int newpages)
{
    unsigned long vaddr, end;
    pgd_t *kpgd, *pgd, *pgd_base;
    int i, j, k;
    pmd_t *kpmd, *pmd;
    pte_t *kpte, *pte, *pte_base;
    int low_pfn = min(cur_low_pfn+newpages,(int)max_low_pfn);

    /*
     * This can be zero as well - no problem, in that case we exit
     * the loops anyway due to the PTRS_PER_* conditions.
     */
    end = (unsigned long)__va(low_pfn*PAGE_SIZE);

    pgd_base = init_mm.pgd;
    i = pgd_index(PAGE_OFFSET);
    pgd = pgd_base + i;

    for (; i < PTRS_PER_PGD; pgd++, i++) {
        vaddr = i*PGDIR_SIZE;
        if (end && (vaddr >= end))
            break;
        pmd = (pmd_t *)pgd;
        for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
            vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
            if (end && (vaddr >= end))
                break;

            /* Filled in for us already? */
            if ( pmd_val(*pmd) & _PAGE_PRESENT )
                continue;

            pte_base = pte = (pte_t *) __get_free_page(GFP_KERNEL);

            for (k = 0; k < PTRS_PER_PTE; pte++, k++) {
                vaddr = i*PGDIR_SIZE + j*PMD_SIZE + k*PAGE_SIZE;
                if (end && (vaddr >= end))
                    break;
                *pte = mk_pte(virt_to_page(vaddr), PAGE_KERNEL);
            }
            kpgd = pgd_offset_k((unsigned long)pte_base);
            kpmd = pmd_offset(kpgd, (unsigned long)pte_base);
            kpte = pte_offset_kernel(kpmd, (unsigned long)pte_base);
            queue_l1_entry_update(kpte,
                                  (*(unsigned long *)kpte)&~_PAGE_RW);
            set_pmd(pmd, __pmd(_KERNPG_TABLE + __pa(pte_base)));
            XEN_flush_page_update_queue();
        }
    }
}

/*
 * claim_new_pages() asks xen to increase this domain's memory  reservation
 * and return a list of the new pages of memory.  This new pages are
 * added to the free list of the memory manager.
 *
 * Available RAM does not normally change while Linux runs.  To make this work,
 * the linux mem= boottime command line param must say how big memory could
 * possibly grow.  Then setup_arch() in arch/xen/kernel/setup.c
 * sets max_pfn, max_low_pfn and the zones according to
 * this max memory size.   The page tables themselves can only be
 * extended after xen has assigned new pages to this domain.
 */

static unsigned long
claim_new_pages(unsigned long num_pages)
{
    unsigned long new_page_cnt, pfn;
    unsigned long * parray, *curr;

    if (most_seen_pages+num_pages> max_pfn)
        num_pages = max_pfn-most_seen_pages;
    if (num_pages==0) return -EINVAL;

    parray = (unsigned long *)vmalloc(num_pages * sizeof(unsigned long));
    if ( parray == NULL )
    {
        printk(KERN_ERR "claim_new_pages: Unable to vmalloc parray\n");
        return 0;
    }

    new_page_cnt = HYPERVISOR_dom_mem_op(MEMOP_increase_reservation, 
                                parray, num_pages, 0);
    if ( new_page_cnt != num_pages )
    {
        printk(KERN_WARNING
            "claim_new_pages: xen granted only %lu of %lu requested pages\n",
            new_page_cnt, num_pages);

        /* 
         * Avoid xen lockup when user forgot to setdomainmaxmem. Xen
         * usually can dribble out a few pages and then hangs.
         */
        if ( new_page_cnt < 1000 )
        {
            printk(KERN_WARNING "Remember to use setdomainmaxmem\n");
            HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation, 
                                parray, new_page_cnt, 0);
            return -EFAULT;
        }
    }
    memcpy(phys_to_machine_mapping+most_seen_pages, parray,
           new_page_cnt * sizeof(unsigned long));

    pagetable_extend(most_seen_pages,new_page_cnt);

    for ( pfn = most_seen_pages, curr = parray;
          pfn < most_seen_pages+new_page_cnt;
          pfn++, curr++ )
    {
        struct page *page = mem_map + pfn;

#ifndef CONFIG_HIGHMEM
        if ( pfn>=max_low_pfn )
        {
            printk(KERN_WARNING "Warning only %ldMB will be used.\n",
               pfn>>PAGE_TO_MB_SHIFT);
            printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
            break;
        }
#endif
        queue_machphys_update(*curr, pfn);
        if ( pfn < max_low_pfn )
            queue_l1_entry_update(
                get_ptep((unsigned long)__va(pfn << PAGE_SHIFT)),
                ((*curr) << PAGE_SHIFT) | pgprot_val(PAGE_KERNEL));
        
        XEN_flush_page_update_queue();
        
        /* this next bit mimics arch/xen/mm/init.c:one_highpage_init() */
        ClearPageReserved(page);
        if ( pfn >= max_low_pfn )
            set_bit(PG_highmem, &page->flags);
        set_page_count(page, 1);
        __free_page(page);
    }

    vfree(parray);

    return new_page_cnt;
}


static int balloon_try_target(int target)
{
    int change, reclaim;

    if ( target < current_pages )
    {
        int change = inflate_balloon(current_pages-target);
        if ( change <= 0 )
            return change;

        current_pages -= change;
        printk(KERN_INFO "Relinquish %dMB to xen. Domain now has %luMB\n",
            change>>PAGE_TO_MB_SHIFT, current_pages>>PAGE_TO_MB_SHIFT);
    }
    else if ( target > current_pages )
    {
        reclaim = min((unsigned long)target,most_seen_pages) - current_pages;

        if ( reclaim )
        {
            change = deflate_balloon( reclaim );
            if ( change <= 0 )
                return change;
            current_pages += change;
            printk(KERN_INFO "Reclaim %dMB from xen. Domain now has %luMB\n",
                change>>PAGE_TO_MB_SHIFT, current_pages>>PAGE_TO_MB_SHIFT);
        }

        if ( most_seen_pages < target )
        {
            int growth = claim_new_pages(target-most_seen_pages);
            if ( growth <= 0 )
                return growth;
            most_seen_pages += growth;
            current_pages += growth;
            printk(KERN_INFO "Granted %dMB new mem. Dom now has %luMB\n",
                growth>>PAGE_TO_MB_SHIFT, current_pages>>PAGE_TO_MB_SHIFT);
        }
    }

    return 1;
}


static void balloon_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
{
    switch ( msg->subtype )
    {
    case CMSG_MEM_REQUEST_SET:
        if ( msg->length != sizeof(mem_request_t) )
            goto parse_error;
        {
            mem_request_t *req = (mem_request_t *)&msg->msg[0];
            req->status = balloon_try_target(req->target);
        }
        break;        
    default:
        goto parse_error;
    }

    ctrl_if_send_response(msg);
    return;

 parse_error:
    msg->length = 0;
    ctrl_if_send_response(msg);
}


static int balloon_write(struct file *file, const char *buffer,
                         size_t count, loff_t *offp)
{
    char memstring[64], *endchar;
    int len, i;
    unsigned long target;
    unsigned long long targetbytes;

    /* Only admin can play with the balloon :) */
    if ( !capable(CAP_SYS_ADMIN) )
        return -EPERM;

    if ( count > sizeof(memstring) )
        return -EFBIG;

    len = strnlen_user(buffer, count);
    if ( len == 0 ) return -EBADMSG;
    if ( len == 1 ) return 1; /* input starts with a NUL char */
    if ( strncpy_from_user(memstring, buffer, len) < 0 )
        return -EFAULT;

    endchar = memstring;
    for ( i = 0; i < len; ++i, ++endchar )
        if ( (memstring[i] < '0') || (memstring[i] > '9') )
            break;
    if ( i == 0 )
        return -EBADMSG;

    targetbytes = memparse(memstring,&endchar);
    target = targetbytes >> PAGE_SHIFT;

    i = balloon_try_target(target);

    if ( i <= 0 ) return i;

    *offp += len;
    return len;
}


static int balloon_read(struct file *filp, char *buffer,
                        size_t count, loff_t *offp)
{
    static char priv_buf[32];
    char *priv_bufp = priv_buf;
    int len;
    len = sprintf(priv_buf,"%lu\n",current_pages<<PAGE_SHIFT);

    len -= *offp;
    priv_bufp += *offp;
    if (len>count) len = count;
    if (len<0) len = 0;

    copy_to_user(buffer, priv_bufp, len);

    *offp += len;
    return len;
}

static struct file_operations balloon_fops = {
    .read  = balloon_read,
    .write = balloon_write
};

static int __init balloon_init(void)
{
    printk(KERN_ALERT "Starting Xen Balloon driver\n");

    most_seen_pages = current_pages = min(xen_start_info.nr_pages,max_pfn);
    if ( (balloon_pde = create_xen_proc_entry("memory_target", 0644)) == NULL )
    {
        printk(KERN_ALERT "Unable to create balloon driver proc entry!");
        return -1;
    }

    balloon_pde->owner     = THIS_MODULE;
    balloon_pde->nlink     = 1;
    balloon_pde->proc_fops = &balloon_fops;

    (void)ctrl_if_register_receiver(CMSG_MEM_REQUEST, balloon_ctrlif_rx,
                                    CALLBACK_IN_BLOCKING_CONTEXT);

    /* 
     * make_module a new phys map if mem= says xen can give us memory  to grow
     */
    if ( max_pfn > xen_start_info.nr_pages )
    {
        extern unsigned long *phys_to_machine_mapping;
        unsigned long *newmap;
        newmap = (unsigned long *)vmalloc(max_pfn * sizeof(unsigned long));
        memset(newmap, ~0, max_pfn * sizeof(unsigned long));
        memcpy(newmap, phys_to_machine_mapping,
               xen_start_info.nr_pages * sizeof(unsigned long));
        phys_to_machine_mapping = newmap;
    }

    return 0;
}

static void __exit balloon_cleanup(void)
{
    if ( balloon_pde != NULL )
    {
        remove_xen_proc_entry("memory_target");
        balloon_pde = NULL;
    }
}

module_init(balloon_init);
module_exit(balloon_cleanup);