From patchwork Tue Sep 5 21:42:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13375097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB828CA100D for ; Tue, 5 Sep 2023 21:42:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C136E8E000F; Tue, 5 Sep 2023 17:42:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC5C38E000B; Tue, 5 Sep 2023 17:42:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EF468E000F; Tue, 5 Sep 2023 17:42:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8FAE88E000B for ; Tue, 5 Sep 2023 17:42:50 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5FBF9A092C for ; Tue, 5 Sep 2023 21:42:50 +0000 (UTC) X-FDA: 81203868900.03.E713EC6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 4DFAD40024 for ; Tue, 5 Sep 2023 21:42:48 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="faMc/Agw"; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693950168; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cw3DqXN0nAqgICZcLU+deZuKVK3JziD/s0/YHmFn328=; b=n5RrNoyUIXQmyUjGwxWYasfrLflG+XlJZdcJopYYsuOYKm1c6SJq7oZIS3883GUGlWZ3X/ TekEbvWNxD1ftEvtjn5TAbciyBlQ+2cGxJeRA6LjnsJs2kPs7+heusvgMaJ9zv0NW0JBjo jIPjxtHN/qeECKI5NPu4dqlTS6TSskM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693950168; a=rsa-sha256; cv=none; b=uuD17IQa+FrsgZ+ec3a9sND6HmtHZKgZFHSj6hk/KwUxBdd5GpPr52rJsQE3T+dRHjrWv7 1FjeNPBCbrragpBTL1uX/qrae/MTJfy1ryuyjaaX/8wWft5KQGMVyqDWQImNcvqr6zBWVE C78WerWLE6WSTex8YX30TGpSbYGvjNs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="faMc/Agw"; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693950167; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cw3DqXN0nAqgICZcLU+deZuKVK3JziD/s0/YHmFn328=; b=faMc/Agws4WT7KtKSTPFZTEP3D8U+bE77P93vXUQAp6iBW1jeHcvnckl/zpuS23N6HzhoW AzzmRb1Q1p8ox9n2d3cFAZoIO8kBsw6xol0qppULsId30oqKBVVeF9pGkFVSLaekpsjPj9 YSauur6BIEzBLTihO0gvseCX/JvlUZ4= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-534-sGJfONGKPXKs7lFZG7c0RQ-1; Tue, 05 Sep 2023 17:42:46 -0400 X-MC-Unique: sGJfONGKPXKs7lFZG7c0RQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-76f1cc68e65so91077285a.1 for ; Tue, 05 Sep 2023 14:42:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693950166; x=1694554966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cw3DqXN0nAqgICZcLU+deZuKVK3JziD/s0/YHmFn328=; b=YtJvlgAWk/vpbzbkm9OGnN8BqqbI7GnWZ4sr/6+JxkjscS1cOMM/d0brFquX2Ip8AK b0KTO/VaIf0+co1OvFoYZRo9j32ru1zNDCmm6yTTHViAt3ZWDujEUNSKQ/OLEYUZUYoi EAqV4Z0v/3zxE4d5kcRBzoypbXV6I2DGllU6zj6pGgMITy2aPChUV3LWjk2Ib+cy3ulz m+F9k1LzINrdp+/2Dgynhmup2+SqtVNPGZzdJO6bPW5HHXoKvnhmj9J2XQTytNg3co3M dBx+prlZj1M6/T1rGzh6BA/HkYFniN5WyvERpnL9mwJjbUbdmp8hYgrwaRjgRn4hVq8V suVg== X-Gm-Message-State: AOJu0YyI3rPjs88gjOLic/yaTmGETuxTBfpxw8tyMSWsbQJAqc3a1WvC 0avtHXapHUoc+BGLN2ObivFzYHewoIKzCI8iAd0wnulfc2/t0vvLMapcSAtY6tSupmeAg5BuNaZ s+0LGB1ePHr6m55uQyZY= X-Received: by 2002:a05:620a:4712:b0:76f:1b38:e74a with SMTP id bs18-20020a05620a471200b0076f1b38e74amr15367605qkb.4.1693950165776; Tue, 05 Sep 2023 14:42:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEjHmAQEBZtigx4G4B3sp1ASlOU6CNSzPZeJM4WgpbfHkl1dq3l/QojNcctzg5rIDRzuMLaUA== X-Received: by 2002:a05:620a:4712:b0:76f:1b38:e74a with SMTP id bs18-20020a05620a471200b0076f1b38e74amr15367597qkb.4.1693950165524; Tue, 05 Sep 2023 14:42:45 -0700 (PDT) Received: from x1n.redhat.com (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id i2-20020a37c202000000b007682af2c8aasm4396938qkm.126.2023.09.05.14.42.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 14:42:45 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Anish Moorthy , Axel Rasmussen , Alexander Viro , Mike Kravetz , Peter Zijlstra , Andrew Morton , Mike Rapoport , Christian Brauner , peterx@redhat.com, linux-fsdevel@vger.kernel.org, Andrea Arcangeli , Ingo Molnar , James Houghton , Nadav Amit Subject: [PATCH 6/7] selftests/mm: Create uffd_fault_thread_create|join() Date: Tue, 5 Sep 2023 17:42:34 -0400 Message-ID: <20230905214235.320571-7-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230905214235.320571-1-peterx@redhat.com> References: <20230905214235.320571-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 4DFAD40024 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: c9exbrk1wtnufa7c8zzzzao9jbimiiru X-HE-Tag: 1693950168-693602 X-HE-Meta: U2FsdGVkX184lrZYsoT6WLUChX+Bkt/Fmu6ljh2Wpack+edRoYyGYh2bzseZYeN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HYh1LGPX1KimR0rYQ1imdiJH5sntbJ9iDtRybbR0mNibAewMjNtqkeRGdh3wQizzehWzYitEEbVOXv7tl7/bldGyYLGekR3fo2ZtxdlubQwfsM4k7INpDDTK8eVBHQk+D14INat6dsufFhINQ0+TSSz5mdA/k7B1ejCA5LzUiKopme/TMZLETaGlNcwgaUiiI0WI+lVh3SD0++sh/ag2J/qQBudPBrj+viJymZsBMOVBQtzYL7uviSja32Bylvjb9jeKanUMlrYwwJG1DHZmRZzgYhor9iceoeCet5X2vIodnL+E8NdNYzFiQqO193E7ClLR8n4I276yTSxk0wGmMT4ZgGP4vL7LEam3nXOJHGrQ6gyi19eK X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make them common functions to be reused. Signed-off-by: Peter Xu --- 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; }