Skip to contentSkip to conversation

use MTRR for write combining if PAT is not available

RFC PATCH Jan Beulich 5 participants 10 messages Oct 13, 2009 – Oct 19, 2009

Subsystems (4)
lore
Share & tools
lore
#0 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 12.10.09 20:32 >>>

(quoted diff) +0 -0
@@ -268,11 +269,14 @@ EXPORT_SYMBOL(ioremap_nocache);
 */
 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)

Review EXPORT_SYMBOL(ioremap_nocache); · line 275

Quoted 10 lines
(quoted diff) +4 -5
 {
-	if (pat_enabled)
-		return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
-					__builtin_return_address(0));
-	else
-		return ioremap_nocache(phys_addr, size);
+	if (!pat_enabled) {
+		void __iomem *ret = ioremap_nocache(phys_addr, size);
+		if (ret)
+			mtrr_add(phys_addr, size, MTRR_TYPE_WRCOMB, false);

This won't work if phys_addr or size aren't page aligned, or if size isn't a power of two.

Quoted 8 lines
(quoted diff) +4 -0
+		return ret;
+	}
+	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
+				__builtin_return_address(0));
 }
 EXPORT_SYMBOL(ioremap_wc);
 

Review int set_memory_wc(unsigned long addr, int numpages) · line 1018

Quoted 10 lines
(quoted diff) +5 -2
@@ -1010,8 +1011,13 @@ int set_memory_wc(unsigned long addr, int numpages)
 {
 	int ret;
 
-	if (!pat_enabled)
-		return set_memory_uc(addr, numpages);
+	if (!pat_enabled) {
+		ret = set_memory_uc(addr, numpages);
+		if (!ret)
+			mtrr_add(__pa(addr), numpages * PAGE_SIZE,
+				 MTRR_TYPE_WRCOMB, false);

Similarly, this requires numpages to be a power of two.

Review int set_memory_wc(unsigned long addr, int numpages) · line 1023

Quoted 5 lines
(quoted diff) +2 -0
+		return ret;
+	}
 
 	ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
 		_PAGE_CACHE_WC, NULL);

I think user mode code handles this by splitting the request and trying to establish as many entries as possible (and those as big as possible).

Also, in all cases you pass 'false' for the 'increment' parameter, in order to avoid having to tear down the established entries. While this may be reasonable for kernel initiated mappings, I don't think that's acceptable for such originating from user mode.

Jan

#1 lore raw
Quoted 8 lines · Jan Beulich

Review

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 12.10.09 20:32 >>>

