@@ -19,11 +19,18 @@
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/time.h>
+#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
+#include <cutils/memory.h>
#include <atomic>
+#include <vector>
+#include <fcntl.h>
+
+/* qti_virtio_mem uapi header */
+#include <linux/qti_virtio_mem.h>
#define SIZE_1MB 0x00100000
#define SIZE_1KB 0x00000400
@@ -205,23 +212,71 @@ using namespace std;
* are needed to support the functionality of vmrd.
*/
+/* qti_virtio_mem device fd */
+static int virtio_mem_fd = -1;
+
+/* mem_buf fds returned by virtio-mem driver */
+static vector<int> array_memfd;
+
+#define QVM_VIRTIO_MEM_DEV_PATH "/dev/qti_virtio_mem"
+#define QVM_SYS_DEVICE_PATH "/sys/devices/virtual/qti_virtio_mem/qti_virtio_mem"
+#define QVM_BLOCK_SIZE_PATH QVM_SYS_DEVICE_PATH"/device_block_size"
+#define QVM_MAX_PLUGIN_THRES_PATH QVM_SYS_DEVICE_PATH"/max_plugin_threshold"
+#define QVM_NUM_BLOCK_PLUGGED_PATH QVM_SYS_DEVICE_PATH"/device_block_plugged"
+
+static int virtio_mem_plug_memory(int64_t size, const std::string& name)
+{
+ struct qti_virtio_mem_ioc_hint_create_arg arg = {};
+ int ret;
+
+ if (virtio_mem_fd < 0)
+ return -ENOTTY;
+
+ arg.size = size;
+ strlcpy(arg.name, name.c_str(), sizeof(arg.name));
+
+ ret = ioctl(virtio_mem_fd, QTI_VIRTIO_MEM_IOC_HINT_CREATE, &arg);
+ if (ret) {
+ LOGE("MemorySizeHint() failed");
+ return ret;
+ }
+
+ return arg.fd;
+}
+
static int memory_plug_init(void) {
- LOGE("memory plug request not supported");
- return -ENOTTY;
+ virtio_mem_fd = open(QVM_VIRTIO_MEM_DEV_PATH, O_RDONLY | O_CLOEXEC);
+ if (virtio_mem_fd < 0) {
+ LOGE("Unable to open %s: %s", QVM_VIRTIO_MEM_DEV_PATH, strerror(errno));
+ return errno;
+ }
+
+ return 0;
}
static void memory_plug_deinit(void) {
- LOGE("memory plug request not supported");
+ if (virtio_mem_fd >= 0)
+ close(virtio_mem_fd);
}
/*
* Plugs in memory of given size into the system by requesting it from host VM.
* This call is expected to be blocking call.
*/
+
static int memory_plug_request(uint64_t size) {
- (void) size;
- LOGE("Memory plug request not supported");
- return -ENOTTY;
+ int memfd;
+
+ memfd = virtio_mem_plug_memory(size * SIZE_1MB, "vmrd");
+ if (memfd < 0) {
+ LOGE("failed to suggest memory size hint");
+ return -1;
+ }
+
+ LOGI("Memory of size %lu MB plugged-in successfully", size);
+ array_memfd.push_back(memfd);
+
+ return 0;
}
/*
@@ -230,8 +285,18 @@ static int memory_plug_request(uint64_t size) {
* his call is expected to be blocking call.
*/
static int memory_unplug_request(uint64_t size) {
- (void) size;
- LOGE("Memory unplug request not supported");
+ int res;
+
+ if (array_memfd.size()) {
+ res = close(array_memfd.back());
+ array_memfd.pop_back();
+ if (res)
+ LOGE("Failed to unplug one memory chunk of size %lu MB", size);
+
+ return res;
+ }
+
+ LOGE("No memory available to unplug");
return -ENOTTY;
}
@@ -243,8 +308,32 @@ static int memory_unplug_request(uint64_t size) {
* This call is expected to be blocking call.
*/
static int __unused memory_unplug_all_request(void) {
- LOGE("Memory unplug all request not supported");
- return -ENOTTY;
+ uint64_t initial_count, unplugged_count = 0, res;
+
+ initial_count = array_memfd.size();
+ if (!initial_count) {
+ LOGE("No memory available to unplug");
+ return 0;
+ }
+
+ while (array_memfd.size()) {
+ LOGI("releasing one memory chunk to host VM");
+ res = close(array_memfd.back());
+ array_memfd.pop_back();
+ if (res)
+ LOGE("failed to unplug one memory chunk");
+ else
+ unplugged_count++;
+ }
+
+ if (unplugged_count < initial_count)
+ LOGI("not all memory chunks were unplugged. initial_count: %lu unplugged_count: %lu",
+ initial_count, unplugged_count);
+ else
+ LOGI("Successfully unplugged all memory chunks. unplugged_count: %lu",
+ unplugged_count);
+
+ return unplugged_count;
}
static int write_file(const char *file_path, char *s) {
@@ -305,7 +394,15 @@ static char *read_file(const char *file_path) {
* Memory block size or resolution.
*/
static int get_memory_plugin_resolution(uint64_t *plugin_resolution_mb) {
- *plugin_resolution_mb = DEFAULT_PLUGIN_RESOLUTION_MB;
+ char *buf;
+
+ buf = read_file(QVM_BLOCK_SIZE_PATH);
+ if (!buf)
+ return -EINVAL;
+
+ *plugin_resolution_mb = strtoul(buf, 0, 10);
+ *plugin_resolution_mb /= SIZE_1MB;
+
return 0;
}
@@ -313,7 +410,15 @@ static int get_memory_plugin_resolution(uint64_t *plugin_resolution_mb) {
* Total max memory that the system (guest VM) allows to be pluuged-in.
*/
static int get_max_memory_plugin_allowed(uint64_t *max_memory_plugin_mb) {
- *max_memory_plugin_mb = DEFAULT_MAX_MEMORY_PLUGIN_MB;
+ char *buf;
+
+ buf = read_file(QVM_MAX_PLUGIN_THRES_PATH);
+ if (!buf)
+ return -EINVAL;
+
+ *max_memory_plugin_mb = strtoul(buf, 0, 10);
+ *max_memory_plugin_mb /= SIZE_1MB;
+
return 0;
}