x86: fix MTRR code
PATCH 1/1 x86: fix MTRR code
While after a previous submission (about one and a half year ago) I got told that the MTRR code is to be replaced by PAT use, this hasn't happened. Hence I'd like to ask to reconsider this patch until PAT handling code shows up.
Until not so long ago, there were system log messages pointing to inconsistent MTRR setup of the video frame buffer caused by the way vesafb and X worked. While vesafb was fixed meanwhile, I believe fixing it there only hides a shortcoming in the MTRR code itself, in that that code is not symmetric with respect to the ordering of attempts to set up two (or more) regions where one contains the other. In the current shape, it permits only setting up sub-regions of pre-exisiting ones. The patch below makes this symmetric.
While working on that I noticed a few more inconsistencies in that code, namely
- use of 'unsigned int' for sizes in many, but not all places (the patch is converting this to use 'unsigned long' everywhere, which specifically might be necessary for x86-64 once a processor supporting more than 44 physical address bits would become available)
- the code to correct inconsistent settings during secondary processor startup tried (if necessary) to correct, among other things, the value in IA32_MTRR_DEF_TYPE, however the newly computed value would never get used (i.e. stored in the respective MSR)
- the generic range validation code checked that the end of the to-be-added range would be above 1MB; the value checked should have been the start of the range
- when contained regions are detected, previously this was allowed only when the old region was uncacheable; this can be symmetric (i.e. the new region can also be uncacheable) and even further as per Intel's documentation write-trough and write-back for either region is also compatible with the respective opposite in the other
arch/i386/kernel/cpu/mtrr/amd.c +1 -1
@@ -7,7 +7,7 @@static void amd_get_mtrr (unsigned int reg,unsigned long *base, -unsigned int *size,mtrr_type * type) +unsigned long *size,mtrr_type * type) {unsigned long low, high;
arch/i386/kernel/cpu/mtrr/centaur.c +5 -4
@@ -17,7 +17,7 @@ static u8 centaur_mcr_type; /* 0 for win*/ static int -centaur_get_free_region (unsigned long base,unsigned long size) +centaur_get_free_region (unsigned long base,unsigned long size,int replace_reg)/* [SUMMARY] Get a free MTRR. <base> The starting (base) address of the region. <size> The size (in bytes) of the region.
@@ -26,10 +26,11 @@ centaur_get_free_region(unsigned long ba {int i, max;mtrr_type ltype; -unsigned long lbase; -unsigned int lsize; +unsigned long lbase, lsize; max = num_var_ranges; +if (replace_reg >=0 && replace_reg < max) +return replace_reg;for (i =0 ; i < max; ++i) {if (centaur_mcr_reserved & (1 << i))continue ;
@@ -49,7 +50,7 @@ mtrr_centaur_report_mcr(int mcr, u32 lo,static void centaur_get_mcr (unsigned int reg,unsigned long *base, -unsigned int *size,mtrr_type * type) +unsigned long *size,mtrr_type * type) { *base = centaur_mcr[reg].high >>PAGE_SHIFT ; *size = -(centaur_mcr[reg].low &0xfffff000 ) >>PAGE_SHIFT ;
arch/i386/kernel/cpu/mtrr/cyrix.c +20 -5
@@ -9,7 +9,7 @@ int arr3_protected;static void cyrix_get_arr (unsigned int reg,unsigned long *base, -unsigned int *size,mtrr_type * type) +unsigned long *size,mtrr_type * type) {unsigned long flags;unsigned char arr, ccr3, rcr, shift;
@@ -77,7 +77,7 @@ cyrix_get_arr(unsigned int reg, unsigned }static int -cyrix_get_free_region (unsigned long base,unsigned long size) +cyrix_get_free_region (unsigned long base,unsigned long size,int replace_reg)/* [SUMMARY] Get a free ARR. <base> The starting (base) address of the region. <size> The size (in bytes) of the region.
@@ -86,9 +86,24 @@ cyrix_get_free_region(unsigned long base {int i;mtrr_type ltype; -unsigned long lbase; -unsigned int lsize; +unsigned long lbase, lsize; +switch (replace_reg) { +case 7 : +if (size <0x40 ) +break ; +case 6 : +case 5 : +case 4 : +return replace_reg; +case 3 : +if (arr3_protected) +break ; +case 2 : +case 1 : +case 0 : +return replace_reg; + }/* If we are to set up a region >32M then look at ARR7 immediately */ if (size >0x2000 ) {cyrix_get_arr (7 , &lbase, &lsize, <ype);
@@ -214,7 +229,7 @@ static void cyrix_set_arr(unsigned int rtypedef struct {unsigned long base; -unsigned int size; +unsigned long size;mtrr_type type; }arr_state_t ;
arch/i386/kernel/cpu/mtrr/generic.c +66 -12
176 lines not on this page · raw message
arch/i386/kernel/cpu/mtrr/if.c +17 -11
@@ -17,7 +17,7 @@ extern unsigned int *usage_table;#define FILE_FCOUNT (f) (((struct seq_file *)((f)->private_data))->private) -static char *mtrr_strings[MTRR_NUM_TYPES ] = +static const char *const mtrr_strings[MTRR_NUM_TYPES ] = {"uncachable" ,/* 0 */ "write-combining" ,/* 1 */
@@ -28,7 +28,7 @@ static char *mtrr_strings[MTRR_NUM_TYPES "write-back", /* 6 */ }; -char *mtrr_attrib_to_str (int x) +const char *mtrr_attrib_to_str (int x) {return (x <=6 ) ? mtrr_strings[x] :"?" ; }
@@ -155,6 +155,7 @@ mtrr_ioctl(struct file *file, unsigned i {int err =0 ;mtrr_type type; +unsigned long size;struct mtrr_sentry sentry;struct mtrr_gentry gentry;void __user *arg = (void __user *) __arg;
@@ -235,15 +236,15 @@ mtrr_ioctl(struct file *file, unsigned icase MTRRIOC_GET_ENTRY :if (gentry.regnum >= num_var_ranges)return -EINVAL ; - mtrr_if->get (gentry.regnum, &gentry.base, &gentry.size, &type); + mtrr_if->get (gentry.regnum, &gentry.base, &size, &type);/* Hide entries that go above 4GB */ -if (gentry.base + gentry.size >0x100000 - || gentry.size ==0x100000 ) +if (gentry.base + size -1 >= (1UL << (8 *sizeof (gentry.size) -PAGE_SHIFT )) + || size >= (1UL << (8 *sizeof (gentry.size) -PAGE_SHIFT ))) gentry.base = gentry.size = gentry.type =0 ;else { gentry.base <<=PAGE_SHIFT ; - gentry.size <<=PAGE_SHIFT ; + gentry.size = size <<PAGE_SHIFT ; gentry.type = type; }
@@ -273,8 +274,14 @@ mtrr_ioctl(struct file *file, unsigned icase MTRRIOC_GET_PAGE_ENTRY :if (gentry.regnum >= num_var_ranges)return -EINVAL ; - mtrr_if->get (gentry.regnum, &gentry.base, &gentry.size, &type); - gentry.type = type; + mtrr_if->get (gentry.regnum, &gentry.base, &size, &type); +/* Hide entries that would overflow */ + if (size != (__typeof__(gentry.size))size) + gentry.base = gentry.size = gentry.type =0 ; +else { + gentry.size = size; + gentry.type = type; + }break ; }
@@ -353,8 +360,7 @@ static int mtrr_seq_show(struct seq_filechar factor;int i, max, len;mtrr_type type; -unsigned long base; -unsigned int size; +unsigned long base, size; len =0 ; max = num_var_ranges;
@@ -373,7 +379,7 @@ static int mtrr_seq_show(struct seq_file }/* RED-PEN: base can be > 32bit */ len +=seq_printf (seq, -"reg%02i: base=0x%05lx000 (%4liMB), size=%4i%cB: %s, count=%d\n" , +"reg%02i: base=0x%05lx000 (%4luMB), size=%4lu%cB: %s, count=%d\n" , i, base, base >> (20 -PAGE_SHIFT ), size, factor,mtrr_attrib_to_str (type), usage_table[i]); }
arch/i386/kernel/cpu/mtrr/main.c +40 -15
115 lines not on this page · raw message
arch/i386/kernel/cpu/mtrr/mtrr.h +13 -12
@@ -43,15 +43,16 @@ struct mtrr_ops {void (*set_all)(void );void (*get)(unsigned int reg,unsigned long *base, -unsigned int *size,mtrr_type * type); -int (*get_free_region) (unsigned long base,unsigned long size); - +unsigned long *size,mtrr_type * type); +int (*get_free_region)(unsigned long base,unsigned long size, +int replace_reg);int (*validate_add_page)(unsigned long base,unsigned long size,unsigned int type);int (*have_wrcomb)(void ); }; -extern int generic_get_free_region (unsigned long base,unsigned long size); +extern int generic_get_free_region (unsigned long base,unsigned long size, +int replace_reg);extern int generic_validate_add_page (unsigned long base,unsigned long size,unsigned int type);
@@ -62,17 +63,17 @@ extern int positive_have_wrcomb(void);/* library functions for processor-specific routines */ struct set_mtrr_context {unsigned long flags; -unsigned long deftype_lo; -unsigned long deftype_hi;unsigned long cr4val; -unsigned long ccr3; +u32 deftype_lo; +u32 deftype_hi; +u32 ccr3; };struct mtrr_var_range { -unsigned long base_lo; -unsigned long base_hi; -unsigned long mask_lo; -unsigned long mask_hi; +u32 base_lo; +u32 base_hi; +u32 mask_lo; +u32 mask_hi; };void set_mtrr_done (struct set_mtrr_context *ctxt);
@@ -92,6 +93,6 @@ extern struct mtrr_ops * mtrr_if;extern unsigned int num_var_ranges;void mtrr_state_warn (void ); -char *mtrr_attrib_to_str (int x); +const char *mtrr_attrib_to_str (int x);void mtrr_wrmsr (unsigned ,unsigned ,unsigned );
Quoted 2 lines · Jan Beulich
While after a previous submission (about one and a half year ago) I got told that the MTRR code is to be replaced by PAT use, this hasn't
Work is going on, patches exist, but do need more work and more testing. I hope to put a subset (without new user interface) into -mm* for testing soon.
Quoted 2 lines
happened. Hence I'd like to ask to reconsider this patch until PAT handling code shows up.
Added thanks.
Next time please only do one change / patch please.
I also got one reject in generic.c for
- if (base + size < 0x100) {
+ if (base < 0x100) {
but I hope that's ok
-Andi
Quoted 6 lines
I also got one reject in
generic.cforarch/i386/kernel/cpu/mtrr/generic.c +1
-1-if (base + size <0x100 ) { +if (base <0x100 ) {but I hope that's ok
Hmm, not really - it applied cleanly on 2.6.19-rc5.
Jan