(quoted diff) +4 -0
+	if (!pat_enabled) {
+		void __iomem *ret = ioremap_nocache(phys_addr, size);
+		if (ret)
+			mtrr_add(phys_addr, size, MTRR_TYPE_WRCOMB, false);

This won't work if phys_addr or size aren't page aligned, or if size isn't a power of two.

No, at least the comments in mtrr_add and mtrr_check state that it is just required that phys_addr and size are multiple of PAGE_SIZE. And I'm not sure if it is always safe to round these up/down to the next PAGE boundary. If it is not, maybe it is better to fail...

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	if (!pat_enabled) {
+		ret = set_memory_uc(addr, numpages);
+		if (!ret)
+			mtrr_add(__pa(addr), numpages * PAGE_SIZE,
+				 MTRR_TYPE_WRCOMB, false);

Similarly, this requires numpages to be a power of two.

Same as above.

Quoted 2 lines

I think user mode code handles this by splitting the request and trying to establish as many entries as possible (and those as big as possible).

Seems not required here...

Quoted 4 lines

Also, in all cases you pass 'false' for the 'increment' parameter, in order to avoid having to tear down the established entries. While this may be reasonable for kernel initiated mappings, I don't think that's acceptable for such originating from user mode.

Well, therefore I would have to remember which mmap successfully set up which MTRR entries. And it must be ensured that for each mmap there always is a munmap. If this is indeed always ensured, and if it's worth to maintain the required data, I'd be happy to add a corresponding mtrr_del there.

Kind regards,
Thomas

#2 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 13.10.09 23:29 >>>

Quoted 8 lines · Jan Beulich

Review

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 12.10.09 20:32 >>>

(quoted diff) +4 -0
+	if (!pat_enabled) {
+		void __iomem *ret = ioremap_nocache(phys_addr, size);
+		if (ret)
+			mtrr_add(phys_addr, size, MTRR_TYPE_WRCOMB, false);

This won't work if phys_addr or size aren't page aligned, or if size isn't a power of two.

Quoted 4 lines

No, at least the comments in mtrr_add and mtrr_check state that it is just required that phys_addr and size are multiple of PAGE_SIZE. And I'm not sure if it is always safe to round these up/down to the next PAGE boundary. If it is not, maybe it is better to fail...

That function isn't the limiting factor, generic_validate_add_page() is what you need to look at (plus the spec on how the MTRR ranges are calculated by the CPU from the base/mask register pairs).

Jan

#3 lore raw
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 13.10.09 23:29 >>>

No, at least the comments in mtrr_add and mtrr_check state that it is just required that phys_addr and size are multiple of PAGE_SIZE. And I'm not sure if it is always safe to round these up/down to the next PAGE boundary. If it is not, maybe it is better to fail...

That function isn't the limiting factor, generic_validate_add_page() is what you need to look at (plus the spec on how the MTRR ranges are calculated by the CPU from the base/mask register pairs).

Yes, you are right. Sorry for not looking into generic_validate_add_page() before. Thank you for showing!

I added a function mtrr_add_unaligned() that tries to create as many MTRR entries as necessary, beginning with the biggest regions. It does not check the return values of each mtrr_add(), nor does it return the indexes of the created MTRR entries. So it seems to be only useful with increment=false. Or do you have a better idea?

Kind regards,
Thomas

Quoted 1 line

From 3708f0f4c729de32445ba13ab16b6268920af0bb Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Wed, 14 Oct 2009 19:25:33 +0200
Subject: [PATCH 1/2] 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 |   25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)
arch/x86/include/asm/mtrr.h +6 -0
index 4365ffd..0ad8e68 100644
@@ -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, bool increment);
 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, bool increment)
+{
+}
 static inline int mtrr_del(int reg, unsigned long base, unsigned long size)
 {
     return -ENODEV;
arch/x86/kernel/cpu/mtrr/main.c +25 -0
index 84e83de..7417ebb 100644
@@ -487,6 +487,31 @@ 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, bool increment)
+{
+	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) {
+			mtrr_add(ptr1, size, type, increment);
+			ptr1 += size;
+		}
+		if (base + size <= ptr2) {
+			ptr2 -= size;
+			mtrr_add(ptr2, size, type, increment);
+		}
+		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
Quoted 1 line

From 3533b9e66c5144844a0b0864d0f57f43d57aea1a Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Thu, 8 Oct 2009 21:24:07 +0200
Subject: [PATCH 2/2] Use MTRR for write combining mmap/ioremap 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, we fall back to uncached mmap. Then libpciaccess thinks it succeeded mapping with write combining anabled and does not set up suited MTRR entries. ;-(

So when falling back to uncached mapping, we better try to set up MTRR entries automatically. To match this modified PCI mmap behavior, also ioremap_wc and set_memory_wc are adjusted.

---
 arch/x86/mm/ioremap.c  |   15 ++++++++++-----
 arch/x86/mm/pageattr.c |   10 ++++++++--
 arch/x86/pci/i386.c    |    6 ++++++
 3 files changed, 24 insertions(+), 7 deletions(-)
arch/x86/mm/ioremap.c +10 -5
index 334e63c..abe40fa 100644
@@ -21,6 +21,7 @@
 #include <asm/tlbflush.h>
 #include <asm/pgalloc.h>
 #include <asm/pat.h>
+#include <asm/mtrr.h>
 
 #include "physaddr.h"
 
@@ -268,11 +269,15 @@ EXPORT_SYMBOL(ioremap_nocache);
  */
 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
 {
-	if (pat_enabled)
-		return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
-					__builtin_return_address(0));
-	else
-		return ioremap_nocache(phys_addr, size);
+	if (!pat_enabled) {
+		void __iomem *ret = ioremap_nocache(phys_addr, size);
+		if (ret)
+			mtrr_add_unaligned(phys_addr, size,
+					   MTRR_TYPE_WRCOMB, false);
+		return ret;
+	}
+	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
+				__builtin_return_address(0));
 }
 EXPORT_SYMBOL(ioremap_wc);
 
