blob: 22caf808bfaba97e45662de0f4150632bcb85d2f [file] [log] [blame]
Sasha Levin1e214a52011-11-03 10:20:04 +02001/*
2 * Virtio balloon implementation, inspired by Dor Laor and Marcelo
Rusty Russell6b35e402008-02-04 23:50:12 -05003 * Tosatti's implementations.
4 *
5 * Copyright 2008 Rusty Russell IBM Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Sasha Levin1e214a52011-11-03 10:20:04 +020021
Rusty Russell6b35e402008-02-04 23:50:12 -050022#include <linux/virtio.h>
23#include <linux/virtio_balloon.h>
24#include <linux/swap.h>
Petr Mladekfad7b7b2016-01-25 17:38:05 +010025#include <linux/workqueue.h>
Johann Felix Soden6659a0f2008-02-06 01:40:22 -080026#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040028#include <linux/module.h>
Rafael Aquinie2250422012-12-11 16:02:45 -080029#include <linux/balloon_compaction.h>
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +103030#include <linux/oom.h>
Michael S. Tsirkin3d2a3772015-03-10 11:55:08 +103031#include <linux/wait.h>
Igor Redko5057dcd2016-03-17 14:19:08 -070032#include <linux/mm.h>
Minchan Kimb1123ea62016-07-26 15:23:09 -070033#include <linux/mount.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010034#include <linux/magic.h>
Rusty Russell6b35e402008-02-04 23:50:12 -050035
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +030036/*
37 * Balloon device works in 4K page units. So each page is pointed to by
38 * multiple balloon pages. All memory counters in this driver are in balloon
39 * page units.
40 */
Rafael Aquinie2250422012-12-11 16:02:45 -080041#define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
42#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +103043#define OOM_VBALLOON_DEFAULT_PAGES 256
44#define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80
45
46static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES;
47module_param(oom_pages, int, S_IRUSR | S_IWUSR);
48MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +030049
Minchan Kimb1123ea62016-07-26 15:23:09 -070050#ifdef CONFIG_BALLOON_COMPACTION
51static struct vfsmount *balloon_mnt;
52#endif
53
Michael S. Tsirkin25e65e42015-01-15 13:33:31 +020054struct virtio_balloon {
Rusty Russell6b35e402008-02-04 23:50:12 -050055 struct virtio_device *vdev;
Adam Litke9564e132009-11-30 10:14:15 -060056 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
Rusty Russell6b35e402008-02-04 23:50:12 -050057
Petr Mladekfad7b7b2016-01-25 17:38:05 +010058 /* The balloon servicing is delegated to a freezable workqueue. */
Petr Mladekfd0e21c2016-01-25 17:38:06 +010059 struct work_struct update_balloon_stats_work;
60 struct work_struct update_balloon_size_work;
Rusty Russell6b35e402008-02-04 23:50:12 -050061
Petr Mladekfad7b7b2016-01-25 17:38:05 +010062 /* Prevent updating balloon when it is being canceled. */
63 spinlock_t stop_update_lock;
64 bool stop_update;
Rusty Russell6b35e402008-02-04 23:50:12 -050065
66 /* Waiting for host to ack the pages we released. */
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +030067 wait_queue_head_t acked;
Rusty Russell6b35e402008-02-04 23:50:12 -050068
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +030069 /* Number of balloon pages we've told the Host we're not using. */
Rusty Russell6b35e402008-02-04 23:50:12 -050070 unsigned int num_pages;
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +030071 /*
Rafael Aquinie2250422012-12-11 16:02:45 -080072 * The pages we've told the Host we're not using are enqueued
73 * at vb_dev_info->pages list.
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +030074 * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
75 * to num_pages above.
76 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070077 struct balloon_dev_info vb_dev_info;
Rafael Aquinie2250422012-12-11 16:02:45 -080078
79 /* Synchronize access/update to this struct virtio_balloon elements */
80 struct mutex balloon_lock;
Rusty Russell6b35e402008-02-04 23:50:12 -050081
82 /* The array of pfns we tell the Host about. */
83 unsigned int num_pfns;
Michael S. Tsirkin87c94032016-05-17 13:31:18 +030084 __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
Adam Litke9564e132009-11-30 10:14:15 -060085
86 /* Memory statistics */
87 struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +103088
89 /* To register callback in oom notifier call chain */
90 struct notifier_block nb;
Rusty Russell6b35e402008-02-04 23:50:12 -050091};
92
93static struct virtio_device_id id_table[] = {
94 { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
95 { 0 },
96};
97
Hollis Blanchard1b4aa2f2008-11-13 15:48:33 -060098static u32 page_to_balloon_pfn(struct page *page)
99{
100 unsigned long pfn = page_to_pfn(page);
101
102 BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
103 /* Convert pfn from Linux page size to balloon page size. */
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300104 return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
105}
106
107static struct page *balloon_pfn_to_page(u32 pfn)
108{
109 BUG_ON(pfn % VIRTIO_BALLOON_PAGES_PER_PAGE);
110 return pfn_to_page(pfn / VIRTIO_BALLOON_PAGES_PER_PAGE);
Hollis Blanchard1b4aa2f2008-11-13 15:48:33 -0600111}
112
Rusty Russell6b35e402008-02-04 23:50:12 -0500113static void balloon_ack(struct virtqueue *vq)
114{
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300115 struct virtio_balloon *vb = vq->vdev->priv;
Rusty Russell6b35e402008-02-04 23:50:12 -0500116
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300117 wake_up(&vb->acked);
Rusty Russell6b35e402008-02-04 23:50:12 -0500118}
119
120static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
121{
122 struct scatterlist sg;
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300123 unsigned int len;
Rusty Russell6b35e402008-02-04 23:50:12 -0500124
125 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
126
Rusty Russell6b35e402008-02-04 23:50:12 -0500127 /* We should always be able to add one buffer to an empty queue. */
Rusty Russell4951cc92014-03-13 11:23:40 +1030128 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
Michael S. Tsirkin946cfe02010-04-12 16:18:28 +0300129 virtqueue_kick(vq);
Rusty Russell6b35e402008-02-04 23:50:12 -0500130
131 /* When host has read buffer, this completes via balloon_ack */
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300132 wait_event(vb->acked, virtqueue_get_buf(vq, &len));
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100133
Rusty Russell6b35e402008-02-04 23:50:12 -0500134}
135
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300136static void set_page_pfns(struct virtio_balloon *vb,
137 __virtio32 pfns[], struct page *page)
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300138{
139 unsigned int i;
140
141 /* Set balloon pfns pointing at this page.
142 * Note that the first pfn points at start of the page. */
143 for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300144 pfns[i] = cpu_to_virtio32(vb->vdev,
145 page_to_balloon_pfn(page) + i);
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300146}
147
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100148static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
Rusty Russell6b35e402008-02-04 23:50:12 -0500149{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700150 struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100151 unsigned num_allocated_pages;
Rafael Aquinie2250422012-12-11 16:02:45 -0800152
Rusty Russell6b35e402008-02-04 23:50:12 -0500153 /* We can only do one array worth at a time. */
154 num = min(num, ARRAY_SIZE(vb->pfns));
155
Rafael Aquinie2250422012-12-11 16:02:45 -0800156 mutex_lock(&vb->balloon_lock);
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300157 for (vb->num_pfns = 0; vb->num_pfns < num;
158 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
Rafael Aquinie2250422012-12-11 16:02:45 -0800159 struct page *page = balloon_page_enqueue(vb_dev_info);
160
Rusty Russell6b35e402008-02-04 23:50:12 -0500161 if (!page) {
Joe Perches800ba5e2012-10-31 09:27:22 +1030162 dev_info_ratelimited(&vb->vdev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800163 "Out of puff! Can't get %u pages\n",
164 VIRTIO_BALLOON_PAGES_PER_PAGE);
Rusty Russell6b35e402008-02-04 23:50:12 -0500165 /* Sleep for at least 1/5 of a second before retry. */
166 msleep(200);
167 break;
168 }
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300169 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300170 vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
Denis V. Lunev997e1202015-08-20 00:49:49 +0300171 if (!virtio_has_feature(vb->vdev,
172 VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
173 adjust_managed_page_count(page, -1);
Rusty Russell6b35e402008-02-04 23:50:12 -0500174 }
175
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100176 num_allocated_pages = vb->num_pfns;
Rafael Aquinie2250422012-12-11 16:02:45 -0800177 /* Did we get any? */
178 if (vb->num_pfns != 0)
179 tell_host(vb, vb->inflate_vq);
180 mutex_unlock(&vb->balloon_lock);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100181
182 return num_allocated_pages;
Rusty Russell6b35e402008-02-04 23:50:12 -0500183}
184
Denis V. Lunevb4d34032015-08-20 00:49:48 +0300185static void release_pages_balloon(struct virtio_balloon *vb)
Rusty Russell6b35e402008-02-04 23:50:12 -0500186{
187 unsigned int i;
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300188 struct page *page;
Rusty Russell6b35e402008-02-04 23:50:12 -0500189
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300190 /* Find pfns pointing at start of each page, get pages and free them. */
Denis V. Lunevb4d34032015-08-20 00:49:48 +0300191 for (i = 0; i < vb->num_pfns; i += VIRTIO_BALLOON_PAGES_PER_PAGE) {
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300192 page = balloon_pfn_to_page(virtio32_to_cpu(vb->vdev,
193 vb->pfns[i]));
Denis V. Lunev997e1202015-08-20 00:49:49 +0300194 if (!virtio_has_feature(vb->vdev,
195 VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
196 adjust_managed_page_count(page, 1);
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700197 put_page(page); /* balloon reference */
Rusty Russell6b35e402008-02-04 23:50:12 -0500198 }
199}
200
Raushaniya Maksudova1fd9c672014-11-10 09:35:29 +1030201static unsigned leak_balloon(struct virtio_balloon *vb, size_t num)
Rusty Russell6b35e402008-02-04 23:50:12 -0500202{
Raushaniya Maksudova1fd9c672014-11-10 09:35:29 +1030203 unsigned num_freed_pages;
Rusty Russell6b35e402008-02-04 23:50:12 -0500204 struct page *page;
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700205 struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
Rusty Russell6b35e402008-02-04 23:50:12 -0500206
207 /* We can only do one array worth at a time. */
208 num = min(num, ARRAY_SIZE(vb->pfns));
209
Rafael Aquinie2250422012-12-11 16:02:45 -0800210 mutex_lock(&vb->balloon_lock);
Konstantin Neumoin37cf99e2016-07-11 15:28:59 +0300211 /* We can't release more pages than taken */
212 num = min(num, (size_t)vb->num_pages);
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300213 for (vb->num_pfns = 0; vb->num_pfns < num;
214 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
Rafael Aquinie2250422012-12-11 16:02:45 -0800215 page = balloon_page_dequeue(vb_dev_info);
216 if (!page)
217 break;
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300218 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
Michael S. Tsirkin3ccc9372012-04-12 16:38:00 +0300219 vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
Rusty Russell6b35e402008-02-04 23:50:12 -0500220 }
221
Raushaniya Maksudova1fd9c672014-11-10 09:35:29 +1030222 num_freed_pages = vb->num_pfns;
Dave Hansenbf50e692011-04-07 10:43:25 -0700223 /*
224 * Note that if
225 * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
226 * is true, we *have* to do it in this order
227 */
Luiz Capitulino8c6bab42013-07-02 15:35:13 +0930228 if (vb->num_pfns != 0)
229 tell_host(vb, vb->deflate_vq);
Denis V. Lunevb4d34032015-08-20 00:49:48 +0300230 release_pages_balloon(vb);
Minchan Kimf68b9922015-12-28 08:35:12 +0900231 mutex_unlock(&vb->balloon_lock);
Raushaniya Maksudova1fd9c672014-11-10 09:35:29 +1030232 return num_freed_pages;
Rusty Russell6b35e402008-02-04 23:50:12 -0500233}
234
Adam Litke9564e132009-11-30 10:14:15 -0600235static inline void update_stat(struct virtio_balloon *vb, int idx,
236 u16 tag, u64 val)
237{
238 BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
Michael S. Tsirkindf81b292015-04-15 10:17:43 +0930239 vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
240 vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
Adam Litke9564e132009-11-30 10:14:15 -0600241}
242
243#define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
244
Ladi Prosek9646b262017-03-28 18:46:58 +0200245static unsigned int update_balloon_stats(struct virtio_balloon *vb)
Adam Litke9564e132009-11-30 10:14:15 -0600246{
247 unsigned long events[NR_VM_EVENT_ITEMS];
248 struct sysinfo i;
Ladi Prosek9646b262017-03-28 18:46:58 +0200249 unsigned int idx = 0;
Igor Redko5057dcd2016-03-17 14:19:08 -0700250 long available;
Adam Litke9564e132009-11-30 10:14:15 -0600251
252 all_vm_events(events);
253 si_meminfo(&i);
254
Igor Redko5057dcd2016-03-17 14:19:08 -0700255 available = si_mem_available();
256
Arnd Bergmannf0bb2d52017-03-28 18:46:59 +0200257#ifdef CONFIG_VM_EVENT_COUNTERS
Adam Litke9564e132009-11-30 10:14:15 -0600258 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
259 pages_to_bytes(events[PSWPIN]));
260 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
261 pages_to_bytes(events[PSWPOUT]));
262 update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
263 update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
Arnd Bergmannf0bb2d52017-03-28 18:46:59 +0200264#endif
Adam Litke9564e132009-11-30 10:14:15 -0600265 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
266 pages_to_bytes(i.freeram));
267 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
268 pages_to_bytes(i.totalram));
Igor Redko5057dcd2016-03-17 14:19:08 -0700269 update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
270 pages_to_bytes(available));
Ladi Prosek9646b262017-03-28 18:46:58 +0200271
272 return idx;
Adam Litke9564e132009-11-30 10:14:15 -0600273}
274
275/*
276 * While most virtqueues communicate guest-initiated requests to the hypervisor,
277 * the stats queue operates in reverse. The driver initializes the virtqueue
278 * with a single buffer. From that point forward, all conversations consist of
279 * a hypervisor request (a call to this function) which directs us to refill
Adam Litke1f34c712009-12-10 16:35:15 -0600280 * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100281 * we delegate the job to a freezable workqueue that will do the actual work via
282 * stats_handle_request().
Adam Litke9564e132009-11-30 10:14:15 -0600283 */
Adam Litke1f34c712009-12-10 16:35:15 -0600284static void stats_request(struct virtqueue *vq)
Adam Litke9564e132009-11-30 10:14:15 -0600285{
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300286 struct virtio_balloon *vb = vq->vdev->priv;
Adam Litke9564e132009-11-30 10:14:15 -0600287
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100288 spin_lock(&vb->stop_update_lock);
289 if (!vb->stop_update)
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100290 queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100291 spin_unlock(&vb->stop_update_lock);
Adam Litke1f34c712009-12-10 16:35:15 -0600292}
Adam Litke9564e132009-11-30 10:14:15 -0600293
Adam Litke1f34c712009-12-10 16:35:15 -0600294static void stats_handle_request(struct virtio_balloon *vb)
295{
296 struct virtqueue *vq;
297 struct scatterlist sg;
Ladi Prosek9646b262017-03-28 18:46:58 +0200298 unsigned int len, num_stats;
Adam Litke1f34c712009-12-10 16:35:15 -0600299
Ladi Prosek9646b262017-03-28 18:46:58 +0200300 num_stats = update_balloon_stats(vb);
Adam Litke9564e132009-11-30 10:14:15 -0600301
Adam Litke1f34c712009-12-10 16:35:15 -0600302 vq = vb->stats_vq;
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300303 if (!virtqueue_get_buf(vq, &len))
304 return;
Ladi Prosek9646b262017-03-28 18:46:58 +0200305 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
Rusty Russell4951cc92014-03-13 11:23:40 +1030306 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
Michael S. Tsirkin946cfe02010-04-12 16:18:28 +0300307 virtqueue_kick(vq);
Adam Litke9564e132009-11-30 10:14:15 -0600308}
309
Rusty Russell6b35e402008-02-04 23:50:12 -0500310static void virtballoon_changed(struct virtio_device *vdev)
311{
312 struct virtio_balloon *vb = vdev->priv;
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100313 unsigned long flags;
Rusty Russell6b35e402008-02-04 23:50:12 -0500314
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100315 spin_lock_irqsave(&vb->stop_update_lock, flags);
316 if (!vb->stop_update)
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100317 queue_work(system_freezable_wq, &vb->update_balloon_size_work);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100318 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
Rusty Russell6b35e402008-02-04 23:50:12 -0500319}
320
Rusty Russellbdc16812008-03-17 22:58:15 -0500321static inline s64 towards_target(struct virtio_balloon *vb)
Rusty Russell6b35e402008-02-04 23:50:12 -0500322{
David Gibson1a872282012-04-12 15:36:34 +1000323 s64 target;
Michael S. Tsirkindf81b292015-04-15 10:17:43 +0930324 u32 num_pages;
David Gibson1a872282012-04-12 15:36:34 +1000325
Michael S. Tsirkindf81b292015-04-15 10:17:43 +0930326 virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages,
327 &num_pages);
Rusty Russell855e0c52013-10-14 18:11:51 +1030328
Michael S. Tsirkindf81b292015-04-15 10:17:43 +0930329 /* Legacy balloon config space is LE, unlike all other devices. */
330 if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
331 num_pages = le32_to_cpu((__force __le32)num_pages);
332
333 target = num_pages;
David Gibson1a872282012-04-12 15:36:34 +1000334 return target - vb->num_pages;
Rusty Russell6b35e402008-02-04 23:50:12 -0500335}
336
337static void update_balloon_size(struct virtio_balloon *vb)
338{
Michael S. Tsirkindf81b292015-04-15 10:17:43 +0930339 u32 actual = vb->num_pages;
340
341 /* Legacy balloon config space is LE, unlike all other devices. */
342 if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
343 actual = (__force u32)cpu_to_le32(actual);
Rusty Russell6b35e402008-02-04 23:50:12 -0500344
Luiz Capitulino3459f112013-12-05 13:04:10 +1030345 virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual,
Rusty Russell855e0c52013-10-14 18:11:51 +1030346 &actual);
Rusty Russell6b35e402008-02-04 23:50:12 -0500347}
348
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +1030349/*
350 * virtballoon_oom_notify - release pages when system is under severe
351 * memory pressure (called from out_of_memory())
352 * @self : notifier block struct
353 * @dummy: not used
354 * @parm : returned - number of freed pages
355 *
356 * The balancing of memory by use of the virtio balloon should not cause
357 * the termination of processes while there are pages in the balloon.
358 * If virtio balloon manages to release some memory, it will make the
359 * system return and retry the allocation that forced the OOM killer
360 * to run.
361 */
362static int virtballoon_oom_notify(struct notifier_block *self,
363 unsigned long dummy, void *parm)
364{
365 struct virtio_balloon *vb;
366 unsigned long *freed;
367 unsigned num_freed_pages;
368
369 vb = container_of(self, struct virtio_balloon, nb);
370 if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
371 return NOTIFY_OK;
372
373 freed = parm;
374 num_freed_pages = leak_balloon(vb, oom_pages);
375 update_balloon_size(vb);
376 *freed += num_freed_pages;
377
378 return NOTIFY_OK;
379}
380
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100381static void update_balloon_stats_func(struct work_struct *work)
Rusty Russell6b35e402008-02-04 23:50:12 -0500382{
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100383 struct virtio_balloon *vb;
Rusty Russell6b35e402008-02-04 23:50:12 -0500384
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100385 vb = container_of(work, struct virtio_balloon,
386 update_balloon_stats_work);
387 stats_handle_request(vb);
388}
Rusty Russell6b35e402008-02-04 23:50:12 -0500389
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100390static void update_balloon_size_func(struct work_struct *work)
Rusty Russell6b35e402008-02-04 23:50:12 -0500391{
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100392 struct virtio_balloon *vb;
393 s64 diff;
Michael S. Tsirkin3d2a3772015-03-10 11:55:08 +1030394
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100395 vb = container_of(work, struct virtio_balloon,
396 update_balloon_size_work);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100397 diff = towards_target(vb);
Michael S. Tsirkin3d2a3772015-03-10 11:55:08 +1030398
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100399 if (diff > 0)
400 diff -= fill_balloon(vb, diff);
401 else if (diff < 0)
402 diff += leak_balloon(vb, -diff);
403 update_balloon_size(vb);
Rusty Russell1f74ef02014-03-13 11:23:38 +1030404
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100405 if (diff)
406 queue_work(system_freezable_wq, work);
Rusty Russell6b35e402008-02-04 23:50:12 -0500407}
408
Amit Shahbe91c332011-12-22 16:58:34 +0530409static int init_vqs(struct virtio_balloon *vb)
Rusty Russell6b35e402008-02-04 23:50:12 -0500410{
Adam Litke9564e132009-11-30 10:14:15 -0600411 struct virtqueue *vqs[3];
Adam Litke1f34c712009-12-10 16:35:15 -0600412 vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800413 static const char * const names[] = { "inflate", "deflate", "stats" };
Adam Litke9564e132009-11-30 10:14:15 -0600414 int err, nvqs;
Rusty Russell6b35e402008-02-04 23:50:12 -0500415
Amit Shahbe91c332011-12-22 16:58:34 +0530416 /*
417 * We expect two virtqueues: inflate and deflate, and
418 * optionally stat.
419 */
Adam Litke9564e132009-11-30 10:14:15 -0600420 nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200421 err = virtio_find_vqs(vb->vdev, nvqs, vqs, callbacks, names, NULL);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600422 if (err)
Amit Shahbe91c332011-12-22 16:58:34 +0530423 return err;
Rusty Russell6b35e402008-02-04 23:50:12 -0500424
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600425 vb->inflate_vq = vqs[0];
426 vb->deflate_vq = vqs[1];
Adam Litke9564e132009-11-30 10:14:15 -0600427 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
428 struct scatterlist sg;
Ladi Prosek9646b262017-03-28 18:46:58 +0200429 unsigned int num_stats;
Adam Litke9564e132009-11-30 10:14:15 -0600430 vb->stats_vq = vqs[2];
431
432 /*
433 * Prime this virtqueue with one buffer so the hypervisor can
Rusty Russell4951cc92014-03-13 11:23:40 +1030434 * use it to signal us later (it can't be broken yet!).
Adam Litke9564e132009-11-30 10:14:15 -0600435 */
Ladi Prosek9646b262017-03-28 18:46:58 +0200436 num_stats = update_balloon_stats(vb);
Ladi Prosekfc865322017-03-23 08:04:18 +0100437
Ladi Prosek9646b262017-03-28 18:46:58 +0200438 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
Rusty Russell92549ab2013-03-20 15:44:30 +1030439 if (virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, GFP_KERNEL)
Rusty Russellf96fde42012-01-12 15:44:42 +1030440 < 0)
Adam Litke9564e132009-11-30 10:14:15 -0600441 BUG();
Michael S. Tsirkin946cfe02010-04-12 16:18:28 +0300442 virtqueue_kick(vb->stats_vq);
Adam Litke9564e132009-11-30 10:14:15 -0600443 }
Amit Shahbe91c332011-12-22 16:58:34 +0530444 return 0;
445}
446
Rafael Aquinie2250422012-12-11 16:02:45 -0800447#ifdef CONFIG_BALLOON_COMPACTION
448/*
449 * virtballoon_migratepage - perform the balloon page migration on behalf of
450 * a compation thread. (called under page lock)
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700451 * @vb_dev_info: the balloon device
Rafael Aquinie2250422012-12-11 16:02:45 -0800452 * @newpage: page that will replace the isolated page after migration finishes.
453 * @page : the isolated (old) page that is about to be migrated to newpage.
454 * @mode : compaction mode -- not used for balloon page migration.
455 *
456 * After a ballooned page gets isolated by compaction procedures, this is the
457 * function that performs the page migration on behalf of a compaction thread
458 * The page migration for virtio balloon is done in a simple swap fashion which
459 * follows these two macro steps:
460 * 1) insert newpage into vb->pages list and update the host about it;
461 * 2) update the host about the old page removed from vb->pages list;
462 *
463 * This function preforms the balloon page migration task.
464 * Called through balloon_mapping->a_ops->migratepage
465 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700466static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
Rafael Aquinie2250422012-12-11 16:02:45 -0800467 struct page *newpage, struct page *page, enum migrate_mode mode)
468{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700469 struct virtio_balloon *vb = container_of(vb_dev_info,
470 struct virtio_balloon, vb_dev_info);
Rafael Aquinie2250422012-12-11 16:02:45 -0800471 unsigned long flags;
472
Rafael Aquinie2250422012-12-11 16:02:45 -0800473 /*
474 * In order to avoid lock contention while migrating pages concurrently
475 * to leak_balloon() or fill_balloon() we just give up the balloon_lock
476 * this turn, as it is easier to retry the page migration later.
477 * This also prevents fill_balloon() getting stuck into a mutex
478 * recursion in the case it ends up triggering memory compaction
479 * while it is attempting to inflate the ballon.
480 */
481 if (!mutex_trylock(&vb->balloon_lock))
482 return -EAGAIN;
483
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700484 get_page(newpage); /* balloon reference */
485
Rafael Aquinie2250422012-12-11 16:02:45 -0800486 /* balloon's page migration 1st step -- inflate "newpage" */
487 spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700488 balloon_page_insert(vb_dev_info, newpage);
Rafael Aquinie2250422012-12-11 16:02:45 -0800489 vb_dev_info->isolated_pages--;
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700490 __count_vm_event(BALLOON_MIGRATE);
Rafael Aquinie2250422012-12-11 16:02:45 -0800491 spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
492 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300493 set_page_pfns(vb, vb->pfns, newpage);
Rafael Aquinie2250422012-12-11 16:02:45 -0800494 tell_host(vb, vb->inflate_vq);
495
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700496 /* balloon's page migration 2nd step -- deflate "page" */
Rafael Aquinie2250422012-12-11 16:02:45 -0800497 balloon_page_delete(page);
498 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
Michael S. Tsirkin87c94032016-05-17 13:31:18 +0300499 set_page_pfns(vb, vb->pfns, page);
Rafael Aquinie2250422012-12-11 16:02:45 -0800500 tell_host(vb, vb->deflate_vq);
501
502 mutex_unlock(&vb->balloon_lock);
503
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700504 put_page(page); /* balloon reference */
505
Minchan Kimdd4123f2016-07-26 15:26:50 -0700506 return MIGRATEPAGE_SUCCESS;
Rafael Aquinie2250422012-12-11 16:02:45 -0800507}
Minchan Kimb1123ea62016-07-26 15:23:09 -0700508
509static struct dentry *balloon_mount(struct file_system_type *fs_type,
510 int flags, const char *dev_name, void *data)
511{
512 static const struct dentry_operations ops = {
513 .d_dname = simple_dname,
514 };
515
516 return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops,
517 BALLOON_KVM_MAGIC);
518}
519
520static struct file_system_type balloon_fs = {
521 .name = "balloon-kvm",
522 .mount = balloon_mount,
523 .kill_sb = kill_anon_super,
524};
525
Rafael Aquinie2250422012-12-11 16:02:45 -0800526#endif /* CONFIG_BALLOON_COMPACTION */
527
Amit Shahbe91c332011-12-22 16:58:34 +0530528static int virtballoon_probe(struct virtio_device *vdev)
529{
530 struct virtio_balloon *vb;
531 int err;
532
Michael S. Tsirkin2d9becc2015-01-12 16:23:37 +0200533 if (!vdev->config->get) {
534 dev_err(&vdev->dev, "%s failure: config access disabled\n",
535 __func__);
536 return -EINVAL;
537 }
538
Amit Shahbe91c332011-12-22 16:58:34 +0530539 vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
540 if (!vb) {
541 err = -ENOMEM;
542 goto out;
543 }
544
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100545 INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
546 INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100547 spin_lock_init(&vb->stop_update_lock);
548 vb->stop_update = false;
Amit Shahbe91c332011-12-22 16:58:34 +0530549 vb->num_pages = 0;
Rafael Aquinie2250422012-12-11 16:02:45 -0800550 mutex_init(&vb->balloon_lock);
Michael S. Tsirkin9c378ab2012-07-02 10:33:08 +0300551 init_waitqueue_head(&vb->acked);
Amit Shahbe91c332011-12-22 16:58:34 +0530552 vb->vdev = vdev;
Amit Shahbe91c332011-12-22 16:58:34 +0530553
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700554 balloon_devinfo_init(&vb->vb_dev_info);
Rafael Aquinie2250422012-12-11 16:02:45 -0800555
Amit Shahbe91c332011-12-22 16:58:34 +0530556 err = init_vqs(vb);
557 if (err)
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700558 goto out_free_vb;
Rusty Russell6b35e402008-02-04 23:50:12 -0500559
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +1030560 vb->nb.notifier_call = virtballoon_oom_notify;
561 vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY;
562 err = register_oom_notifier(&vb->nb);
563 if (err < 0)
Minchan Kimb1123ea62016-07-26 15:23:09 -0700564 goto out_del_vqs;
565
566#ifdef CONFIG_BALLOON_COMPACTION
567 balloon_mnt = kern_mount(&balloon_fs);
568 if (IS_ERR(balloon_mnt)) {
569 err = PTR_ERR(balloon_mnt);
570 unregister_oom_notifier(&vb->nb);
571 goto out_del_vqs;
572 }
573
574 vb->vb_dev_info.migratepage = virtballoon_migratepage;
575 vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
576 if (IS_ERR(vb->vb_dev_info.inode)) {
577 err = PTR_ERR(vb->vb_dev_info.inode);
578 kern_unmount(balloon_mnt);
579 unregister_oom_notifier(&vb->nb);
580 vb->vb_dev_info.inode = NULL;
581 goto out_del_vqs;
582 }
583 vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
584#endif
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +1030585
Michael S. Tsirkin88660f7fb2015-03-05 13:24:41 +1030586 virtio_device_ready(vdev);
587
Konstantin Neumoin8424af52016-09-29 13:17:12 +0300588 if (towards_target(vb))
589 virtballoon_changed(vdev);
Rusty Russell6b35e402008-02-04 23:50:12 -0500590 return 0;
591
Minchan Kimb1123ea62016-07-26 15:23:09 -0700592out_del_vqs:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600593 vdev->config->del_vqs(vdev);
Rusty Russell6b35e402008-02-04 23:50:12 -0500594out_free_vb:
595 kfree(vb);
596out:
597 return err;
598}
599
Amit Shahc877bab2012-04-27 00:45:57 +0530600static void remove_common(struct virtio_balloon *vb)
Rusty Russell6b35e402008-02-04 23:50:12 -0500601{
Rusty Russell6b35e402008-02-04 23:50:12 -0500602 /* There might be pages left in the balloon: free them. */
603 while (vb->num_pages)
604 leak_balloon(vb, vb->num_pages);
Amit Shahb8ae0eb2012-04-27 00:45:56 +0530605 update_balloon_size(vb);
Rusty Russell6b35e402008-02-04 23:50:12 -0500606
607 /* Now we reset the device so we can clean up the queues. */
Amit Shahc877bab2012-04-27 00:45:57 +0530608 vb->vdev->config->reset(vb->vdev);
Rusty Russell6b35e402008-02-04 23:50:12 -0500609
Amit Shahc877bab2012-04-27 00:45:57 +0530610 vb->vdev->config->del_vqs(vb->vdev);
611}
612
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800613static void virtballoon_remove(struct virtio_device *vdev)
Amit Shahc877bab2012-04-27 00:45:57 +0530614{
615 struct virtio_balloon *vb = vdev->priv;
616
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +1030617 unregister_oom_notifier(&vb->nb);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100618
619 spin_lock_irq(&vb->stop_update_lock);
620 vb->stop_update = true;
621 spin_unlock_irq(&vb->stop_update_lock);
Petr Mladekfd0e21c2016-01-25 17:38:06 +0100622 cancel_work_sync(&vb->update_balloon_size_work);
623 cancel_work_sync(&vb->update_balloon_stats_work);
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100624
Amit Shahc877bab2012-04-27 00:45:57 +0530625 remove_common(vb);
Yisheng Xie9c57b582017-02-24 15:00:40 -0800626#ifdef CONFIG_BALLOON_COMPACTION
Minchan Kimb1123ea62016-07-26 15:23:09 -0700627 if (vb->vb_dev_info.inode)
628 iput(vb->vb_dev_info.inode);
Yisheng Xie9c57b582017-02-24 15:00:40 -0800629
630 kern_unmount(balloon_mnt);
631#endif
Rusty Russell6b35e402008-02-04 23:50:12 -0500632 kfree(vb);
633}
634
Aaron Lu89107002013-09-17 09:25:23 +0930635#ifdef CONFIG_PM_SLEEP
Amit Shahe5629662011-12-22 16:58:35 +0530636static int virtballoon_freeze(struct virtio_device *vdev)
637{
Amit Shah4eb05d52012-02-29 17:42:51 +0530638 struct virtio_balloon *vb = vdev->priv;
639
Amit Shahe5629662011-12-22 16:58:35 +0530640 /*
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100641 * The workqueue is already frozen by the PM core before this
Amit Shahe5629662011-12-22 16:58:35 +0530642 * function is called.
643 */
Amit Shahc877bab2012-04-27 00:45:57 +0530644 remove_common(vb);
Amit Shahe5629662011-12-22 16:58:35 +0530645 return 0;
646}
647
Amit Shahc45b4162012-04-27 00:45:55 +0530648static int virtballoon_restore(struct virtio_device *vdev)
Amit Shah4eb05d52012-02-29 17:42:51 +0530649{
650 struct virtio_balloon *vb = vdev->priv;
651 int ret;
652
653 ret = init_vqs(vdev->priv);
654 if (ret)
655 return ret;
656
Michael S. Tsirkin486d2e62014-10-15 10:22:33 +1030657 virtio_device_ready(vdev);
658
Petr Mladekfad7b7b2016-01-25 17:38:05 +0100659 if (towards_target(vb))
660 virtballoon_changed(vdev);
Amit Shah4eb05d52012-02-29 17:42:51 +0530661 update_balloon_size(vb);
662 return 0;
663}
Amit Shahe5629662011-12-22 16:58:35 +0530664#endif
665
Michael S. Tsirkine41b1352017-06-13 20:56:44 +0300666static int virtballoon_validate(struct virtio_device *vdev)
667{
668 __virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM);
669 return 0;
670}
671
Adam Litke9564e132009-11-30 10:14:15 -0600672static unsigned int features[] = {
673 VIRTIO_BALLOON_F_MUST_TELL_HOST,
674 VIRTIO_BALLOON_F_STATS_VQ,
Raushaniya Maksudova5a10b7d2014-11-10 09:36:29 +1030675 VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
Adam Litke9564e132009-11-30 10:14:15 -0600676};
Rusty Russellc45a6812008-05-02 21:50:50 -0500677
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800678static struct virtio_driver virtio_balloon_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500679 .feature_table = features,
680 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell6b35e402008-02-04 23:50:12 -0500681 .driver.name = KBUILD_MODNAME,
682 .driver.owner = THIS_MODULE,
683 .id_table = id_table,
Michael S. Tsirkine41b1352017-06-13 20:56:44 +0300684 .validate = virtballoon_validate,
Rusty Russell6b35e402008-02-04 23:50:12 -0500685 .probe = virtballoon_probe,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800686 .remove = virtballoon_remove,
Rusty Russell6b35e402008-02-04 23:50:12 -0500687 .config_changed = virtballoon_changed,
Aaron Lu89107002013-09-17 09:25:23 +0930688#ifdef CONFIG_PM_SLEEP
Amit Shahe5629662011-12-22 16:58:35 +0530689 .freeze = virtballoon_freeze,
690 .restore = virtballoon_restore,
Amit Shahe5629662011-12-22 16:58:35 +0530691#endif
Rusty Russell6b35e402008-02-04 23:50:12 -0500692};
693
Rusty Russellb2a17022013-02-13 16:59:28 +1030694module_virtio_driver(virtio_balloon_driver);
Rusty Russell6b35e402008-02-04 23:50:12 -0500695MODULE_DEVICE_TABLE(virtio, id_table);
696MODULE_DESCRIPTION("Virtio balloon driver");
697MODULE_LICENSE("GPL");