From 3cb1b0ce091998532a30793e3272925da4e6f3aa Mon Sep 17 00:00:00 2001 From: Jiajie Li Date: Mon, 7 Feb 2022 14:31:34 +0800 Subject: [PATCH 1/2] hugepages: hugepages files maybe leftover Before qemu uses the hugepage memory directory /dev/hugepages/libvirt/qemu/xxx, The directory may be deleted because of the destroy virtual machine. Cause qemu to create files directly under /dev/hugepages/libvirt/qemu/. After the file is created, the file is not cleaned up by unlink, and when the virtual machine is destroyed, libvirt will only clean up /dev/hugepages/libvirt/qemu/xxx directory. After creating the hugepage file, execute unlink to clean up the file to fix the problem. Signed-off-by: Jinhua Cao Signed-off-by: Jiajie Li --- include/qemu/mmap-alloc.h | 3 +++ softmmu/physmem.c | 10 +++++++++- util/mmap-alloc.c | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index 90d0eee705..707202e5be 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -1,6 +1,9 @@ #ifndef QEMU_MMAP_ALLOC_H #define QEMU_MMAP_ALLOC_H +#define HUGETLBFS_MAGIC 0x958458f6 + +size_t qemu_fd_getfiletype(int fd); size_t qemu_fd_getpagesize(int fd); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 3524c04c2a..3b9a61448c 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1496,7 +1496,14 @@ static int file_ram_open(const char *path, /* @path names a file that doesn't exist, create it */ fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd >= 0) { - *created = true; + info_report("open %s success \n", path); + /* if fd file type is HUGETLBFS_MAGIC, unlink it, */ + /* in case to prevent residue after qemu killed */ + if (qemu_fd_getfiletype(fd) == HUGETLBFS_MAGIC) { + unlink(path); + } else { + *created = true; + } break; } } else if (errno == EISDIR) { @@ -1515,6 +1522,7 @@ static int file_ram_open(const char *path, fd = mkstemp(filename); if (fd >= 0) { + info_report("mkstemp %s success \n", filename); unlink(filename); g_free(filename); break; diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 893d864354..4993dd5bfa 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -29,6 +29,28 @@ #include #endif +size_t qemu_fd_getfiletype(int fd) +{ + struct statfs fs; + int ret; + + if (fd != -1) { + do { + ret = fstatfs(fd, &fs); + } while (ret != 0 && errno == EINTR); + + if (ret != 0) { + fprintf(stderr, "Couldn't fstatfs() fd: %s\n", + strerror(errno)); + return -1; + } + return fs.f_type; + } else { + fprintf(stderr, "fd is invalid \n"); + return -1; + } +} + size_t qemu_fd_getpagesize(int fd) { #ifdef CONFIG_LINUX -- 2.27.0