Skip to contentSkip to conversation

Allow bitmasks with the badpage bootoption

PATCH In review Erik Bosman 2 participants 5 messages Apr 4, 2006 – Apr 5, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow bitmasks with the badpage bootoption

Hi,

The current "badpage" boot option only allows you to select individual pages, which is not an option for me, since I have close to 32M of bad ram. My patch allows the use of masks to select many pages at once, much like the linux BADRAM patch.

Masks can be specified using a forward slash, like, for example:
badpage=0xa2000/0xef000,0x00100/0xfff00,0x01000

This way, it's compatible with the current scheme.

I also wrote a small testprogram to test the algorithm.

Regards,
Erik Bosman

xen/common/page_alloc.c +24 -4
diff -ru xen-3.0.1-orig/xen/common/page_alloc.c xen-3.0.1/xen/common/page_alloc.c
@@ -155,7 +155,7 @@
 
 void init_boot_pages(physaddr_t ps, physaddr_t pe)
 {
-    unsigned long bad_pfn;
+    unsigned long bad_pfn, bad_mask, i;
     char *p;
 
     ps = round_pgup(ps);
@@ -170,16 +170,36 @@
     while ( *p != '\0' )
     {
         bad_pfn = simple_strtoul(p, &p, 0);
+        bad_mask = ~0; /* mask specific to one page */
+
+        if ( *p == '/' )
+        {
+            p++;
+            bad_mask = simple_strtoul(p, &p, 0);
+            bad_pfn &= bad_mask; /* ignore address bits not covered by the mask */
+        }
 
         if ( *p == ',' )
             p++;
         else if ( *p != '\0' )
             break;
 
-        if ( (bad_pfn < max_page) && !allocated_in_map(bad_pfn) )
-        {
+        if ( bad_mask == ~0 )
             printk("Marking page %lx as bad\n", bad_pfn);
-            map_alloc(bad_pfn, 1);
+        else
+            printk("Marking pages %lx/%lx as bad\n",
+                   bad_pfn, bad_mask);
+
+        /* iterate over pages covered by the mask */
+        for ( i=0; (bad_pfn|i) < max_page ; i++ )
+        {
+            if ( !(i & bad_mask) )
+            {
+                if ( !allocated_in_map(bad_pfn|i) )
+                    map_alloc(bad_pfn|i, 1);
+                else
+                    printk("Warning, bad page %lx in use\n", bad_pfn|i);
+            }
         }
     }
 }

/*
* Small testprogram to test the page masking algoritm */

#include <assert.h>

#include <stdio.h>
#include <string.h>

#define N_PAGES 0x1000000
#define printk printf

/* test-page allocation table */
char pages0[(N_PAGES>>3) + 1];
char pages[(N_PAGES>>3) + 1];
unsigned long memsize;

void map_alloc(unsigned long page_nr, int _)

{
	assert(!allocated_in_map(page_nr));
	assert(page_nr<memsize);
	pages[page_nr>>3] |= 1<<(page_nr&0x7);
}

int allocated_in_map(unsigned long page_nr)

{
	return pages[page_nr>>3] & (1<<(page_nr&0x7));
}

/* function to be tested */
void mask_iterate_simple(unsigned long bad_pfn, unsigned long bad_mask,
unsigned long max_page)

{
	unsigned long i;

	bad_pfn &= bad_mask;
/*--- BEGIN CODE FROM THE PATCH -------------------------------------*/
	for ( i=0; (bad_pfn|i) < max_page; i++)
	{
		if ( !(i&bad_mask) )
		{
			if ( !allocated_in_map(bad_pfn|i) )
				map_alloc(i|bad_pfn, 1);
			else
				printk("Warning, bad page %lx in use\n", bad_pfn|i);
		}
	}

/*--- END CODE FROM THE PATCH ---------------------------------------*/ }

/* alternative, faster, but more complex algorithm */
void mask_iterate_quick(unsigned long bad_pfn, unsigned long bad_mask,
unsigned long max_page)

{
	unsigned long i=0, step;

	bad_pfn &= bad_mask; /* bad_masked bits in the page address should be ignored */

	/* iterates over the bad_mask */
	while ( (bad_pfn|i) < max_page )
	{
		if ( !allocated_in_map(bad_pfn|i) )
			map_alloc(i|bad_pfn, 1);
		else
			printk("Warning, bad page %lx in use\n", bad_pfn|i);

		step = 1;

		if ( i == ~bad_mask ) /* break before integer overflow */
			break;

		/* the following lines are an inc() operation, skipping masked bits */
		while ( step & (bad_mask|i) )
			step <<= 1;

		i |= step;
		i &= ~(step-1);
	}
}

/* functions to compare against */
void range_iterate(unsigned long bad_psfn, unsigned long bad_pefn,
unsigned long max_page)

{
	unsigned long i;

	for ( i = bad_psfn; i <= bad_pefn; i++ )
		if ( (i < max_page) && !allocated_in_map(i) )
			map_alloc(i, 1);
}

/* tests and testroutines */
static struct

{
	unsigned long page_nr;
	unsigned long mask;
	unsigned long memsize;
} tests[] =
{
	{0x1c46, 0xff02, 0x10000},
	{0x1c46, 0xff02, N_PAGES},
	{0x2a000, 0xef000, 0x3c000}, /* my badram problem */
	{0x2a000, 0xef000, 0x40000},
	{0x00000, 0x00000, 0x00001},
	{0x10000, 0x11111, 0x00001},
	{0x10000, 0x11111, 0x11111},
	{0x11111, 0x11111, 0x11111},
	{0x11111, 0x00000, 0x11111},
	{0x10101, 0x01010, 0x11111},

/*
from random import randint

for i in range(200):

    print "\t{0x%x, 0x%x, 0x%x}," % tuple([ randint(0, 0xffffff) for i in range(3) ])

*/

	/* 190 lines of random data snipped */
	{0x160dba, 0xfd810a, 0xc325c},
	{0xcf2c46, 0xa485e7, 0x4803},
	{0x32b0d7, 0xcb17e, 0x873b6f},
	{0xbce68, 0xa3e2aa, 0x92a7a1},
	{0x7bb8ee, 0xab048, 0xa937a6},
	{0x443e64, 0xaf1a17, 0xdcac7c},
	{0xdc6f8e, 0x4f894f, 0xc5a1c8},
	{0x7f620, 0x22bf9f, 0xa83a56},
	{0x22173, 0xf04ffe, 0xa78071},
	{0x5ea795, 0xe303cf, 0x55b54b},
	{0,0,0}
};

static struct

{
	unsigned long page_nr;
	unsigned long mask;
	unsigned long memsize;
	unsigned long start_range;
	unsigned long end_range;
} range_tests[] =
{
	{0x2a000, 0xff000, 0x3c000, 0x2a000, 0x2afff},
	{0x3a000, 0xff000, 0x3c000, 0x3a000, 0x3afff},
	{0x00000, 0x00000, 0xfffff, 0x00000, 0xfffff},
	{0x00000, 0xf0000, 0xfffff, 0x00000, 0x0ffff},
	{0,0,0,0,0}
};

static struct

{
	void (*test)(unsigned long,unsigned long,unsigned long);
	char *name;
} routine[] =
{
	{mask_iterate_simple, "mask_iterate_simple"},
	{mask_iterate_quick, "mask_iterate_quick"},
	{0,0}
};

void test_compare(void)

{
	int i, j;

	for (i=0; tests[i].memsize; i++)
	{
		printf("TEST %d: ", i); fflush(stdout);
		memsize=tests[i].memsize;
		memset(pages, 0, (tests[i].memsize>>3)+1);

		routine[0].test(tests[i].page_nr, tests[i].mask, tests[i].memsize);
		memcpy(pages0, pages, (tests[i].memsize>>3)+1);
	
		for (j=1; routine[j].test; j++)
		{
			memset(pages, 0, (tests[i].memsize>>3)+1);
			routine[j].test(tests[i].page_nr, tests[i].mask, tests[i].memsize);

			printf(bcmp(pages0, pages, (tests[i].memsize>>3)+1)?"fail\n":"pass\n");
		}
	}
}

void test_against_range(void)

{
	int i, j;

	for (i=0; range_tests[i].memsize; i++)
	{
		printf("TEST %d: ", i); fflush(stdout);
		memsize=range_tests[i].memsize;
		memset(pages, 0, (range_tests[i].memsize>>3)+1);

		range_iterate(range_tests[i].start_range,range_tests[i].end_range,
		              range_tests[i].memsize);
		
		memcpy(pages0, pages, (range_tests[i].memsize>>3)+1);
	
		for (j=0; routine[j].test; j++)
		{
			memset(pages, 0, (range_tests[i].memsize>>3)+1);
			routine[j].test(range_tests[i].page_nr, range_tests[i].mask,
			                range_tests[i].memsize);

			printf(bcmp(pages0, pages, (range_tests[i].memsize>>3)+1)?
			                           "fail ":"pass ");
		}
		printf("\n");
	}
}

int main(int argc, char **argv)
{

	printf("---test against range----\n");
	test_against_range();
	printf("---test eachother----\n");
	test_compare();
}

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 8 lines · Erik Bosman

The current "badpage" boot option only allows you to select individual pages, which is not an option for me, since I have close to 32M of bad ram. My patch allows the use of masks to select many pages at once, much like the linux BADRAM patch.

Masks can be specified using a forward slash, like, for example:
badpage=0xa2000/0xef000,0x00100/0xfff00,0x01000

The masking scheme is unnecessarily restrictive. Why not just allow specification of inclusive ranges of pages? For example: badpage=0xa2000-0xa2fff,0x100-0x1ff,0x123-0x456

-- Keir

#2 lore raw
Quoted 7 lines · Keir Fraser

The masking scheme is unnecessarily restrictive. Why not just allow specification of inclusive ranges of pages? For example:
badpage=0xa2000-0xa2fff,0x100-0x1ff,0x123-0x456

-- Keir

This was how I first implemented it for my machine, since it only requires two ranges: badpage=0x2a000-0x2afff,0x3a000-0x3afff

But I thought a mask would be better since it resembles the way memory usually breaks (not that I have much experience with this.)

On the other hand, ranges are much easier to understand.

I've attached my original patch.

Erik Bosman

xen/common/page_alloc.c +20 -7
diff -ru xen-3.0.1-orig/xen/common/page_alloc.c xen-3.0.1-range/xen/common/page_alloc.c
@@ -155,7 +155,7 @@
 
 void init_boot_pages(physaddr_t ps, physaddr_t pe)
 {
-    unsigned long bad_pfn;
+    unsigned long bad_psfn, bad_pefn, i;
     char *p;
 
     ps = round_pgup(ps);
@@ -169,18 +169,31 @@
     p = opt_badpage;
     while ( *p != '\0' )
     {
-        bad_pfn = simple_strtoul(p, &p, 0);
+        bad_psfn = simple_strtoul(p, &p, 0);
+        bad_pefn = bad_psfn;
+
+        if ( *p == '-' )
+        {
+            p++;
+            bad_pefn = simple_strtoul(p, &p, 0);
+            if ( bad_pefn < bad_psfn )
+                bad_pefn = bad_psfn;
+        }
 
         if ( *p == ',' )
             p++;
         else if ( *p != '\0' )
             break;
 
-        if ( (bad_pfn < max_page) && !allocated_in_map(bad_pfn) )
-        {
-            printk("Marking page %lx as bad\n", bad_pfn);
-            map_alloc(bad_pfn, 1);
-        }
+        if (bad_pefn == bad_psfn)
+            printk("Marking page %lx as bad\n", bad_psfn);
+        else
+            printk("Marking pages %lx through %lx as bad\n",
+                   bad_psfn, bad_pefn);
+
+        for ( i = bad_psfn; i <= bad_pefn; i++ )
+            if ( (i < max_page) && !allocated_in_map(i) )
+                map_alloc(i, 1);
     }
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 9 lines · Erik Bosman

This was how I first implemented it for my machine, since it only requires two ranges: badpage=0x2a000-0x2afff,0x3a000-0x3afff

But I thought a mask would be better since it resembles the way memory usually breaks (not that I have much experience with this.)

On the other hand, ranges are much easier to understand.

I've attached my original patch.

Better, but:
1. Name the variables bad_spfn, bad_epfn
2. Format 'if (bad_epfn == bad_spfn)' as surrounding code (it's
missing spaces just inside the brackets).
3. Re-send with a 'Signed-off-by:' line in your email.

-- Keir

#4 lore raw
xen/common/page_alloc.c +20 -7
diff -ru xen-3.0.1-orig/xen/common/page_alloc.c xen-3.0.1-range/xen/common/page_alloc.c
@@ -155,7 +155,7 @@
 
 void init_boot_pages(physaddr_t ps, physaddr_t pe)
 {
-    unsigned long bad_pfn;
+    unsigned long bad_spfn, bad_epfn, i;
     char *p;
 
     ps = round_pgup(ps);
@@ -169,18 +169,31 @@
     p = opt_badpage;
     while ( *p != '\0' )
     {
-        bad_pfn = simple_strtoul(p, &p, 0);
+        bad_spfn = simple_strtoul(p, &p, 0);
+        bad_epfn = bad_spfn;
+
+        if ( *p == '-' )
+        {
+            p++;
+            bad_epfn = simple_strtoul(p, &p, 0);
+            if ( bad_epfn < bad_spfn )
+                bad_epfn = bad_spfn;
+        }
 
         if ( *p == ',' )
             p++;
         else if ( *p != '\0' )
             break;
 
-        if ( (bad_pfn < max_page) && !allocated_in_map(bad_pfn) )
-        {
-            printk("Marking page %lx as bad\n", bad_pfn);
-            map_alloc(bad_pfn, 1);
-        }
+        if ( bad_epfn == bad_spfn )
+            printk("Marking page %lx as bad\n", bad_spfn);
+        else
+            printk("Marking pages %lx through %lx as bad\n",
+                   bad_spfn, bad_epfn);
+
+        for ( i = bad_spfn; i <= bad_epfn; i++ )
+            if ( (i < max_page) && !allocated_in_map(i) )
+                map_alloc(i, 1);
     }
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel