use MTRR for write combining if PAT is not available
Subsystems (3)
Quoted 5 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Thomas Schlichter <[email protected]> 17.10.09 21:48 >>>
What do you think about these patches?
Functionality-wise this looks fine to me; whether the core sysfs changes are acceptable I can't judge, though.
OK, who can? Or shall I simply ask get_maintainer.pl again?
Quoted 3 lines
However, I would recommend folding the last two arguments of
mtrr_add_unaligned(), i.e. usemtrr_usage!= NULL as the increment argument passed tomtrr_add().
Good idea, I will do so this evening...
Quoted 2 lines
And patches 5 and 6 would apparently not build right now, as they're failing to pass the new 5th argument to
mtrr_add_unaligned().
*Grml* you are of course right. But I am not sure if these both patches are a goot idea at all.
Quoted 5 lines
Also, why do you add x86-specific code to
drivers/pci-sysfs.cwhen the function called there (pci_mmap_page_range()) already is arch-specific? Moving your addition there would also allow covering the related (legacy?) procfs based functionality...pci_release()could also become arch-specific, with a fall-back definition to NULL.
You are right, I should do that...
Quoted 3 lines
Additionally, I would suggest making those code portions depend on
CONFIG_X86_MTRRrather thanCONFIG_X86, so that you don't needlessly (try to) allocate themtrr_usagevector.
Good idea, I will do so.
Thank you very much for your feedback, I'll hopefully come up with an even better version tonight...
Kind regards,
Thomas
Quoted 2 lines · Jan Beulich
Functionality-wise this looks fine to me; whether the core sysfs changes are acceptable I can't judge, though.
OK, I think I should have addressed your comments. Unfortunately I had to use a little "hack" to make pci_mmap_page_range() work for sysfs and proc. I placed a "private" pointer in the beginning of both per-file private structures. So this pointer can be accessed independent from the caller. I hope this is acceptable.
I dropped the ioremap() and set_memory_wc() patches, I could not implement reference counting for them and it may interact too much with existing GPU drivers.
Again, this series should not change the current behavior if either MTRR is disabled or PAT is enabled. But it helps in the case that MTRR is enabled and PAT is not available.
What should be done now to get this series on the right "track"?
Kind regards,
Thomas
Quoted 1 line
From
88d77b939afa1934eb2158f6d28db500eab0d345Mon Sep 17 00:00:00 2001
From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 21:17:16 +0200
Subject: [PATCH 4/4] Use MTRR for pci_mmap_resource_wc if PAT is not available
X.org uses libpciaccess which tries to mmap with write combining enabled via /sys/bus/pci/devices/*/resource0_wc. Currently, when PAT is not enabled, the kernel does fall back to uncached mmap. Then libpciaccess thinks it succeeded mapping with write combining enabled and does not set up suited MTRR entries. ;-(
So when falling back to uncached mapping, we better try to set up MTRR entries automatically. When the resource file is closed, we remove the MTRR entries again.
--- arch/x86/include/asm/pci.h | 2 ++ arch/x86/pci/i386.c | 38 ++++++++++++++++++++++++++++++++++++++ drivers/pci/pci-sysfs.c | 8 ++++++++ drivers/pci/pci.h | 6 ++++++ drivers/pci/proc.c | 5 ++++- 5 files changed, 58 insertions(+), 1 deletions(-)
arch/x86/include/asm/pci.h +2 -0
@@ -64,6 +64,8 @@ struct irq_routing_table *pcibios_get_irq_routing_table(void);int pcibios_set_irq_routing (struct pci_dev *dev,int pin,int irq); +#define HAVE_PCI_RELEASE_FILE +#define HAVE_PCI_MMAP extern int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, enum pci_mmap_state mmap_state,
arch/x86/pci/i386.c +38 -0
@@ -31,7 +31,9 @@#include <linux/ioport.h> #include <linux/errno.h> #include <linux/bootmem.h> +#include <linux/fs.h> +#include <asm/mtrr.h> #include <asm/pat.h> #include <asm/e820.h> #include <asm/pci_x86.h>
@@ -274,6 +276,7 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, enum pci_mmap_state mmap_state, int write_combine) {unsigned long prot; +int **p_mtrr_usage = (int **)vma->vm_file->private_data;/* I/O space cannot be accessed via normal processor loads and * stores on this platform.
@@ -301,5 +304,40 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, vma->vm_ops = &pci_mmap_ops; +#ifdef CONFIG_MTRR +if (!pat_enabled && write_combine) { +if (*p_mtrr_usage ==NULL ) + *p_mtrr_usage =kzalloc (num_var_ranges *sizeof (int ), +GFP_KERNEL ); +mtrr_add_unaligned (vma->vm_pgoff <<PAGE_SHIFT , + vma->vm_end - vma->vm_start, +MTRR_TYPE_WRCOMB , *p_mtrr_usage); + } +#endif // CONFIG_MTRR + +return 0 ; +} + +int pci_release_file (struct file *file) +{ +#ifdef CONFIG_MTRR +int i; +int **p_mtrr_usage = (int **)file->private_data; +int *mtrr_usage = *p_mtrr_usage; + +if (!mtrr_usage) +return 0 ; + +for (i =0 ; i < num_var_ranges; ++i) { +while (mtrr_usage[i] >0 ) { +mtrr_del (i,0 ,0 ); + --mtrr_usage[i]; + } + } + +kfree (*p_mtrr_usage); + *p_mtrr_usage =NULL ; +#endif // CONFIG_MTRR +return 0 ; }
drivers/pci/pci-sysfs.c +8 -0
@@ -733,6 +733,13 @@ pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,return pci_mmap_resource (kobj, attr, vma,1 ); } +static int +pci_release_resource (struct kobject *kobj,struct bin_attribute *attr, +struct file *file) +{ +return pci_release_file (file); +} +/** * pci_remove_resource_files - cleanup resource files * @pdev: dev to cleanup
@@ -782,6 +789,7 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)sprintf (res_attr_name,"resource%d" , num); res_attr->mmap = pci_mmap_resource_uc; } + res_attr->release = pci_release_resource; res_attr->attr.name = res_attr_name; res_attr->attr.mode =S_IRUSR |S_IWUSR ; res_attr->size =pci_resource_len (pdev, num);
drivers/pci/pci.h +6 -0
@@ -84,6 +84,12 @@ static inline void pci_vpd_release(struct pci_dev *dev) dev->vpd->ops->release (dev); } +#ifdef HAVE_PCI_RELEASE_FILE +extern int pci_release_file (struct file *file); +#else +static inline int pci_release_file (struct file *file) {return 0 ; } +#endif +/* PCI /proc functions */ #ifdef CONFIG_PROC_FS extern int pci_proc_attach_device (struct pci_dev *dev);
drivers/pci/proc.c +4 -1
@@ -197,6 +197,7 @@ proc_bus_pci_write(struct file *file, const char __user *buf, size_t nbytes, lof }struct pci_filp_private { +void *private;enum pci_mmap_state mmap_state;int write_combine; };
@@ -277,7 +278,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)static int proc_bus_pci_open (struct inode *inode,struct file *file) { -struct pci_filp_private *fpriv =kmalloc (sizeof (*fpriv),GFP_KERNEL ); +struct pci_filp_private *fpriv =kzalloc (sizeof (*fpriv),GFP_KERNEL );if (!fpriv)return -ENOMEM ;
@@ -292,6 +293,8 @@ static int proc_bus_pci_open(struct inode *inode, struct file *file)static int proc_bus_pci_release (struct inode *inode,struct file *file) { +pci_release_file (file); +kfree (file->private_data); file->private_data =NULL ;
Signature · 1 line
1.6.5.1
Quoted 1 line
From
7cd44581d15aaa3b2ef38a4d76ba19dd50e7cb7bMon Sep 17 00:00:00 2001
From: Thomas Schlichter <[email protected]>
Date: Wed, 14 Oct 2009 19:25:33 +0200
Subject: [PATCH 1/4] Add new mtrr_add_unaligned function
This function creates multiple MTRR entries for unaligned memory regions.
--- arch/x86/include/asm/mtrr.h | 6 ++++++ arch/x86/kernel/cpu/mtrr/main.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 0 deletions(-)
arch/x86/include/asm/mtrr.h +6 -0
@@ -116,6 +116,8 @@ extern int mtrr_add(unsigned long base, unsigned long size, unsigned int type, bool increment);extern int mtrr_add_page (unsigned long base,unsigned long size,unsigned int type,bool increment); +extern void mtrr_add_unaligned (unsigned long base,unsigned long size, +unsigned int type,int *mtrr_usage);extern int mtrr_del (int reg,unsigned long base,unsigned long size);extern int mtrr_del_page (int reg,unsigned long base,unsigned long size);extern void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi);
@@ -146,6 +148,10 @@ static inline int mtrr_add_page(unsigned long base, unsigned long size, {return -ENODEV ; } +static inline void mtrr_add_unaligned (unsigned long base,unsigned long size, +unsigned int type,int *mtrr_usage) +{ +}static inline int mtrr_del (int reg,unsigned long base,unsigned long size) {return -ENODEV ;
arch/x86/kernel/cpu/mtrr/main.c +30 -0
@@ -487,6 +487,36 @@ int mtrr_add(unsigned long base, unsigned long size, unsigned int type, }EXPORT_SYMBOL (mtrr_add); +void mtrr_add_unaligned (unsigned long base,unsigned long size, +unsigned int type,int *mtrr_usage) +{ +int i; +unsigned long ptr1, ptr2, end = base + size; + +// round down size to next power ot two + size =__rounddown_pow_of_two (size); + +// accordingly align pointers + ptr1 = ptr2 = (base + size -1 ) & ~(size -1 ); + +while (size >=PAGE_SIZE ) { +if (ptr1 + size <= end) { + i =mtrr_add (ptr1, size, type, !!mtrr_usage); +if (i >=0 && mtrr_usage) + ++mtrr_usage[i]; + ptr1 += size; + } +if (base + size <= ptr2) { + ptr2 -= size; + i =mtrr_add (ptr2, size, type, !!mtrr_usage); +if (i >=0 && mtrr_usage) + ++mtrr_usage[i]; + } + size >>=1 ; + } +} +EXPORT_SYMBOL (mtrr_add_unaligned); +/** * mtrr_del_page - delete a memory type region * @reg: Register returned by mtrr_add
Signature · 1 line
1.6.5.1
Quoted 1 line
From
4299569a5ed27b2a0bc2c0a04014edc4bb29bb7eMon Sep 17 00:00:00 2001
From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:36:05 +0200
Subject: [PATCH 2/4] Make num_var_ranges accessible outside MTRR code
Code outside MTRR will have to remember which MTRR entries are used for a given purpose. Therefore it has to access num_var_ranges which holds the value of the maximum count of MTRR entries available. So we make this value accessible from outside the core MTRR code.
--- arch/x86/include/asm/mtrr.h | 2 ++ arch/x86/kernel/cpu/mtrr/mtrr.h | 1 - 2 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/include/asm/mtrr.h +2 -0
@@ -72,6 +72,8 @@ typedef __u8 mtrr_type;#define MTRR_NUM_FIXED_RANGES 88#define MTRR_MAX_VAR_RANGES 256 +extern unsigned int num_var_ranges; +struct mtrr_state_type {struct mtrr_var_range var_ranges[MTRR_MAX_VAR_RANGES ];mtrr_type fixed_ranges[MTRR_NUM_FIXED_RANGES ];
arch/x86/kernel/cpu/mtrr/mtrr.h +0 -1
@@ -61,7 +61,6 @@ extern struct mtrr_ops *mtrr_if;#define is_cpu (vnd) (mtrr_if && mtrr_if->vendor == X86_VENDOR_##vnd)#define use_intel () (mtrr_if && mtrr_if->use_intel_if == 1) -extern unsigned int num_var_ranges;extern u64 mtrr_tom2;extern struct mtrr_state_type mtrr_state;
Signature · 1 line
1.6.5.1
Quoted 1 line
From
92735813dbd6ea3c9ee56a7e23d7a8d8483e9e7dMon Sep 17 00:00:00 2001
From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:39:11 +0200
Subject: [PATCH 3/4] Provide per-file private data for bin sysfs files
For binary sysfs files, provide a per-file private field in struct bin_buffer. To be easily accessible, it is located on head of the struct. Additionally add a release() callback that may be used to e.g. free the private data on file release.
--- fs/sysfs/bin.c | 10 +++++++++- include/linux/sysfs.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletions(-)
fs/sysfs/bin.c +9 -1
@@ -37,6 +37,7 @@static DEFINE_MUTEX (sysfs_bin_lock);struct bin_buffer { +void *private;struct mutex mutex;void *buffer;int mmapped;
@@ -438,14 +439,21 @@ static int open(struct inode * inode, struct file * file)static int release (struct inode * inode,struct file * file) {struct bin_buffer *bb = file->private_data; +struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; +struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; +struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; +int rc =0 ; +if (attr->release) + rc = attr->release (kobj, attr, file); +mutex_lock (&sysfs_bin_lock);hlist_del (&bb->list);mutex_unlock (&sysfs_bin_lock);kfree (bb->buffer);kfree (bb); -return 0 ; +return rc; }const struct file_operations bin_fops = {
include/linux/sysfs.h +3 -0
@@ -19,6 +19,7 @@struct kobject ;struct module ; +struct file ;/* FIXME * The *owner field is no longer used.
@@ -72,6 +73,8 @@ struct bin_attribute { char *, loff_t, size_t);int (*mmap)(struct kobject *,struct bin_attribute *attr,struct vm_area_struct *vma); +int (*release)(struct kobject *,struct bin_attribute *attr, +struct file *file); };struct sysfs_ops {
Signature · 1 line
1.6.5.1
Quoted 15 lines · Thomas Schlichter
Quoted 2 lines · Jan Beulich
Functionality-wise this looks fine to me; whether the core sysfs changes are acceptable I can't judge, though.
OK, I think I should have addressed your comments. Unfortunately I had to use a little "hack" to make
pci_mmap_page_range()work for sysfs and proc. I placed a "private" pointer in the beginning of both per-file private structures. So this pointer can be accessed independent from the caller. I hope this is acceptable.I dropped the
ioremap()andset_memory_wc()patches, I could not implement reference counting for them and it may interact too much with existing GPU drivers.
Quoted 5 lines
Again, this series should not change the current behavior if either MTRR is disabled or PAT is enabled. But it helps in the case that MTRR is enabled and PAT is not available.
What should be done now to get this series on the right "track"?
Can we eliminate mtrr_add_unaligned() as Suresh suggested, and still make it work on your testbox?
Once that's done i'll look at putting it into the x86 tree for testing. The acks of Suresh/Venki/Jan would be nice to have.
Ingo