From: Peter Xu <peterx@redhat.com>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org
Cc: Anish Moorthy <amoorthy@google.com>,
Axel Rasmussen <axelrasmussen@google.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Mike Kravetz <mike.kravetz@oracle.com>,
Peter Zijlstra <peterz@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>,
Christian Brauner <brauner@kernel.org>,
peterx@redhat.com, linux-fsdevel@vger.kernel.org,
Andrea Arcangeli <aarcange@redhat.com>,
Ingo Molnar <mingo@redhat.com>,
James Houghton <jthoughton@google.com>,
Nadav Amit <nadav.amit@gmail.com>
Subject: [PATCH 6/7] selftests/mm: Create uffd_fault_thread_create|join()
Date: Tue, 5 Sep 2023 17:42:34 -0400 [thread overview]
Message-ID: <20230905214235.320571-7-peterx@redhat.com> (raw)
In-Reply-To: <20230905214235.320571-1-peterx@redhat.com>
Make them common functions to be reused.
Signed-off-by: Peter Xu <peterx@redhat.com>
---
tools/testing/selftests/mm/uffd-common.c | 46 ++++++++++++++++++++++
tools/testing/selftests/mm/uffd-common.h | 4 ++
tools/testing/selftests/mm/uffd-stress.c | 49 ++++--------------------
3 files changed, 57 insertions(+), 42 deletions(-)
diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index aded06cab285..851284395b29 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -555,6 +555,52 @@ void *uffd_poll_thread(void *arg)
return NULL;
}
+void *uffd_read_thread(void *arg)
+{
+ struct uffd_args *args = (struct uffd_args *)arg;
+ struct uffd_msg msg;
+
+ sem_post(&uffd_read_sem);
+ /* from here cancellation is ok */
+
+ for (;;) {
+ if (uffd_read_msg(uffd, &msg))
+ continue;
+ uffd_handle_page_fault(&msg, args);
+ }
+
+ return NULL;
+}
+
+void uffd_fault_thread_create(pthread_t *thread, pthread_attr_t *attr,
+ struct uffd_args *args, bool poll)
+{
+ if (poll) {
+ if (pthread_create(thread, attr, uffd_poll_thread, args))
+ err("uffd_poll_thread create");
+ } else {
+ if (pthread_create(thread, attr, uffd_read_thread, args))
+ err("uffd_read_thread create");
+ sem_wait(&uffd_read_sem);
+ }
+}
+
+void uffd_fault_thread_join(pthread_t thread, int cpu, bool poll)
+{
+ char c = 1;
+
+ if (poll) {
+ if (write(pipefd[cpu*2+1], &c, 1) != 1)
+ err("pipefd write error");
+ } else {
+ if (pthread_cancel(thread))
+ err("pthread_cancel()");
+ }
+
+ if (pthread_join(thread, NULL))
+ err("pthread_join()");
+}
+
static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
unsigned long offset)
{
diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h
index 521523baded1..9d66ad5c52cb 100644
--- a/tools/testing/selftests/mm/uffd-common.h
+++ b/tools/testing/selftests/mm/uffd-common.h
@@ -114,6 +114,10 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_args *args);
int __copy_page(int ufd, unsigned long offset, bool retry, bool wp);
int copy_page(int ufd, unsigned long offset, bool wp);
void *uffd_poll_thread(void *arg);
+void *uffd_read_thread(void *arg);
+void uffd_fault_thread_create(pthread_t *thread, pthread_attr_t *attr,
+ struct uffd_args *args, bool poll);
+void uffd_fault_thread_join(pthread_t thread, int cpu, bool poll);
int uffd_open_dev(unsigned int flags);
int uffd_open_sys(unsigned int flags);
diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
index 7219f55ae794..915795e33432 100644
--- a/tools/testing/selftests/mm/uffd-stress.c
+++ b/tools/testing/selftests/mm/uffd-stress.c
@@ -125,23 +125,6 @@ static int copy_page_retry(int ufd, unsigned long offset)
return __copy_page(ufd, offset, true, test_uffdio_wp);
}
-static void *uffd_read_thread(void *arg)
-{
- struct uffd_args *args = (struct uffd_args *)arg;
- struct uffd_msg msg;
-
- sem_post(&uffd_read_sem);
- /* from here cancellation is ok */
-
- for (;;) {
- if (uffd_read_msg(uffd, &msg))
- continue;
- uffd_handle_page_fault(&msg, args);
- }
-
- return NULL;
-}
-
static void *background_thread(void *arg)
{
unsigned long cpu = (unsigned long) arg;
@@ -186,16 +169,10 @@ static int stress(struct uffd_args *args)
if (pthread_create(&locking_threads[cpu], &attr,
locking_thread, (void *)cpu))
return 1;
- if (bounces & BOUNCE_POLL) {
- if (pthread_create(&uffd_threads[cpu], &attr, uffd_poll_thread, &args[cpu]))
- err("uffd_poll_thread create");
- } else {
- if (pthread_create(&uffd_threads[cpu], &attr,
- uffd_read_thread,
- (void *)&args[cpu]))
- return 1;
- sem_wait(&uffd_read_sem);
- }
+
+ uffd_fault_thread_create(&uffd_threads[cpu], &attr,
+ &args[cpu], bounces & BOUNCE_POLL);
+
if (pthread_create(&background_threads[cpu], &attr,
background_thread, (void *)cpu))
return 1;
@@ -220,21 +197,9 @@ static int stress(struct uffd_args *args)
if (pthread_join(locking_threads[cpu], NULL))
return 1;
- for (cpu = 0; cpu < nr_cpus; cpu++) {
- char c;
- if (bounces & BOUNCE_POLL) {
- if (write(pipefd[cpu*2+1], &c, 1) != 1)
- err("pipefd write error");
- if (pthread_join(uffd_threads[cpu],
- (void *)&args[cpu]))
- return 1;
- } else {
- if (pthread_cancel(uffd_threads[cpu]))
- return 1;
- if (pthread_join(uffd_threads[cpu], NULL))
- return 1;
- }
- }
+ for (cpu = 0; cpu < nr_cpus; cpu++)
+ uffd_fault_thread_join(uffd_threads[cpu], cpu,
+ bounces & BOUNCE_POLL);
return 0;
}
--
2.41.0
next prev parent reply other threads:[~2023-09-05 21:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-05 21:42 [PATCH 0/7] mm/userfaultfd/poll: Scale userfaultfd wakeups Peter Xu
2023-09-05 21:42 ` [PATCH 1/7] mm/userfaultfd: Make uffd read() wait event exclusive Peter Xu
2023-09-05 21:42 ` [PATCH 2/7] poll: Add a poll_flags for poll_queue_proc() Peter Xu
2023-09-05 23:21 ` kernel test robot
2023-09-06 17:31 ` kernel test robot
2023-09-06 20:53 ` kernel test robot
2023-09-11 20:00 ` Peter Xu
2023-09-05 21:42 ` [PATCH 3/7] poll: POLL_ENQUEUE_EXCLUSIVE Peter Xu
2023-09-05 21:42 ` [PATCH 4/7] fs/userfaultfd: Use exclusive waitqueue for poll() Peter Xu
2023-09-05 21:42 ` [PATCH 5/7] selftests/mm: Replace uffd_read_mutex with a semaphore Peter Xu
2023-09-05 21:42 ` Peter Xu [this message]
2023-09-05 21:42 ` [PATCH 7/7] selftests/mm: uffd perf test Peter Xu
2023-09-07 19:18 ` [PATCH 0/7] mm/userfaultfd/poll: Scale userfaultfd wakeups Axel Rasmussen
2023-09-08 22:01 ` Peter Xu
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=20230905214235.320571-7-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=amoorthy@google.com \
--cc=axelrasmussen@google.com \
--cc=brauner@kernel.org \
--cc=jthoughton@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mike.kravetz@oracle.com \
--cc=mingo@redhat.com \
--cc=nadav.amit@gmail.com \
--cc=peterz@infradead.org \
--cc=rppt@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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;
as well as URLs for NNTP newsgroup(s).