Skip to contentSkip to conversation

x86/mm: Add debug code to detect illegal page_lock and put_page_type ordering

PATCH Superseded George Dunlap 3 participants 4 messages Jan 24, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: Add debug code to detect illegal page_lock and put_page_type ordering

The fix for XSA-242 depends on the same cpu never calling _put_page_type() while holding a page_lock() for that page. By having no locking discipline between pages, the current code also assumes that we will never lock two pages on the same cpu.

Add a check to debug builds to verify that both of these are true.

---
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/mm.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
xen/arch/x86/mm.c +31 -0
index 5a1b472432..762e21fb40 100644
@@ -1813,10 +1813,25 @@ static int free_l4_table(struct page_info *page)
     return rc;
 }
 
+#ifndef NDEBUG
+/*
+ * Check to make sure that we never nest page_lock() calls on a single
+ * cpu (which may deadlock if two cpus attempt to lock the same pages
+ * in a different order), and that we never call _put_page_type() on a
+ * page while we hold its page_lock() (which would deadlock after
+ * XSA-242).
+ */
+static DEFINE_PER_CPU(struct page_info *, current_page_lock);
+#endif
+
 int page_lock(struct page_info *page)
 {
     unsigned long x, nx;
 
+#ifndef NDEBUG
+    ASSERT(this_cpu(current_page_lock) == NULL);
+#endif
+
     do {
         while ( (x = page->u.inuse.type_info) & PGT_locked )
             cpu_relax();
@@ -1827,6 +1842,10 @@ int page_lock(struct page_info *page)
             return 0;
     } while ( cmpxchg(&page->u.inuse.type_info, x, nx) != x );
 
+#ifndef NDEBUG
+    this_cpu(current_page_lock) = page;
+#endif
+
     return 1;
 }
 
@@ -1834,6 +1853,10 @@ void page_unlock(struct page_info *page)
 {
     unsigned long x, nx, y = page->u.inuse.type_info;
 
+#ifndef NDEBUG
+    ASSERT(this_cpu(current_page_lock) == page);
+#endif
+
     do {
         x = y;
         ASSERT((x & PGT_count_mask) && (x & PGT_locked));
@@ -1842,6 +1865,10 @@ void page_unlock(struct page_info *page)
         /* We must not drop the last reference here. */
         ASSERT(nx & PGT_count_mask);
     } while ( (y = cmpxchg(&page->u.inuse.type_info, x, nx)) != x );
+
+#ifndef NDEBUG
+    this_cpu(current_page_lock) = NULL;
+#endif
 }
 
 /*
@@ -2420,6 +2447,10 @@ static int _put_page_type(struct page_info *page, bool preemptible,
 {
     unsigned long nx, x, y = page->u.inuse.type_info;
 
+#ifndef NDEBUG
+    ASSERT(this_cpu(current_page_lock) != page);
+#endif
+
     for ( ; ; )
     {
         x  = y;
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 8 lines · George Dunlap

The fix for XSA-242 depends on the same cpu never calling _put_page_type() while holding a page_lock() for that page. By having no locking discipline between pages, the current code also assumes that we will never lock two pages on the same cpu.

Add a check to debug builds to verify that both of these are true.

Is there any requirement or expectation that we only ever lock a single page at once?

If yes, then this check needs to be more than just a debug check.  If not, then the code as presented may falsely explode.

Independently of this, it would probably be cleaner to split these out into predicates to avoid all the ifdefary.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
In reply to George Dunlap
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 13:48, <[email protected]> wrote:

The fix for XSA-242 depends on the same cpu never calling _put_page_type() while holding a page_lock() for that page. By having no locking discipline between pages, the current code also assumes that we will never lock two pages on the same cpu.

Add a check to debug builds to verify that both of these are true.

Generally fine, but way too much #ifdef-ary for my taste (see below).

Quoted 5 lines
xen/arch/x86/mm.c +0 -0
@@ -1813,10 +1813,25 @@ static int free_l4_table(struct page_info *page)
     return rc;
 }

xen/arch/x86/mm.c static int free_l4_table(struct page_info *page) · line 1824

Quoted 10 lines
xen/arch/x86/mm.c +9 -0
 
+#ifndef NDEBUG
+/*
+ * Check to make sure that we never nest page_lock() calls on a single
+ * cpu (which may deadlock if two cpus attempt to lock the same pages
+ * in a different order), and that we never call _put_page_type() on a
+ * page while we hold its page_lock() (which would deadlock after
+ * XSA-242).
+ */
+static DEFINE_PER_CPU(struct page_info *, current_page_lock);

current_locked_page or current_page_locked? The way you have it, the variable name rather suggests some kind of lock.

xen/arch/x86/mm.c static int free_l4_table(struct page_info *page) · line 1833

Quoted 9 lines
xen/arch/x86/mm.c +5 -0
+#endif
+
 int page_lock(struct page_info *page)
 {
     unsigned long x, nx;
 
+#ifndef NDEBUG
+    ASSERT(this_cpu(current_page_lock) == NULL);
+#endif
    ASSERT(!get_current_locked_page());

xen/arch/x86/mm.c int page_lock(struct page_info *page) · line 1847

Quoted 7 lines
xen/arch/x86/mm.c +3 -0
@@ -1827,6 +1842,10 @@ int page_lock(struct page_info *page)
             return 0;
     } while ( cmpxchg(&page->u.inuse.type_info, x, nx) != x );
 
+#ifndef NDEBUG
+    this_cpu(current_page_lock) = page;
+#endif
    set_current_locked_page(page);

... with suitable wrappers (empty in the non-debug case) defined.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · George Dunlap

The fix for XSA-242 depends on the same cpu never calling _put_page_type() while holding a page_lock() for that page. By having no locking discipline between pages, the current code also assumes that we will never lock two pages on the same cpu.

Quoted 6 lines
Quoted 3 lines

Add a check to debug builds to verify that both of these are true.

Is there any requirement or expectation that we only ever lock a single page at once?

Every page effectively has its own lock, and AFAICT there is no locking discipline on pages. So if a single pcpu ever locks two pages we have the potential for a classic deadlock situation:
P1: Lock A
P2: Lock B
P1: Spin-and-wait for lock B
P2: Spin-and-wait for lock A #

Quoted 2 lines

If yes, then this check needs to be more than just a debug check.  If not, then the code as presented may falsely explode.

Well there may come a day when we do grab two locks on a single pcpu, but in such a way as there can never be a circular dependency. Technically that would be a false positive, but I would argue that the ASSERT() will still be doing its job, alerting us to the fact that we need to think carefully about the new circumstances, update the documentation, and update the assert.

Personally I'm happy to just document the requirement and invoke an ASSERT(). If we want to be more rigorous about making sure there are no triggerable deadlocks, one option might be to have page_lock() return 0 if this cpu already holds another lock. (Haven't looked at that in detail.)

One of the things I'm more worried about is the requirement ever to call put_page_type() on a page if you already hold the page_lock() for that page. Jan argued that when called together they "generally" act on different pages, but that's not the kind of word you normally want to use when you're talking about security issues.

Quoted 2 lines

Independently of this, it would probably be cleaner to split these out into predicates to avoid all the ifdefary.

Yes, I wasn't happy with those either -- I'll take Jan's suggestion unless you have a better one.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel