Skip to contentSkip to conversation

walking the page lists needs the page_alloc lock

RFC PATCH In review Tim Deegan 3 participants 9 messages Jul 23, 2010 – Aug 13, 2010

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

RFC PATCH 1/1 walking the page lists needs the page_alloc lock

There are a few places in Xen where we walk a domain's page lists without holding the page_alloc lock. They race with updates to the page lists, which are normally rare but can be quite common under PoD when the domain is close to its memory limit and the PoD reclaimer is busy. This patch protects those places by taking the page_alloc lock.

I think this is OK for the two debug-key printouts - they don't run from irq context and look deadlock-free. The tboot change seems safe too unless tboot shutdown functions are called from irq context or with the page_alloc lock held. The p2m one is the scariest but there are already code paths in PoD that take the page_alloc lock with the p2m lock held so it's no worse than existing code.

xen/arch/x86/domain.c +4 -0
diff -r e8dbc1262f52 xen/arch/x86/domain.c
@@ -139,12 +139,14 @@ void dump_pageframe_info(struct domain *
     }
     else
     {
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each ( page, &d->page_list )
         {
             printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                    _p(page_to_mfn(page)),
                    page->count_info, page->u.inuse.type_info);
         }
+        spin_unlock(&d->page_alloc_lock);
     }
 
     if ( is_hvm_domain(d) )
@@ -152,12 +154,14 @@ void dump_pageframe_info(struct domain *
         p2m_pod_dump_data(d);
     }
 
+    spin_lock(&d->page_alloc_lock);
     page_list_for_each ( page, &d->xenpage_list )
     {
         printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                _p(page_to_mfn(page)),
                page->count_info, page->u.inuse.type_info);
     }
+    spin_unlock(&d->page_alloc_lock);
 }
 
 struct domain *alloc_domain_struct(void)
xen/arch/x86/mm/p2m.c +5 -1
diff -r e8dbc1262f52 xen/arch/x86/mm/p2m.c
@@ -1833,6 +1833,7 @@ int p2m_alloc_table(struct domain *d,
         goto error;
 
     /* Copy all existing mappings from the page list and m2p */
+    spin_lock(&d->page_alloc_lock);
     page_list_for_each(page, &d->page_list)
     {
         mfn = page_to_mfn(page);
@@ -1848,13 +1849,16 @@ int p2m_alloc_table(struct domain *d,
 #endif
              && gfn != INVALID_M2P_ENTRY
             && !set_p2m_entry(d, gfn, mfn, 0, p2m_ram_rw) )
-            goto error;
+            goto error_unlock;
     }
+    spin_unlock(&d->page_alloc_lock);
 
     P2M_PRINTK("p2m table initialised (%u pages)\n", page_count);
     p2m_unlock(p2m);
     return 0;
 
+error_unlock:
+    spin_unlock(&d->page_alloc_lock);
  error:
     P2M_PRINTK("failed to initialize p2m table, gfn=%05lx, mfn=%"
                PRI_mfn "\n", gfn, mfn_x(mfn));
xen/arch/x86/numa.c +2 -0
diff -r e8dbc1262f52 xen/arch/x86/numa.c
@@ -385,11 +385,13 @@ static void dump_numa(unsigned char key)
 		for_each_online_node(i)
 			page_num_node[i] = 0;
 
+		spin_lock(&d->page_alloc_lock);
 		page_list_for_each(page, &d->page_list)
 		{
 			i = phys_to_nid((paddr_t)page_to_mfn(page) << PAGE_SHIFT);
 			page_num_node[i]++;
 		}
+		spin_unlock(&d->page_alloc_lock);
 
 		for_each_online_node(i)
 			printk("    Node %u: %u\n", i, page_num_node[i]);
xen/arch/x86/tboot.c +2 -0
diff -r e8dbc1262f52 xen/arch/x86/tboot.c
@@ -211,12 +211,14 @@ static void tboot_gen_domain_integrity(c
             continue;
         printk("MACing Domain %u\n", d->domain_id);
 
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each(page, &d->page_list)
         {
             void *pg = __map_domain_page(page);
             vmac_update(pg, PAGE_SIZE, &ctx);
             unmap_domain_page(pg);
         }
+        spin_unlock(&d->page_alloc_lock);
 
         if ( !is_idle_domain(d) )
         {
author
#1 lore raw
Quoted 5 lines · Tim Deegan

There are a few places in Xen where we walk a domain's page lists without holding the page_alloc lock. They race with updates to the page lists, which are normally rare but can be quite common under PoD when the domain is close to its memory limit and the PoD reclaimer is busy. This patch protects those places by taking the page_alloc lock.

I should say that the other place I found is in construct_dom0(), which I left because it (a) can't really race with allocations and (b) calls process_pending_softirqs() within the page_list_for_each().

Tim.

Quoted 111 lines

I think this is OK for the two debug-key printouts - they don't run from irq context and look deadlock-free. The tboot change seems safe too unless tboot shutdown functions are called from irq context or with the page_alloc lock held. The p2m one is the scariest but there are already code paths in PoD that take the page_alloc lock with the p2m lock held so it's no worse than existing code.

diff -r e8dbc1262f52 xen/arch/x86/domain.c

xen/arch/x86/domain.c +4 -0
@@ -139,12 +139,14 @@ void dump_pageframe_info(struct domain *
     }
     else
     {
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each ( page, &d->page_list )
         {
             printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                    _p(page_to_mfn(page)),
                    page->count_info, page->u.inuse.type_info);
         }
+        spin_unlock(&d->page_alloc_lock);
     }
 
     if ( is_hvm_domain(d) )
@@ -152,12 +154,14 @@ void dump_pageframe_info(struct domain *
         p2m_pod_dump_data(d);
     }
 
+    spin_lock(&d->page_alloc_lock);
     page_list_for_each ( page, &d->xenpage_list )
     {
         printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                _p(page_to_mfn(page)),
                page->count_info, page->u.inuse.type_info);
     }
+    spin_unlock(&d->page_alloc_lock);
 }
 
 struct domain *alloc_domain_struct(void)

