From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Fuad Tabba <tabba@google.com>
Cc: kvm@vger.kernel.org, julien.thierry.kdev@gmail.com,
andre.przywara@arm.com, will@kernel.org
Subject: Re: [PATCH kvmtool v1 06/17] Use memfd for hugetlbfs when allocating guest ram
Date: Thu, 24 Nov 2022 10:19:15 +0000 [thread overview]
Message-ID: <Y39FIxcUhvdNGNfJ@monolith.localdoman> (raw)
In-Reply-To: <20221115111549.2784927-7-tabba@google.com>
Hi,
On Tue, Nov 15, 2022 at 11:15:38AM +0000, Fuad Tabba wrote:
> This removes the need of using a temporary file for the fd.
I'm confused by this. The man page for memfd_create says that it creates an
anonymous file that lives in RAM.
>
> Signed-off-by: Fuad Tabba <tabba@google.com>
> ---
> util/util.c | 25 ++++++++++++++++++++-----
> 1 file changed, 20 insertions(+), 5 deletions(-)
>
> diff --git a/util/util.c b/util/util.c
> index e6c0951..d6ceb5d 100644
> --- a/util/util.c
> +++ b/util/util.c
> @@ -10,6 +10,14 @@
> #include <sys/stat.h>
> #include <sys/statfs.h>
>
> +#ifndef MFD_HUGETLB
> +#define MFD_HUGETLB 0x0004U
> +#endif
> +
> +#ifndef MFD_HUGE_SHIFT
> +#define MFD_HUGE_SHIFT 26
> +#endif
Hm... on my machine these are defined in linux/memfd.h, maybe you are
missing the include?
> +
> static void report(const char *prefix, const char *err, va_list params)
> {
> char msg[1024];
> @@ -96,10 +104,12 @@ static u64 get_hugepage_blk_size(const char *htlbfs_path)
>
> static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
> {
> - char mpath[PATH_MAX];
> + const char *name = "kvmtool";
> + unsigned int flags = 0;
> int fd;
> void *addr;
> u64 blk_size;
> + int htsize;
>
> blk_size = get_hugepage_blk_size(htlbfs_path);
> if (blk_size == 0 || blk_size > size) {
> @@ -107,13 +117,18 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)
> (unsigned long long)blk_size, (unsigned long long)size);
> }
>
> + htsize = __builtin_ctzl(blk_size);
> + if ((1ULL << htsize) != blk_size)
> + die("Hugepage size must be a power of 2.\n");
> +
> + flags |= MFD_HUGETLB;
> + flags |= htsize << MFD_HUGE_SHIFT;
If I understand the intention correctly, this entire sequence can be
rewritten using is_power_of_two() from util.h:
if (!is_power_of_two(blk_size))
die("Hugepage size must be a power of 2");
flags |= MFD_HUGETLB;
flags |= blk_size << MFD_HUGE_SHIFT;
Also, die() automatically adds the newline at the end of the string.
That's unless you specifically wanted two newline characters at the end of
the message.
> +
> kvm->ram_pagesize = blk_size;
>
> - snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", htlbfs_path);
> - fd = mkstemp(mpath);
> + fd = memfd_create(name, flags);
> if (fd < 0)
> - die("Can't open %s for hugetlbfs map\n", mpath);
> - unlink(mpath);
> + die("Can't memfd_create for hugetlbfs map\n");
die_perror("memfd_create")? That way you also print the error number and
the message associated with it. Same thing with the other die statements
here, replacing them with die_perror() looks like it would be helpful.
Thanks,
Alex
> if (ftruncate(fd, size) < 0)
> die("Can't ftruncate for mem mapping size %lld\n",
> (unsigned long long)size);
> --
> 2.38.1.431.g37b22c650d-goog
>
next prev parent reply other threads:[~2022-11-24 10:19 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-15 11:15 [PATCH kvmtool v1 00/17] Use memfd for guest vm memory allocation Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 01/17] Initialize the return value in kvm__for_each_mem_bank() Fuad Tabba
2022-11-15 11:59 ` Andre Przywara
2022-11-23 16:08 ` Alexandru Elisei
2022-11-23 17:43 ` Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 02/17] Make mmap_hugetlbfs() static Fuad Tabba
2022-11-15 17:58 ` Andre Przywara
2022-11-15 11:15 ` [PATCH kvmtool v1 03/17] Rename parameter in mmap_anon_or_hugetlbfs() Fuad Tabba
2022-11-23 16:40 ` Alexandru Elisei
2022-11-23 17:44 ` Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 04/17] Add hostmem va to debug print Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 05/17] Factor out getting the hugetlb block size Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 06/17] Use memfd for hugetlbfs when allocating guest ram Fuad Tabba
2022-11-24 10:19 ` Alexandru Elisei [this message]
2022-11-24 10:45 ` Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 07/17] Make blk_size a parameter and pass it to mmap_hugetlbfs() Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 08/17] Use memfd for all guest ram allocations Fuad Tabba
2022-11-24 11:01 ` Alexandru Elisei
2022-11-24 15:19 ` Fuad Tabba
2022-11-24 17:14 ` Alexandru Elisei
2022-11-25 10:43 ` Alexandru Elisei
2022-11-25 10:58 ` Fuad Tabba
2022-11-25 10:44 ` Fuad Tabba
2022-11-25 11:31 ` Alexandru Elisei
2022-11-28 8:49 ` Fuad Tabba
2022-11-29 18:09 ` Alexandru Elisei
2022-11-30 17:54 ` Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 09/17] Allocate pvtime memory with memfd Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 10/17] Allocate vesa " Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 11/17] Add a function that allocates aligned memory if specified Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 12/17] Use new function to align memory Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 13/17] Remove struct fields and code used for alignment Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 14/17] Replace kvm_arch_delete_ram with kvm_delete_ram Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 15/17] Remove no-longer unused macro Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 16/17] Factor out set_user_memory_region code Fuad Tabba
2022-11-15 11:15 ` [PATCH kvmtool v1 17/17] Pass the memory file descriptor and offset when registering ram Fuad Tabba
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y39FIxcUhvdNGNfJ@monolith.localdoman \
--to=alexandru.elisei@arm.com \
--cc=andre.przywara@arm.com \
--cc=julien.thierry.kdev@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=tabba@google.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox