From patchwork Tue Sep 5 21:42:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13375098 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 720F8CA100D for ; Tue, 5 Sep 2023 21:42:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51D268E0010; Tue, 5 Sep 2023 17:42:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 499918E000B; Tue, 5 Sep 2023 17:42:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 312218E0010; Tue, 5 Sep 2023 17:42:51 -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 1B9238E000B for ; Tue, 5 Sep 2023 17:42:51 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F05C91CA7EF for ; Tue, 5 Sep 2023 21:42:50 +0000 (UTC) X-FDA: 81203868900.03.7864D9E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id BA5222002B for ; Tue, 5 Sep 2023 21:42:48 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bAO22uVa; spf=pass (imf03.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=uQJce4v94fBnSsGo2eU4ZKQw/Wk8m14ksf+Px8TLSGM=; b=7UN6LRfo5kzYoFFaJ30i15/JdNRl1MU7zSZU4tW23y/bFMk8TqMo9PO+uECsIgDE0RJAco AAK7Lh06RBTlxSHro/2oy4jNa0ARW13qotizGzgkflzE3NlfQMdq6/YUig3jHBzruTRN0u 9w74D0QHcr255/8UDT3i45vK6uNQzvE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bAO22uVa; spf=pass (imf03.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693950168; a=rsa-sha256; cv=none; b=Hx40Gsa0J+iKhERHoyVebFhDTCTTOAXo6S7haXSP2MmoC2zrae5s6TtVpPciIFsfN6Fz6e +LHGKROblDpcyg/mbB19hAlgVhHW6vckH65gI4+i74I/sI8OqQUtKl8c/UFqlv99XVJQBJ xw04/LHa5COQ/S29cOjcUTFp71TXYa0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693950168; 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=uQJce4v94fBnSsGo2eU4ZKQw/Wk8m14ksf+Px8TLSGM=; b=bAO22uVaAb8ZdKkd8XzdL7plbo+Ncc0zUdp8qneJ9XBqMyfVSk8oZvDRhQOkYfI6R5VGmy bRfNkFpIg6ta65nfkaMNtoGPH5oNKgowkfkntlzCGVUvSE7tE3yPayGFcQ8umOOL6VSAEz lVXzb9ySxRjPcG27mQtjjco3dISwkZc= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-125-v1eyI8SWPAGq_IhV48Opzw-1; Tue, 05 Sep 2023 17:42:44 -0400 X-MC-Unique: v1eyI8SWPAGq_IhV48Opzw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-76f0d1c4045so44104185a.1 for ; Tue, 05 Sep 2023 14:42:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693950164; x=1694554964; 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=uQJce4v94fBnSsGo2eU4ZKQw/Wk8m14ksf+Px8TLSGM=; b=lcPczEenmKfXyyNrgcUj8B1DNg4+UeP5RDfQBdi+F6wPrampB0BWkfr+meJ0LFaEom uZeNoW8WlkBWbt5y2vlfmSQnuAv0SDmIVSm4zUOZNbWNQaHcqC+yHELzuai5Fy6aRX3W RhKEabkR8S8GdJN00SspZoU64acYrGsleDTGFN9ndtds0Ps9Q+4OhhATIoeQETKXw3o/ caocZnifZorL2RKro3Y5NrvItIpBZ/c5lTgOA1mltJOV+okCTPa4nlGmtBR9q3OqJRuW NgFlIe920DEKcjmscavktTiYrwPZ3fKci2ep0+y5hOLBKMGyWHnfjwVTTKUdFfwuknyb RAlw== X-Gm-Message-State: AOJu0YzsxTHoEOtUDfPUwcybH0mOroXszh3GrPdV3MFFyziBnnveOup0 tNtR1Y+BWHNO9SnQeZCwrto57jekBte+M4w6G3og12YzdvEb3MahpDkEd6YFMWXvB1WEbCd5w3d 60vyxdlOWE2Y= X-Received: by 2002:a05:620a:1aa4:b0:76f:1614:577d with SMTP id bl36-20020a05620a1aa400b0076f1614577dmr16479566qkb.4.1693950164460; Tue, 05 Sep 2023 14:42:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH0CeamzOJT7Goeqq7g3UHhRHuitMC171tC//j49BSbILvNCZigPeU/QwAtYa7Vbv4u1ikW+A== X-Received: by 2002:a05:620a:1aa4:b0:76f:1614:577d with SMTP id bl36-20020a05620a1aa400b0076f1614577dmr16479552qkb.4.1693950164161; Tue, 05 Sep 2023 14:42:44 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 14:42:43 -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 5/7] selftests/mm: Replace uffd_read_mutex with a semaphore Date: Tue, 5 Sep 2023 17:42:33 -0400 Message-ID: <20230905214235.320571-6-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: BA5222002B X-Rspam-User: X-Stat-Signature: ddgb4ccxqs9bbohdxn9snq3jo53ic8zp X-Rspamd-Server: rspam01 X-HE-Tag: 1693950168-232416 X-HE-Meta: U2FsdGVkX1/58mapCLzjbXSKuT23yqMFq3vyM/esCjR0jITIFkGwZY8Jx0l42rVW7KD5odKu0PQZ5Ko844od5NwMZJ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fvs5prxdkmxfT2Aj4hT1oMurv07pqYBuicJXxoyMknZOwRvYKrhPBnpKOhr2CJNXCdxQ3TTmRUjRsxEpLBe9eUxYshaIy33Dif+A2acFdu1ULNED4PpPl0Qnp3Ey8oWZsZIjhxng2ENVsBBCSYO12fsbpu4ZMav4Ks+5OH0ZeMSZkMvQp/XuJFsG6F8L+wq4sHUMkx+10VeC3JDLf1xDIwVTKAiSCMIn309ghoDc2qXDrmkDEfwPfQ2L1B2MbAxza9V7TjDz4r/FTHwoYaAGMI6UE+gswWMiJs8NnEjcrguedK/hN9C+9m6DARyxtX1ZbFNAyTvkPcM4HCVXInjd2larQ1AZ6lQfoazY 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: Each uffd read threads unlocks the read mutex first, probably just to make sure the thread is reaching a stage where pthread_cancel() can always work before the main thread moves on. However keeping the mutex locked always and unlock in the thread is a bit hacky. Replacing it with a semaphore which should be much clearer, where the main thread will wait() and the thread will just post(). Move it to uffd-common.* to be reused later. Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-common.c | 1 + tools/testing/selftests/mm/uffd-common.h | 2 ++ tools/testing/selftests/mm/uffd-stress.c | 8 +++----- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index 02b89860e193..aded06cab285 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -17,6 +17,7 @@ bool map_shared; bool test_uffdio_wp = true; unsigned long long *count_verify; uffd_test_ops_t *uffd_test_ops; +sem_t uffd_read_sem; static int uffd_mem_fd_create(off_t mem_size, bool hugetlb) { diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h index 7c4fa964c3b0..521523baded1 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "../kselftest.h" #include "vm_util.h" @@ -97,6 +98,7 @@ extern bool map_shared; extern bool test_uffdio_wp; extern unsigned long long *count_verify; extern volatile bool test_uffdio_copy_eexist; +extern sem_t uffd_read_sem; extern uffd_test_ops_t anon_uffd_test_ops; extern uffd_test_ops_t shmem_uffd_test_ops; diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index 469e0476af26..7219f55ae794 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -125,14 +125,12 @@ static int copy_page_retry(int ufd, unsigned long offset) return __copy_page(ufd, offset, true, test_uffdio_wp); } -pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER; - static void *uffd_read_thread(void *arg) { struct uffd_args *args = (struct uffd_args *)arg; struct uffd_msg msg; - pthread_mutex_unlock(&uffd_read_mutex); + sem_post(&uffd_read_sem); /* from here cancellation is ok */ for (;;) { @@ -196,7 +194,7 @@ static int stress(struct uffd_args *args) uffd_read_thread, (void *)&args[cpu])) return 1; - pthread_mutex_lock(&uffd_read_mutex); + sem_wait(&uffd_read_sem); } if (pthread_create(&background_threads[cpu], &attr, background_thread, (void *)cpu)) @@ -258,7 +256,7 @@ static int userfaultfd_stress(void) zeropage = area; bzero(zeropage, page_size); - pthread_mutex_lock(&uffd_read_mutex); + sem_init(&uffd_read_sem, 0, 0); pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 16*1024*1024);