Skip to contentSkip to conversation

use MTRR for write combining if PAT is not available

RFC PATCH Thomas Schlichter 6 participants 16 messages Oct 21, 2009 – Oct 23, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Quoted 6 lines · Suresh Siddha

I think we can simplify this by just using mtrr_add_page() and avoid all the complexity that comes with mtrr_add_unaligned().

pci_mmap_range() should call one mtrr_add_page() if the base and size are nicely aligned. Almost all the cases, this is the usage sequence here anyway.

Quoted 2 lines · Ingo Molnar

Can we eliminate mtrr_add_unaligned() as Suresh suggested, and still make it work on your testbox?

Yes, I had that in the first place, but Jan suggested to extend it to also handle non-aligned, non-power-of-two cases:
http://marc.info/?l=linux-kernel&m=125541951529918&w=2

So if it's OK for Jan, I'll reduce the functionality again and use mtrr_add() instead. Btw. this simply means to drop mtrr_add_unaligned(), all the other parts are still required for reference counting and a proper mtrr_del() on file close.

Kind regards,
Thomas

#1 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Thomas Schlichter <[email protected]> 21.10.09 15:45 >>>

Quoted 2 lines · Ingo Molnar

Can we eliminate mtrr_add_unaligned() as Suresh suggested, and still make it work on your testbox?

Yes, I had that in the first place, but Jan suggested to extend it to also handle non-aligned, non-power-of-two cases:
http://marc.info/?l=linux-kernel&m=125541951529918&w=2

I merely pointed out it wouldn't work for unaligned addresses or sizes passed in.

Quoted 4 lines

So if it's OK for Jan, I'll reduce the functionality again and use mtrr_add() instead. Btw. this simply means to drop mtrr_add_unaligned(), all the other parts are still required for reference counting and a proper mtrr_del() on file close.

I'm perfectly fine with just handling the aligned case, as long as the code checks that the alignment constraints are met.

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 4 lines

So if it's OK for Jan, I'll reduce the functionality again and use mtrr_add() instead. Btw. this simply means to drop mtrr_add_unaligned(), all the other parts are still required for reference counting and a proper mtrr_del() on file close.

I'm perfectly fine with just handling the aligned case, as long as the code checks that the alignment constraints are met.

It could even emit a debug warning if they are not met - that way we'll see how important the unaligned case is.

Ingo

#3 lore raw
Quoted 6 lines · Ingo Molnar
Quoted 2 lines · Jan Beulich

I'm perfectly fine with just handling the aligned case, as long as the code checks that the alignment constraints are met.

It could even emit a debug warning if they are not met - that way we'll see how important the unaligned case is.

OK, so I think the attached patches should do it. Hopefully I have addressed all your comments.

This series works for my test machine, without it, or without X running, I have these MTRR entries:
reg00: base=0x000000000 ( 0MB), size= 2048MB, count=1: write-back
reg01: base=0x06ff00000 ( 1791MB), size= 1MB, count=1: uncachable
reg02: base=0x070000000 ( 1792MB), size= 256MB, count=1: uncachable

With the patches applied and X running I get these:
reg00: base=0x000000000 ( 0MB), size= 2048MB, count=1: write-back
reg01: base=0x06ff00000 ( 1791MB), size= 1MB, count=1: uncachable
reg02: base=0x070000000 ( 1792MB), size= 256MB, count=1: uncachable
reg03: base=0x0d0000000 ( 3328MB), size= 256MB, count=1: write-combining

Best regards,
Thomas

Quoted 1 line

From e946206915e3b023eb331499d73014105429200c Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:36:05 +0200
Subject: [PATCH 1/3] 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 4365ffd..1e7a824 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.1
Quoted 1 line

From 7210888ded750f87f5509bdd5b95363a476ce307 Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 12:39:11 +0200
Subject: [PATCH 2/3] 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
index 60c702b..28c039a 100644
@@ -37,6 +37,7 @@
 static DEFINE_MUTEX(sysfs_bin_lock);
 
 struct bin_buffer {
+	void				*private;
 	struct mutex			mutex;
 	void				*buffer;
 	int				mmapped;
@@ -438,6 +439,13 @@ 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);
@@ -445,7 +453,7 @@ static int release(struct inode * inode, struct file * file)
 
 	kfree(bb->buffer);
 	kfree(bb);
