Skip to contentSkip to conversation

kmmio/mmiotrace: fix double free of kmmio_fault_pages

PATCH Superseded Marcin Slusarz 3 participants 5 messages Jun 5, 2010 – Jun 7, 2010

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kmmio/mmiotrace: fix double free of kmmio_fault_pages

After every iounmap mmiotrace has to free kmmio_fault_pages, but it can't do it directly, so it defers freeing by RCU.

It usually works, but when mmiotraced code calls ioremap-iounmap multiple times without sleeping between (so RCU won't kick in and start freeing) it can be given the same virtual address, so at every iounmap mmiotrace will schedule the same pages for release. Obviously it will explode on second free.

Fix it by marking kmmio_fault_pages which are scheduled for release and not adding them second time.

Cc Pekka Paalanen <[email protected]>
Cc Stuart Bennett <[email protected]>
---
 arch/x86/mm/kmmio.c |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/mm/kmmio.c +13 -3
index 5d0e67f..e5d5e2c 100644
@@ -45,6 +45,8 @@ struct kmmio_fault_page {
 	 * Protected by kmmio_lock, when linked into kmmio_page_table.
 	 */
 	int count;
+
+	bool scheduled_for_release;
 };
 
 struct kmmio_delayed_release {
@@ -398,8 +400,11 @@ static void release_kmmio_fault_page(unsigned long page,
 	BUG_ON(f->count < 0);
 	if (!f->count) {
 		disarm_kmmio_fault_page(f);
-		f->release_next = *release_list;
-		*release_list = f;
+		if (!f->scheduled_for_release) {
+			f->release_next = *release_list;
+			*release_list = f;
+			f->scheduled_for_release = true;
+		}
 	}
 }
 
@@ -471,8 +476,10 @@ static void remove_kmmio_fault_pages(struct rcu_head *head)
 			prevp = &f->release_next;
 		} else {
 			*prevp = f->release_next;
+			f->release_next = NULL;
+			f->scheduled_for_release = false;
 		}
-		f = f->release_next;
+		f = *prevp;
 	}
 	spin_unlock_irqrestore(&kmmio_lock, flags);
 
@@ -510,6 +517,9 @@ void unregister_kmmio_probe(struct kmmio_probe *p)
 	kmmio_count--;
 	spin_unlock_irqrestore(&kmmio_lock, flags);
 
