Skip to contentSkip to conversation

tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

PATCH In review Dan Magenheimer 3 participants 7 messages Feb 16, 2010 – Feb 18, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

(Sorry, I was testing with this tweak to my previously posted patch but had neglected to post it before the patch was taken.)

With tmem enabled, when available memory is scarce, don't allow order==0 pages to be taken from the "midsize alloc zone" but DO attempt to relinquish a page from tmem. Else many things fail when tmem has absorbed nearly all system memory.

xen/common/page_alloc.c +8 -3
diff -r 364001067e26 xen/common/page_alloc.c
@@ -311,9 +311,13 @@ static struct page_info *alloc_heap_page
      * TMEM: When available memory is scarce, allow only mid-size allocations
      * to avoid worst of fragmentation issues.
      */
-    if ( opt_tmem && ((order == 0) || (order >= 9)) &&
-         (total_avail_pages <= midsize_alloc_zone_pages) )
-        goto fail;
+    if ( opt_tmem && (total_avail_pages <= midsize_alloc_zone_pages) )
+    {
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;
+    }
 
     /*
      * Start with requested node, but exhaust all node memory in requested 
@@ -341,6 +345,7 @@ static struct page_info *alloc_heap_page
     }
 
     /* Try to free memory from tmem */
+try_tmem:
     if ( (pg = tmem_relinquish_pages(order,memflags)) != NULL )
     {
         /* reassigning an already allocated anonymous heap page */
#1 lore raw

xen/common/page_alloc.c static struct page_info *alloc_heap_page · line 319

Quoted 9 lines · Dan Magenheimer
xen/common/page_alloc.c +6 -3
-    if ( opt_tmem && ((order == 0) || (order >= 9)) &&
-         (total_avail_pages <= midsize_alloc_zone_pages) )
-        goto fail;
+    if ( opt_tmem && (total_avail_pages <= midsize_alloc_zone_pages) )
+    {
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;

Why not try_tmem in the case that order>=9, too, rather than fail outright?

-- Keir

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

Keir Fraser <[email protected]> 17.02.10 13:10 >>>

xen/common/page_alloc.c static struct page_info *alloc_heap_page · line 319

Quoted 9 lines · Dan Magenheimer
xen/common/page_alloc.c +6 -3
-    if ( opt_tmem && ((order == 0) || (order >= 9)) &&
-         (total_avail_pages <= midsize_alloc_zone_pages) )
-        goto fail;
+    if ( opt_tmem && (total_avail_pages <= midsize_alloc_zone_pages) )
+    {
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;
Quoted 1 line

Why not try_tmem in the case that order>=9, too, rather than fail outright?

It could be done that way, but wouldn't have any effect, as tmem doesn't even try to relinquish any memory when order > 0.

Jan

#3 lore raw
Quoted 13 lines

From: Jan Beulich [mailto:[email protected]]
Subject: Re: [PATCH] tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

Quoted 2 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 17.02.10 13:10 >>>

On 16/02/2010 18:30, "Dan Magenheimer" <[email protected]>

Quoted 6 lines · wrote:
Quoted 5 lines
xen/common/page_alloc.c +4 -0
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;
Quoted 5 lines
Quoted 1 line

Why not try_tmem in the case that order>=9, too, rather than fail

outright?

It could be done that way, but wouldn't have any effect, as tmem doesn't even try to relinquish any memory when order > 0.

Correct. To explain (if anyone is interested):

Tmem maintains queues of order==0 pages internally because if a page is released to the xenheap/domheap, it must be scrubbed. But tmem is highly likely to use the page again (and SOON). If tmem immediately reclaims the page, the scrubbing is wasted cycles. But if it does not and some other xenheap/domheap allocation obtains the page, the contents of an unscrubbed page could reveal data from another domain so would be a potential security hole.

When a domain is being created, a large number of pages may be (scrubbed and) transferred from tmem to Xen/domheap. While this transfer, in combination with the "buddying" in xenheap/domheap, may result in some order>0 chunks of memory, there is no guarantee that it will.

I considered adding some kind of "buddying" to tmem's "free" pages (and the interface to tmem_relinquish_pages() from alloc_heap_pages() allows for an order>0 to be requested), but due to fragmentation it would only rarely have any value, especially for order>1, so I never implemented it.

So, in the end, the real solution is to change any allocations in Xen, at least any allocations that occur after dom0 is running, to no longer require order>0 allocations.

Dan

#4 lore raw

Hi Keir --

Hmmm... one other consequence of the change you made to the patch (as checked in as 20955 in staging) is that every attempt to allocate any 2MB page for a new domain (when memory is scarce) will result in a complaint printk'ed from tmem_relinquish_pages() before the domain builder falls back to order==0 pages. This verbosity is probably not desirable in a product, though it may be very desirable with debug enabled as we track down other order>0 allocations.

Changing back to the "goto fail" avoids the verbosity without losing the debug capability.

Dan

Quoted 56 lines

-----Original Message-----
From: Dan Magenheimer
Sent: Wednesday, February 17, 2010 8:13 AM
To: Jan Beulich; Keir Fraser
Cc: [email protected]
Subject: RE: [PATCH] tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

Quoted 2 lines

From: Jan Beulich [mailto:[email protected]]
Subject: Re: [PATCH] tmem: fix to 20945 "When tmem is enabled,

reserve

Quoted 11 lines

a fraction of memory"

Quoted 2 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 17.02.10 13:10 >>>

On 16/02/2010 18:30, "Dan Magenheimer" <[email protected]>

Quoted 6 lines · wrote:
Quoted 5 lines
xen/common/page_alloc.c +4 -0
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;
Quoted 5 lines
Quoted 1 line

Why not try_tmem in the case that order>=9, too, rather than fail

outright?

It could be done that way, but wouldn't have any effect, as tmem doesn't even try to relinquish any memory when order > 0.

Correct. To explain (if anyone is interested):

Tmem maintains queues of order==0 pages internally because if a page is released to the xenheap/domheap, it must be scrubbed. But tmem is highly likely to use the page again (and SOON). If tmem immediately reclaims the page, the scrubbing is wasted cycles. But if it does not and some other xenheap/domheap allocation obtains the page, the contents of an unscrubbed page could reveal data from another domain so would be a potential security hole.

When a domain is being created, a large number of pages may be (scrubbed and) transferred from tmem to Xen/domheap. While this transfer, in combination with the "buddying" in xenheap/domheap, may result in some order>0 chunks of memory, there is no guarantee that it will.

I considered adding some kind of "buddying" to tmem's "free" pages (and the interface to tmem_relinquish_pages() from alloc_heap_pages() allows for an order>0 to be requested), but due to fragmentation it would only rarely have any value, especially for order>1, so I never implemented it.

So, in the end, the real solution is to change any allocations in Xen, at least any allocations that occur after dom0 is running, to no longer require order>0 allocations.

Dan

#5 lore raw

If you don't want verbosity in the failed-allocation path, remove your printk. Notice non-tmem code doesn't make a noise in this case. tmem_relinquish_pages() takes an order parameter, and the normal path through the allocator unconditionally calls it. Hence it doesn't make sense to logically separate order=0 from order>=9 in this case either, from the p.o.v. of the caller.

-- Keir

Quoted 72 lines · Dan Magenheimer

Hi Keir --

Hmmm... one other consequence of the change you made to the patch (as checked in as 20955 in staging) is that every attempt to allocate any 2MB page for a new domain (when memory is scarce) will result in a complaint printk'ed from tmem_relinquish_pages() before the domain builder falls back to order==0 pages. This verbosity is probably not desirable in a product, though it may be very desirable with debug enabled as we track down other order>0 allocations.

Changing back to the "goto fail" avoids the verbosity without losing the debug capability.

Dan

Quoted 56 lines

-----Original Message-----
From: Dan Magenheimer
Sent: Wednesday, February 17, 2010 8:13 AM
To: Jan Beulich; Keir Fraser
Cc: [email protected]
Subject: RE: [PATCH] tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

Quoted 2 lines

From: Jan Beulich [mailto:[email protected]]
Subject: Re: [PATCH] tmem: fix to 20945 "When tmem is enabled,

reserve

Quoted 11 lines

a fraction of memory"

Quoted 2 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 17.02.10 13:10 >>>

On 16/02/2010 18:30, "Dan Magenheimer" <[email protected]>

Quoted 6 lines · wrote:
Quoted 5 lines
xen/common/page_alloc.c +4 -0
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;
Quoted 5 lines
Quoted 1 line

Why not try_tmem in the case that order>=9, too, rather than fail

outright?

It could be done that way, but wouldn't have any effect, as tmem doesn't even try to relinquish any memory when order > 0.

Correct. To explain (if anyone is interested):

Tmem maintains queues of order==0 pages internally because if a page is released to the xenheap/domheap, it must be scrubbed. But tmem is highly likely to use the page again (and SOON). If tmem immediately reclaims the page, the scrubbing is wasted cycles. But if it does not and some other xenheap/domheap allocation obtains the page, the contents of an unscrubbed page could reveal data from another domain so would be a potential security hole.

When a domain is being created, a large number of pages may be (scrubbed and) transferred from tmem to Xen/domheap. While this transfer, in combination with the "buddying" in xenheap/domheap, may result in some order>0 chunks of memory, there is no guarantee that it will.

I considered adding some kind of "buddying" to tmem's "free" pages (and the interface to tmem_relinquish_pages() from alloc_heap_pages() allows for an order>0 to be requested), but due to fragmentation it would only rarely have any value, especially for order>1, so I never implemented it.

So, in the end, the real solution is to change any allocations in Xen, at least any allocations that occur after dom0 is running, to no longer require order>0 allocations.

Dan

#6 lore raw

Hmmm... OK, would you mind putting an #ifndef NDEBUG around the printk("...failing order...)" in tmem_relinquish_pages() for now then? Cleaning this up properly is too much surgery until post-4.0.

(Do you want an officially submitted patch?)

Quoted 99 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Thursday, February 18, 2010 12:38 AM
To: Dan Magenheimer; Jan Beulich
Cc: [email protected]
Subject: Re: [PATCH] tmem: fix to 20945 "When tmem is enabled, reserve a fraction of memory"

If you don't want verbosity in the failed-allocation path, remove your printk. Notice non-tmem code doesn't make a noise in this case. tmem_relinquish_pages() takes an order parameter, and the normal path through the allocator unconditionally calls it. Hence it doesn't make sense
to logically separate order=0 from order>=9 in this case either, from the
p.o.v. of the caller.

-- Keir

Quoted 11 lines · Dan Magenheimer

Hi Keir --

Hmmm... one other consequence of the change you made to the patch (as checked in as 20955 in staging) is that every attempt to allocate any 2MB page for a new domain (when memory is scarce) will result in a complaint printk'ed from tmem_relinquish_pages() before the domain builder falls back to order==0 pages. This verbosity is probably not desirable in a product, though it may be very desirable with debug enabled as we track down other order>0 allocations.

Quoted 11 lines

Changing back to the "goto fail" avoids the verbosity without losing the debug capability.

Dan

Quoted 6 lines

-----Original Message-----
From: Dan Magenheimer
Sent: Wednesday, February 17, 2010 8:13 AM
To: Jan Beulich; Keir Fraser
Cc: [email protected]
Subject: RE: [PATCH] tmem: fix to 20945 "When tmem is enabled,

reserve

Quoted 9 lines
Quoted 9 lines

a fraction of memory"

Quoted 2 lines

From: Jan Beulich [mailto:[email protected]]
Subject: Re: [PATCH] tmem: fix to 20945 "When tmem is enabled,

reserve

Quoted 4 lines

a fraction of memory"

Quoted 2 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 17.02.10 13:10 >>>

On 16/02/2010 18:30, "Dan Magenheimer"

<[email protected]>

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines · wrote:

xen/common/page_alloc.c static struct page_info *alloc_heap_page · line 319

Quoted 5 lines
xen/common/page_alloc.c +4 -0
+        if ( order == 0)
+            goto try_tmem;
+        if ( order >= 9)
+            goto fail;

Why not try_tmem in the case that order>=9, too, rather than fail

outright?

It could be done that way, but wouldn't have any effect, as tmem doesn't even try to relinquish any memory when order > 0.

Quoted 7 lines
Quoted 7 lines

Correct. To explain (if anyone is interested):

Tmem maintains queues of order==0 pages internally because if a page is released to the xenheap/domheap, it must be scrubbed. But tmem is highly likely to use the page again (and SOON). If tmem immediately reclaims the page, the scrubbing is wasted cycles. But if it does not and some other xenheap/domheap

allocation

Quoted 21 lines
Quoted 21 lines

obtains the page, the contents of an unscrubbed page could reveal data from another domain so would be a potential security hole.

When a domain is being created, a large number of pages may be (scrubbed and) transferred from tmem to Xen/domheap. While this transfer, in combination with the "buddying" in xenheap/domheap, may result in some order>0 chunks of memory, there is no guarantee that it will.

I considered adding some kind of "buddying" to tmem's "free" pages (and the interface to tmem_relinquish_pages() from alloc_heap_pages() allows for an order>0 to be requested), but due to fragmentation it would only rarely have any value, especially for order>1, so I never implemented it.

So, in the end, the real solution is to change any allocations in Xen, at least any allocations that occur after dom0 is running, to no longer require order>0 allocations.

Dan