-	return 0;
+	return rc;
 }
 
 const struct file_operations bin_fops = {
include/linux/sysfs.h +3 -0
index 9d68fed..751ea68 100644
@@ -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 1 line

From e3317e73726152380cd05f75c87c433b9185b291 Mon Sep 17 00:00:00 2001

From: Thomas Schlichter <[email protected]>
Date: Sat, 17 Oct 2009 21:17:16 +0200
Subject: [PATCH 3/3] 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        |   60 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci-sysfs.c    |    8 ++++++
 drivers/pci/pci.h          |    6 ++++
 drivers/pci/proc.c         |    5 +++-
 5 files changed, 80 insertions(+), 1 deletions(-)
arch/x86/include/asm/pci.h +2 -0
index ada8c20..e94aeb1 100644
@@ -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 +60 -0

72 lines not on this page · raw message

drivers/pci/pci-sysfs.c +8 -0
index 0f6382f..4f36835 100644
@@ -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
index d92d195..cff3b7d 100644
@@ -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
index 593bb84..91c8a48 100644
@@ -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
#4 lore raw
Quoted 2 lines · Thomas Schlichter

OK, so I think the attached patches should do it. Hopefully I have addressed all your comments.

Thomas,

I have couple of issues with this patchset still. pci_mmap_page_range() doesn't get called for each fork(). So, we won't be ref counting the mtrr usage properly.

I need to think a bit more carefully on this. Can I get back to you early next week on this, as I am traveling and need to think through this?

We already keep track of some of the PAT ref counting using track_pfn_vma_copy(). And we need to extend/use something similar here.

Even if we need to extend sysfs or pci vma ops, we need to increment and decrement the ref count of the mtrr register that gets used. There is no need to go through num_var_ranges etc.

thanks,
suresh

#5 lore raw
Quoted 14 lines · Suresh Siddha
Quoted 2 lines · Thomas Schlichter

OK, so I think the attached patches should do it. Hopefully I have addressed all your comments.

Thomas,

I have couple of issues with this patchset still. pci_mmap_page_range() doesn't get called for each fork(). So, we won't be ref counting the mtrr usage properly.

I need to think a bit more carefully on this. Can I get back to you early next week on this, as I am traveling and need to think through this?

Quoted 6 lines

We already keep track of some of the PAT ref counting using track_pfn_vma_copy(). And we need to extend/use something similar here.

Even if we need to extend sysfs or pci vma ops, we need to increment and decrement the ref count of the mtrr register that gets used. There is no need to go through num_var_ranges etc.

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Signature · 2 lines
Eric Anholt
[email protected]                         [email protected]
#6 lore raw
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

#7 lore raw
Quoted 7 lines · On Thu, 22 Oct 2009 14:47:30 -0700 Suresh Siddha <[email protected]>…
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

Yeah, I think that will be fine. In fact, older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

Jesse

#8 lore raw
Quoted 8 lines · Jesse Barnes
Quoted 5 lines · On Thu, 22 Oct 2009 14:47:30 -0700 Suresh Siddha <[email protected]>…
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Quoted 7 lines
Quoted 2 lines

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

Yeah, I think that will be fine. In fact, older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

Eric, care to send the patch?

#9 lore raw
Quoted 12 lines · Suresh Siddha
Quoted 11 lines · Jesse Barnes
Quoted 7 lines · On Thu, 22 Oct 2009 14:47:30 -0700 Suresh Siddha <[email protected]>…
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

Quoted 6 lines
Quoted 4 lines

Yeah, I think that will be fine. In fact, older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

Eric, care to send the patch?

I don't have a patch, I was just suggesting a way to handle the submitter's problem that won't involve complicated changes that nobody else will be testing since everyone *should* have a graphics driver for their graphics hardware.

Signature · 2 lines
Eric Anholt
[email protected]                         [email protected]
#10 lore raw
Quoted 18 lines · On Thu, 22 Oct 2009 18:53:19 -0700 Eric Anholt <[email protected]> wrote:
Quoted 17 lines · Suresh Siddha
Quoted 15 lines · Jesse Barnes
Quoted 7 lines · On Thu, 22 Oct 2009 14:47:30 -0700 Suresh Siddha <[email protected]>…
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

Yeah, I think that will be fine. In fact, older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

Quoted 6 lines
Quoted 1 line

Eric, care to send the patch?

I don't have a patch, I was just suggesting a way to handle the submitter's problem that won't involve complicated changes that nobody else will be testing since everyone *should* have a graphics driver for their graphics hardware.

Here's a quick & dirty version, totally untested. A cleaner approach would be to separate the WC mapping routines and hide the return -EINVAL in arch specific code...

Jesse

drivers/pci/pci-sysfs.c +7 -0
index 0f6382f..41010bb 100644
@@ -23,6 +23,9 @@
 #include <linux/mm.h>
 #include <linux/capability.h>
 #include <linux/pci-aspm.h>
+#ifdef CONFIG_X86
+#include <asm/pat.h>
+#endif
 #include "pci.h"
 
 static int sysfs_initialized;	/* = 0 */
@@ -730,6 +733,10 @@ static int
 pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
 		     struct vm_area_struct *vma)
 {
+#ifdef CONFIG_X86
+	if (!pat_enabled)
+		return -EINVAL;
+#endif
 	return pci_mmap_resource(kobj, attr, vma, 1);
 }
 
