From: Nikita Kalyazin <kalyazin@amazon.com>
To: James Houghton <jthoughton@google.com>
Cc: <akpm@linux-foundation.org>, <pbonzini@redhat.com>,
<shuah@kernel.org>, <kvm@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-mm@kvack.org>, <lorenzo.stoakes@oracle.com>,
<david@redhat.com>, <ryan.roberts@arm.com>,
<quic_eberman@quicinc.com>, <peterx@redhat.com>, <graf@amazon.de>,
<jgowans@amazon.com>, <roypat@amazon.co.uk>, <derekmn@amazon.com>,
<nsaenz@amazon.es>, <xmarcalx@amazon.com>
Subject: Re: [PATCH v2 5/5] KVM: selftests: test userfaultfd minor for guest_memfd
Date: Thu, 3 Apr 2025 18:02:36 +0100 [thread overview]
Message-ID: <3a54adea-98d0-4af3-a121-6574b0c18f80@amazon.com> (raw)
In-Reply-To: <CADrL8HXm_UDKvrsNe6Guvo_pPvCN9ZJBKe=p0HM-iYZWufbEfA@mail.gmail.com>
On 02/04/2025 22:10, James Houghton wrote:
> On Wed, Apr 2, 2025 at 9:08 AM Nikita Kalyazin <kalyazin@amazon.com> wrote:
>>
>> The test demonstrates that a minor userfaultfd event in guest_memfd can
>> be resolved via a memcpy followed by a UFFDIO_CONTINUE ioctl.
>>
>> Signed-off-by: Nikita Kalyazin <kalyazin@amazon.com>
>> ---
>> .../testing/selftests/kvm/guest_memfd_test.c | 94 +++++++++++++++++++
>> 1 file changed, 94 insertions(+)
>>
>> diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
>> index 38c501e49e0e..9b47b796f3aa 100644
>> --- a/tools/testing/selftests/kvm/guest_memfd_test.c
>> +++ b/tools/testing/selftests/kvm/guest_memfd_test.c
>> @@ -10,12 +10,16 @@
>> #include <errno.h>
>> #include <stdio.h>
>> #include <fcntl.h>
>> +#include <pthread.h>
>>
>> #include <linux/bitmap.h>
>> #include <linux/falloc.h>
>> +#include <linux/userfaultfd.h>
>> #include <sys/mman.h>
>> #include <sys/types.h>
>> #include <sys/stat.h>
>> +#include <sys/syscall.h>
>> +#include <sys/ioctl.h>
>>
>> #include "kvm_util.h"
>> #include "test_util.h"
>> @@ -206,6 +210,93 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
>> close(fd1);
>> }
>>
>> +struct fault_args {
>> + char *addr;
>> + volatile char value;
>
> I think you should/must put volatile on `addr` and not on `value`.
This was to prevent the compiler from omitting the write to the value,
because it's never read later on.
>
>> +};
>> +
>> +static void *fault_thread_fn(void *arg)
>> +{
>> + struct fault_args *args = arg;
>> +
>> + /* Trigger page fault */
>> + args->value = *args->addr;
>> + return NULL;
>> +}
>> +
>> +static void test_uffd_missing(int fd, size_t page_size, size_t total_size)
>
> test_uffd_minor? :)
>
>> +{
>> + struct uffdio_register uffd_reg;
>> + struct uffdio_continue uffd_cont;
>> + struct uffd_msg msg;
>> + struct fault_args args;
>> + pthread_t fault_thread;
>> + void *mem, *mem_nofault, *buf = NULL;
>> + int uffd, ret;
>> + off_t offset = page_size;
>> + void *fault_addr;
>> +
>> + ret = posix_memalign(&buf, page_size, total_size);
>> + TEST_ASSERT_EQ(ret, 0);
>> +
>> + uffd = syscall(__NR_userfaultfd, O_CLOEXEC);
>> + TEST_ASSERT(uffd != -1, "userfaultfd creation should succeed");
>> +
>> + struct uffdio_api uffdio_api = {
>> + .api = UFFD_API,
>> + .features = UFFD_FEATURE_MISSING_SHMEM,
>
> I think you mean UFFD_FEATURE_MINOR_SHMEM...?
>
> And I'm trying to think through what feature we should expose for
> guest_memfd; UFFD_FEATURE_MINOR_SHMEM already indicates support for
> shmem.
>
> We could have UFFD_FEATURE_MINOR_GUESTMEMFD, perhaps that's enough.
Yes, I will introduce UFFD_FEATURE_MINOR_GUEST_MEMFD in the next version.
>
> Or we could have UFFD_FEATURE_MINOR_GENERIC (or nothing at all!). Some
> VMAs might not support the minor mode, and the user will figure that
> out when UFFDIO_REGISTER fails.
My concern is the exact reason of the failure may not be apparent to the
caller in that case.
>
>> + };
>> + ret = ioctl(uffd, UFFDIO_API, &uffdio_api);
>> + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_API) should succeed");
>> +
>> + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
>> + TEST_ASSERT(mem != MAP_FAILED, "mmap should succeed");
>> +
>> + mem_nofault = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
>> + TEST_ASSERT(mem_nofault != MAP_FAILED, "mmap should succeed");
>> +
>> + uffd_reg.range.start = (unsigned long)mem;
>> + uffd_reg.range.len = total_size;
>> + uffd_reg.mode = UFFDIO_REGISTER_MODE_MINOR;
>> + ret = ioctl(uffd, UFFDIO_REGISTER, &uffd_reg);
>> + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_REGISTER) should succeed");
>> +
>> + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
>> + offset, page_size);
>> + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
>> +
>> + fault_addr = mem + offset;
>> + args.addr = fault_addr;
>> +
>> + ret = pthread_create(&fault_thread, NULL, fault_thread_fn, &args);
>> + TEST_ASSERT(ret == 0, "pthread_create should succeed");
>> +
>> + ret = read(uffd, &msg, sizeof(msg));
>> + TEST_ASSERT(ret != -1, "read from userfaultfd should succeed");
>> + TEST_ASSERT(msg.event == UFFD_EVENT_PAGEFAULT, "event type should be pagefault");
>> + TEST_ASSERT((void *)(msg.arg.pagefault.address & ~(page_size - 1)) == fault_addr,
>> + "pagefault should occur at expected address");
>> +
>> + memcpy(mem_nofault + offset, buf + offset, page_size);
>> +
>> + uffd_cont.range.start = (unsigned long)fault_addr;
>> + uffd_cont.range.len = page_size;
>> + uffd_cont.mode = 0;
>> + ret = ioctl(uffd, UFFDIO_CONTINUE, &uffd_cont);
>> + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_CONTINUE) should succeed");
>> +
>> + ret = pthread_join(fault_thread, NULL);
>> + TEST_ASSERT(ret == 0, "pthread_join should succeed");
>
> And maybe also:
>
> /* Right value? */
> TEST_ASSERT(args.value == *(char *)mem_nofault));
> /* No second fault? */
> TEST_ASSERT(args.value == *(char *)mem);
Good idea, thanks. I don't need the volatile anymore :)
>
>> +
>> + ret = munmap(mem_nofault, total_size);
>> + TEST_ASSERT(!ret, "munmap should succeed");
>> +
>> + ret = munmap(mem, total_size);
>> + TEST_ASSERT(!ret, "munmap should succeed");
>> + free(buf);
>> + close(uffd);
>> +}
>> +
>> unsigned long get_shared_type(void)
>> {
>> #ifdef __x86_64__
>> @@ -244,6 +335,9 @@ void test_vm_type(unsigned long type, bool is_shared)
>> test_fallocate(fd, page_size, total_size);
>> test_invalid_punch_hole(fd, page_size, total_size);
>>
>> + if (is_shared)
>> + test_uffd_missing(fd, page_size, total_size);
>> +
>> close(fd);
>> kvm_vm_release(vm);
>> }
>> --
>> 2.47.1
>>
next prev parent reply other threads:[~2025-04-03 17:02 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-02 16:07 [PATCH v2 0/5] KVM: guest_memfd: support for uffd minor Nikita Kalyazin
2025-04-02 16:07 ` [PATCH v2 1/5] mm: userfaultfd: generic continue for non hugetlbfs Nikita Kalyazin
2025-04-02 19:04 ` James Houghton
2025-04-03 17:01 ` Nikita Kalyazin
2025-04-02 16:07 ` [PATCH v2 2/5] KVM: guest_memfd: add kvm_gmem_vma_is_gmem Nikita Kalyazin
2025-04-02 16:07 ` [PATCH v2 3/5] mm: userfaultfd: allow to register continue for guest_memfd Nikita Kalyazin
2025-04-02 21:25 ` James Houghton
2025-04-03 17:01 ` Nikita Kalyazin
2025-04-02 16:07 ` [PATCH v2 4/5] KVM: guest_memfd: add support for userfaultfd minor Nikita Kalyazin
2025-04-02 16:07 ` [PATCH v2 5/5] KVM: selftests: test userfaultfd minor for guest_memfd Nikita Kalyazin
2025-04-02 21:10 ` James Houghton
2025-04-03 17:02 ` Nikita Kalyazin [this message]
2025-11-25 18:38 [PATCH v2 0/5] mm, kvm: add guest_memfd support for uffd minor faults Mike Rapoport
2025-11-25 18:38 ` [PATCH v2 5/5] KVM: selftests: test userfaultfd minor for guest_memfd Mike Rapoport
2025-11-26 15:23 ` Liam R. Howlett
2025-11-26 16:49 ` Nikita Kalyazin
2025-11-27 10:39 ` Mike Rapoport
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=3a54adea-98d0-4af3-a121-6574b0c18f80@amazon.com \
--to=kalyazin@amazon.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=graf@amazon.de \
--cc=jgowans@amazon.com \
--cc=jthoughton@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=nsaenz@amazon.es \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=quic_eberman@quicinc.com \
--cc=roypat@amazon.co.uk \
--cc=ryan.roberts@arm.com \
--cc=shuah@kernel.org \
--cc=xmarcalx@amazon.com \
/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
all inboxes | Powered by JetHome®