WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH]: Fix mem= kernel parameter

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH]: Fix mem= kernel parameter
From: Chris Lalancette <clalance@xxxxxxxxxx>
Date: Tue, 22 May 2007 10:25:58 -0400
Delivery-date: Tue, 22 May 2007 07:24:20 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: Mozilla Thunderbird 1.0.8-1.1.fc4 (X11/20060501)
All,
     Attached is a patch to fix the mem= parameter to a Xenified kernel, i.e.
/boot/grub/grub.conf:

title RHEL5 i386 Xen (2.6.18-8.1.3.el5xen)
        root (hd0,8)
        kernel /boot/xen.gz-2.6.18-8.1.3.el5 com1=115200,8n1
        module /boot/vmlinuz-2.6.18-8.1.3.el5xen ro root=LABEL=RHEL5_i386 mem=1G
        module /boot/initrd-2.6.18-8.1.3.el5xen.img

While this isn't a common thing to do, it would be nice if this "just worked"
the same way it does on bare-metal, without crashing (which is what it currently
does).  The problem comes down to this piece of code in
arch/[i386,x86_64]/kernel/setup-xen.c:

        /* Make sure we have a correctly sized P->M table. */
        if (!xen_feature(XENFEAT_auto_translated_physmap)) {
                phys_to_machine_mapping = alloc_bootmem_low_pages(
                     max_pfn * sizeof(unsigned long));
                memset(phys_to_machine_mapping, ~0,
                       max_pfn * sizeof(unsigned long));
                memcpy(phys_to_machine_mapping,
                       (unsigned long *)xen_start_info->mfn_list,
                       xen_start_info->nr_pages * sizeof(unsigned long));
                free_bootmem(
                     __pa(xen_start_info->mfn_list),
                     PFN_PHYS(PFN_UP(xen_start_info->nr_pages *
                                     sizeof(unsigned long))));

There are really 3 cases that need to be considered:
1) max_pfn == xen_start_info->nr_pages - this is the normal case, when no kernel
or HV parameters are passed when dom0 boots
2)  max_pfn > xen_start_info->nr_pages - this is the case when a domU is
started, and maxmem > memory in the configuration file
3)  max_pfn < xen_start_info->nr_pages - this is the case when specifying mem=
(or highmem= on i386) on the dom0 or domU command line.

The third case is the one that is currently broken.   Looking at the code above,
you can see that the array allocated for the p2m table is too small in that
case, and during the subsequent memcpy() can cause a page fault and then an
OOPs.  The attached patch handles the third case by decreasing the reservation,
and only copying the appropriate number of entries into the p2m table.

The patch is against a 3.0.3-based 2.6.18 kernel, but should apply with
relatively little problem on current unstable.

Comments/question welcome.

Chris Lalancette
--- linux-2.6.18.noarch/arch/i386/kernel/setup-xen.c.orig
+++ linux-2.6.18.noarch/arch/i386/kernel/setup-xen.c
@@ -1553,6 +1553,7 @@ void __init setup_arch(char **cmdline_p)
        int i, j, k, fpp;
        struct physdev_set_iopl set_iopl;
        unsigned long max_low_pfn;
+       unsigned long p2m_pages;
 
        /* Force a quick death if the kernel panics (not domain 0). */
        extern int panic_timeout;
@@ -1693,6 +1694,32 @@ void __init setup_arch(char **cmdline_p)
        find_smp_config();
 #endif
 
+       p2m_pages = max_pfn;
+       if (xen_start_info->nr_pages > max_pfn) {
+               /*
+                * the max_pfn was shrunk (probably by mem= or highmem=
+                * kernel parameter); shrink reservation with the HV
+                */
+               struct xen_memory_reservation reservation = {
+                       .address_bits = 0,
+                       .extent_order = 0,
+                       .domid = DOMID_SELF
+               };
+               unsigned int difference;
+               int ret;
+
+               difference = xen_start_info->nr_pages - max_pfn;
+
+               set_xen_guest_handle(reservation.extent_start,
+                                    ((unsigned long 
*)xen_start_info->mfn_list) + max_pfn);
+               reservation.nr_extents = difference;
+               ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
+                                          &reservation);
+               BUG_ON (ret != difference);
+       }
+       else if (max_pfn > xen_start_info->nr_pages)
+               p2m_pages = xen_start_info->nr_pages;
+
        /* Make sure we have a correctly sized P->M table. */
        if (!xen_feature(XENFEAT_auto_translated_physmap)) {
                phys_to_machine_mapping = alloc_bootmem_low_pages(
@@ -1701,7 +1728,7 @@ void __init setup_arch(char **cmdline_p)
                       max_pfn * sizeof(unsigned long));
                memcpy(phys_to_machine_mapping,
                       (unsigned long *)xen_start_info->mfn_list,
-                      xen_start_info->nr_pages * sizeof(unsigned long));
+                      p2m_pages * sizeof(unsigned long));
                free_bootmem(
                     __pa(xen_start_info->mfn_list),
                     PFN_PHYS(PFN_UP(xen_start_info->nr_pages *
--- linux-2.6.18.noarch/arch/x86_64/kernel/setup-xen.c.orig
+++ linux-2.6.18.noarch/arch/x86_64/kernel/setup-xen.c
@@ -782,6 +782,33 @@ void __init setup_arch(char **cmdline_p)
 #ifdef CONFIG_XEN
        {
                int i, j, k, fpp;
+               unsigned long p2m_pages;
+
+               p2m_pages = end_pfn;
+               if (xen_start_info->nr_pages > end_pfn) {
+                       /*
+                        * the end_pfn was shrunk (probably by mem= or highmem=
+                        * kernel parameter); shrink reservation with the HV
+                        */
+                       struct xen_memory_reservation reservation = {
+                               .address_bits = 0,
+                               .extent_order = 0,
+                               .domid = DOMID_SELF
+                       };
+                       unsigned int difference;
+                       int ret;
+                       
+                       difference = xen_start_info->nr_pages - end_pfn;
+                       
+                       set_xen_guest_handle(reservation.extent_start,
+                                            ((unsigned long 
*)xen_start_info->mfn_list) + end_pfn);
+                       reservation.nr_extents = difference;
+                       ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
+                                                  &reservation);
+                       BUG_ON (ret != difference);
+               }
+               else if (end_pfn > xen_start_info->nr_pages)
+                       p2m_pages = xen_start_info->nr_pages;
 
                if (!xen_feature(XENFEAT_auto_translated_physmap)) {
                        /* Make sure we have a large enough P->M table. */
@@ -791,7 +818,7 @@ void __init setup_arch(char **cmdline_p)
                               end_pfn * sizeof(unsigned long));
                        memcpy(phys_to_machine_mapping,
                               (unsigned long *)xen_start_info->mfn_list,
-                              xen_start_info->nr_pages * sizeof(unsigned 
long));
+                              p2m_pages * sizeof(unsigned long));
                        free_bootmem(
                                __pa(xen_start_info->mfn_list),
                                PFN_PHYS(PFN_UP(xen_start_info->nr_pages *
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
<Prev in Thread] Current Thread [Next in Thread>