#15 lore raw
In reply to Eric Anholt
Quoted 18 lines · Eric Anholt
Quoted 17 lines · Suresh Siddha
Quoted 15 lines · Jesse Barnes
Quoted 7 lines · On Thu, 22 Oct 2009 14:47:30 -0700 Suresh Siddha <[email protected]>…
Quoted 3 lines · Eric Anholt

Can we just not create the _wc sysfs entry if we don't have PAT? I don't think there's userland relying on its presence as opposed to the non-_wc entry.

Yes indeed. Jesse do you see an issue with this? This is simple and clean. Thanks Eric.

Yeah, I think that will be fine. In fact, older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

Quoted 6 lines
Quoted 1 line

Eric, care to send the patch?

I don't have a patch, I was just suggesting a way to handle the submitter's problem that won't involve complicated changes that nobody else will be testing since everyone *should* have a graphics driver for their graphics hardware.

I can send the patch early next week unless Thomas or someone else beats me.

#11 lore raw
In reply to Jesse Barnes
Quoted 3 lines · Jesse Barnes

Here's a quick & dirty version, totally untested. A cleaner approach would be to separate the WC mapping routines and hide the return -EINVAL in arch specific code...

Jesse How about this patch? Doing this in x86 is cleaner.

I would like Acks/sign-offs-by Thomas, Eric and Jesse, if it is ok with this patch and works.

thanks,
suresh
---

From: Suresh Siddha <[email protected]>
Subject: x86, pat: return EINVAL for pci mmap WC request for !pat_enabled

Thomas Schlichter reported:

Quoted 5 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, 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. ;-(

Instead of silently mapping pci mmap region as UC minus in the case of !pat_enabled and wc request, we can return error. Eric Anholt mentioned that caller (like X) typically follows up with UC minus pci mmap request and if there is a free mtrr slot, caller will manage adding WC mtrr.

Jesse Barnes says:

Quoted 3 lines

Older versions of libpciaccess will behave better if we do it that way (iirc it only allocates an MTRR if the resource_wc file doesn't exist or fails to get mapped).

---

arch/x86/pci/i386.c +9 -0
index b22d13b..a672f12 100644
@@ -282,6 +282,15 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
 		return -EINVAL;
 
 	prot = pgprot_val(vma->vm_page_prot);
+
+	/*
+ 	 * Return error if pat is not enabled and write_combine is requested.
+ 	 * Caller can followup with UC MINUS request and add a WC mtrr if there
+ 	 * is a free mtrr slot.
+ 	 */
+	if (!pat_enabled && write_combine)
+		return -EINVAL;
+
 	if (pat_enabled && write_combine)
 		prot |= _PAGE_CACHE_WC;
 	else if (pat_enabled || boot_cpu_data.x86 > 3)
#12 lore raw
Quoted 9 lines · Suresh Siddha
Quoted 3 lines · Jesse Barnes

Here's a quick & dirty version, totally untested. A cleaner approach would be to separate the WC mapping routines and hide the return -EINVAL in arch specific code...

Jesse How about this patch? Doing this in x86 is cleaner.

I would like Acks/sign-offs-by Thomas, Eric and Jesse, if it is ok with this patch and works.

Hmm, at this point I already was more than a week ago:
http://marc.info/?l=linux-kernel&m=125537770514713&w=2

OK, I also modified ioremap() and set_memory_wc() but your patch is just part of what I did there...

And Eric Anholt answered:

Quoted 2 lines

Seems like we should install an MTRR instead. Requiring userland to set up the MTRR on the kernel's behalf is backwards.

Where I totally agree with him.

Regards,
Thomas

#13 lore raw
Quoted 5 lines · Thomas Schlichter

Hmm, at this point I already was more than a week ago:
http://marc.info/?l=linux-kernel&m=125537770514713&w=2

OK, I also modified ioremap() and set_memory_wc() but your patch is just part of what I did there...

oops! Sorry I read the later emails in this thread carefully but not the conversation with Thomas Hellstrom. Anyhow, glad that you were on top of this. Thanks.

Ingo, can you please queue this patch with a sign-off from Thomas Schlichter too?

thanks,
suresh

#14 lore raw
Quoted 7 lines · Suresh Siddha
Quoted 5 lines · Thomas Schlichter

Hmm, at this point I already was more than a week ago:
http://marc.info/?l=linux-kernel&m=125537770514713&w=2

OK, I also modified ioremap() and set_memory_wc() but your patch is just part of what I did there...

Quoted 6 lines

oops! Sorry I read the later emails in this thread carefully but not the conversation with Thomas Hellstrom. Anyhow, glad that you were on top of this. Thanks.

Ingo, can you please queue this patch with a sign-off from Thomas Schlichter too?

Please resend the latest with all the acks/signoffs added, with the subject line changed to '[PATCH] ...' and all people Cc:-ed.

Thanks,

Ingo