arch/x86/mm/pageattr.c +8 -2
index dd38bfb..c25f697 100644
@@ -23,6 +23,7 @@
 #include <asm/pgalloc.h>
 #include <asm/proto.h>
 #include <asm/pat.h>
+#include <asm/mtrr.h>
 
 /*
  * The current flushing context - we pass it instead of 5 arguments:
@@ -1010,8 +1011,13 @@ int set_memory_wc(unsigned long addr, int numpages)
 {
 	int ret;
 
-	if (!pat_enabled)
-		return set_memory_uc(addr, numpages);
+	if (!pat_enabled) {
+		ret = set_memory_uc(addr, numpages);
+		if (!ret)
+			mtrr_add_unaligned(__pa(addr), numpages * PAGE_SIZE,
+					   MTRR_TYPE_WRCOMB, false);
+		return ret;
+	}
 
 	ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
 		_PAGE_CACHE_WC, NULL);
arch/x86/pci/i386.c +6 -0
index b22d13b..8379e9b 100644
@@ -33,6 +33,7 @@
 #include <linux/bootmem.h>
 
 #include <asm/pat.h>
+#include <asm/mtrr.h>
 #include <asm/e820.h>
 #include <asm/pci_x86.h>
 #include <asm/io_apic.h>
@@ -301,5 +302,10 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
 
 	vma->vm_ops = &pci_mmap_ops;
 
+	if (!pat_enabled && write_combine)
+		mtrr_add_unaligned(vma->vm_pgoff << PAGE_SHIFT,
+				   vma->vm_end - vma->vm_start,
+				   MTRR_TYPE_WRCOMB, false);
+
 	return 0;
 }
Signature · 1 line
1.6.5
#4 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 14.10.09 21:14 >>>

I added a function mtrr_add_unaligned() that tries to create as many MTRR entries as necessary, beginning with the biggest regions. It does not check the return values of each mtrr_add(), nor does it return the indexes of the created MTRR entries. So it seems to be only useful with increment=false. Or do you have a better idea?

I don't have immediate thoughts on how to address this, but nevertheless I continue to think that the issue must be solved in some way, even more that now you may be leaking multiple MTRRs.

Jan

#5 lore raw
Quoted 11 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 14.10.09 21:14 >>>

I added a function mtrr_add_unaligned() that tries to create as many MTRR entries as necessary, beginning with the biggest regions. It does not check the return values of each mtrr_add(), nor does it return the indexes of the created MTRR entries. So it seems to be only useful with increment=false. Or do you have a better idea?

I don't have immediate thoughts on how to address this, but nevertheless I continue to think that the issue must be solved in some way, even more that now you may be leaking multiple MTRRs.

OK, it required some changes on different edges, but now I have it...

Patches 0001-0003 introduce functionality/changes to the MTRR and sysfs subsystems. Patch 0004 is the main patch which sets up the MTRR entries when pci memory regions are mmapped. The Patches 0005-0006 also change ioremap() and set_mempry_wc() to set up MTRR entries. These two are completely optional, especially and there is currently no way to automatically remove MTRR entries set up with them.

What do you think about these patches?

Kind regards,
Thomas

Quoted 1 line

From 4f000ec18079924b1191118dccf468bba50c402d Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 21:29:43 +0200
Subject: [PATCH 5/6] Use MTRR for write combining ioremap

If PAT is not enabled, set up write combining MTRR entries in ioremap().

---
 arch/x86/mm/ioremap.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/mm/ioremap.c +10 -5
index 334e63c..abe40fa 100644
@@ -21,6 +21,7 @@
 #include <asm/tlbflush.h>
 #include <asm/pgalloc.h>
 #include <asm/pat.h>
+#include <asm/mtrr.h>
 
 #include "physaddr.h"
 
@@ -268,11 +269,15 @@ EXPORT_SYMBOL(ioremap_nocache);
  */
 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
 {
-	if (pat_enabled)
-		return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
-					__builtin_return_address(0));
-	else
-		return ioremap_nocache(phys_addr, size);
+	if (!pat_enabled) {
+		void __iomem *ret = ioremap_nocache(phys_addr, size);
+		if (ret)
+			mtrr_add_unaligned(phys_addr, size,
+					   MTRR_TYPE_WRCOMB, false);
+		return ret;
+	}
+	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
+				__builtin_return_address(0));
 }
 EXPORT_SYMBOL(ioremap_wc);
 
