]> xenbits.xensource.com Git - people/vhanquez/xen.git/commitdiff
xen: fix grant table bug
authorKeir Fraser <keir.fraser@citrix.com>
Mon, 31 Mar 2008 15:45:04 +0000 (16:45 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Mon, 31 Mar 2008 15:45:04 +0000 (16:45 +0100)
A PV OS has two grant table data structures: the grant table itself
and a free list.  The free list is composed of an array of pages,
which grow dynamically as the guest OS requires more grants.  While
the grant table contains 8-byte entries, the free list contains 4-byte
entries.  So we have half as many pages in the free list than in the
grant table.

There was a bug in the free list allocation code. The free list was
indexed as if it was the same size as the grant table.  But it's only
half as large.  So memory got corrupted, and I was seeing crashes in
the slab allocator later on.

Signed-off-by: Michael Abd-El-Malek <mabdelmalek@cmu.edu>
linux-2.6.18-xen changeset:   502:4018c0da336008e5dfb1163bddbdfbc328c8f5c4
linux-2.6.18-xen date:        Mon Mar 31 11:03:07 2008 +0100

linux-2.6-xen-sparse/drivers/xen/core/gnttab.c

index c3b39f9aeb30362d061b3e5fde32b276b2811323..71d9714e48ea4cac1e4f6a6fc7ce2c58c80fdc8f 100644 (file)
@@ -50,7 +50,7 @@
 /* External tools reserve first few grant table entries. */
 #define NR_RESERVED_ENTRIES 8
 #define GNTTAB_LIST_END 0xffffffff
-#define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(grant_entry_t))
+#define ENTRIES_PER_GRANT_FRAME (PAGE_SIZE / sizeof(grant_entry_t))
 
 static grant_ref_t **gnttab_list;
 static unsigned int nr_grant_frames;
@@ -68,6 +68,9 @@ static int gnttab_expand(unsigned int req_entries);
 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
 #define gnttab_entry(entry) (gnttab_list[(entry) / RPP][(entry) % RPP])
 
+#define nr_freelist_frames(grant_frames)                               \
+       (((grant_frames) * ENTRIES_PER_GRANT_FRAME + RPP - 1) / RPP)
+
 static int get_free_entries(int count)
 {
        unsigned long flags;
@@ -369,24 +372,25 @@ EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
 static int grow_gnttab_list(unsigned int more_frames)
 {
        unsigned int new_nr_grant_frames, extra_entries, i;
+       unsigned int nr_glist_frames, new_nr_glist_frames;
 
        new_nr_grant_frames = nr_grant_frames + more_frames;
-       extra_entries       = more_frames * GREFS_PER_GRANT_FRAME;
+       extra_entries       = more_frames * ENTRIES_PER_GRANT_FRAME;
 
-       for (i = nr_grant_frames; i < new_nr_grant_frames; i++)
-       {
+       nr_glist_frames = nr_freelist_frames(nr_grant_frames);
+       new_nr_glist_frames = nr_freelist_frames(new_nr_grant_frames);
+       for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
                gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
                if (!gnttab_list[i])
                        goto grow_nomem;
        }
 
-
-       for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
-            i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
+       for (i = ENTRIES_PER_GRANT_FRAME * nr_grant_frames;
+            i < ENTRIES_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
                gnttab_entry(i) = i + 1;
 
        gnttab_entry(i) = gnttab_free_head;
-       gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
+       gnttab_free_head = ENTRIES_PER_GRANT_FRAME * nr_grant_frames;
        gnttab_free_count += extra_entries;
 
        nr_grant_frames = new_nr_grant_frames;
@@ -396,7 +400,7 @@ static int grow_gnttab_list(unsigned int more_frames)
        return 0;
        
 grow_nomem:
-       for ( ; i >= nr_grant_frames; i--)
+       for ( ; i >= nr_glist_frames; i--)
                free_page((unsigned long) gnttab_list[i]);
        return -ENOMEM;
 }
@@ -564,8 +568,8 @@ static int gnttab_expand(unsigned int req_entries)
        unsigned int cur, extra;
 
        cur = nr_grant_frames;
-       extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
-                GREFS_PER_GRANT_FRAME);
+       extra = ((req_entries + (ENTRIES_PER_GRANT_FRAME-1)) /
+                ENTRIES_PER_GRANT_FRAME);
        if (cur + extra > max_nr_grant_frames())
                return -ENOSPC;
 
@@ -578,7 +582,7 @@ static int gnttab_expand(unsigned int req_entries)
 int __devinit gnttab_init(void)
 {
        int i;
-       unsigned int max_nr_glist_frames;
+       unsigned int max_nr_glist_frames, nr_glist_frames;
        unsigned int nr_init_grefs;
 
        if (!is_running_on_xen())
@@ -590,16 +594,15 @@ int __devinit gnttab_init(void)
        /* Determine the maximum number of frames required for the
         * grant reference free list on the current hypervisor.
         */
-       max_nr_glist_frames = (boot_max_nr_grant_frames *
-                              GREFS_PER_GRANT_FRAME /
-                              (PAGE_SIZE / sizeof(grant_ref_t)));
+       max_nr_glist_frames = nr_freelist_frames(boot_max_nr_grant_frames);
 
        gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
                              GFP_KERNEL);
        if (gnttab_list == NULL)
                return -ENOMEM;
 
-       for (i = 0; i < nr_grant_frames; i++) {
+       nr_glist_frames = nr_freelist_frames(nr_grant_frames);
+       for (i = 0; i < nr_glist_frames; i++) {
                gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
                if (gnttab_list[i] == NULL)
                        goto ini_nomem;
@@ -608,7 +611,7 @@ int __devinit gnttab_init(void)
        if (gnttab_resume() < 0)
                return -ENODEV;
 
-       nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
+       nr_init_grefs = nr_grant_frames * ENTRIES_PER_GRANT_FRAME;
 
        for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
                gnttab_entry(i) = i + 1;