History

virtio-mem: Paravirtualized memory hotplug

Each virtio-mem device owns exactly one memory region. It is responsible for adding/removing memory from that memory region on request. When the device driver starts up, the requested amount of memory is queried and then plugged to Linux. On request, further memory can be plugged or unplugged. This patch only implements the plugging part. On x86-64, memory can currently be plugged in 4MB ("subblock") granularity. When required, a new memory block will be added (e.g., usually 128MB on x86-64) in order to plug more subblocks. Only x86-64 was tested for now. The online_page callback is used to keep unplugged subblocks offline when onlining memory - similar to the Hyper-V balloon driver. Unplugged pages are marked PG_offline, to tell dump tools (e.g., makedumpfile) to skip them. User space is usually responsible for onlining the added memory. The memory hotplug notifier is used to synchronize virtio-mem activity against memory onlining/offlining. Each virtio-mem device can belong to a NUMA node, which allows us to easily add/remove small chunks of memory to/from a specific NUMA node by using multiple virtio-mem devices. Something that works even when the guest has no idea about the NUMA topology. One way to view virtio-mem is as a "resizable DIMM" or a DIMM with many "sub-DIMMS". This patch directly introduces the basic infrastructure to implement memory unplug. Especially the memory block states and subblock bitmaps will be heavily used there. Notes: - In case memory is to be onlined by user space, we limit the amount of offline memory blocks, to not run out of memory. This is esp. an issue if memory is added faster than it is getting onlined. - Suspend/Hibernate is not supported due to the way virtio-mem devices behave. Limited support might be possible in the future. - Reloading the device driver is not supported.

David Hildenbrand committed 6 years ago

Commit 5f1f79b1 parent 18e643c5 files · +1751 −0Browse files

Showing the changes to include/uapi/linux (2 of 5 files). Show all

Unified · Split

2 files
  1. include/uapi/linux/virtio_ids.h +1 −0
  2. include/uapi/linux/virtio_mem.h +200 −0
include/uapi/linux/virtio_ids.h+1−0
@@ -44,6 +44,7 @@ · 43 unmodified lines
4444 #define VIRTIO_ID_VSOCK 19 /* virtio vsock transport */
4545 #define VIRTIO_ID_CRYPTO 20 /* virtio crypto */
4646 #define VIRTIO_ID_IOMMU 23 /* virtio IOMMU */
47+#define VIRTIO_ID_MEM 24 /* virtio mem */
4748 #define VIRTIO_ID_FS 26 /* virtio filesystem */
4849 #define VIRTIO_ID_PMEM 27 /* virtio pmem */
4950 #define VIRTIO_ID_MAC80211_HWSIM 29 /* virtio mac80211-hwsim */
include/uapi/linux/virtio_mem.h+200−0
@@ -0,0 +1,200 @@
1+/* SPDX-License-Identifier: BSD-3-Clause */
2+/*
3+ * Virtio Mem Device
4+ *
5+ * Copyright Red Hat, Inc. 2020
6+ *
7+ * Authors:
8+ * David Hildenbrand <david@redhat.com>
9+ *
10+ * This header is BSD licensed so anyone can use the definitions
11+ * to implement compatible drivers/servers:
12+ *
13+ * Redistribution and use in source and binary forms, with or without
14+ * modification, are permitted provided that the following conditions
15+ * are met:
16+ * 1. Redistributions of source code must retain the above copyright
17+ * notice, this list of conditions and the following disclaimer.
18+ * 2. Redistributions in binary form must reproduce the above copyright
19+ * notice, this list of conditions and the following disclaimer in the
20+ * documentation and/or other materials provided with the distribution.
21+ * 3. Neither the name of IBM nor the names of its contributors
22+ * may be used to endorse or promote products derived from this software
23+ * without specific prior written permission.
24+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR
28+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35+ * SUCH DAMAGE.
36+ */
37+
38+#ifndef _LINUX_VIRTIO_MEM_H
39+#define _LINUX_VIRTIO_MEM_H
40+
41+#include <linux/types.h>
42+#include <linux/virtio_types.h>
43+#include <linux/virtio_ids.h>
44+#include <linux/virtio_config.h>
45+
46+/*
47+ * Each virtio-mem device manages a dedicated region in physical address
48+ * space. Each device can belong to a single NUMA node, multiple devices
49+ * for a single NUMA node are possible. A virtio-mem device is like a
50+ * "resizable DIMM" consisting of small memory blocks that can be plugged
51+ * or unplugged. The device driver is responsible for (un)plugging memory
52+ * blocks on demand.
53+ *
54+ * Virtio-mem devices can only operate on their assigned memory region in
55+ * order to (un)plug memory. A device cannot (un)plug memory belonging to
56+ * other devices.
57+ *
58+ * The "region_size" corresponds to the maximum amount of memory that can
59+ * be provided by a device. The "size" corresponds to the amount of memory
60+ * that is currently plugged. "requested_size" corresponds to a request
61+ * from the device to the device driver to (un)plug blocks. The
62+ * device driver should try to (un)plug blocks in order to reach the
63+ * "requested_size". It is impossible to plug more memory than requested.
64+ *
65+ * The "usable_region_size" represents the memory region that can actually
66+ * be used to (un)plug memory. It is always at least as big as the
67+ * "requested_size" and will grow dynamically. It will only shrink when
68+ * explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).
69+ *
70+ * There are no guarantees what will happen if unplugged memory is
71+ * read/written. Such memory should, in general, not be touched. E.g.,
72+ * even writing might succeed, but the values will simply be discarded at
73+ * random points in time.
74+ *
75+ * It can happen that the device cannot process a request, because it is
76+ * busy. The device driver has to retry later.
77+ *
78+ * Usually, during system resets all memory will get unplugged, so the
79+ * device driver can start with a clean state. However, in specific
80+ * scenarios (if the device is busy) it can happen that the device still
81+ * has memory plugged. The device driver can request to unplug all memory
82+ * (VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the
83+ * device is busy.
84+ */
85+
86+/* --- virtio-mem: guest -> host requests --- */
87+
88+/* request to plug memory blocks */
89+#define VIRTIO_MEM_REQ_PLUG 0
90+/* request to unplug memory blocks */
91+#define VIRTIO_MEM_REQ_UNPLUG 1
92+/* request to unplug all blocks and shrink the usable size */
93+#define VIRTIO_MEM_REQ_UNPLUG_ALL 2
94+/* request information about the plugged state of memory blocks */
95+#define VIRTIO_MEM_REQ_STATE 3
96+
97+struct virtio_mem_req_plug {
98+ __virtio64 addr;
99+ __virtio16 nb_blocks;
100+};
101+
102+struct virtio_mem_req_unplug {
103+ __virtio64 addr;
104+ __virtio16 nb_blocks;
105+};
106+
107+struct virtio_mem_req_state {
108+ __virtio64 addr;
109+ __virtio16 nb_blocks;
110+};
111+
112+struct virtio_mem_req {
113+ __virtio16 type;
114+ __virtio16 padding[3];
115+
116+ union {
117+ struct virtio_mem_req_plug plug;
118+ struct virtio_mem_req_unplug unplug;
119+ struct virtio_mem_req_state state;
120+ } u;
121+};
122+
123+
124+/* --- virtio-mem: host -> guest response --- */
125+
126+/*
127+ * Request processed successfully, applicable for
128+ * - VIRTIO_MEM_REQ_PLUG
129+ * - VIRTIO_MEM_REQ_UNPLUG
130+ * - VIRTIO_MEM_REQ_UNPLUG_ALL
131+ * - VIRTIO_MEM_REQ_STATE
132+ */
133+#define VIRTIO_MEM_RESP_ACK 0
134+/*
135+ * Request denied - e.g. trying to plug more than requested, applicable for
136+ * - VIRTIO_MEM_REQ_PLUG
137+ */
138+#define VIRTIO_MEM_RESP_NACK 1
139+/*
140+ * Request cannot be processed right now, try again later, applicable for
141+ * - VIRTIO_MEM_REQ_PLUG
142+ * - VIRTIO_MEM_REQ_UNPLUG
143+ * - VIRTIO_MEM_REQ_UNPLUG_ALL
144+ */
145+#define VIRTIO_MEM_RESP_BUSY 2
146+/*
147+ * Error in request (e.g. addresses/alignment), applicable for
148+ * - VIRTIO_MEM_REQ_PLUG
149+ * - VIRTIO_MEM_REQ_UNPLUG
150+ * - VIRTIO_MEM_REQ_STATE
151+ */
152+#define VIRTIO_MEM_RESP_ERROR 3
153+
154+
155+/* State of memory blocks is "plugged" */
156+#define VIRTIO_MEM_STATE_PLUGGED 0
157+/* State of memory blocks is "unplugged" */
158+#define VIRTIO_MEM_STATE_UNPLUGGED 1
159+/* State of memory blocks is "mixed" */
160+#define VIRTIO_MEM_STATE_MIXED 2
161+
162+struct virtio_mem_resp_state {
163+ __virtio16 state;
164+};
165+
166+struct virtio_mem_resp {
167+ __virtio16 type;
168+ __virtio16 padding[3];
169+
170+ union {
171+ struct virtio_mem_resp_state state;
172+ } u;
173+};
174+
175+/* --- virtio-mem: configuration --- */
176+
177+struct virtio_mem_config {
178+ /* Block size and alignment. Cannot change. */
179+ __u32 block_size;
180+ __u32 padding;
181+ /* Start address of the memory region. Cannot change. */
182+ __u64 addr;
183+ /* Region size (maximum). Cannot change. */
184+ __u64 region_size;
185+ /*
186+ * Currently usable region size. Can grow up to region_size. Can
187+ * shrink due to VIRTIO_MEM_REQ_UNPLUG_ALL (in which case no config
188+ * update will be sent).
189+ */
190+ __u64 usable_region_size;
191+ /*
192+ * Currently used size. Changes due to plug/unplug requests, but no
193+ * config updates will be sent.
194+ */
195+ __u64 plugged_size;
196+ /* Requested size. New plug requests cannot exceed it. Can change. */
197+ __u64 requested_size;
198+};
199+
200+#endif /* _LINUX_VIRTIO_MEM_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t