From patchwork Sat Sep 29 08:43:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 10620681 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B31B06CB for ; Sat, 29 Sep 2018 08:43:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A6BC729630 for ; Sat, 29 Sep 2018 08:43:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9AB3629734; Sat, 29 Sep 2018 08:43:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9080429630 for ; Sat, 29 Sep 2018 08:43:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91FDA8E0003; Sat, 29 Sep 2018 04:43:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8CC798E0001; Sat, 29 Sep 2018 04:43:26 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7966A8E0003; Sat, 29 Sep 2018 04:43:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by kanga.kvack.org (Postfix) with ESMTP id 4AEA98E0001 for ; Sat, 29 Sep 2018 04:43:26 -0400 (EDT) Received: by mail-qt1-f197.google.com with SMTP id p52-v6so8099545qtf.9 for ; Sat, 29 Sep 2018 01:43:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=jr2FkhdpO4Zy0gr3lTuVC3vLaaL0bHiEHlC2dEEztIs=; b=sLPtn28dwqWx+Q4O5uqRDtwQ0DGRHUoT2z2Qtl1zjcKVX23zHDZeFzHgaX1Tw+FpBS UIG8NDBbltriQOW2bCr6S3KVK7JH6ih1O7nYxZ7ymKoLEQh9+taR9By6SkT7qF80bGvn CYQIxMAqMYk8yucKlPRlOyDG2K4ujPzNhdw13BmsHT4gkap4Idtz4eFxKP7UNqtJ/EEq cCnppfmghwWqJXPgc4ficW5HAjrXtvn4di5JzkSIY46O/OewWvCJ7TjgLD/u9O0/W7Bx GpLrUhoCTu0CrpFpzRWQFjyAR1Phfr/uWBFKqmtKOYyXWavG295UbDBG9o5qVDIxjKR+ aUHw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfoiE8JyBQpEhPmO0pHK8J+nSNvn5CdcFzeRwm3+ZaPq9UtGm3GR5 TR6mQD3gLNJUze6aTSu7Zu5DU5gu309i6SEA9DEf2UOaO33qjikbZ6a2ODKqwl2uMXzDZPqEQeN qxKSASSq9QWhSC+hnLc9iGj7U/PuFaIoMsHPDCHKEjizb/asFxEFeuFczV3Jq7R7VzQ== X-Received: by 2002:a0c:f988:: with SMTP id t8-v6mr1730646qvn.72.1538210606062; Sat, 29 Sep 2018 01:43:26 -0700 (PDT) X-Google-Smtp-Source: ACcGV60qfdu9VSajL4OSlp03nuthQ326EwCj4LCI1KXlinSeS4V509PLyMVLt0cPQ62w1/KFgrSz X-Received: by 2002:a0c:f988:: with SMTP id t8-v6mr1730621qvn.72.1538210605421; Sat, 29 Sep 2018 01:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538210605; cv=none; d=google.com; s=arc-20160816; b=ITO6pieApnYi5cxRcGmKiumUbc3YS2X8w6WpIdvoN3P7cncdNNJkmOX5PrQbTwvdg+ 51jBAUnbOzDDdJNc5AbPUBiYBruv+Ssk/PDoGFCYtJ1tVowZPfi4GY/eeGK4r03o9l5h Pwq1k/ck/HfukpVfHVARcANzdF7UEbFiOWPZP6eamrHAVyTrLqRs5POVNUOW6i6nQWxD eJqQ7Ko7uD2hztkgo4Jg2FKQfPRuxflKCuf9m8J6zQWRHStPdlxV/FjJNEqZY7n6iRhJ mQy2RiWJLTDF9g2XvK9Y98Vv+YZui9T2J2XhT1wz1OSrUh6M+ZvMtUsIQhGyRCkH1S7b Byxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=jr2FkhdpO4Zy0gr3lTuVC3vLaaL0bHiEHlC2dEEztIs=; b=XkKuh/ofH/w6rsKNAMYoAJtqksjNq86gCEfvYofzoTm1pe//ajVTuy6VGQNGGO6ijB TVtAe6qAN5cgFCuO09pHziBgqoblNQFGB6UzD9EN/3qx1u6CsCR7/TaRR29F9+tWP6XE OUpc8sWETf3Kx9OmeCy53gcUAG1V3Yqo3qWlCQOoXXfaByLn6a83eAWG0SkSrSFQutbF Y7ZEcBrOd5eC5ywGzJ3us9Mxgq8x7OP05oCRR2HXdZkVuAokwJGMiWYbzyq7PiAuUYjt F2Trf5+vDGBtpWK4x/jZqgdfC0Dkg2qAoNOL8ECq0w5iJWD9mm5uEpJMJovgW0tGEgdG 8G/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id s18-v6si548103qke.161.2018.09.29.01.43.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Sep 2018 01:43:25 -0700 (PDT) Received-SPF: pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7B9D4308338E; Sat, 29 Sep 2018 08:43:24 +0000 (UTC) Received: from xz-x1.nay.redhat.com (dhcp-14-128.nay.redhat.com [10.66.14.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 299B3106A790; Sat, 29 Sep 2018 08:43:19 +0000 (UTC) From: Peter Xu To: linux-kernel@vger.kernel.org Cc: Shuah Khan , Jerome Glisse , Mike Rapoport , Mike Kravetz , peterx@redhat.com, linux-mm@kvack.org, Zi Yan , "Kirill A . Shutemov" , linux-kselftest@vger.kernel.org, Shaohua Li , Andrea Arcangeli , "Dr . David Alan Gilbert" , Andrew Morton Subject: [PATCH 1/3] userfaultfd: selftest: cleanup help messages Date: Sat, 29 Sep 2018 16:43:09 +0800 Message-Id: <20180929084311.15600-2-peterx@redhat.com> In-Reply-To: <20180929084311.15600-1-peterx@redhat.com> References: <20180929084311.15600-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Sat, 29 Sep 2018 08:43:24 +0000 (UTC) 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: X-Virus-Scanned: ClamAV using ClamSMTP Firstly, the help in the comment region is obsolete, now we support three parameters. Since at it, change it and move it into the help message of the program. Also, the help messages dumped here and there is obsolete too. Use a single usage() helper. Signed-off-by: Peter Xu Acked-by: Mike Rapoport --- tools/testing/selftests/vm/userfaultfd.c | 44 ++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 7b8171e3128a..2a84adaf8cf8 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -34,18 +34,6 @@ * per-CPU threads 1 by triggering userfaults inside * pthread_mutex_lock will also verify the atomicity of the memory * transfer (UFFDIO_COPY). - * - * The program takes two parameters: the amounts of physical memory in - * megabytes (MiB) of the area and the number of bounces to execute. - * - * # 100MiB 99999 bounces - * ./userfaultfd 100 99999 - * - * # 1GiB 99 bounces - * ./userfaultfd 1000 99 - * - * # 10MiB-~6GiB 999 bounces, continue forever unless an error triggers - * while ./userfaultfd $[RANDOM % 6000 + 10] 999; do true; done */ #define _GNU_SOURCE @@ -115,6 +103,28 @@ pthread_attr_t attr; ~(unsigned long)(sizeof(unsigned long long) \ - 1))) +const char *examples = + "# 100MiB 99999 bounces\n" + "./userfaultfd anon 100 99999\n" + "\n" + "# 1GiB 99 bounces\n" + "./userfaultfd anon 1000 99\n" + "\n" + "# 10MiB-~6GiB 999 bounces, continue forever unless an error triggers\n" + "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n" + "\n"; + +static void usage(void) +{ + fprintf(stderr, "\nUsage: ./userfaultfd " + "[hugetlbfs_file]\n\n"); + fprintf(stderr, "Supported : anon, hugetlb, " + "hugetlb_shared, shmem\n\n"); + fprintf(stderr, "Examples:\n\n"); + fprintf(stderr, examples); + exit(1); +} + static int anon_release_pages(char *rel_area) { int ret = 0; @@ -1272,8 +1282,7 @@ static void sigalrm(int sig) int main(int argc, char **argv) { if (argc < 4) - fprintf(stderr, "Usage: [hugetlbfs_file]\n"), - exit(1); + usage(); if (signal(SIGALRM, sigalrm) == SIG_ERR) fprintf(stderr, "failed to arm SIGALRM"), exit(1); @@ -1286,20 +1295,19 @@ int main(int argc, char **argv) nr_cpus; if (!nr_pages_per_cpu) { fprintf(stderr, "invalid MiB\n"); - fprintf(stderr, "Usage: \n"), exit(1); + usage(); } bounces = atoi(argv[3]); if (bounces <= 0) { fprintf(stderr, "invalid bounces\n"); - fprintf(stderr, "Usage: \n"), exit(1); + usage(); } nr_pages = nr_pages_per_cpu * nr_cpus; if (test_type == TEST_HUGETLB) { if (argc < 5) - fprintf(stderr, "Usage: hugetlb \n"), - exit(1); + usage(); huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755); if (huge_fd < 0) { fprintf(stderr, "Open of %s failed", argv[3]); From patchwork Sat Sep 29 08:43:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 10620685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 06BAE6CB for ; Sat, 29 Sep 2018 08:43:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE48C29630 for ; Sat, 29 Sep 2018 08:43:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E1DB529734; Sat, 29 Sep 2018 08:43:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 71DBD29630 for ; Sat, 29 Sep 2018 08:43:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54B028E0004; Sat, 29 Sep 2018 04:43:31 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4FB618E0005; Sat, 29 Sep 2018 04:43:31 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C40D8E0004; Sat, 29 Sep 2018 04:43:31 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by kanga.kvack.org (Postfix) with ESMTP id 0FDDE8E0001 for ; Sat, 29 Sep 2018 04:43:31 -0400 (EDT) Received: by mail-qt1-f200.google.com with SMTP id p52-v6so8099740qtf.9 for ; Sat, 29 Sep 2018 01:43:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=ye/75/THLV4vdRy1JAIrltokdN0e0k9yoEs0CUVH5OA=; b=dLsbRHvMAIr1kAd9fJFNsohwLnhJqdFFuGBLVyKkG38YdCF2IrzIYFdQlEEd/VceS/ WSCYkcRiVQ1c6hmiv2g1XqwwdwWHrGHaEhFOw0ZSn0y9A4RokgjDgiGCaTJS+VWgG/Cb o6H4CY0Ou0f/oQDSPfIq91m8Z8UEuNIFC5SNeBTtX8HbNiDP4RHirWfS4z3TDt/XsXsh 7+Z+4S3juaX4w8WdtHWG9lePYHeHzyOGCGJdvFB0tJyL2NyJbkbLKWlg+Fm8YHvF/iW6 /DAHZg/XQJOMNezRoux+nVvpgfSznJ8ZCs0cT9A7uhX1pbEZZ6FMUayAWPuP44bRDxmZ YaLA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfohThTpxtmRg2EApuVVQJiVJMc0lRq8b1/JRbCXd7xGBC/b2l7xs u3A2VIpj7WQkQr+ZxqFr3beGfyA9uGL5Bl7xIFiiZ5qS1Z2QACAOx49PwYT6mlu0EtHUXbeVOhF 1eQsHj/WmqlScJZyKIpezYnDWurFo/o2wfJpglLLvocMaIrrHzr2EFK8dx57EcuYKLg== X-Received: by 2002:a0c:fe87:: with SMTP id d7-v6mr1747394qvs.102.1538210610792; Sat, 29 Sep 2018 01:43:30 -0700 (PDT) X-Google-Smtp-Source: ACcGV60yeniDDkZMTpU0oBW4eDV8kacxbq1B2iZtk6Z5rPILN2QFTm0OlEJ9tFxyLJyUFuBllLMP X-Received: by 2002:a0c:fe87:: with SMTP id d7-v6mr1747374qvs.102.1538210610219; Sat, 29 Sep 2018 01:43:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538210610; cv=none; d=google.com; s=arc-20160816; b=0t9rDv28DC7A9/Hx47vcSrzBIdPhp0ZKm3TeINWPInGtLNYBrw4Mj4K6nvztmQ/D1/ 5KPm74lltxhTldPMAV5aIPO3nq682MswUhO43nH8mR1PaEFwSDKZf4174f4YK6tsnAEm SneJoCsQ+tWwhLkBXRdbkuH5WvDKxbivj3QbHPqNio9YMRcnl7A9roFJn7zMd9sT1wnG tcGp+7dd6srck0yzc9KNc3lb4px0aibADcieQVWgcIRdcnmSy6Vo8ZiCoTdiav8G9PQN 23woWIP1Jm+jz5NROF/9Wv629Nl8fcw/LhA6XBUswFa8kxlamHomuJSdXQ+c0uL8FLKo 51YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=ye/75/THLV4vdRy1JAIrltokdN0e0k9yoEs0CUVH5OA=; b=IRv1nuoL94RmrIEe96k5xn6zLy54Y9L9U0m88nNGfOQAPbvxGAeaG1ZpHJ8sTE8rcx npDqBIOFyOZdrO3QdQXQjx5tayDCavAmlXjut5sgvsLGlNlWOMLaGcVtnssWjs9pl7Uw qv7bysIGv/u0Vv7wKDH1IqvOjamtzKRArnOPGKM8yjy5GUyDabboKBizdQ+DZ92BG8UO 9mB28uwfasM0Bf5h9KYFWBH8W4JfKAm43NdG/u8XIJu7yCJi+ke4mGSVvQBfpS7JB/Na gj/Ot/3KMh1rx0VPdIaLsE4I/D5A4bcCAfXHm5whcjC2+U0KkSXeLzzAVSrpsPKta0HG Jpfg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id b3-v6si2106869qvo.193.2018.09.29.01.43.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Sep 2018 01:43:30 -0700 (PDT) Received-SPF: pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C8398762B; Sat, 29 Sep 2018 08:43:29 +0000 (UTC) Received: from xz-x1.nay.redhat.com (dhcp-14-128.nay.redhat.com [10.66.14.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0C6A106A790; Sat, 29 Sep 2018 08:43:24 +0000 (UTC) From: Peter Xu To: linux-kernel@vger.kernel.org Cc: Shuah Khan , Jerome Glisse , Mike Rapoport , Mike Kravetz , peterx@redhat.com, linux-mm@kvack.org, Zi Yan , "Kirill A . Shutemov" , linux-kselftest@vger.kernel.org, Shaohua Li , Andrea Arcangeli , "Dr . David Alan Gilbert" , Andrew Morton Subject: [PATCH 2/3] userfaultfd: selftest: generalize read and poll Date: Sat, 29 Sep 2018 16:43:10 +0800 Message-Id: <20180929084311.15600-3-peterx@redhat.com> In-Reply-To: <20180929084311.15600-1-peterx@redhat.com> References: <20180929084311.15600-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Sat, 29 Sep 2018 08:43:29 +0000 (UTC) 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: X-Virus-Scanned: ClamAV using ClamSMTP We do very similar things in read and poll modes, but we're copying the codes around. Share the codes properly on reading the message and handling the page fault to make the code cleaner. Meanwhile this solves previous mismatch of behaviors between the two modes on that the old code: - did not check EAGAIN case in read() mode - ignored BOUNCE_VERIFY check in read() mode Signed-off-by: Peter Xu --- tools/testing/selftests/vm/userfaultfd.c | 76 +++++++++++++----------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 2a84adaf8cf8..f79706f13ce7 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -449,6 +449,42 @@ static int copy_page(int ufd, unsigned long offset) return __copy_page(ufd, offset, false); } +static int uffd_read_msg(int ufd, struct uffd_msg *msg) +{ + int ret = read(uffd, msg, sizeof(*msg)); + + if (ret != sizeof(*msg)) { + if (ret < 0) + if (errno == EAGAIN) + return 1; + else + perror("blocking read error"), exit(1); + else + fprintf(stderr, "short read\n"), exit(1); + } + + return 0; +} + +/* Return 1 if page fault handled by us; otherwise 0 */ +static int uffd_handle_page_fault(struct uffd_msg *msg) +{ + unsigned long offset; + + if (msg->event != UFFD_EVENT_PAGEFAULT) + fprintf(stderr, "unexpected msg event %u\n", + msg->event), exit(1); + + if (bounces & BOUNCE_VERIFY && + msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) + fprintf(stderr, "unexpected write fault\n"), exit(1); + + offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; + offset &= ~(page_size-1); + + return copy_page(uffd, offset); +} + static void *uffd_poll_thread(void *arg) { unsigned long cpu = (unsigned long) arg; @@ -456,7 +492,6 @@ static void *uffd_poll_thread(void *arg) struct uffd_msg msg; struct uffdio_register uffd_reg; int ret; - unsigned long offset; char tmp_chr; unsigned long userfaults = 0; @@ -480,25 +515,15 @@ static void *uffd_poll_thread(void *arg) if (!(pollfd[0].revents & POLLIN)) fprintf(stderr, "pollfd[0].revents %d\n", pollfd[0].revents), exit(1); - ret = read(uffd, &msg, sizeof(msg)); - if (ret < 0) { - if (errno == EAGAIN) - continue; - perror("nonblocking read error"), exit(1); - } + if (uffd_read_msg(uffd, &msg)) + continue; switch (msg.event) { default: fprintf(stderr, "unexpected msg event %u\n", msg.event), exit(1); break; case UFFD_EVENT_PAGEFAULT: - if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) - fprintf(stderr, "unexpected write fault\n"), exit(1); - offset = (char *)(unsigned long)msg.arg.pagefault.address - - area_dst; - offset &= ~(page_size-1); - if (copy_page(uffd, offset)) - userfaults++; + userfaults += uffd_handle_page_fault(&msg); break; case UFFD_EVENT_FORK: close(uffd); @@ -526,8 +551,6 @@ static void *uffd_read_thread(void *arg) { unsigned long *this_cpu_userfaults; struct uffd_msg msg; - unsigned long offset; - int ret; this_cpu_userfaults = (unsigned long *) arg; *this_cpu_userfaults = 0; @@ -536,24 +559,9 @@ static void *uffd_read_thread(void *arg) /* from here cancellation is ok */ for (;;) { - ret = read(uffd, &msg, sizeof(msg)); - if (ret != sizeof(msg)) { - if (ret < 0) - perror("blocking read error"), exit(1); - else - fprintf(stderr, "short read\n"), exit(1); - } - if (msg.event != UFFD_EVENT_PAGEFAULT) - fprintf(stderr, "unexpected msg event %u\n", - msg.event), exit(1); - if (bounces & BOUNCE_VERIFY && - msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) - fprintf(stderr, "unexpected write fault\n"), exit(1); - offset = (char *)(unsigned long)msg.arg.pagefault.address - - area_dst; - offset &= ~(page_size-1); - if (copy_page(uffd, offset)) - (*this_cpu_userfaults)++; + if (uffd_read_msg(uffd, &msg)) + continue; + (*this_cpu_userfaults) += uffd_handle_page_fault(&msg); } return (void *)NULL; } From patchwork Sat Sep 29 08:43:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 10620687 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8A11A1750 for ; Sat, 29 Sep 2018 08:43:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7AC012AF98 for ; Sat, 29 Sep 2018 08:43:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6CCD12AFA1; Sat, 29 Sep 2018 08:43:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 090BD2AF98 for ; Sat, 29 Sep 2018 08:43:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC2878E0005; Sat, 29 Sep 2018 04:43:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E4AF88E0001; Sat, 29 Sep 2018 04:43:35 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3B448E0005; Sat, 29 Sep 2018 04:43:35 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id A508F8E0001 for ; Sat, 29 Sep 2018 04:43:35 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id z18-v6so8335927qki.22 for ; Sat, 29 Sep 2018 01:43:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=05flFEVEhG7qTUpNjE7Sf2JlGt3wwT/uLXi6I+/HmrI=; b=DmFL8PgdATY8VNIFI3Y0v7Lr75qxu/OR8C1RbCRsRVEbPG9Ojkkgl99UsjcEaDcUpN EdAXonGt75wyzxtd+z3dpa0OzDLtJU+6hVqKhrB6LasWUblLG4OJad/gciWAvlNvCgYY R1KlW/5P4cVQDI+WPDRIQpwoE0Y8lWBK3A12KkLFw+UQtANSo0iqb/+GPfMEYFTxSwg6 94izizAoAbLIM+QuMEmzy1vs6xHQ5iTFYXHdywgfMa1OofBz5qulNEyJt3Y7mUAvkSSQ LI0pkE5y78pO4e7SDMxzUL8DXUKoWamwTjARBYfFQGzZHQZDMe4uHd2MZV0+gxdPDPMz N5OA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfohVTIQlbj1uc98zvpyTmHaluv2Nihe5Ai98k9YUHEtYuyJcpu4q +xZy1jWJRqHCtGpQFzXXEvkxkGP+Ms+qWz8hDFzvfYam1UMvhNvIWVQyN2N9r3GwnWTkJUIe+dv pJ42z8T9k+4qh41knTKyTF4RYqth9VXlIAxrdjZ4JPikS8iddlYk4K6eooeQPa3vqUA== X-Received: by 2002:ad4:404a:: with SMTP id r10-v6mr1696910qvp.246.1538210615424; Sat, 29 Sep 2018 01:43:35 -0700 (PDT) X-Google-Smtp-Source: ACcGV61Maz4btNV+lArI5NiNT/tNkU6s4msbSfmVDcftgXeM0qlJeFdhd3IZ4qopOhfh4IObXT6p X-Received: by 2002:ad4:404a:: with SMTP id r10-v6mr1696898qvp.246.1538210614931; Sat, 29 Sep 2018 01:43:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538210614; cv=none; d=google.com; s=arc-20160816; b=asra/2FerKG747vJvmWH6nfMjHvzrpQr5/cNMVDUCptHpOhGHvK2fhmNfIBtj00F3s /aZc8psAdCqzlvRlXM82qofgX9qh9HQhZMTN4rnRX+7HY4ynZpaMwYd5ceG5doI8Ok4Y t2aRF4pjooHFaYYLibzVd/zTzmiomr+k2qekA1+/H05L6mObX+jvVT/kHja71AKTBhPt luwPsoe4IrIi5r8xYDWJ7KwZsAlqgQGfssTp45ztk9eBtME8uOdwa3hM10qT9Ngnqyr8 Tl9miRIVjPIOj0pdVDSgunmtYGbvQWsAB8wLoJ/xaSF4edyIviUvOcxIsHQg6ZNnoP1/ Gxew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=05flFEVEhG7qTUpNjE7Sf2JlGt3wwT/uLXi6I+/HmrI=; b=S4rE1ZLLI8zv9xV5HsRQGa5n51TNrUGrh+exobSARBLJF9s45JON5vh8R6PJPH3QvK ntdyWEQpszD5H9xHGzTbu5GxyKjYLsLDngpVdMKFpPPhXyVn/nxnZC7YfnH9bIxriqLh 1RBgfmqjCSHGQ+hEZ6IMEn9Y1DB5YGF41AgWTXELEMoH6wM1SGyqY9uFMTSKOMrsOLOd a4g28UTRGYLx7F1vk+gfwneQWm8MFco+qBdUT/tRxTgVr5f/UUhieefoh47iNq3AmSPJ caIRK/R38udpI6Xx3CK8gWjl5H3FCSQbi4PcemioAZXR+rITc1PfG09MLK0AMRbUYZU7 kC0Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id m21-v6si1431024qtb.280.2018.09.29.01.43.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Sep 2018 01:43:34 -0700 (PDT) Received-SPF: pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of peterx@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 203D688306; Sat, 29 Sep 2018 08:43:34 +0000 (UTC) Received: from xz-x1.nay.redhat.com (dhcp-14-128.nay.redhat.com [10.66.14.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id C5694106A790; Sat, 29 Sep 2018 08:43:29 +0000 (UTC) From: Peter Xu To: linux-kernel@vger.kernel.org Cc: Shuah Khan , Jerome Glisse , Mike Rapoport , Mike Kravetz , peterx@redhat.com, linux-mm@kvack.org, Zi Yan , "Kirill A . Shutemov" , linux-kselftest@vger.kernel.org, Shaohua Li , Andrea Arcangeli , "Dr . David Alan Gilbert" , Andrew Morton Subject: [PATCH 3/3] userfaultfd: selftest: recycle lock threads first Date: Sat, 29 Sep 2018 16:43:11 +0800 Message-Id: <20180929084311.15600-4-peterx@redhat.com> In-Reply-To: <20180929084311.15600-1-peterx@redhat.com> References: <20180929084311.15600-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Sat, 29 Sep 2018 08:43:34 +0000 (UTC) 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: X-Virus-Scanned: ClamAV using ClamSMTP Now we recycle the uffd servicing threads earlier than the lock threads. It might happen that when the lock thread is still blocked at a pthread mutex lock while the servicing thread has already quitted for the cpu so the lock thread will be blocked forever and hang the test program. To fix the possible race, recycle the lock threads first. This never happens with current missing-only tests, but when I start to run the write-protection tests (the feature is not yet posted upstream) it happens every time of the run possibly because in that new test we'll need to service two page faults for each lock operation. Signed-off-by: Peter Xu Acked-by: Mike Rapoport --- tools/testing/selftests/vm/userfaultfd.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index f79706f13ce7..a388675b15af 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -623,6 +623,12 @@ static int stress(unsigned long *userfaults) if (uffd_test_ops->release_pages(area_src)) return 1; + + finished = 1; + for (cpu = 0; cpu < nr_cpus; cpu++) + if (pthread_join(locking_threads[cpu], NULL)) + return 1; + for (cpu = 0; cpu < nr_cpus; cpu++) { char c; if (bounces & BOUNCE_POLL) { @@ -640,11 +646,6 @@ static int stress(unsigned long *userfaults) } } - finished = 1; - for (cpu = 0; cpu < nr_cpus; cpu++) - if (pthread_join(locking_threads[cpu], NULL)) - return 1; - return 0; }