xen/mm.h: add helper function to test-and-clear _PGC_allocated
PATCH 1/1 xen/mm.h: add helper function to test-and-clear _PGC_allocated
The _PGC_allocated flag is set on a page when it is assigned to a domain along with an initial reference count of 1. To clear this initial reference count 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, clear_assignment_reference(), to replace all the open-coded test-and-clear/put_page occurrences and incorporates in that an ASSERTion that an additional page reference is held.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Tamas K Lengyel <[email protected]>
Cc: George Dunlap <[email protected]>
--- xen/arch/arm/domain.c | 4 +--- xen/arch/x86/domain.c | 3 +-- xen/arch/x86/hvm/ioreq.c | 11 ++--------- xen/arch/x86/mm.c | 3 +-- xen/arch/x86/mm/mem_sharing.c | 9 +++------ xen/arch/x86/mm/p2m-pod.c | 4 +--- xen/arch/x86/mm/p2m.c | 3 +-- xen/common/grant_table.c | 3 +-- xen/common/memory.c | 5 ++--- xen/common/xenoprof.c | 3 +-- xen/include/xen/mm.h | 11 +++++++++++ 11 files changed, 25 insertions(+), 34 deletions(-)
xen/arch/arm/domain.c +1 -3
@@ -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); - +clear_assignment_reference (page);put_page (page);if (hypercall_preempt_check () )
xen/arch/x86/domain.c +1 -2
@@ -1939,8 +1939,7 @@ static int relinquish_memory(BUG (); } -if (test_and_clear_bit (_PGC_allocated, &page->count_info) ) -put_page (page); +clear_assignment_reference (page);/* * Forcibly invalidate top-most, still valid page tables at this point
xen/arch/x86/hvm/ioreq.c +2 -9
@@ -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); +clear_assignment_reference (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); - +clear_assignment_reference (page);put_page_and_type (page); }
xen/arch/x86/mm.c +1 -2
@@ -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); +clear_assignment_reference (page);if ( !test_and_clear_bit (_PGC_xen_heap, &page->count_info) )ASSERT_UNREACHABLE (); page->u.inuse.type_info =0 ;
xen/arch/x86/mm/mem_sharing.c +3 -6
@@ -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); +clear_assignment_reference (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); +clear_assignment_reference (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); +clear_assignment_reference (page);put_page (page); }put_gfn (d, gfn);
xen/arch/x86/mm/p2m-pod.c +1 -3
@@ -274,9 +274,7 @@ p2m_pod_set_cache_target(struct p2m_domain *p2m, unsigned long pod_target, int pif (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); - +clear_assignment_reference (page + i);put_page (page + i); if ( preemptible && pod_target != p2m->pod.count &&
xen/arch/x86/mm/p2m.c +1 -2
@@ -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); +clear_assignment_reference (page);/* Remove mapping from p2m table */ ret = p2m_set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
xen/common/grant_table.c +1 -2
@@ -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); +clear_assignment_reference (pg);if ( pg->count_info & ~PGC_xen_heap ) {
xen/common/memory.c +2 -3
@@ -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) ) +clear_assignment_reference (page);put_page (page);
xen/common/xenoprof.c +1 -2
@@ -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); +clear_assignment_reference (page); } }
xen/include/xen/mm.h +11 -0
@@ -658,4 +658,15 @@ static inline void share_xen_page_with_privileged_guests( share_xen_page_with_guest(page, dom_xen, flags); } +static inline void clear_assignment_reference (struct page_info *page) +{ +/* +* It is unsafe to clear _PGC_allocated without holding an additional +* reference. +*/ +ASSERT ((page->count_info & PGC_count_mask) >1 ); +if (test_and_clear_bit (_PGC_allocated, &page->count_info) ) +put_page (page); +} +#endif /* __XEN_MM_H__ */
Signature · 7 lines
2.20.1.2.gb21ebb671 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines · Paul Durrant
xen/arch/x86/hvm/ioreq.c +0
-0@@ -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 ;
xen/arch/x86/hvm/ioreq.c static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) · line 422
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1
-7-/* -* 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); - +clear_assignment_reference (page);put_page_and_type (page); }
Is there a specific reason you drop the comment? It doesn't become less relevant than when it was added, does it?
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 661
Quoted 7 lines
xen/include/xen/mm.h +1
-0@@ -658,4 +658,15 @@ static inline void share_xen_page_with_privileged_guests(share_xen_page_with_guest (page, dom_xen, flags); } +static inline void clear_assignment_reference (struct page_info *page)
I think the function should have 'page' in it's name. Perhaps page_deassign() / page_dealloc() are also misleading, but how about page_put_alloc() or page_put_alloc_ref()?
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 667
Quoted 6 lines
xen/include/xen/mm.h +6
-0+{ +/* +* It is unsafe to clear _PGC_allocated without holding an additional +* reference. +*/ +ASSERT ((page->count_info & PGC_count_mask) >1 );
While this isn't really in line with our goal of wanting to limit damage also in release builds, I agree that there's no really good alternative here. Crashing the owner of the page wouldn't help much, and bailing from the function wouldn't necessarily be better either. Hence I think this would better be BUG_ON().
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 670
Quoted 3 lines
xen/include/xen/mm.h +3
-0+if (test_and_clear_bit (_PGC_allocated, &page->count_info) ) +put_page (page); +}
On the whole I have to admit I'm not entirely convinced the "open- coding" as you call it (to me it's not really open-coding as long as there is no helper) is such a bad thing here: Without the helper it is slightly more obvious at the use sites what's actually going on. But maybe that's indeed just me.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 17 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 10 July 2019 23:53
To: Paul Durrant <[email protected]>
Cc: [email protected]; Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monne <[email protected]>; Volodymyr Babchuk <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tamas K Lengyel <[email protected]>; Tim (Xen.org) <[email protected]>; Wei Liu <[email protected]>
Subject: Re: [Xen-devel] [PATCH]xen/mm.h: add helper function to test-and-clear_PGC_allocatedQuoted 4 lines · Paul Durrant
xen/arch/x86/hvm/ioreq.c +0
-0@@ -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 ;
Quoted 13 lines
xen/arch/x86/hvm/ioreq.c static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) · line 422
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1
-7-/* -* 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); - +clear_assignment_reference (page);put_page_and_type (page); }Is there a specific reason you drop the comment? It doesn't become less relevant than when it was added, does it?
Not sure, since what's actually going on is now internal to the function. If I change the function name to clear_allocation_reference() then I think the comment probably becomes extraneous.
Quoted 7 lines
Quoted 7 lines
xen/include/xen/mm.h +0
-0@@ -658,4 +658,15 @@ static inline void share_xen_page_with_privileged_guests(share_xen_page_with_guest (page, dom_xen, flags); }
Quoted 6 lines
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 661
Quoted 1 line
xen/include/xen/mm.h +1
-0+static inline void clear_assignment_reference (struct page_info *page)I think the function should have 'page' in it's name. Perhaps
page_deassign()/page_dealloc()are also misleading, but how aboutpage_put_alloc()orpage_put_alloc_ref()?
Ok, I think page_put_alloc_ref() is most descriptive (particularly w.r.t. the above discussion).
Quoted 7 lines
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 667
Quoted 6 lines
xen/include/xen/mm.h +6
-0+{ +/* +* It is unsafe to clear _PGC_allocated without holding an additional +* reference. +*/ +ASSERT ((page->count_info & PGC_count_mask) >1 );
Quoted 5 lines
While this isn't really in line with our goal of wanting to limit damage also in release builds, I agree that there's no really good alternative here. Crashing the owner of the page wouldn't help much, and bailing from the function wouldn't necessarily be better either. Hence I think this would better be
BUG_ON().
Ok.
Quoted 10 lines
xen/include/xen/mm.h static inline void share_xen_page_with_privileged_guests( · line 670
Quoted 4 lines
xen/include/xen/mm.h +3
-0+if (test_and_clear_bit (_PGC_allocated, &page->count_info) ) +put_page (page); +}On the whole I have to admit I'm not entirely convinced the "open- coding" as you call it (to me it's not really open-coding as long as there is no helper) is such a bad thing here: Without the helper it is slightly more obvious at the use sites what's actually going on. But maybe that's indeed just me.
I still think a helper is better, but I'll add a comment to describe what it is doing.
Paul
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 22 lines · Paul Durrant
Quoted 8 lines
From: Jan Beulich <[email protected]>
Sent: 10 July 2019 23:53Quoted 4 lines · Paul Durrant
xen/arch/x86/hvm/ioreq.c +0
-0@@ -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 ;Quoted 13 lines
xen/arch/x86/hvm/ioreq.c static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) · line 422
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1
-7-/* -* 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); - +clear_assignment_reference (page);put_page_and_type (page); }Is there a specific reason you drop the comment? It doesn't become less relevant than when it was added, does it?
Quoted 3 lines
Not sure, since what's actually going on is now internal to the function. If I change the function name to
clear_allocation_reference()then I think the comment probably becomes extraneous.
Well, the perspective I'm taking is that the ordering constraint wrt put_page_and_type() doesn't go away and is a relevant part of what the comment talks about.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 35 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 July 2019 10:18
To: Paul Durrant <[email protected]>
Cc: JulienGrall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Roger Pau Monne <[email protected]>; Volodymyr Babchuk <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tamas K Lengyel <[email protected]>; Tim (Xen.org) <[email protected]>; Wei Liu <[email protected]>
Subject: Re: [Xen-devel] [PATCH]xen/mm.h: add helper function to test-and-clear_PGC_allocatedQuoted 22 lines · Paul Durrant
Quoted 21 lines
From: Jan Beulich <[email protected]>
Sent: 10 July 2019 23:53Quoted 4 lines · Paul Durrant
xen/arch/x86/hvm/ioreq.c +0
-0@@ -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 ;xen/arch/x86/hvm/ioreq.c static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) · line 422
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1
-7-/* -* 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); - +clear_assignment_reference (page);put_page_and_type (page); }Is there a specific reason you drop the comment? It doesn't become less relevant than when it was added, does it?
Quoted 7 lines
Quoted 3 lines
Not sure, since what's actually going on is now internal to the function. If I change the function name to
clear_allocation_reference()then I think the comment probably becomes extraneous.Well, the perspective I'm taking is that the ordering constraint wrt
put_page_and_type()doesn't go away and is a relevant part of what the comment talks about.
Ok. Would you be happy fixing the comment to your taste on commit then, as I'm not sure exactly what you want to say?
Paul
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel