]> xenbits.xensource.com Git - people/royger/xen.git/commitdiff
mm.h: add helper function to test-and-clear _PGC_allocated
authorPaul Durrant <paul.durrant@citrix.com>
Tue, 16 Jul 2019 07:10:36 +0000 (09:10 +0200)
committerJan Beulich <jbeulich@suse.com>
Tue, 16 Jul 2019 07:10:36 +0000 (09:10 +0200)
The _PGC_allocated flag is set on a page when it is assigned to a domain
along with an initial reference count of at least 1. To clear this
'allocation' reference it is necessary to test-and-clear _PGC_allocated and
then only drop the reference if the test-and-clear succeeds. This is open-
coded in many places. It is also unsafe to test-and-clear _PGC_allocated
unless the caller holds an additional reference.

This patch adds a helper function, put_page_alloc_ref(), to replace all the
open-coded test-and-clear/put_page occurrences. That helper function
incorporates a check that an additional page reference is held and will
BUG() if it is not.

Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
xen/arch/arm/domain.c
xen/arch/x86/domain.c
xen/arch/x86/hvm/ioreq.c
xen/arch/x86/mm.c
xen/arch/x86/mm/mem_sharing.c
xen/arch/x86/mm/p2m-pod.c
xen/arch/x86/mm/p2m.c
xen/common/grant_table.c
xen/common/memory.c
xen/common/xenoprof.c
xen/include/xen/mm.h

index 4f44d5c742b99255d6e8594992b11f3549510747..941bbff4fe45544008453bec638879047b9f9860 100644 (file)
@@ -926,9 +926,7 @@ static int relinquish_memory(struct domain *d, struct page_list_head *list)
              */
             continue;
 
-        if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-            put_page(page);
-
+        put_page_alloc_ref(page);
         put_page(page);
 
         if ( hypercall_preempt_check() )
index 147f96a09efe53d6219d144417867ffd5a98d03f..e791d86892b3833e65d6c088ceee4ad6db325b00 100644 (file)
@@ -1939,8 +1939,7 @@ static int relinquish_memory(
             BUG();
         }
 
-        if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-            put_page(page);
+        put_page_alloc_ref(page);
 
         /*
          * Forcibly invalidate top-most, still valid page tables at this point
index 7a80cfb28b9369bc3f2b092f2c8bed0bdc4a7d87..a79cabb6803976d226d97c0b3aa290f1128c5e18 100644 (file)
@@ -398,8 +398,7 @@ static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
     return 0;
 
  fail:
-    if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-        put_page(page);
+    put_page_alloc_ref(page);
     put_page_and_type(page);
 
     return -ENOMEM;
@@ -418,13 +417,7 @@ static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
     unmap_domain_page_global(iorp->va);
     iorp->va = NULL;
 
-    /*
-     * Check whether we need to clear the allocation reference before
-     * dropping the explicit references taken by get_page_and_type().
-     */
-    if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-        put_page(page);
-
+    put_page_alloc_ref(page);
     put_page_and_type(page);
 }
 
index df2c0130f1f6a47f2df63bb30def73db9b811c1d..138662e7778bed710b28f711d1d98684ba0285fa 100644 (file)
@@ -498,8 +498,7 @@ void share_xen_page_with_guest(struct page_info *page, struct domain *d,
 
 void free_shared_domheap_page(struct page_info *page)
 {
-    if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-        put_page(page);
+    put_page_alloc_ref(page);
     if ( !test_and_clear_bit(_PGC_xen_heap, &page->count_info) )
         ASSERT_UNREACHABLE();
     page->u.inuse.type_info = 0;
index f16a3f532443a9830ec18ad283b4437648b75b2a..58d9157fa82d12b71a4604e24c811ee11c12353e 100644 (file)
@@ -1000,8 +1000,7 @@ static int share_pages(struct domain *sd, gfn_t sgfn, shr_handle_t sh,
     mem_sharing_page_unlock(firstpg);
 
     /* Free the client page */
-    if(test_and_clear_bit(_PGC_allocated, &cpage->count_info))
-        put_page(cpage);
+    put_page_alloc_ref(cpage);
     put_page(cpage);
 
     /* We managed to free a domain page. */
@@ -1082,8 +1081,7 @@ int mem_sharing_add_to_physmap(struct domain *sd, unsigned long sgfn, shr_handle
                     ret = -EOVERFLOW;
                     goto err_unlock;
                 }
-                if ( test_and_clear_bit(_PGC_allocated, &cpage->count_info) )
-                    put_page(cpage);
+                put_page_alloc_ref(cpage);
                 put_page(cpage);
             }
         }
@@ -1177,8 +1175,7 @@ int __mem_sharing_unshare_page(struct domain *d,
                 domain_crash(d);
                 return -EOVERFLOW;
             }
-            if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-                put_page(page);
+            put_page_alloc_ref(page);
             put_page(page);
         }
         put_gfn(d, gfn);
