Skip to contentSkip to conversation

mm: Safe to clear PGC_allocated on xenheap pages without an extra reference

PATCH In review George Dunlap 3 participants 4 messages Aug 2, 2019 – Aug 6, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Safe to clear PGC_allocated on xenheap pages without an extra reference

Commits ec83f825627 "mm.h: add helper function to test-and-clear _PGC_allocated" (and subsequent fix-up 44a887d021d "mm.h: fix BUG_ON() condition in put_page_alloc_ref()") introduced a BUG_ON() to detect unsafe behavior of callers.

Unfortunately this condition still turns out to be too strict. xenheap pages are somewhat "magic": calling free_domheap_pages() on them will not cause free_heap_pages() to be called: whichever part of Xen allocated them specially must call free_xenheap_pages() specifically. (They'll also be handled appropriately at domain destruction time.)

Only crash Xen when put_page_alloc_ref() finds only a single refcount if the page is not a xenheap page.

---
NB this has been compile-tested only.

CC Andrew Cooper <[email protected]>
CC Jan Beulich <[email protected]>
CC Paul Durrant <[email protected]>
---
 xen/include/xen/mm.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/include/xen/mm.h +11 -6
index 977e45aae7..297fc6ad7b 100644
@@ -666,15 +666,20 @@ static inline void share_xen_page_with_privileged_guests(
 static inline void put_page_alloc_ref(struct page_info *page)
 {
     /*
-     * Whenever a page is assigned to a domain then the _PGC_allocated bit
-     * is set and the reference count is set to at least 1. This function
-     * clears that 'allocation reference' but it is unsafe to do so without
-     * the caller holding an additional reference. I.e. the allocation
-     * reference must never be the last reference held.
+     * Whenever a page is assigned to a domain then the _PGC_allocated
+     * bit is set and the reference count is set to at least 1. This
+     * function clears that 'allocation reference' but it is unsafe to
+     * do so to domheap pages without the caller holding an additional
+     * reference. I.e. the allocation reference must never be the last
+     * reference held.
+     * 
+     * (It's safe for xenheap pages, because put_page() will not cause
+     * them to be freed.)
      */
     if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
     {
-        BUG_ON((page->count_info & PGC_count_mask) <= 1);
+        BUG_ON((page->count_info & PGC_count_mask) <= 1 &&
+               !(page->count_info & PGC_xen_heap));
         put_page(page);
     }
 }
Signature · 7 lines
2.20.1


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

Commits ec83f825627 "mm.h: add helper function to test-and-clear _PGC_allocated" (and subsequent fix-up 44a887d021d "mm.h: fix BUG_ON() condition in put_page_alloc_ref()") introduced a BUG_ON() to detect unsafe behavior of callers.

Unfortunately this condition still turns out to be too strict. xenheap pages are somewhat "magic": calling free_domheap_pages() on them will not cause free_heap_pages() to be called: whichever part of Xen allocated them specially must call free_xenheap_pages() specifically. (They'll also be handled appropriately at domain destruction time.)

Quoted 4 lines

Only crash Xen when put_page_alloc_ref() finds only a single refcount if the page is not a xenheap page.

This works for me, so Tested-by: Andrew Cooper <[email protected]>

Some suggestions however...

Quoted 20 lines

---
NB this has been compile-tested only.

CC Andrew Cooper <[email protected]>
CC Jan Beulich <[email protected]>
CC Paul Durrant <[email protected]>
---
 xen/include/xen/mm.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/include/xen/mm.h +0 -2
index 977e45aae7..297fc6ad7b 100644
@@ -666,15 +666,20 @@ static inline void share_xen_page_with_privileged_guests(
 static inline void put_page_alloc_ref(struct page_info *page)
 {
     /*
-     * Whenever a page is assigned to a domain then the _PGC_allocated bit
-     * is set and the reference count is set to at least 1. This function

xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 675

Quoted 10 lines
xen/include/xen/mm.h +7 -3
-     * clears that 'allocation reference' but it is unsafe to do so without
-     * the caller holding an additional reference. I.e. the allocation
-     * reference must never be the last reference held.
+     * Whenever a page is assigned to a domain then the _PGC_allocated
+     * bit is set and the reference count is set to at least 1. This
+     * function clears that 'allocation reference' but it is unsafe to
+     * do so to domheap pages without the caller holding an additional
+     * reference. I.e. the allocation reference must never be the last
+     * reference held.
+     * 

Trailing whitespace.

xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 682

Quoted 8 lines
xen/include/xen/mm.h +4 -1
+     * (It's safe for xenheap pages, because put_page() will not cause
+     * them to be freed.)
      */
     if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
     {
-        BUG_ON((page->count_info & PGC_count_mask) <= 1);
+        BUG_ON((page->count_info & PGC_count_mask) <= 1 &&
+               !(page->count_info & PGC_xen_heap));

I know this is a matter of preference, but I think the logic would be easier to follow as

BUG_ON(!(page->count_info & PGC_xen_heap) &&
       (page->count_info & PGC_count_mask) <= 1);

i.e. check for domheap pages before looking at the refcount.

~Andrew

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to George Dunlap
Quoted 12 lines · George Dunlap

Commits ec83f825627 "mm.h: add helper function to test-and-clear _PGC_allocated" (and subsequent fix-up 44a887d021d "mm.h: fix BUG_ON() condition in put_page_alloc_ref()") introduced a BUG_ON() to detect unsafe behavior of callers.

Unfortunately this condition still turns out to be too strict. xenheap pages are somewhat "magic": calling free_domheap_pages() on them will not cause free_heap_pages() to be called: whichever part of Xen allocated them specially must call free_xenheap_pages() specifically. (They'll also be handled appropriately at domain destruction time.)

Quoted 4 lines

Only crash Xen when put_page_alloc_ref() finds only a single refcount if the page is not a xenheap page.

albeit with a suggestion:

Quoted 16 lines
xen/include/xen/mm.h +5 -5
@@ -666,15 +666,20 @@ static inline void share_xen_page_with_privileged_guests(
  static inline void put_page_alloc_ref(struct page_info *page)
  {
      /*
-     * Whenever a page is assigned to a domain then the _PGC_allocated bit
-     * is set and the reference count is set to at least 1. This function
-     * clears that 'allocation reference' but it is unsafe to do so without
-     * the caller holding an additional reference. I.e. the allocation
-     * reference must never be the last reference held.
+     * Whenever a page is assigned to a domain then the _PGC_allocated
+     * bit is set and the reference count is set to at least 1. This
+     * function clears that 'allocation reference' but it is unsafe to
+     * do so to domheap pages without the caller holding an additional
+     * reference. I.e. the allocation reference must never be the last

xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 682

Quoted 10 lines
xen/include/xen/mm.h +6 -1
+     * reference held.
+     *
+     * (It's safe for xenheap pages, because put_page() will not cause
+     * them to be freed.)
       */
      if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
      {
-        BUG_ON((page->count_info & PGC_count_mask) <= 1);
+        BUG_ON((page->count_info & PGC_count_mask) <= 1 &&
+               !(page->count_info & PGC_xen_heap));

This can be had with a single conditional:

         BUG_ON((page->count_info & (PGC_xen_heap | PGC_count_mask)) <= 1);

Jan

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

#3 lore raw
Quoted 50 lines · Jan Beulich
Quoted 12 lines · George Dunlap

Commits ec83f825627 "mm.h: add helper function to test-and-clear _PGC_allocated" (and subsequent fix-up 44a887d021d "mm.h: fix BUG_ON() condition in put_page_alloc_ref()") introduced a BUG_ON() to detect unsafe behavior of callers.

Unfortunately this condition still turns out to be too strict. xenheap pages are somewhat "magic": calling free_domheap_pages() on them will not cause free_heap_pages() to be called: whichever part of Xen allocated them specially must call free_xenheap_pages() specifically. (They'll also be handled appropriately at domain destruction time.)

Quoted 4 lines

Only crash Xen when put_page_alloc_ref() finds only a single refcount if the page is not a xenheap page.

albeit with a suggestion:

Quoted 16 lines
xen/include/xen/mm.h +5 -5
@@ -666,15 +666,20 @@ static inline void share_xen_page_with_privileged_guests(
  static inline void put_page_alloc_ref(struct page_info *page)
  {
      /*
-     * Whenever a page is assigned to a domain then the _PGC_allocated bit
-     * is set and the reference count is set to at least 1. This function
-     * clears that 'allocation reference' but it is unsafe to do so without
-     * the caller holding an additional reference. I.e. the allocation
-     * reference must never be the last reference held.
+     * Whenever a page is assigned to a domain then the _PGC_allocated
+     * bit is set and the reference count is set to at least 1. This
+     * function clears that 'allocation reference' but it is unsafe to
+     * do so to domheap pages without the caller holding an additional
+     * reference. I.e. the allocation reference must never be the last

xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 682

Quoted 10 lines
xen/include/xen/mm.h +6 -1
+     * reference held.
+     *
+     * (It's safe for xenheap pages, because put_page() will not cause
+     * them to be freed.)
       */
      if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
      {
-        BUG_ON((page->count_info & PGC_count_mask) <= 1);
+        BUG_ON((page->count_info & PGC_count_mask) <= 1 &&
+               !(page->count_info & PGC_xen_heap));

This can be had with a single conditional:

Quoted 1 line
         BUG_ON((page->count_info & (PGC_xen_heap | PGC_count_mask)) <= 1);

I'll commit this version. Thanks.

-George

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