diff -r e8dbc1262f52 xen/arch/x86/mm/p2m.c

xen/arch/x86/mm/p2m.c +5 -1
@@ -1833,6 +1833,7 @@ int p2m_alloc_table(struct domain *d,
         goto error;
 
     /* Copy all existing mappings from the page list and m2p */
+    spin_lock(&d->page_alloc_lock);
     page_list_for_each(page, &d->page_list)
     {
         mfn = page_to_mfn(page);
@@ -1848,13 +1849,16 @@ int p2m_alloc_table(struct domain *d,
 #endif
              && gfn != INVALID_M2P_ENTRY
             && !set_p2m_entry(d, gfn, mfn, 0, p2m_ram_rw) )
-            goto error;
+            goto error_unlock;
     }
+    spin_unlock(&d->page_alloc_lock);
 
     P2M_PRINTK("p2m table initialised (%u pages)\n", page_count);
     p2m_unlock(p2m);
     return 0;
 
+error_unlock:
+    spin_unlock(&d->page_alloc_lock);
  error:
     P2M_PRINTK("failed to initialize p2m table, gfn=%05lx, mfn=%"
                PRI_mfn "\n", gfn, mfn_x(mfn));

diff -r e8dbc1262f52 xen/arch/x86/numa.c

xen/arch/x86/numa.c +2 -0
@@ -385,11 +385,13 @@ static void dump_numa(unsigned char key)
 		for_each_online_node(i)
 			page_num_node[i] = 0;
 
+		spin_lock(&d->page_alloc_lock);
 		page_list_for_each(page, &d->page_list)
 		{
 			i = phys_to_nid((paddr_t)page_to_mfn(page) << PAGE_SHIFT);
 			page_num_node[i]++;
 		}
+		spin_unlock(&d->page_alloc_lock);
 
 		for_each_online_node(i)
 			printk("    Node %u: %u\n", i, page_num_node[i]);

diff -r e8dbc1262f52 xen/arch/x86/tboot.c

xen/arch/x86/tboot.c +2 -0
@@ -211,12 +211,14 @@ static void tboot_gen_domain_integrity(c
             continue;
         printk("MACing Domain %u\n", d->domain_id);
 
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each(page, &d->page_list)
         {
             void *pg = __map_domain_page(page);
             vmac_update(pg, PAGE_SIZE, &ctx);
             unmap_domain_page(pg);
         }
+        spin_unlock(&d->page_alloc_lock);
 
         if ( !is_idle_domain(d) )
         {

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
In reply to Tim Deegan
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Tim Deegan

There are a few places in Xen where we walk a domain's page lists without holding the page_alloc lock. They race with updates to the page lists, which are normally rare but can be quite common under PoD when the domain is close to its memory limit and the PoD reclaimer is busy. This patch protects those places by taking the page_alloc lock.

I think this is OK for the two debug-key printouts - they don't run from irq context and look deadlock-free. The tboot change seems safe too

While the comment says the patch would leave debug key printouts alone, ...

xen/arch/x86/domain.c void dump_pageframe_info(struct domain *

Quoted 11 lines

unless tboot shutdown functions are called from irq context or with the page_alloc lock held. The p2m one is the scariest but there are already code paths in PoD that take the page_alloc lock with the p2m lock held so it's no worse than existing code.

diff -r e8dbc1262f52 xen/arch/x86/domain.c

xen/arch/x86/domain.c +0 -0
@@ -139,12 +139,14 @@ void dump_pageframe_info(struct domain *

... the actual patch still touches a respective function. It would seem to me that this part ought to be reverted.

Quoted 19 lines
xen/arch/x86/domain.c +3 -0
     }
     else
     {
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each ( page, &d->page_list )
         {
             printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                    _p(page_to_mfn(page)),
                    page->count_info, page->u.inuse.type_info);
         }
+        spin_unlock(&d->page_alloc_lock);
     }
 
     if ( is_hvm_domain(d) )
@@ -152,12 +154,14 @@ void dump_pageframe_info(struct domain *
         p2m_pod_dump_data(d);
     }
 
+    spin_lock(&d->page_alloc_lock);

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 167

Quoted 10 lines
xen/arch/x86/domain.c +1 -0
     page_list_for_each ( page, &d->xenpage_list )
     {
         printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                _p(page_to_mfn(page)),
                page->count_info, page->u.inuse.type_info);
     }
+    spin_unlock(&d->page_alloc_lock);
 }
 
 struct domain *alloc_domain_struct(void)

Sorry for not noticing this earlier.

Jan

author
#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Tim Deegan

There are a few places in Xen where we walk a domain's page lists without holding the page_alloc lock. They race with updates to the page lists, which are normally rare but can be quite common under PoD when the domain is close to its memory limit and the PoD reclaimer is busy. This patch protects those places by taking the page_alloc lock.

Quoted 5 lines
Quoted 2 lines

I think this is OK for the two debug-key printouts - they don't run from irq context and look deadlock-free. The tboot change seems safe too

While the comment says the patch would leave debug key printouts alone, ...

Sorry, my intention was to say that changes to the debug-key printouts are safe, not that they didn't require changes.

The debug-key printouts (in particular the NUMA one) are where I actually hit this bug on a running system.

Tim.

Quoted 49 lines

xen/arch/x86/domain.c void dump_pageframe_info(struct domain *

Quoted 11 lines

unless tboot shutdown functions are called from irq context or with the page_alloc lock held. The p2m one is the scariest but there are already code paths in PoD that take the page_alloc lock with the p2m lock held so it's no worse than existing code.

diff -r e8dbc1262f52 xen/arch/x86/domain.c

xen/arch/x86/domain.c +0 -0
@@ -139,12 +139,14 @@ void dump_pageframe_info(struct domain *

... the actual patch still touches a respective function. It would seem to me that this part ought to be reverted.

Quoted 19 lines
xen/arch/x86/domain.c +3 -0
     }
     else
     {
+        spin_lock(&d->page_alloc_lock);
         page_list_for_each ( page, &d->page_list )
         {
             printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                    _p(page_to_mfn(page)),
                    page->count_info, page->u.inuse.type_info);
         }
+        spin_unlock(&d->page_alloc_lock);
     }
 
     if ( is_hvm_domain(d) )
@@ -152,12 +154,14 @@ void dump_pageframe_info(struct domain *
         p2m_pod_dump_data(d);
     }
 
+    spin_lock(&d->page_alloc_lock);

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 167

Quoted 10 lines
xen/arch/x86/domain.c +1 -0
     page_list_for_each ( page, &d->xenpage_list )
     {
         printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
                _p(page_to_mfn(page)),
                page->count_info, page->u.inuse.type_info);
     }
+    spin_unlock(&d->page_alloc_lock);
 }
 
 struct domain *alloc_domain_struct(void)

Sorry for not noticing this earlier.

Jan

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#4 lore raw
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Tim Deegan

There are a few places in Xen where we walk a domain's page lists without holding the page_alloc lock. They race with updates to the page lists, which are normally rare but can be quite common under PoD when the domain is close to its memory limit and the PoD reclaimer is busy. This patch protects those places by taking the page_alloc lock.

I think this is OK for the two debug-key printouts - they don't run from irq context and look deadlock-free. The tboot change seems safe too

Quoted 8 lines
Quoted 2 lines

While the comment says the patch would leave debug key printouts alone, ...

Sorry, my intention was to say that changes to the debug-key printouts are safe, not that they didn't require changes.

The debug-key printouts (in particular the NUMA one) are where I actually hit this bug on a running system.

But then, to avoid a hanging system, these should be trylock-s rather than plain locks, shouldn't they?

Jan

#5 lore raw
Quoted 8 lines · Jan Beulich
Quoted 5 lines

Sorry, my intention was to say that changes to the debug-key printouts are safe, not that they didn't require changes.

The debug-key printouts (in particular the NUMA one) are where I actually hit this bug on a running system.

But then, to avoid a hanging system, these should be trylock-s rather than plain locks, shouldn't they?

