make balloon pages movable by compaction
Subsystems (6)
PATCH v3 1/4 mm: introduce compaction and migration for virtio ballooned pages
In reply to Rafael AquiniThis patch introduces the helper functions as well as the necessary changes to teach compaction and migration bits how to cope with pages which are part of a guest memory balloon, in order to make them movable by memory compaction procedures.
--- include/linux/mm.h | 15 +++++++ mm/compaction.c | 126 ++++++++++++++++++++++++++++++++++++++++++++-------- mm/migrate.c | 30 ++++++++++++- 3 files changed, 151 insertions(+), 20 deletions(-)
include/linux/mm.h +15 -0
@@ -1629,5 +1629,20 @@ static inline unsigned int debug_guardpage_minorder(void) { return 0; }static inline bool page_is_guard (struct page *page) {return false; }#endif /* CONFIG_DEBUG_PAGEALLOC */ +#if (defined(CONFIG_VIRTIO_BALLOON ) || \ + defined(CONFIG_VIRTIO_BALLOON_MODULE )) && defined(CONFIG_COMPACTION ) +extern bool putback_balloon_page (struct page *); +extern struct address_space *balloon_mapping; + +static inline bool is_balloon_page (struct page *page) +{ +return (page->mapping == balloon_mapping) ? true : false; +} +#else +static inline bool is_balloon_page (struct page *page) {return false; } +static inline bool isolate_balloon_page (struct page *page) {return false; } +static inline bool putback_balloon_page (struct page *page) {return false; } +#endif /* (VIRTIO_BALLOON || VIRTIO_BALLOON_MODULE) && COMPACTION */ +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
mm/compaction.c +107 -19
151 lines not on this page · raw message
mm/migrate.c +29 -1
@@ -78,7 +78,10 @@ void putback_lru_pages(struct list_head *l)list_del (&page->lru);dec_zone_page_state (page,NR_ISOLATED_ANON +page_is_file_cache (page)); -putback_lru_page (page); +if (unlikely (is_balloon_page (page))) +WARN_ON (!putback_balloon_page (page)); +else +putback_lru_page (page); } }
@@ -783,6 +786,17 @@ static int __unmap_and_move(struct page *page, struct page *newpage, } } +if (is_balloon_page (page)) { +/* +* A ballooned page does not need any special attention from +* physical to virtual reverse mapping procedures. +* Skip any attempt to unmap PTEs or to remap swap cache, +* in order to avoid burning cycles at rmap level. +*/ + remap_swapcache =0 ; + goto skip_unmap; + } +/* * Corner case handling: * 1. When a new swap-cache page is read into, it is added to the LRU
@@ -852,6 +866,20 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private, goto out; rc =__unmap_and_move (page, newpage, force, offlining, mode); + +if (is_balloon_page (newpage)) { +/* +* A ballooned page has been migrated already. Now, it is the +* time to wrap-up counters, handle the old page back to Buddy +* and return. +*/ +list_del (&page->lru); +dec_zone_page_state (page,NR_ISOLATED_ANON + +page_is_file_cache (page)); +put_page (page); +__free_page (page); +return rc; + } out:if (rc != -EAGAIN ) {/*
Signature · 1 line
1.7.10.4
PATCH v3 2/4 virtio_balloon: handle concurrent accesses to virtio_balloon struct elements
In reply to Rafael AquiniThis patch introduces access sychronization to critical elements of struct virtio_balloon, in order to allow the thread concurrency compaction/migration bits might ended up imposing to the balloon driver on several situations.
--- drivers/virtio/virtio_balloon.c | 45 +++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-)
drivers/virtio/virtio_balloon.c +34 -11
@@ -51,6 +51,10 @@ struct virtio_balloon/* Number of balloon pages we've told the Host we're not using. */ unsigned int num_pages; + +/* Protect 'pages', 'pfns' & 'num_pnfs' against concurrent updates */ +spinlock_t pfn_list_lock; +/* * The pages we've told the Host we're not using. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
@@ -97,21 +101,23 @@ static void balloon_ack(struct virtqueue *vq)complete (&vb->acked); } -static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq) -{ -struct scatterlist sg; - -sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +/* Protection for concurrent accesses to balloon virtqueues and vb->acked */ +DEFINE_MUTEX (vb_queue_completion); +static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq, +struct scatterlist *sg) +{ +mutex_lock (&vb_queue_completion);init_completion (&vb->acked);/* We should always be able to add one buffer to an empty queue. */ -if (virtqueue_add_buf (vq, &sg,1 ,0 , vb,GFP_KERNEL ) <0 ) +if (virtqueue_add_buf (vq, sg,1 ,0 , vb,GFP_KERNEL ) <0 )BUG ();virtqueue_kick (vq);/* When host has read buffer, this completes via balloon_ack */ wait_for_completion (&vb->acked); +mutex_unlock (&vb_queue_completion); }static void set_page_pfns (u32 pfns[],struct page *page)
@@ -126,9 +132,12 @@ static void set_page_pfns(u32 pfns[], struct page *page)static void fill_balloon (struct virtio_balloon *vb,size_t num) { +struct scatterlist sg; +int alloc_failed =0 ;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |
@@ -138,8 +147,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)dev_printk (KERN_INFO , &vb->vdev->dev,"Out of puff! Can't get %zu pages\n" , num); -/* Sleep for at least 1/5 of a second before retry. */ -msleep (200 ); + alloc_failed =1 ;break ; }set_page_pfns (vb->pfns + vb->num_pfns, page);
@@ -149,10 +157,19 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) }/* Didn't get any? Oh well. */ -if (vb->num_pfns ==0 ) +if (vb->num_pfns ==0 ) { +spin_unlock (&vb->pfn_list_lock);return ; + } + +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); -tell_host (vb, vb->inflate_vq); +/* alloc_page failed, sleep for at least 1/5 of a sec before retry. */ +if (alloc_failed) +msleep (200 ); + +tell_host (vb, vb->inflate_vq, &sg); }static void release_pages_by_pfn (const u32 pfns[],unsigned int num)
@@ -169,10 +186,12 @@ static void release_pages_by_pfn(const u32 pfns[], unsigned int num)static void leak_balloon (struct virtio_balloon *vb,size_t num) {struct page *page; +struct scatterlist sg;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { page = list_first_entry(&vb->pages, struct page, lru);
@@ -180,13 +199,15 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)set_page_pfns (vb->pfns + vb->num_pfns, page); vb->num_pages -=VIRTIO_BALLOON_PAGES_PER_PAGE ; } +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock);/* * Note that if * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST); * is true, we *have* to do it in this order */ -tell_host (vb, vb->deflate_vq); +tell_host (vb, vb->deflate_vq, &sg);release_pages_by_pfn (vb->pfns, vb->num_pfns); }
@@ -356,6 +377,8 @@ static int virtballoon_probe(struct virtio_device *vdev) }INIT_LIST_HEAD (&vb->pages); +spin_lock_init (&vb->pfn_list_lock); + vb->num_pages =0 ;init_waitqueue_head (&vb->config_change); vb->vdev = vdev;
Signature · 1 line
1.7.10.4
PATCH v3 3/4 virtio_balloon: introduce migration primitives to balloon pages
In reply to Rafael AquiniThis patch makes balloon pages movable at allocation time and introduces the infrastructure needed to perform the balloon page migration operation.
--- drivers/virtio/virtio_balloon.c | 96 ++++++++++++++++++++++++++++++++++++++- include/linux/virtio_balloon.h | 4 ++ 2 files changed, 98 insertions(+), 2 deletions(-)
drivers/virtio/virtio_balloon.c +94 -2
@@ -27,6 +27,8 @@#include <linux/delay.h> #include <linux/slab.h> #include <linux/module.h> +#include <linux/fs.h> +#include <linux/pagemap.h> /* * Balloon device works in 4K page units. So each page is pointed to by
@@ -140,8 +142,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { -struct page *page =alloc_page (GFP_HIGHUSER | __GFP_NORETRY | - __GFP_NOMEMALLOC | __GFP_NOWARN); +struct page *page =alloc_page (GFP_HIGHUSER_MOVABLE | + __GFP_NORETRY | __GFP_NOWARN | + __GFP_NOMEMALLOC);if (!page) {if (printk_ratelimit ()) dev_printk(KERN_INFO, &vb->vdev->dev,
@@ -154,6 +157,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) vb->num_pages +=VIRTIO_BALLOON_PAGES_PER_PAGE ; totalram_pages--;list_add (&page->lru, &vb->pages); + page->mapping = balloon_mapping; }/* Didn't get any? Oh well. */
@@ -195,6 +199,7 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { page = list_first_entry(&vb->pages, struct page, lru); + page->mapping =NULL ;list_del (&page->lru);set_page_pfns (vb->pfns + vb->num_pfns, page); vb->num_pages -=VIRTIO_BALLOON_PAGES_PER_PAGE ;
@@ -365,6 +370,77 @@ static int init_vqs(struct virtio_balloon *vb)return 0 ; } +/* +* Populate balloon_mapping->a_ops->migratepage method to perform the balloon +* page migration task. +* +* After a ballooned page gets isolated by compaction procedures, this is the +* function that performs the page migration on behalf of move_to_new_page(), +* when the last calls (page)->mapping->a_ops->migratepage. +* +* Page migration for virtio balloon is done in a simple swap fashion which +* follows these two steps: +* 1) insert newpage into vb->pages list and update the host about it; +* 2) update the host about the removed old page from vb->pages list; +*/ +int virtballoon_migratepage (struct address_space *mapping, +struct page *newpage,struct page *page,enum migrate_mode mode) +{ +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +struct scatterlist sg; + +/* balloon's page migration 1st step */ +spin_lock (&vb->pfn_list_lock); + vb->num_pfns =VIRTIO_BALLOON_PAGES_PER_PAGE ; +list_add (&newpage->lru, &vb->pages); +set_page_pfns (vb->pfns, newpage); +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); +tell_host (vb, vb->inflate_vq, &sg); + +/* balloon's page migration 2nd step */ +spin_lock (&vb->pfn_list_lock); + vb->num_pfns =VIRTIO_BALLOON_PAGES_PER_PAGE ; +set_page_pfns (vb->pfns, page); +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); +tell_host (vb, vb->deflate_vq, &sg); + +return 0 ; +} + +/* +* Populate balloon_mapping->a_ops->invalidatepage method to help compaction on +* isolating a page from the balloon page list. +*/ +void virtballoon_isolatepage (struct page *page,unsigned long mode) +{ +struct address_space *mapping = page->mapping; +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +spin_lock (&vb->pfn_list_lock); +list_del (&page->lru); +spin_unlock (&vb->pfn_list_lock); +} + +/* +* Populate balloon_mapping->a_ops->freepage method to help compaction on +* re-inserting an isolated page into the balloon page list. +*/ +void virtballoon_putbackpage (struct page *page) +{ +struct address_space *mapping = page->mapping; +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +spin_lock (&vb->pfn_list_lock); +list_add (&page->lru, &vb->pages); +spin_unlock (&vb->pfn_list_lock); +} + +static const struct address_space_operations virtio_balloon_aops = { + .migratepage = virtballoon_migratepage, + .invalidatepage = virtballoon_isolatepage, + .freepage = virtballoon_putbackpage, +}; +static int virtballoon_probe (struct virtio_device *vdev) {struct virtio_balloon *vb;
@@ -384,6 +460,19 @@ static int virtballoon_probe(struct virtio_device *vdev) vb->vdev = vdev; vb->need_stats_update =0 ; +/* Init the ballooned page->mapping special balloon_mapping */ + balloon_mapping =kmalloc (sizeof (*balloon_mapping),GFP_KERNEL ); +if (!balloon_mapping) { + err = -ENOMEM ; + goto out_free_mapping; + } + +INIT_RADIX_TREE (&balloon_mapping->page_tree,GFP_ATOMIC | __GFP_NOWARN); +INIT_LIST_HEAD (&balloon_mapping->i_mmap_nonlinear); +spin_lock_init (&balloon_mapping->tree_lock); + balloon_mapping->a_ops = &virtio_balloon_aops; + balloon_mapping->backing_dev_info = (void *)vb; + err =init_vqs (vb);if (err) goto out_free_vb;
@@ -398,6 +487,8 @@ static int virtballoon_probe(struct virtio_device *vdev) out_del_vqs: vdev->config->del_vqs (vdev); +out_free_mapping: +kfree (balloon_mapping); out_free_vb:kfree (vb); out:
@@ -424,6 +515,7 @@ static void __devexit virtballoon_remove(struct virtio_device *vdev)kthread_stop (vb->thread);remove_common (vb);kfree (vb); +kfree (balloon_mapping); }#ifdef CONFIG_PM
include/linux/virtio_balloon.h +4 -0
@@ -56,4 +56,8 @@ struct virtio_balloon_stat {u64 val; } __attribute__((packed)); +#if !defined(CONFIG_COMPACTION ) +struct address_space *balloon_mapping; +#endif +#endif /* _LINUX_VIRTIO_BALLOON_H */
Signature · 1 line
1.7.10.4
PATCH v3 4/4 mm: add vm event counters for balloon pages compaction
In reply to Rafael AquiniThis patch is only for testing report purposes and shall be dropped in case of the rest of this patchset getting accepted for merging.
--- drivers/virtio/virtio_balloon.c | 1 + include/linux/vm_event_item.h | 2 ++ mm/compaction.c | 1 + mm/migrate.c | 6 ++++-- mm/vmstat.c | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-)
drivers/virtio/virtio_balloon.c +1 -0
@@ -406,6 +406,7 @@ int virtballoon_migratepage(struct address_space *mapping,spin_unlock (&vb->pfn_list_lock);tell_host (vb, vb->deflate_vq, &sg); +count_vm_event (COMPACTBALLOONMIGRATED );return 0 ; }
include/linux/vm_event_item.h +2 -0
@@ -40,6 +40,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,#ifdef CONFIG_COMPACTION COMPACTBLOCKS ,COMPACTPAGES ,COMPACTPAGEFAILED ,COMPACTSTALL ,COMPACTFAIL ,COMPACTSUCCESS , +COMPACTBALLOONMIGRATED ,COMPACTBALLOONFAILED , +COMPACTBALLOONISOLATED ,COMPACTBALLOONFREED , #endif #ifdef CONFIG_HUGETLB_PAGE HTLB_BUDDY_PGALLOC, HTLB_BUDDY_PGALLOC_FAIL,
mm/compaction.c +1 -0
@@ -72,6 +72,7 @@ static bool isolate_balloon_page(struct page *page)if (is_balloon_page (page) && (page_count (page) ==2 )) {__isolate_balloon_page (page);unlock_page (page); +count_vm_event (COMPACTBALLOONISOLATED );return true; }unlock_page (page);
mm/migrate.c +4 -2
@@ -78,9 +78,10 @@ void putback_lru_pages(struct list_head *l)list_del (&page->lru);dec_zone_page_state (page,NR_ISOLATED_ANON +page_is_file_cache (page)); -if (unlikely (is_balloon_page (page))) +if (unlikely (is_balloon_page (page))) { +count_vm_event (COMPACTBALLOONFAILED );WARN_ON (!putback_balloon_page (page)); -else + }else putback_lru_page (page); } }
@@ -878,6 +879,7 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private, page_is_file_cache(page));put_page (page);__free_page (page); +count_vm_event (COMPACTBALLOONFREED );return rc; } out:
mm/vmstat.c +4 -0
@@ -767,6 +767,10 @@ const char * const vmstat_text[] = {"compact_stall" ,"compact_fail" ,"compact_success" , +"compact_balloon_migrated" , +"compact_balloon_failed" , +"compact_balloon_isolated" , +"compact_balloon_freed" , #endif #ifdef CONFIG_HUGETLB_PAGE
Signature · 1 line
1.7.10.4
v3 cover make balloon pages movable by compaction
Memory fragmentation introduced by ballooning might reduce significantly the number of 2MB contiguous memory blocks that can be used within a guest, thus imposing performance penalties associated with the reduced number of transparent huge pages that could be used by the guest workload.
This patchset follows the main idea discussed at 2012 LSFMMS section:
"Ballooning for transparent huge pages" -- http://lwn.net/Articles/490114/
to introduce the required changes to the virtio_balloon driver, as well as changes to the core compaction & migration bits, in order to make those subsystems aware of ballooned pages and allow memory balloon pages become movable within a guest, thus avoiding the aforementioned fragmentation issue.
Rafael Aquini (4):
mm: introduce compaction and migration for virtio ballooned pages
virtio_balloon: handle concurrent accesses to virtio_balloon struct
elements
virtio_balloon: introduce migration primitives to balloon pages
mm: add vm event counters for balloon pages compaction
drivers/virtio/virtio_balloon.c | 142 +++++++++++++++++++++++++++++++++++---- include/linux/mm.h | 15 +++++ include/linux/virtio_balloon.h | 4 ++ include/linux/vm_event_item.h | 2 + mm/compaction.c | 127 ++++++++++++++++++++++++++++------ mm/migrate.c | 32 ++++++++- mm/vmstat.c | 4 ++ 7 files changed, 293 insertions(+), 33 deletions(-)
Changelog:
V3: address reviwers nitpick suggestions (Mel, Minchan)
V2: address Mel Gorman's review comments
Preliminary test results:
(2 VCPU 1024mB RAM KVM guest running 3.5.0_rc5+)
* 64mB balloon:
[root@localhost ~]# awk '/compact/ {print}' /proc/vmstatcompact_blocks_moved 0compact_pages_moved 0compact_pagemigrate_failed 0compact_stall 0compact_fail 0compact_success 0compact_balloon_migrated 0compact_balloon_failed 0compact_balloon_isolated 0compact_balloon_freed 0
[root@localhost ~]#
[root@localhost ~]# for i in $(seq 1 4); do echo 1 > /proc/sys/vm/compact_memory & done &>/dev/null
[1] Done echo 1 > /proc/sys/vm/compact_memory [2] Done echo 1 > /proc/sys/vm/compact_memory [3]- Done echo 1 > /proc/sys/vm/compact_memory [4]+ Done echo 1 > /proc/sys/vm/compact_memory
[root@localhost ~]#
[root@localhost ~]# awk '/compact/ {print}' /proc/vmstat compact_blocks_moved 2717compact_pages_moved 46697 compact_pagemigrate_failed 75compact_stall 0compact_fail 0compact_success 0compact_balloon_migrated 16384 compact_balloon_failed 0 compact_balloon_isolated 16384 compact_balloon_freed 16384
* 128mB balloon:
[root@localhost ~]# awk '/compact/ {print}' /proc/vmstatcompact_blocks_moved 0compact_pages_moved 0compact_pagemigrate_failed 0compact_stall 0compact_fail 0compact_success 0compact_balloon_migrated 0compact_balloon_failed 0compact_balloon_isolated 0compact_balloon_freed 0
[root@localhost ~]#
[root@localhost ~]# for i in $(seq 1 4); do echo 1 > /proc/sys/vm/compact_memory & done &>/dev/null
[1] Done echo 1 > /proc/sys/vm/compact_memory [2] Done echo 1 > /proc/sys/vm/compact_memory [3]- Done echo 1 > /proc/sys/vm/compact_memory [4]+ Done echo 1 > /proc/sys/vm/compact_memory
[root@localhost ~]#
[root@localhost ~]# awk '/compact/ {print}' /proc/vmstat compact_blocks_moved 2598compact_pages_moved 47660 compact_pagemigrate_failed 103compact_stall 0compact_fail 0compact_success 0compact_balloon_migrated 26652 compact_balloon_failed 76 compact_balloon_isolated 26728 compact_balloon_freed 26652
Signature · 7 lines
1.7.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 1/4 mm: introduce compaction and migration for virtio ballooned pages
This patch introduces the helper functions as well as the necessary changes to teach compaction and migration bits how to cope with pages which are part of a guest memory balloon, in order to make them movable by memory compaction procedures.
--- include/linux/mm.h | 15 +++++++ mm/compaction.c | 126 ++++++++++++++++++++++++++++++++++++++++++++-------- mm/migrate.c | 30 ++++++++++++- 3 files changed, 151 insertions(+), 20 deletions(-)
include/linux/mm.h +15 -0
@@ -1629,5 +1629,20 @@ static inline unsigned int debug_guardpage_minorder(void) { return 0; }static inline bool page_is_guard (struct page *page) {return false; }#endif /* CONFIG_DEBUG_PAGEALLOC */ +#if (defined(CONFIG_VIRTIO_BALLOON ) || \ + defined(CONFIG_VIRTIO_BALLOON_MODULE )) && defined(CONFIG_COMPACTION ) +extern bool putback_balloon_page (struct page *); +extern struct address_space *balloon_mapping; + +static inline bool is_balloon_page (struct page *page) +{ +return (page->mapping == balloon_mapping) ? true : false; +} +#else +static inline bool is_balloon_page (struct page *page) {return false; } +static inline bool isolate_balloon_page (struct page *page) {return false; } +static inline bool putback_balloon_page (struct page *page) {return false; } +#endif /* (VIRTIO_BALLOON || VIRTIO_BALLOON_MODULE) && COMPACTION */ +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
mm/compaction.c +107 -19
151 lines not on this page · raw message
mm/migrate.c +29 -1
@@ -78,7 +78,10 @@ void putback_lru_pages(struct list_head *l)list_del (&page->lru);dec_zone_page_state (page,NR_ISOLATED_ANON +page_is_file_cache (page)); -putback_lru_page (page); +if (unlikely (is_balloon_page (page))) +WARN_ON (!putback_balloon_page (page)); +else +putback_lru_page (page); } }
@@ -783,6 +786,17 @@ static int __unmap_and_move(struct page *page, struct page *newpage, } } +if (is_balloon_page (page)) { +/* +* A ballooned page does not need any special attention from +* physical to virtual reverse mapping procedures. +* Skip any attempt to unmap PTEs or to remap swap cache, +* in order to avoid burning cycles at rmap level. +*/ + remap_swapcache =0 ; + goto skip_unmap; + } +/* * Corner case handling: * 1. When a new swap-cache page is read into, it is added to the LRU
@@ -852,6 +866,20 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private, goto out; rc =__unmap_and_move (page, newpage, force, offlining, mode); + +if (is_balloon_page (newpage)) { +/* +* A ballooned page has been migrated already. Now, it is the +* time to wrap-up counters, handle the old page back to Buddy +* and return. +*/ +list_del (&page->lru); +dec_zone_page_state (page,NR_ISOLATED_ANON + +page_is_file_cache (page)); +put_page (page); +__free_page (page); +return rc; + } out:if (rc != -EAGAIN ) {/*
Signature · 7 lines
1.7.10.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 2/4 virtio_balloon: handle concurrent accesses to virtio_balloon struct elements
In reply to Rafael AquiniThis patch introduces access sychronization to critical elements of struct virtio_balloon, in order to allow the thread concurrency compaction/migration bits might ended up imposing to the balloon driver on several situations.
--- drivers/virtio/virtio_balloon.c | 45 +++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-)
drivers/virtio/virtio_balloon.c +34 -11
@@ -51,6 +51,10 @@ struct virtio_balloon/* Number of balloon pages we've told the Host we're not using. */ unsigned int num_pages; + +/* Protect 'pages', 'pfns' & 'num_pnfs' against concurrent updates */ +spinlock_t pfn_list_lock; +/* * The pages we've told the Host we're not using. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
@@ -97,21 +101,23 @@ static void balloon_ack(struct virtqueue *vq)complete (&vb->acked); } -static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq) -{ -struct scatterlist sg; - -sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +/* Protection for concurrent accesses to balloon virtqueues and vb->acked */ +DEFINE_MUTEX (vb_queue_completion); +static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq, +struct scatterlist *sg) +{ +mutex_lock (&vb_queue_completion);init_completion (&vb->acked);/* We should always be able to add one buffer to an empty queue. */ -if (virtqueue_add_buf (vq, &sg,1 ,0 , vb,GFP_KERNEL ) <0 ) +if (virtqueue_add_buf (vq, sg,1 ,0 , vb,GFP_KERNEL ) <0 )BUG ();virtqueue_kick (vq);/* When host has read buffer, this completes via balloon_ack */ wait_for_completion (&vb->acked); +mutex_unlock (&vb_queue_completion); }static void set_page_pfns (u32 pfns[],struct page *page)
@@ -126,9 +132,12 @@ static void set_page_pfns(u32 pfns[], struct page *page)static void fill_balloon (struct virtio_balloon *vb,size_t num) { +struct scatterlist sg; +int alloc_failed =0 ;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |
@@ -138,8 +147,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)dev_printk (KERN_INFO , &vb->vdev->dev,"Out of puff! Can't get %zu pages\n" , num); -/* Sleep for at least 1/5 of a second before retry. */ -msleep (200 ); + alloc_failed =1 ;break ; }set_page_pfns (vb->pfns + vb->num_pfns, page);
@@ -149,10 +157,19 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) }/* Didn't get any? Oh well. */ -if (vb->num_pfns ==0 ) +if (vb->num_pfns ==0 ) { +spin_unlock (&vb->pfn_list_lock);return ; + } + +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); -tell_host (vb, vb->inflate_vq); +/* alloc_page failed, sleep for at least 1/5 of a sec before retry. */ +if (alloc_failed) +msleep (200 ); + +tell_host (vb, vb->inflate_vq, &sg); }static void release_pages_by_pfn (const u32 pfns[],unsigned int num)
@@ -169,10 +186,12 @@ static void release_pages_by_pfn(const u32 pfns[], unsigned int num)static void leak_balloon (struct virtio_balloon *vb,size_t num) {struct page *page; +struct scatterlist sg;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { page = list_first_entry(&vb->pages, struct page, lru);
@@ -180,13 +199,15 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)set_page_pfns (vb->pfns + vb->num_pfns, page); vb->num_pages -=VIRTIO_BALLOON_PAGES_PER_PAGE ; } +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock);/* * Note that if * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST); * is true, we *have* to do it in this order */ -tell_host (vb, vb->deflate_vq); +tell_host (vb, vb->deflate_vq, &sg);release_pages_by_pfn (vb->pfns, vb->num_pfns); }
@@ -356,6 +377,8 @@ static int virtballoon_probe(struct virtio_device *vdev) }INIT_LIST_HEAD (&vb->pages); +spin_lock_init (&vb->pfn_list_lock); + vb->num_pages =0 ;init_waitqueue_head (&vb->config_change); vb->vdev = vdev;
Signature · 7 lines
1.7.10.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 3/4 virtio_balloon: introduce migration primitives to balloon pages
In reply to Rafael AquiniThis patch makes balloon pages movable at allocation time and introduces the infrastructure needed to perform the balloon page migration operation.
--- drivers/virtio/virtio_balloon.c | 96 ++++++++++++++++++++++++++++++++++++++- include/linux/virtio_balloon.h | 4 ++ 2 files changed, 98 insertions(+), 2 deletions(-)
drivers/virtio/virtio_balloon.c +94 -2
@@ -27,6 +27,8 @@#include <linux/delay.h> #include <linux/slab.h> #include <linux/module.h> +#include <linux/fs.h> +#include <linux/pagemap.h> /* * Balloon device works in 4K page units. So each page is pointed to by
@@ -140,8 +142,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { -struct page *page =alloc_page (GFP_HIGHUSER | __GFP_NORETRY | - __GFP_NOMEMALLOC | __GFP_NOWARN); +struct page *page =alloc_page (GFP_HIGHUSER_MOVABLE | + __GFP_NORETRY | __GFP_NOWARN | + __GFP_NOMEMALLOC);if (!page) {if (printk_ratelimit ()) dev_printk(KERN_INFO, &vb->vdev->dev,
@@ -154,6 +157,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) vb->num_pages +=VIRTIO_BALLOON_PAGES_PER_PAGE ; totalram_pages--;list_add (&page->lru, &vb->pages); + page->mapping = balloon_mapping; }/* Didn't get any? Oh well. */
@@ -195,6 +199,7 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { page = list_first_entry(&vb->pages, struct page, lru); + page->mapping =NULL ;list_del (&page->lru);set_page_pfns (vb->pfns + vb->num_pfns, page); vb->num_pages -=VIRTIO_BALLOON_PAGES_PER_PAGE ;
@@ -365,6 +370,77 @@ static int init_vqs(struct virtio_balloon *vb)return 0 ; } +/* +* Populate balloon_mapping->a_ops->migratepage method to perform the balloon +* page migration task. +* +* After a ballooned page gets isolated by compaction procedures, this is the +* function that performs the page migration on behalf of move_to_new_page(), +* when the last calls (page)->mapping->a_ops->migratepage. +* +* Page migration for virtio balloon is done in a simple swap fashion which +* follows these two steps: +* 1) insert newpage into vb->pages list and update the host about it; +* 2) update the host about the removed old page from vb->pages list; +*/ +int virtballoon_migratepage (struct address_space *mapping, +struct page *newpage,struct page *page,enum migrate_mode mode) +{ +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +struct scatterlist sg; + +/* balloon's page migration 1st step */ +spin_lock (&vb->pfn_list_lock); + vb->num_pfns =VIRTIO_BALLOON_PAGES_PER_PAGE ; +list_add (&newpage->lru, &vb->pages); +set_page_pfns (vb->pfns, newpage); +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); +tell_host (vb, vb->inflate_vq, &sg); + +/* balloon's page migration 2nd step */ +spin_lock (&vb->pfn_list_lock); + vb->num_pfns =VIRTIO_BALLOON_PAGES_PER_PAGE ; +set_page_pfns (vb->pfns, page); +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); +tell_host (vb, vb->deflate_vq, &sg); + +return 0 ; +} + +/* +* Populate balloon_mapping->a_ops->invalidatepage method to help compaction on +* isolating a page from the balloon page list. +*/ +void virtballoon_isolatepage (struct page *page,unsigned long mode) +{ +struct address_space *mapping = page->mapping; +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +spin_lock (&vb->pfn_list_lock); +list_del (&page->lru); +spin_unlock (&vb->pfn_list_lock); +} + +/* +* Populate balloon_mapping->a_ops->freepage method to help compaction on +* re-inserting an isolated page into the balloon page list. +*/ +void virtballoon_putbackpage (struct page *page) +{ +struct address_space *mapping = page->mapping; +struct virtio_balloon *vb = (void *)mapping->backing_dev_info; +spin_lock (&vb->pfn_list_lock); +list_add (&page->lru, &vb->pages); +spin_unlock (&vb->pfn_list_lock); +} + +static const struct address_space_operations virtio_balloon_aops = { + .migratepage = virtballoon_migratepage, + .invalidatepage = virtballoon_isolatepage, + .freepage = virtballoon_putbackpage, +}; +static int virtballoon_probe (struct virtio_device *vdev) {struct virtio_balloon *vb;
@@ -384,6 +460,19 @@ static int virtballoon_probe(struct virtio_device *vdev) vb->vdev = vdev; vb->need_stats_update =0 ; +/* Init the ballooned page->mapping special balloon_mapping */ + balloon_mapping =kmalloc (sizeof (*balloon_mapping),GFP_KERNEL ); +if (!balloon_mapping) { + err = -ENOMEM ; + goto out_free_mapping; + } + +INIT_RADIX_TREE (&balloon_mapping->page_tree,GFP_ATOMIC | __GFP_NOWARN); +INIT_LIST_HEAD (&balloon_mapping->i_mmap_nonlinear); +spin_lock_init (&balloon_mapping->tree_lock); + balloon_mapping->a_ops = &virtio_balloon_aops; + balloon_mapping->backing_dev_info = (void *)vb; + err =init_vqs (vb);if (err) goto out_free_vb;
@@ -398,6 +487,8 @@ static int virtballoon_probe(struct virtio_device *vdev) out_del_vqs: vdev->config->del_vqs (vdev); +out_free_mapping: +kfree (balloon_mapping); out_free_vb:kfree (vb); out:
@@ -424,6 +515,7 @@ static void __devexit virtballoon_remove(struct virtio_device *vdev)kthread_stop (vb->thread);remove_common (vb);kfree (vb); +kfree (balloon_mapping); }#ifdef CONFIG_PM
include/linux/virtio_balloon.h +4 -0
@@ -56,4 +56,8 @@ struct virtio_balloon_stat {u64 val; } __attribute__((packed)); +#if !defined(CONFIG_COMPACTION ) +struct address_space *balloon_mapping; +#endif +#endif /* _LINUX_VIRTIO_BALLOON_H */
Signature · 7 lines
1.7.10.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 4/4 mm: add vm event counters for balloon pages compaction
In reply to Rafael AquiniThis patch is only for testing report purposes and shall be dropped in case of the rest of this patchset getting accepted for merging.
--- drivers/virtio/virtio_balloon.c | 1 + include/linux/vm_event_item.h | 2 ++ mm/compaction.c | 1 + mm/migrate.c | 6 ++++-- mm/vmstat.c | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-)
drivers/virtio/virtio_balloon.c +1 -0
@@ -406,6 +406,7 @@ int virtballoon_migratepage(struct address_space *mapping,spin_unlock (&vb->pfn_list_lock);tell_host (vb, vb->deflate_vq, &sg); +count_vm_event (COMPACTBALLOONMIGRATED );return 0 ; }
include/linux/vm_event_item.h +2 -0
@@ -40,6 +40,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,#ifdef CONFIG_COMPACTION COMPACTBLOCKS ,COMPACTPAGES ,COMPACTPAGEFAILED ,COMPACTSTALL ,COMPACTFAIL ,COMPACTSUCCESS , +COMPACTBALLOONMIGRATED ,COMPACTBALLOONFAILED , +COMPACTBALLOONISOLATED ,COMPACTBALLOONFREED , #endif #ifdef CONFIG_HUGETLB_PAGE HTLB_BUDDY_PGALLOC, HTLB_BUDDY_PGALLOC_FAIL,
mm/compaction.c +1 -0
@@ -72,6 +72,7 @@ static bool isolate_balloon_page(struct page *page)if (is_balloon_page (page) && (page_count (page) ==2 )) {__isolate_balloon_page (page);unlock_page (page); +count_vm_event (COMPACTBALLOONISOLATED );return true; }unlock_page (page);
mm/migrate.c +4 -2
@@ -78,9 +78,10 @@ void putback_lru_pages(struct list_head *l)list_del (&page->lru);dec_zone_page_state (page,NR_ISOLATED_ANON +page_is_file_cache (page)); -if (unlikely (is_balloon_page (page))) +if (unlikely (is_balloon_page (page))) { +count_vm_event (COMPACTBALLOONFAILED );WARN_ON (!putback_balloon_page (page)); -else + }else putback_lru_page (page); } }
@@ -878,6 +879,7 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private, page_is_file_cache(page));put_page (page);__free_page (page); +count_vm_event (COMPACTBALLOONFREED );return rc; } out:
mm/vmstat.c +4 -0
@@ -767,6 +767,10 @@ const char * const vmstat_text[] = {"compact_stall" ,"compact_fail" ,"compact_success" , +"compact_balloon_migrated" , +"compact_balloon_failed" , +"compact_balloon_isolated" , +"compact_balloon_freed" , #endif #ifdef CONFIG_HUGETLB_PAGE
Signature · 7 lines
1.7.10.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v3 2/4] virtio_balloon: handle concurrent accesses to virtio_balloon struct elements
In reply to Rafael AquiniQuoted 5 lines · Rafael Aquini
This patch introduces access sychronization to critical elements of struct
virtio_balloon, in order to allow the thread concurrency compaction/migration bits might ended up imposing to the balloon driver on several situations.Signed-off-by Rafael Aquini <[email protected]>
That's pretty vague, and it's almost impossible to audit this.
It looks very suspicious though:
Quoted 14 lines
drivers/virtio/virtio_balloon.c +6
-5-static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq) -{ -struct scatterlist sg; - -sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +/* Protection for concurrent accesses to balloon virtqueues and vb->acked */ +DEFINE_MUTEX (vb_queue_completion); +static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq, +struct scatterlist *sg) +{ +mutex_lock (&vb_queue_completion);init_completion (&vb->acked);
drivers/virtio/virtio_balloon.c static void balloon_ack(struct virtqueue *vq) · line 121
Quoted 10 lines
drivers/virtio/virtio_balloon.c +2
-1/* We should always be able to add one buffer to an empty queue. */ -if (virtqueue_add_buf (vq, &sg,1 ,0 , vb,GFP_KERNEL ) <0 ) +if (virtqueue_add_buf (vq, sg,1 ,0 , vb,GFP_KERNEL ) <0 )BUG ();virtqueue_kick (vq);/* When host has read buffer, this completes via balloon_ack */ wait_for_completion (&vb->acked); +mutex_unlock (&vb_queue_completion); }
OK, this lock is superceded by Michael's patch, and AFAICT is not due to any requirement introduced by these patches.
Quoted 33 lines
static void
set_page_pfns(u32 pfns[], struct page *page)drivers/virtio/virtio_balloon.c +7
-3@@ -126,9 +132,12 @@ static void set_page_pfns(u32 pfns[], struct page *page)static void fill_balloon (struct virtio_balloon *vb,size_t num) { +struct scatterlist sg; +int alloc_failed =0 ;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |@@ -138,8 +147,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)dev_printk (KERN_INFO , &vb->vdev->dev,"Out of puff! Can't get %zu pages\n" , num); -/* Sleep for at least 1/5 of a second before retry. */ -msleep (200 ); + alloc_failed =1 ;break ; }set_page_pfns (vb->pfns + vb->num_pfns, page);@@ -149,10 +157,19 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) }/* Didn't get any? Oh well. */ -if (vb->num_pfns ==0 ) +if (vb->num_pfns ==0 ) { +spin_unlock (&vb->pfn_list_lock);return ; + }
drivers/virtio/virtio_balloon.c static void fill_balloon(struct virtio_balloon *vb, size_t num) · line 172
Quoted 10 lines
drivers/virtio/virtio_balloon.c +8
-1+ +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock); -tell_host (vb, vb->inflate_vq); +/* alloc_page failed, sleep for at least 1/5 of a sec before retry. */ +if (alloc_failed) +msleep (200 ); + +tell_host (vb, vb->inflate_vq, &sg);
So, we drop the lock which procects vp->pfns[] and vb->num_pfns, then use it in tell_host? Surely it could be corrupted between there.
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-0
drivers/virtio/virtio_balloon.c struct virtio_balloon · line 60
Quoted 10 lines
drivers/virtio/virtio_balloon.c +4
-0@@ -51,6 +51,10 @@ struct virtio_balloon/* Number of balloon pages we've told the Host we're not using. */ unsigned int num_pages; + +/* Protect 'pages', 'pfns' & 'num_pnfs' against concurrent updates */ +spinlock_t pfn_list_lock; +/* * The pages we've told the Host we're not using. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
You might be better of taking num_pfns and pfns[] out of struct virtio_balloon, and putting them on the stack (maybe 64, not 256).
Cheers,
Rusty.
Re: [PATCH v3 2/4] virtio_balloon: handle concurrent accesses to virtio_balloon struct elements
Howdy Rusty,
First and foremost, thank you very much for taking the time to go through this proposal and provide me with such valuable feedback. I really appreciate that.
Quoted 9 lines · Rusty Russell
Quoted 5 lines · Rafael Aquini
This patch introduces access sychronization to critical elements of struct
virtio_balloon, in order to allow the thread concurrency compaction/migration bits might ended up imposing to the balloon driver on several situations.Signed-off-by Rafael Aquini <[email protected]>That's pretty vague, and it's almost impossible to audit this.
I'll definetely attempt to improve this one.
Despite it looks concise to me as it states the "whats" and the "whys", any clue on how to improve it and turn it into something that would make a lot more sense is very welcome and appreciated. I'm probably failing miserably to express the whole idea because I'm a terrible writer, no doubts about it. :)
Quoted 15 lines
Quoted 15 lines
drivers/virtio/virtio_balloon.c +7
-1+/* Protection for concurrent accesses to balloon virtqueues and vb->acked */ +DEFINE_MUTEX (vb_queue_completion); +static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq, +struct scatterlist *sg) +{ +mutex_lock (&vb_queue_completion);init_completion (&vb->acked);/* We should always be able to add one buffer to an empty queue. */ -if (virtqueue_add_buf (vq, &sg,1 ,0 , vb,GFP_KERNEL ) <0 ) +if (virtqueue_add_buf (vq, sg,1 ,0 , vb,GFP_KERNEL ) <0 )BUG ();virtqueue_kick (vq);
Quoted 8 lines
drivers/virtio/virtio_balloon.c static void balloon_ack(struct virtqueue *vq) · line 121
Quoted 4 lines
drivers/virtio/virtio_balloon.c +1
-0/* When host has read buffer, this completes via balloon_ack */ wait_for_completion (&vb->acked); +mutex_unlock (&vb_queue_completion); }OK, this lock is superceded by Michael's patch, and AFAICT is not due to any requirement introduced by these patches.
Unfortunately, I'm compelled to disagree with you on this one.
Because tell_host() can be called concurrently, this lock is placed to avoid two issues, basically:
a) completion var vb->acked corruption (overriden upon several
initializations);
b) virtqueue operations (inflate/deflate) being called simultaneously;
Even though Michael's patch addresses the case (a), as far as this patch series is concerned, it shows no way to prevent case (b), if two or more threads are calling tell_host() simultaneously.
Quoted 37 lines
Quoted 37 lines
static void
set_page_pfns(u32 pfns[], struct page *page)drivers/virtio/virtio_balloon.c +10
-3@@ -126,9 +132,12 @@ static void set_page_pfns(u32 pfns[], struct page *page)static void fill_balloon (struct virtio_balloon *vb,size_t num) { +struct scatterlist sg; +int alloc_failed =0 ;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |@@ -138,8 +147,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)dev_printk (KERN_INFO , &vb->vdev->dev,"Out of puff! Can't get %zu pages\n" , num); -/* Sleep for at least 1/5 of a second before retry. */ -msleep (200 ); + alloc_failed =1 ;break ; }set_page_pfns (vb->pfns + vb->num_pfns, page);@@ -149,10 +157,19 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) }/* Didn't get any? Oh well. */ -if (vb->num_pfns ==0 ) +if (vb->num_pfns ==0 ) { +spin_unlock (&vb->pfn_list_lock);return ; + } + +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock);
Quoted 10 lines
drivers/virtio/virtio_balloon.c static void fill_balloon(struct virtio_balloon *vb, size_t num) · line 172
Quoted 6 lines
drivers/virtio/virtio_balloon.c +5
-1-tell_host (vb, vb->inflate_vq); +/* alloc_page failed, sleep for at least 1/5 of a sec before retry. */ +if (alloc_failed) +msleep (200 ); + +tell_host (vb, vb->inflate_vq, &sg);So, we drop the lock which procects vp->pfns[] and
vb->num_pfns, then use it intell_host? Surely it could be corrupted between there.
The lock is dropped following these conditions:
a) virtqueue_add_buf() works based on a scatterlist array (buf);
b) vp->pfns[] and vb->num_pfns are not anymore being directly accessed/updated
at tell_host() level;
c) *vb ptr address is only used as a token to identify the buffer at
virtqueue_add_buf() and no particular struct's element is updated/accessed;d) we are not supposed to block/sleep while holding the spinlock;
Changes made to vp->pfns[] and vb->num_pfns after the spinlock is released doesn't matter for a particular thread anymore since the scatterlist setup is now moved outside tell_host() and it's being made within the locked session, and no one else down that codepath directly uses vp->pfns[] or vb->num_pfns to make its decisions.
Unfortunately, I'm not able to see the same corruption window you've spotted. Am I missing something here?
Cheers!
Rafael
Quoted 21 lines
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-0drivers/virtio/virtio_balloon.c struct virtio_balloon · line 60
Quoted 10 lines
drivers/virtio/virtio_balloon.c +4
-0@@ -51,6 +51,10 @@ struct virtio_balloon/* Number of balloon pages we've told the Host we're not using. */ unsigned int num_pages; + +/* Protect 'pages', 'pfns' & 'num_pnfs' against concurrent updates */ +spinlock_t pfn_list_lock; +/* * The pages we've told the Host we're not using. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE You might be better of taking
num_pfnsand pfns[] out of structvirtio_balloon, and putting them on the stack (maybe 64, not 256).Cheers,
Rusty.
Re: [PATCH v3 2/4] virtio_balloon: handle concurrent accesses to virtio_balloon struct elements
In reply to Rusty RussellHowdy Rusty,
First and foremost, thank you very much for taking the time to go through this proposal and provide me with such valuable feedback. I really appreciate that.
Quoted 9 lines · Rusty Russell
Quoted 5 lines · Rafael Aquini
This patch introduces access sychronization to critical elements of struct
virtio_balloon, in order to allow the thread concurrency compaction/migration bits might ended up imposing to the balloon driver on several situations.Signed-off-by Rafael Aquini <[email protected]>That's pretty vague, and it's almost impossible to audit this.
I'll definetely attempt to improve this one.
Despite it looks concise to me as it states the "whats" and the "whys", any clue on how to improve it and turn it into something that would make a lot more sense is very welcome and appreciated. I'm probably failing miserably to express the whole idea because I'm a terrible writer, no doubts about it. :)
Quoted 15 lines
Quoted 15 lines
drivers/virtio/virtio_balloon.c +7
-1+/* Protection for concurrent accesses to balloon virtqueues and vb->acked */ +DEFINE_MUTEX (vb_queue_completion); +static void tell_host (struct virtio_balloon *vb,struct virtqueue *vq, +struct scatterlist *sg) +{ +mutex_lock (&vb_queue_completion);init_completion (&vb->acked);/* We should always be able to add one buffer to an empty queue. */ -if (virtqueue_add_buf (vq, &sg,1 ,0 , vb,GFP_KERNEL ) <0 ) +if (virtqueue_add_buf (vq, sg,1 ,0 , vb,GFP_KERNEL ) <0 )BUG ();virtqueue_kick (vq);
Quoted 8 lines
drivers/virtio/virtio_balloon.c static void balloon_ack(struct virtqueue *vq) · line 121
Quoted 4 lines
drivers/virtio/virtio_balloon.c +1
-0/* When host has read buffer, this completes via balloon_ack */ wait_for_completion (&vb->acked); +mutex_unlock (&vb_queue_completion); }OK, this lock is superceded by Michael's patch, and AFAICT is not due to any requirement introduced by these patches.
Unfortunately, I'm compelled to disagree with you on this one.
Because tell_host() can be called concurrently, this lock is placed to avoid two issues, basically:
a) completion var vb->acked corruption (overriden upon several
initializations);
b) virtqueue operations (inflate/deflate) being called simultaneously;
Even though Michael's patch addresses the case (a), as far as this patch series is concerned, it shows no way to prevent case (b), if two or more threads are calling tell_host() simultaneously.
Quoted 37 lines
Quoted 37 lines
static void
set_page_pfns(u32 pfns[], struct page *page)drivers/virtio/virtio_balloon.c +10
-3@@ -126,9 +132,12 @@ static void set_page_pfns(u32 pfns[], struct page *page)static void fill_balloon (struct virtio_balloon *vb,size_t num) { +struct scatterlist sg; +int alloc_failed =0 ;/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); +spin_lock (&vb->pfn_list_lock);for (vb->num_pfns =0 ; vb->num_pfns < num; vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |@@ -138,8 +147,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)dev_printk (KERN_INFO , &vb->vdev->dev,"Out of puff! Can't get %zu pages\n" , num); -/* Sleep for at least 1/5 of a second before retry. */ -msleep (200 ); + alloc_failed =1 ;break ; }set_page_pfns (vb->pfns + vb->num_pfns, page);@@ -149,10 +157,19 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num) }/* Didn't get any? Oh well. */ -if (vb->num_pfns ==0 ) +if (vb->num_pfns ==0 ) { +spin_unlock (&vb->pfn_list_lock);return ; + } + +sg_init_one (&sg, vb->pfns,sizeof (vb->pfns[0 ]) * vb->num_pfns); +spin_unlock (&vb->pfn_list_lock);
Quoted 10 lines
drivers/virtio/virtio_balloon.c static void fill_balloon(struct virtio_balloon *vb, size_t num) · line 172
Quoted 6 lines
drivers/virtio/virtio_balloon.c +5
-1-tell_host (vb, vb->inflate_vq); +/* alloc_page failed, sleep for at least 1/5 of a sec before retry. */ +if (alloc_failed) +msleep (200 ); + +tell_host (vb, vb->inflate_vq, &sg);So, we drop the lock which procects vp->pfns[] and
vb->num_pfns, then use it intell_host? Surely it could be corrupted between there.
The lock is dropped following these conditions:
a) virtqueue_add_buf() works based on a scatterlist array (buf);
b) vp->pfns[] and vb->num_pfns are not anymore being directly accessed/updated
at tell_host() level;
c) *vb ptr address is only used as a token to identify the buffer at
virtqueue_add_buf() and no particular struct's element is updated/accessed;d) we are not supposed to block/sleep while holding the spinlock;
Changes made to vp->pfns[] and vb->num_pfns after the spinlock is released doesn't matter for a particular thread anymore since the scatterlist setup is now moved outside tell_host() and it's being made within the locked session, and no one else down that codepath directly uses vp->pfns[] or vb->num_pfns to make its decisions.
Unfortunately, I'm not able to see the same corruption window you've spotted. Am I missing something here?
Cheers!
Rafael
Quoted 19 lines
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-0drivers/virtio/virtio_balloon.c struct virtio_balloon · line 60
Quoted 10 lines
drivers/virtio/virtio_balloon.c +4
-0@@ -51,6 +51,10 @@ struct virtio_balloon/* Number of balloon pages we've told the Host we're not using. */ unsigned int num_pages; + +/* Protect 'pages', 'pfns' & 'num_pnfs' against concurrent updates */ +spinlock_t pfn_list_lock; +/* * The pages we've told the Host we're not using. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE You might be better of taking
num_pfnsand pfns[] out of structvirtio_balloon, and putting them on the stack (maybe 64, not 256).
Quoted 2 lines
Cheers,
Rusty.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>