Blame view

mm/balloon_compaction.c 4.69 KB
18468d93e   Rafael Aquini   mm: introduce a c...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * mm/balloon_compaction.c
   *
   * Common interface for making balloon pages movable by compaction.
   *
   * Copyright (C) 2012, Red Hat, Inc.  Rafael Aquini <aquini@redhat.com>
   */
  #include <linux/mm.h>
  #include <linux/slab.h>
  #include <linux/export.h>
  #include <linux/balloon_compaction.h>
  
  /*
18468d93e   Rafael Aquini   mm: introduce a c...
14
15
   * balloon_page_enqueue - allocates a new page and inserts it into the balloon
   *			  page list.
bdb428c82   Bogdan Sikora   lib+mm: fix few s...
16
   * @b_dev_info: balloon device descriptor where we will insert a new page to
18468d93e   Rafael Aquini   mm: introduce a c...
17
18
   *
   * Driver must call it to properly allocate a new enlisted balloon page
bdb428c82   Bogdan Sikora   lib+mm: fix few s...
19
   * before definitively removing it from the guest system.
18468d93e   Rafael Aquini   mm: introduce a c...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   * This function returns the page address for the recently enqueued page or
   * NULL in the case we fail to allocate a new page this turn.
   */
  struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
  {
  	unsigned long flags;
  	struct page *page = alloc_page(balloon_mapping_gfp_mask() |
  					__GFP_NOMEMALLOC | __GFP_NORETRY);
  	if (!page)
  		return NULL;
  
  	/*
  	 * Block others from accessing the 'page' when we get around to
  	 * establishing additional references. We should be the only one
  	 * holding a reference to the 'page' at this point.
  	 */
  	BUG_ON(!trylock_page(page));
  	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
9d1ba8056   Konstantin Khlebnikov   mm/balloon_compac...
38
  	balloon_page_insert(b_dev_info, page);
09316c09d   Konstantin Khlebnikov   mm/balloon_compac...
39
  	__count_vm_event(BALLOON_INFLATE);
18468d93e   Rafael Aquini   mm: introduce a c...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  	unlock_page(page);
  	return page;
  }
  EXPORT_SYMBOL_GPL(balloon_page_enqueue);
  
  /*
   * balloon_page_dequeue - removes a page from balloon's page list and returns
   *			  the its address to allow the driver release the page.
   * @b_dev_info: balloon device decriptor where we will grab a page from.
   *
   * Driver must call it to properly de-allocate a previous enlisted balloon page
   * before definetively releasing it back to the guest system.
   * This function returns the page address for the recently dequeued page or
   * NULL in the case we find balloon's page list temporarily empty due to
   * compaction isolated pages.
   */
  struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
  {
  	struct page *page, *tmp;
  	unsigned long flags;
  	bool dequeued_page;
  
  	dequeued_page = false;
21ea9fb69   Minchan Kim   virtio_balloon: f...
64
  	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
18468d93e   Rafael Aquini   mm: introduce a c...
65
66
67
68
69
70
71
  	list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
  		/*
  		 * Block others from accessing the 'page' while we get around
  		 * establishing additional references and preparing the 'page'
  		 * to be released by the balloon driver.
  		 */
  		if (trylock_page(page)) {
4d88e6f7d   Konstantin Khlebnikov   mm/balloon_compac...
72
  #ifdef CONFIG_BALLOON_COMPACTION
b1123ea6d   Minchan Kim   mm: balloon: use ...
73
  			if (PageIsolated(page)) {
d6d86c0a7   Konstantin Khlebnikov   mm/balloon_compac...
74
75
76
77
  				/* raced with isolation */
  				unlock_page(page);
  				continue;
  			}
4d88e6f7d   Konstantin Khlebnikov   mm/balloon_compac...
78
  #endif
18468d93e   Rafael Aquini   mm: introduce a c...
79
  			balloon_page_delete(page);
09316c09d   Konstantin Khlebnikov   mm/balloon_compac...
80
  			__count_vm_event(BALLOON_DEFLATE);
18468d93e   Rafael Aquini   mm: introduce a c...
81
82
83
84
85
  			unlock_page(page);
  			dequeued_page = true;
  			break;
  		}
  	}