Why? The handler is called in softirq context. It should be safe to spin.

-- Keir

#6 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 3 lines · Jan Beulich
Quoted 3 lines

Sorry, my intention was to say that changes to the debug-key printouts are safe, not that they didn't require changes.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The debug-key printouts (in particular the NUMA one) are where I actually hit this bug on a running system.

But then, to avoid a hanging system, these should be trylock-s rather than plain locks, shouldn't they?

Why? The handler is called in softirq context. It should be safe to spin.

Hmm, indeed. I was looking at others, and at least domain_dump_evtchn_info() also uses a trylock - apparently for no good reason.

Jan

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

But then, to avoid a hanging system, these should be trylock-s rather than plain locks, shouldn't they?

Why? The handler is called in softirq context. It should be safe to spin.

Hmm, indeed. I was looking at others, and at least domain_dump_evtchn_info() also uses a trylock - apparently for no good reason.

Well, since you wrote that function, would you like me to switch domain_dump_evtchn_info() to do a proper spin_lock()?

-- Keir

#8 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 2 lines

But then, to avoid a hanging system, these should be trylock-s rather than plain locks, shouldn't they?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Why? The handler is called in softirq context. It should be safe to spin.

Hmm, indeed. I was looking at others, and at least domain_dump_evtchn_info() also uses a trylock - apparently for no good reason.

Well, since you wrote that function, would you like me to switch domain_dump_evtchn_info() to do a proper spin_lock()?

Yes. I'd want to do a little cleanup to the initial printk()-s at once, like in the patch below.

Jan

xen/common/event_channel.c +4 -7
@@ -1123,14 +1123,11 @@ static void domain_dump_evtchn_info(stru
 
     bitmap_scnlistprintf(keyhandler_scratch, sizeof(keyhandler_scratch),
                          d->poll_mask, d->max_vcpus);
-    printk("Domain %d polling vCPUs: {%s}\n",
-           d->domain_id, keyhandler_scratch);
-
-    if ( !spin_trylock(&d->event_lock) )
-        return;
-
     printk("Event channel information for domain %d:\n"
-           "    port [p/m]\n", d->domain_id);
+           "Polling vCPUs: {%s}\n"
+           "    port [p/m]\n", d->domain_id, keyhandler_scratch);
+
+    spin_lock(&d->event_lock);
 
     for ( port = 1; port < MAX_EVTCHNS(d); ++port )
     {