Skip to content

Commit 0cba644

Browse files
anlshsean-jc
authored andcommitted
KVM: selftests: Use EPOLL in userfaultfd_util reader threads
With multiple reader threads POLLing a single UFFD, the demand paging test suffers from the thundering herd problem: performance degrades as the number of reader threads is increased. Solve this issue [1] by switching the the polling mechanism to EPOLL + EPOLLEXCLUSIVE. Also, change the error-handling convention of uffd_handler_thread_fn. Instead of just printing errors and returning early from the polling loop, check for them via TEST_ASSERT(). "return NULL" is reserved for a successful exit from uffd_handler_thread_fn, i.e. one triggered by a write to the exit pipe. Performance samples generated by the command in [2] are given below. Num Reader Threads, Paging Rate (POLL), Paging Rate (EPOLL) 1 249k 185k 2 201k 235k 4 186k 155k 16 150k 217k 32 89k 198k [1] Single-vCPU performance does suffer somewhat. [2] ./demand_paging_test -u MINOR -s shmem -v 4 -o -r <num readers> Signed-off-by: Anish Moorthy <[email protected]> Acked-by: James Houghton <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent df4ec5a commit 0cba644

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

tools/testing/selftests/kvm/demand_paging_test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <time.h>
16-
#include <poll.h>
1716
#include <pthread.h>
1817
#include <linux/userfaultfd.h>
1918
#include <sys/syscall.h>

tools/testing/selftests/kvm/lib/userfaultfd_util.c

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <poll.h>
1717
#include <pthread.h>
1818
#include <linux/userfaultfd.h>
19+
#include <sys/epoll.h>
1920
#include <sys/syscall.h>
2021

2122
#include "kvm_util.h"
@@ -32,69 +33,64 @@ static void *uffd_handler_thread_fn(void *arg)
3233
int64_t pages = 0;
3334
struct timespec start;
3435
struct timespec ts_diff;
36+
struct epoll_event evt;
37+
int epollfd;
38+
39+
epollfd = epoll_create(1);
40+
TEST_ASSERT(epollfd >= 0, "Failed to create epollfd.");
41+
42+
evt.events = EPOLLIN | EPOLLEXCLUSIVE;
43+
evt.data.u32 = 0;
44+
TEST_ASSERT(!epoll_ctl(epollfd, EPOLL_CTL_ADD, uffd, &evt),
45+
"Failed to add uffd to epollfd");
46+
47+
evt.events = EPOLLIN;
48+
evt.data.u32 = 1;
49+
TEST_ASSERT(!epoll_ctl(epollfd, EPOLL_CTL_ADD, reader_args->pipe, &evt),
50+
"Failed to add pipe to epollfd");
3551

3652
clock_gettime(CLOCK_MONOTONIC, &start);
3753
while (1) {
3854
struct uffd_msg msg;
39-
struct pollfd pollfd[2];
40-
char tmp_chr;
4155
int r;
4256

43-
pollfd[0].fd = uffd;
44-
pollfd[0].events = POLLIN;
45-
pollfd[1].fd = reader_args->pipe;
46-
pollfd[1].events = POLLIN;
47-
48-
r = poll(pollfd, 2, -1);
49-
switch (r) {
50-
case -1:
51-
pr_info("poll err");
52-
continue;
53-
case 0:
54-
continue;
55-
case 1:
56-
break;
57-
default:
58-
pr_info("Polling uffd returned %d", r);
59-
return NULL;
60-
}
57+
r = epoll_wait(epollfd, &evt, 1, -1);
58+
TEST_ASSERT(r == 1,
59+
"Unexpected number of events (%d) from epoll, errno = %d",
60+
r, errno);
6161

62-
if (pollfd[0].revents & POLLERR) {
63-
pr_info("uffd revents has POLLERR");
64-
return NULL;
65-
}
62+
if (evt.data.u32 == 1) {
63+
char tmp_chr;
6664

67-
if (pollfd[1].revents & POLLIN) {
68-
r = read(pollfd[1].fd, &tmp_chr, 1);
65+
TEST_ASSERT(!(evt.events & (EPOLLERR | EPOLLHUP)),
66+
"Reader thread received EPOLLERR or EPOLLHUP on pipe.");
67+
r = read(reader_args->pipe, &tmp_chr, 1);
6968
TEST_ASSERT(r == 1,
70-
"Error reading pipefd in UFFD thread");
69+
"Error reading pipefd in uffd reader thread");
7170
break;
7271
}
7372

74-
if (!(pollfd[0].revents & POLLIN))
75-
continue;
73+
TEST_ASSERT(!(evt.events & (EPOLLERR | EPOLLHUP)),
74+
"Reader thread received EPOLLERR or EPOLLHUP on uffd.");
7675

7776
r = read(uffd, &msg, sizeof(msg));
7877
if (r == -1) {
79-
if (errno == EAGAIN)
80-
continue;
81-
pr_info("Read of uffd got errno %d\n", errno);
82-
return NULL;
78+
TEST_ASSERT(errno == EAGAIN,
79+
"Error reading from UFFD: errno = %d", errno);
80+
continue;
8381
}
8482

85-
if (r != sizeof(msg)) {
86-
pr_info("Read on uffd returned unexpected size: %d bytes", r);
87-
return NULL;
88-
}
83+
TEST_ASSERT(r == sizeof(msg),
84+
"Read on uffd returned unexpected number of bytes (%d)", r);
8985

9086
if (!(msg.event & UFFD_EVENT_PAGEFAULT))
9187
continue;
9288

9389
if (reader_args->delay)
9490
usleep(reader_args->delay);
9591
r = reader_args->handler(reader_args->uffd_mode, uffd, &msg);
96-
if (r < 0)
97-
return NULL;
92+
TEST_ASSERT(r >= 0,
93+
"Reader thread handler fn returned negative value %d", r);
9894
pages++;
9995
}
10096

0 commit comments

Comments
 (0)