21ea9fb69   Minchan Kim   virtio_balloon: f...
86
  	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
18468d93e   Rafael Aquini   mm: introduce a c...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  
  	if (!dequeued_page) {
  		/*
  		 * If we are unable to dequeue a balloon page because the page
  		 * list is empty and there is no isolated pages, then something
  		 * went out of track and some balloon pages are lost.
  		 * BUG() here, otherwise the balloon driver may get stuck into
  		 * an infinite loop while attempting to release all its pages.
  		 */
  		spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  		if (unlikely(list_empty(&b_dev_info->pages) &&
  			     !b_dev_info->isolated_pages))
  			BUG();
  		spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  		page = NULL;
  	}
  	return page;
  }
  EXPORT_SYMBOL_GPL(balloon_page_dequeue);
  
  #ifdef CONFIG_BALLOON_COMPACTION
18468d93e   Rafael Aquini   mm: introduce a c...
108

b1123ea6d   Minchan Kim   mm: balloon: use ...
109
  bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
18468d93e   Rafael Aquini   mm: introduce a c...
110
  {
9d1ba8056   Konstantin Khlebnikov   mm/balloon_compac...
111
  	struct balloon_dev_info *b_dev_info = balloon_page_device(page);
18468d93e   Rafael Aquini   mm: introduce a c...
112
  	unsigned long flags;
d6d86c0a7   Konstantin Khlebnikov   mm/balloon_compac...
113

18468d93e   Rafael Aquini   mm: introduce a c...
114
115
116
117
  	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  	list_del(&page->lru);
  	b_dev_info->isolated_pages++;
  	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
b1123ea6d   Minchan Kim   mm: balloon: use ...
118
119
  
  	return true;
18468d93e   Rafael Aquini   mm: introduce a c...
120
  }
b1123ea6d   Minchan Kim   mm: balloon: use ...
121
  void balloon_page_putback(struct page *page)
18468d93e   Rafael Aquini   mm: introduce a c...
122
  {
9d1ba8056   Konstantin Khlebnikov   mm/balloon_compac...
123
  	struct balloon_dev_info *b_dev_info = balloon_page_device(page);
18468d93e   Rafael Aquini   mm: introduce a c...
124
  	unsigned long flags;
d6d86c0a7   Konstantin Khlebnikov   mm/balloon_compac...
125

18468d93e   Rafael Aquini   mm: introduce a c...
126
127
128
129
130
  	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  	list_add(&page->lru, &b_dev_info->pages);
  	b_dev_info->isolated_pages--;
  	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  }
18468d93e   Rafael Aquini   mm: introduce a c...
131
132
  
  /* move_to_new_page() counterpart for a ballooned page */
b1123ea6d   Minchan Kim   mm: balloon: use ...
133
134
135
  int balloon_page_migrate(struct address_space *mapping,
  		struct page *newpage, struct page *page,
  		enum migrate_mode mode)
18468d93e   Rafael Aquini   mm: introduce a c...
136
  {
9d1ba8056   Konstantin Khlebnikov   mm/balloon_compac...
137
  	struct balloon_dev_info *balloon = balloon_page_device(page);
18468d93e   Rafael Aquini   mm: introduce a c...
138

7db7671f8   Hugh Dickins   mm: page migratio...
139
140
  	VM_BUG_ON_PAGE(!PageLocked(page), page);
  	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
18468d93e   Rafael Aquini   mm: introduce a c...
141

b1123ea6d   Minchan Kim   mm: balloon: use ...
142
143
  	return balloon->migratepage(balloon, newpage, page, mode);
  }
18468d93e   Rafael Aquini   mm: introduce a c...
144

b1123ea6d   Minchan Kim   mm: balloon: use ...
145
146
147
148
149
150
  const struct address_space_operations balloon_aops = {
  	.migratepage = balloon_page_migrate,
  	.isolate_page = balloon_page_isolate,
  	.putback_page = balloon_page_putback,
  };
  EXPORT_SYMBOL_GPL(balloon_aops);
18468d93e   Rafael Aquini   mm: introduce a c...
151

18468d93e   Rafael Aquini   mm: introduce a c...
152
  #endif /* CONFIG_BALLOON_COMPACTION */