index 431386306666a2a47a3a79ac61c5ae238510b6d1..096e2773fb5a913b2c70877f0e0affd8dae42139 100644 (file)
@@ -274,9 +274,7 @@ p2m_pod_set_cache_target(struct p2m_domain *p2m, unsigned long pod_target, int p
             if ( test_and_clear_bit(_PGT_pinned, &(page+i)->u.inuse.type_info) )
                 put_page_and_type(page + i);
 
-            if ( test_and_clear_bit(_PGC_allocated, &(page+i)->count_info) )
-                put_page(page + i);
-
+            put_page_alloc_ref(page + i);
             put_page(page + i);
 
             if ( preemptible && pod_target != p2m->pod.count &&
index 4c9954867c2868eef56d1381a772943034caddb1..883352da7d5280b3a0600f2e6c3108e19c3b815e 100644 (file)
@@ -1609,8 +1609,7 @@ int p2m_mem_paging_evict(struct domain *d, unsigned long gfn_l)
         goto out_put;
 
     /* Decrement guest domain's ref count of the page */
-    if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-        put_page(page);
+    put_page_alloc_ref(page);
 
     /* Remove mapping from p2m table */
     ret = p2m_set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
index e6a0f30a4bcd4076fd06a59b85136c18e72e6f16..f0ca10a7fa976a8c8f05876f4db8061ede1c4de0 100644 (file)
@@ -1707,8 +1707,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
         }
 
         BUG_ON(page_get_owner(pg) != d);
-        if ( test_and_clear_bit(_PGC_allocated, &pg->count_info) )
-            put_page(pg);
+        put_page_alloc_ref(pg);
 
         if ( pg->count_info & ~PGC_xen_heap )
         {
index 03db7bfa9e0be48c9013c1330c4c5d6a2bdafd6e..30d210fc08c25baadbea56f775b4ad2b33706e57 100644 (file)
@@ -388,9 +388,8 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
      * For this purpose (and to match populate_physmap() behavior), the page
      * is kept allocated.
      */
-    if ( !rc && !is_domain_direct_mapped(d) &&
-         test_and_clear_bit(_PGC_allocated, &page->count_info) )
-        put_page(page);
+    if ( !rc && !is_domain_direct_mapped(d) )
+        put_page_alloc_ref(page);
 
     put_page(page);
 
index 8a72e382e65034d69a56deb6c1904be4681d543a..4f3e799ebbcfd2eae8db3efb3192abc6d3382d9e 100644 (file)
@@ -173,8 +173,7 @@ unshare_xenoprof_page_with_guest(struct xenoprof *x)
         struct page_info *page = mfn_to_page(mfn_add(mfn, i));
 
         BUG_ON(page_get_owner(page) != current->domain);
-        if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
-            put_page(page);
+        put_page_alloc_ref(page);
     }
 }
 
index a57974ae511055a9c910468c333003d0a35584d8..5d0c19f01176eef61f9482155dae13ddd8efe873 100644 (file)
@@ -658,4 +658,18 @@ static inline void share_xen_page_with_privileged_guests(
     share_xen_page_with_guest(page, dom_xen, flags);
 }
 
+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.
+     */
+    BUG_ON((page->count_info & PGC_count_mask) <= 1);
+    if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
+        put_page(page);
+}
+
 #endif /* __XEN_MM_H__ */