Signature · 1 line
1.6.5
Quoted 1 line

From 9ef568a835c5033efb8032f1a4415ac2a128d4fc Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Wed, 14 Oct 2009 19:25:33 +0200
Subject: [PATCH 1/6] Add new mtrr_add_unaligned function

This function creates multiple MTRR entries for unaligned memory regions.

---
 arch/x86/include/asm/mtrr.h     |    8 ++++++++
 arch/x86/kernel/cpu/mtrr/main.c |   30 ++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 0 deletions(-)
arch/x86/include/asm/mtrr.h +8 -0
index 4365ffd..2e0d99d 100644
@@ -116,6 +116,9 @@ 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, bool increment,
+			       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 +149,11 @@ 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, bool increment,
+				      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
index 84e83de..5bf769b 100644
@@ -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, bool increment, 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, increment);
+			if (i >= 0 && increment && mtrr_usage)
+				++mtrr_usage[i];
+			ptr1 += size;
+		}
+		if (base + size <= ptr2) {
+			ptr2 -= size;
+			i = mtrr_add(ptr2, size, type, increment);
+			if (i >= 0 && increment && 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
Quoted 1 line

From 513955a4697328d319739799b2bca650df895689 Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:36:05 +0200
Subject: [PATCH 2/6] 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
index 2e0d99d..179a767 100644
@@ -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
index a501dee..ba6a8a5 100644
@@ -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
Quoted 1 line

From 0d7525045927c8056ff28dd9465766862faba227 Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:39:11 +0200
Subject: [PATCH 3/6] Provide per-file private data for bin sysfs files

For binary sysfs files, provide a per-file private field in struct bin_buffer. Therefore this modified struct is exported in the <linux/sysfs.h> header file. Additionally add a release() callback that can be used to free the private data on file release.

---
 fs/sysfs/bin.c        |   18 ++++++++----------
 include/linux/sysfs.h |   14 ++++++++++++++
 2 files changed, 22 insertions(+), 10 deletions(-)
fs/sysfs/bin.c +8 -10
index 60c702b..3647b7a 100644
@@ -36,15 +36,6 @@
  */
 static DEFINE_MUTEX(sysfs_bin_lock);
 
-struct bin_buffer {
-	struct mutex			mutex;
-	void				*buffer;
-	int				mmapped;
-	const struct vm_operations_struct *vm_ops;
-	struct file			*file;
-	struct hlist_node		list;
-};
-
 static int
 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
 {
@@ -438,14 +429,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 +14 -0
index 9d68fed..bcad8ad 100644
@@ -15,6 +15,7 @@
 #include <linux/compiler.h>
 #include <linux/errno.h>
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <asm/atomic.h>
 
 struct kobject;
@@ -60,8 +61,19 @@ struct attribute_group {
 
 #define attr_name(_attr) (_attr).attr.name
 
+struct file;
 struct vm_area_struct;
 
+struct bin_buffer {
+	struct mutex			mutex;
+	void				*buffer;
+	void				*private;
+	int				mmapped;
+	const struct vm_operations_struct *vm_ops;
+	struct file			*file;
+	struct hlist_node		list;
+};
+
 struct bin_attribute {
 	struct attribute	attr;
 	size_t			size;
@@ -72,6 +84,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
Quoted 1 line

From d7d9760bf7052ee53e50beb7e89fc14371e33cfa Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 21:17:16 +0200
Subject: [PATCH 4/6] 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, we fall back to uncached mmap. Then libpciaccess thinks it succeeded mapping with write combining anabled 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.

---
 drivers/pci/pci-sysfs.c |   45 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +43 -2

77 lines not on this page · raw message

Signature · 1 line
1.6.5
Quoted 1 line

From 8eea502750fa642ff9f74e91466646e669eb5791 Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 21:36:37 +0200
Subject: [PATCH 6/6] Set up MTRR entries within set_memory_wc

If PAT is not enabled, set up write combining MTRR entries in set_memory_wc().

---
 arch/x86/mm/pageattr.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/mm/pageattr.c +8 -2
index dd38bfb..c25f697 100644
@@ -23,6 +23,7 @@
 #include <asm/pgalloc.h>
 #include <asm/proto.h>
 #include <asm/pat.h>
+#include <asm/mtrr.h>
 
 /*
  * The current flushing context - we pass it instead of 5 arguments:
@@ -1010,8 +1011,13 @@ int set_memory_wc(unsigned long addr, int numpages)
 {
 	int ret;
 
-	if (!pat_enabled)
-		return set_memory_uc(addr, numpages);
+	if (!pat_enabled) {
+		ret = set_memory_uc(addr, numpages);
+		if (!ret)
+			mtrr_add_unaligned(__pa(addr), numpages * PAGE_SIZE,
+					   MTRR_TYPE_WRCOMB, false);
+		return ret;
+	}
 
 	ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
 		_PAGE_CACHE_WC, NULL);
Signature · 1 line
1.6.5
#6 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 17.10.09 21:48 >>>

Patches 0001-0003 introduce functionality/changes to the MTRR and sysfs subsystems. Patch 0004 is the main patch which sets up the MTRR entries when pci memory regions are mmapped. The Patches 0005-0006 also change ioremap() and set_mempry_wc() to set up MTRR entries. These two are completely optional, especially and there is currently no way to automatically remove MTRR entries set up with them.

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.

However, I would recommend folding the last two arguments of mtrr_add_unaligned(), i.e. use mtrr_usage != NULL as the increment argument passed to mtrr_add().

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().

Also, why do you add x86-specific code to drivers/pci-sysfs.c when 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.

Additionally, I would suggest making those code portions depend on CONFIG_X86_MTRR rather than CONFIG_X86, so that you don't needlessly (try to) allocate the mtrr_usage vector.

Jan

#7 lore raw
Quoted 1 line · Jan Beulich

Functionality-wise this looks fine to me

If we are going to make ioremap() and set_memory_wc() add mtrr's in non-pat case, then we need to delete the added mtrr(s) in the corresponding iounmap() and set_memory_wb() aswell.

hmm, this is becoming too complex. The way i915 and other graphics drivers are using set_memory_wc(), it is def a bad idea to start adding mtrr's behind the back for non-pat case.

Can't we just force PAT option always and we probably don't care about ioremap_wc() on processors were PAT doesn't get enabled because of known errata.

or Perhaps just try to add mtrr only for the pci mmap case like the 4th patch in this series..

thanks,
suresh

#8 lore raw
Quoted 10 lines · Suresh Siddha
Quoted 1 line · Jan Beulich

Functionality-wise this looks fine to me

If we are going to make ioremap() and set_memory_wc() add mtrr's in non-pat case, then we need to delete the added mtrr(s) in the corresponding iounmap() and set_memory_wb() aswell.

hmm, this is becoming too complex. The way i915 and other graphics drivers are using set_memory_wc(), it is def a bad idea to start adding mtrr's behind the back for non-pat case.

Touching MTRRs beyond working around basic bugs like non-cached RAM sounds like madness. The interactions with PAT are ... countless.

Quoted 3 lines

Can't we just force PAT option always and we probably don't care about ioremap_wc() on processors were PAT doesn't get enabled because of known errata.

We can make PAT configurability dependent on EMBEDDED-y - mind sending a patch for that?

Ingo

#9 lore raw
In reply to Thomas Schlichter
Quoted 4 lines

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, we fall back to uncached mmap. Then libpciaccess thinks it succeeded mapping with write combining anabled and does not set up suited MTRR entries. ;-(

I am not familiar with libpciaccess, but I was wondering why that library cannot realize it failed in its endeavour and use other means to accomplish its goals?