+	if (!release_list)
+		return;
+
 	drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
 	if (!drelease) {
 		pr_crit("leaking kmmio_fault_page objects.\n");
Signature · 1 line
1.7.1
#1 lore raw
Quoted 9 lines · On Sat, 5 Jun 2010 18:49:42 +0200 Marcin Slusarz…

After every iounmap mmiotrace has to free kmmio_fault_pages, but it can't do it directly, so it defers freeing by RCU.

It usually works, but when mmiotraced code calls ioremap-iounmap multiple times without sleeping between (so RCU won't kick in and start freeing) it can be given the same virtual address, so at every iounmap mmiotrace will schedule the same pages for release. Obviously it will explode on second free.

Quoted 6 lines

Fix it by marking kmmio_fault_pages which are scheduled for release and not adding them second time.

Cc Pekka Paalanen <[email protected]>
Cc Stuart Bennett <[email protected]>

Excellent work! Unfortunately I cannot review this patch right now, I am sick. The description sounds good, though, and I have no objections.

Thank you very much!

Quoted 57 lines
---
 arch/x86/mm/kmmio.c |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/mm/kmmio.c +0 -0
index 5d0e67f..e5d5e2c 100644
@@ -45,6 +45,8 @@ struct kmmio_fault_page {
 	 * Protected by kmmio_lock, when linked into

kmmio_page_table. */

arch/x86/mm/kmmio.c +13 -3
 	int count;
+
+	bool scheduled_for_release;
 };
 
 struct kmmio_delayed_release {
@@ -398,8 +400,11 @@ static void
 release_kmmio_fault_page(unsigned long page, BUG_ON(f->count < 0);
 	if (!f->count) {
 		disarm_kmmio_fault_page(f);
-		f->release_next = *release_list;
-		*release_list = f;
+		if (!f->scheduled_for_release) {
+			f->release_next = *release_list;
+			*release_list = f;
+			f->scheduled_for_release = true;
+		}
 	}
 }
 
@@ -471,8 +476,10 @@ static void remove_kmmio_fault_pages(struct
 rcu_head *head) prevp = &f->release_next;
 		} else {
 			*prevp = f->release_next;
+			f->release_next = NULL;
+			f->scheduled_for_release = false;
 		}
-		f = f->release_next;
+		f = *prevp;
 	}
 	spin_unlock_irqrestore(&kmmio_lock, flags);
 
@@ -510,6 +517,9 @@ void unregister_kmmio_probe(struct
 kmmio_probe *p) kmmio_count--;
 	spin_unlock_irqrestore(&kmmio_lock, flags);
 
+	if (!release_list)
+		return;
+
 	drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
 	if (!drelease) {
 		pr_crit("leaking kmmio_fault_page objects.\n");
Signature · 1 line
1.7.1
Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/
#2 lore raw
In reply to Marcin Slusarz
Quoted 12 lines · Marcin Slusarz

After every iounmap mmiotrace has to free kmmio_fault_pages, but it can't do it directly, so it defers freeing by RCU.

It usually works, but when mmiotraced code calls ioremap-iounmap multiple times without sleeping between (so RCU won't kick in and start freeing) it can be given the same virtual address, so at every iounmap mmiotrace will schedule the same pages for release. Obviously it will explode on second free.

Fix it by marking kmmio_fault_pages which are scheduled for release and not adding them second time.

Attached patch for mmiotrace testing module allows to reliably reproduce the bug. It can be folded into the main patch.

---

arch/x86/mm/testmmiotrace.c +14 -0
index 8565d94..5f0937b 100644
@@ -90,6 +90,19 @@ static void do_test(unsigned long size)
 	iounmap(p);
 }
 
+static void do_test2(void)
+{
+	void __iomem *p;
+	int i;
+	
+	for (i = 0; i < 10; ++i) {
+		p = ioremap_nocache(mmio_address, 4096);
+		if (p)
+			iounmap(p);
+	}
+	synchronize_rcu(); /* will freeing work? */
+}
+
 static int __init init(void)
 {
 	unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
@@ -104,6 +117,7 @@ static int __init init(void)
 		   "and writing 16 kB of rubbish in there.\n",
 		   size >> 10, mmio_address);
 	do_test(size);
+	do_test2();
 	pr_info("All done.\n");
 	return 0;
 }
#3 lore raw
Quoted 43 lines · On Sat, 5 Jun 2010 21:33:01 +0200 Marcin Slusarz <[email protected]>…
Quoted 12 lines · Marcin Slusarz

After every iounmap mmiotrace has to free kmmio_fault_pages, but it can't do it directly, so it defers freeing by RCU.

It usually works, but when mmiotraced code calls ioremap-iounmap multiple times without sleeping between (so RCU won't kick in and start freeing) it can be given the same virtual address, so at every iounmap mmiotrace will schedule the same pages for release. Obviously it will explode on second free.

Fix it by marking kmmio_fault_pages which are scheduled for release and not adding them second time.

Attached patch for mmiotrace testing module allows to reliably reproduce the bug. It can be folded into the main patch.

---

(quoted diff) +0 -0

b/arch/x86/mm/testmmiotrace.c index 8565d94..5f0937b 100644

arch/x86/mm/testmmiotrace.c +13 -0
@@ -90,6 +90,19 @@ static void do_test(unsigned long size)
 	iounmap(p);
 }
 
+static void do_test2(void)
+{
+	void __iomem *p;
+	int i;
+	
+	for (i = 0; i < 10; ++i) {
+		p = ioremap_nocache(mmio_address, 4096);
+		if (p)
+			iounmap(p);
+	}
+	synchronize_rcu(); /* will freeing work? */
+}
+
 static int __init init(void)
 {
 	unsigned long size = (read_far) ? (8 << 20) : (16 << 10);

arch/x86/mm/testmmiotrace.c static int __init init(void) · line 123

Quoted 7 lines
arch/x86/mm/testmmiotrace.c +1 -0
@@ -104,6 +117,7 @@ static int __init init(void)
 		   "and writing 16 kB of rubbish in there.\n",
 		   size >> 10, mmio_address);
 	do_test(size);
+	do_test2();
 	pr_info("All done.\n");
 	return 0;
 }
Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/
#4 lore raw
In reply to Marcin Slusarz
Quoted 23 lines · Marcin Slusarz
Quoted 12 lines · Marcin Slusarz

After every iounmap mmiotrace has to free kmmio_fault_pages, but it can't do it directly, so it defers freeing by RCU.

It usually works, but when mmiotraced code calls ioremap-iounmap multiple times without sleeping between (so RCU won't kick in and start freeing) it can be given the same virtual address, so at every iounmap mmiotrace will schedule the same pages for release. Obviously it will explode on second free.

Fix it by marking kmmio_fault_pages which are scheduled for release and not adding them second time.

Attached patch for mmiotrace testing module allows to reliably reproduce the bug. It can be folded into the main patch.

---

arch/x86/mm/testmmiotrace.c +0 -0
index 8565d94..5f0937b 100644

arch/x86/mm/testmmiotrace.c static void do_test(unsigned long size) · line 93

Quoted 4 lines
arch/x86/mm/testmmiotrace.c +1 -0
@@ -90,6 +90,19 @@ static void do_test(unsigned long size)
 	iounmap(p);
 }
 
+static void do_test2(void)

Please add a comment about what the test function achieves.

arch/x86/mm/testmmiotrace.c static void do_test(unsigned long size) · line 99

Quoted 6 lines
arch/x86/mm/testmmiotrace.c +6 -0
+{
+	void __iomem *p;
+	int i;
+	
+	for (i = 0; i < 10; ++i) {
+		p = ioremap_nocache(mmio_address, 4096);

s/4096/PAGE_SIZE

Quoted 10 lines
arch/x86/mm/testmmiotrace.c +6 -0
+		if (p)
+			iounmap(p);
+	}
+	synchronize_rcu(); /* will freeing work? */
+}
+
 static int __init init(void)
 {
 	unsigned long size = (read_far) ? (8 << 20) : (16 << 10);

arch/x86/mm/testmmiotrace.c static int __init init(void) · line 120

Quoted 4 lines
arch/x86/mm/testmmiotrace.c +1 -0
@@ -104,6 +117,7 @@ static int __init init(void)
 		   "and writing 16 kB of rubbish in there.\n",
 		   size >> 10, mmio_address);
 	do_test(size);
+	do_test2();

Please name the new function in a bit more meaningful way (such as do_test_remap()).

Looks good, please send the full (folded back) patch anew, with Pekka's Ack in place as well.

Thanks,

Ingo