From patchwork Fri May 7 15:05:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12244855 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85B41C433ED for ; Fri, 7 May 2021 15:06:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EE85F61456 for ; Fri, 7 May 2021 15:06:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE85F61456 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6972A8D0017; Fri, 7 May 2021 11:06:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 534C08D0014; Fri, 7 May 2021 11:06:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38A098D0017; Fri, 7 May 2021 11:06:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0136.hostedemail.com [216.40.44.136]) by kanga.kvack.org (Postfix) with ESMTP id 1360D8D0014 for ; Fri, 7 May 2021 11:06:04 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C4C5A824999B for ; Fri, 7 May 2021 15:06:03 +0000 (UTC) X-FDA: 78114760206.20.A5FE823 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf23.hostedemail.com (Postfix) with ESMTP id E8110A000380 for ; Fri, 7 May 2021 15:05:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620399962; 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=2pOtOZv489j8EE/2pWOw3CG9r3znR3tk6XqlrWP3ILs=; b=aTCEC/v47Tv00i1rl3WVjZLOPJUEROKJMZPILnBszV3X4Ye7WI0+CQmmeWIBlNd75Et3QW UxKayNluvmEK4vjTeUSQCc3+4j8cLhE41se8B9jCdp0K0eki84Yd6L4DzEPm1/6kv4xXqx eBMnoJOTjpDzORgMdaG1/TBS44G5ifw= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-147-e-qFNuU4NNazkQrK3wzb9A-1; Fri, 07 May 2021 11:06:01 -0400 X-MC-Unique: e-qFNuU4NNazkQrK3wzb9A-1 Received: by mail-qv1-f72.google.com with SMTP id r11-20020a0cb28b0000b02901c87a178503so6777408qve.22 for ; Fri, 07 May 2021 08:06:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2pOtOZv489j8EE/2pWOw3CG9r3znR3tk6XqlrWP3ILs=; b=rkpsYT5mIG2q1ydp0HCh1HGjUCUt9sQRZFmqPiTidRTthl4e9hcbsihCzIVlkdHnrl CiWJMlAzVPUkloZKShSH42IX0Wu0iEHJD7pct5kEhxih6+Fs8NRBPNDGfZb/7TmFxwo3 ernSvkU3gplUldpAjWlsEJIZTXa83LhKRAHnbqLDuWPp+S0bjvIFbeZbaVSgF4vh/eUZ LA2eEZlpS5/Khg8VQyGAkXVOBBA4BXQhjo+dwagwziPN8CCc2XlYpkrHOAy1xIGX9p9t cDizm2qijG/FaSkgvxze7G3ThZrl5Zjd0at33cHvya9SEXpf/Rs899TEDcvNoc8XPZfd dlfw== X-Gm-Message-State: AOAM531grroSjXTwFoomAQ+DofX9NjIkaLJRCjuG8vk4R8eDux/XZDaS GSPHqHdIx1necBIDdQm1P1+AN9+HJwrEFJhMejgsxJkKuRGh/zq/eq8Yc/Hm6Y3h2PBBBc75/Oc h9co13MXdDrIR6dFHsa/whpy4NNVGB1LhlBo+8GF9DK6lkFu1MZSlbU0HdyEe X-Received: by 2002:a05:622a:1049:: with SMTP id f9mr9871159qte.140.1620399960261; Fri, 07 May 2021 08:06:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYyMQF6ihKctiWDD8i/xGdzEjnVrEZ2QGcB1BPY2LE+yfrEeYxK3caSAS8BZnFREdCJoRALA== X-Received: by 2002:a05:622a:1049:: with SMTP id f9mr9871104qte.140.1620399959849; Fri, 07 May 2021 08:05:59 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id c141sm950456qke.12.2021.05.07.08.05.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 08:05:58 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Jan Kara , John Hubbard , peterx@redhat.com, Linus Torvalds , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Oleg Nesterov , Andrew Morton , Jann Horn , Andrea Arcangeli , Jason Gunthorpe , Matthew Wilcox , Hugh Dickins Subject: [PATCH v2 1/3] mm/gup_benchmark: Support threading Date: Fri, 7 May 2021 11:05:51 -0400 Message-Id: <20210507150553.208763-2-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210507150553.208763-1-peterx@redhat.com> References: <20210507150553.208763-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="aTCEC/v4"; spf=none (imf23.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E8110A000380 X-Stat-Signature: kh78gitdiaciz83p3ef7718bu3nu8m4b Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf23; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620399954-727892 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: Add a new parameter "-j N" to support concurrent gup test. Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- tools/testing/selftests/vm/gup_test.c | 96 ++++++++++++++++++--------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/vm/gup_test.c b/tools/testing/selftests/vm/gup_test.c index 1e662d59c502a..fe043f67798b0 100644 --- a/tools/testing/selftests/vm/gup_test.c +++ b/tools/testing/selftests/vm/gup_test.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "../../../../mm/gup_test.h" #define MB (1UL << 20) @@ -15,6 +17,12 @@ #define FOLL_WRITE 0x01 /* check pte is writable */ #define FOLL_TOUCH 0x02 /* mark page accessed */ +static unsigned long cmd = GUP_FAST_BENCHMARK; +static int gup_fd, repeats = 1; +static unsigned long size = 128 * MB; +/* Serialize prints */ +static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER; + static char *cmd_to_str(unsigned long cmd) { switch (cmd) { @@ -34,17 +42,55 @@ static char *cmd_to_str(unsigned long cmd) return "Unknown command"; } +void *gup_thread(void *data) +{ + struct gup_test gup = *(struct gup_test *)data; + int i; + + /* Only report timing information on the *_BENCHMARK commands: */ + if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) || + (cmd == PIN_LONGTERM_BENCHMARK)) { + for (i = 0; i < repeats; i++) { + gup.size = size; + if (ioctl(gup_fd, cmd, &gup)) + perror("ioctl"), exit(1); + + pthread_mutex_lock(&print_mutex); + printf("%s: Time: get:%lld put:%lld us", + cmd_to_str(cmd), gup.get_delta_usec, + gup.put_delta_usec); + if (gup.size != size) + printf(", truncated (size: %lld)", gup.size); + printf("\n"); + pthread_mutex_unlock(&print_mutex); + } + } else { + gup.size = size; + if (ioctl(gup_fd, cmd, &gup)) { + perror("ioctl"); + exit(1); + } + + pthread_mutex_lock(&print_mutex); + printf("%s: done\n", cmd_to_str(cmd)); + if (gup.size != size) + printf("Truncated (size: %lld)\n", gup.size); + pthread_mutex_unlock(&print_mutex); + } + + return NULL; +} + int main(int argc, char **argv) { struct gup_test gup = { 0 }; - unsigned long size = 128 * MB; - int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 1; - unsigned long cmd = GUP_FAST_BENCHMARK; + int filed, i, opt, nr_pages = 1, thp = -1, write = 1, nthreads = 1, ret; int flags = MAP_PRIVATE, touch = 0; char *file = "/dev/zero"; + pthread_t *tid; char *p; - while ((opt = getopt(argc, argv, "m:r:n:F:f:abctTLUuwWSHpz")) != -1) { + while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) { switch (opt) { case 'a': cmd = PIN_FAST_BENCHMARK; @@ -74,6 +120,9 @@ int main(int argc, char **argv) /* strtol, so you can pass flags in hex form */ gup.gup_flags = strtol(optarg, 0, 0); break; + case 'j': + nthreads = atoi(optarg); + break; case 'm': size = atoi(optarg) * MB; break; @@ -154,8 +203,8 @@ int main(int argc, char **argv) if (write) gup.gup_flags |= FOLL_WRITE; - fd = open("/sys/kernel/debug/gup_test", O_RDWR); - if (fd == -1) { + gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); + if (gup_fd == -1) { perror("open"); exit(1); } @@ -185,32 +234,17 @@ int main(int argc, char **argv) p[0] = 0; } - /* Only report timing information on the *_BENCHMARK commands: */ - if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) || - (cmd == PIN_LONGTERM_BENCHMARK)) { - for (i = 0; i < repeats; i++) { - gup.size = size; - if (ioctl(fd, cmd, &gup)) - perror("ioctl"), exit(1); - - printf("%s: Time: get:%lld put:%lld us", - cmd_to_str(cmd), gup.get_delta_usec, - gup.put_delta_usec); - if (gup.size != size) - printf(", truncated (size: %lld)", gup.size); - printf("\n"); - } - } else { - gup.size = size; - if (ioctl(fd, cmd, &gup)) { - perror("ioctl"); - exit(1); - } - - printf("%s: done\n", cmd_to_str(cmd)); - if (gup.size != size) - printf("Truncated (size: %lld)\n", gup.size); + tid = malloc(sizeof(pthread_t) * nthreads); + assert(tid); + for (i = 0; i < nthreads; i++) { + ret = pthread_create(&tid[i], NULL, gup_thread, &gup); + assert(ret == 0); + } + for (i = 0; i < nthreads; i++) { + ret = pthread_join(tid[i], NULL); + assert(ret == 0); } + free(tid); return 0; } From patchwork Fri May 7 15:05:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12244857 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 212F7C433B4 for ; Fri, 7 May 2021 15:06:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 982E761415 for ; Fri, 7 May 2021 15:06:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 982E761415 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 202178D0018; Fri, 7 May 2021 11:06:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A5108D0014; Fri, 7 May 2021 11:06:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 007F98D0018; Fri, 7 May 2021 11:06:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id D3E3B8D0014 for ; Fri, 7 May 2021 11:06:05 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8786A62E8 for ; Fri, 7 May 2021 15:06:05 +0000 (UTC) X-FDA: 78114760290.12.2444F12 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf16.hostedemail.com (Postfix) with ESMTP id A17EA8019389 for ; Fri, 7 May 2021 15:05:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620399964; 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=XI4rD6YmuMtYUwEz0cLMaw9lo4eF56mgullQHfANGQM=; b=QDyK1KS+lof3PPleaoGWQSLRv2fXydb6eMXj6Cfnxlq58E1ThjvitByBAhLctPffD/KZtB em376+cMh+1uzPv/zFJQsdLB5aZI6ggJOVTUx+0ji2r/dmCvlS3uQ5kqK3TenhDrpPVXoE 87bE+THuEf1DMXOODunRAxtGeoQ5QdA= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-485-OjWT8c9JNzCeA5K1gJbUhw-1; Fri, 07 May 2021 11:06:03 -0400 X-MC-Unique: OjWT8c9JNzCeA5K1gJbUhw-1 Received: by mail-qk1-f199.google.com with SMTP id c4-20020a3781040000b02902e46e29acf5so6309071qkd.22 for ; Fri, 07 May 2021 08:06:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XI4rD6YmuMtYUwEz0cLMaw9lo4eF56mgullQHfANGQM=; b=ZiSjGzEb3dnr4Lj2EMhYsq7oatmrV9WvRDtFGFW/5/vM9Wzx/rzlg4H+6Etb9AITD5 p4gqbEq/Ce5gqLwDf8/9nbLOmhzEFytIsF/f8KuSycdy+ZMP6D9YaY5frEjjS/OHNN7/ 1q/V1O2xWX4AaV3g4vQLXcNt6wznLyq071OEBcnh9qW1RgDYrvhWI4+2TIPQjx/hXIud ISzZhd1OoYna1nqfSjOWJbniaRrkK2FBmVpQw6FT9xffoMEqJ5zuFSIpnANKtgqplK4w 9+m9MAt+6wZFCYWz9QRFpKtSXitKWPEFQKhqeYBhfNRu/N/sCePGsMqkPtFvBYNL5VZv u0NQ== X-Gm-Message-State: AOAM532oAmt+iGP1rMo0nmsSMJGzDSMlMHCQ7Ub8HiA2SADjUJ/ECFIF psJVEPZoZBG38GEm00D585jsHOhRJR7oHkgn9NCWhxQNlVpGXUPO9PIWNPwv8+t01S45kAj5L35 NzQPM5/lp8IzkGfz9gRejbPwfFo8MTklJ+bfnmcsGdZ4Chpt5znDFHa5sYs5Y X-Received: by 2002:a05:6214:248e:: with SMTP id gi14mr10461281qvb.7.1620399962255; Fri, 07 May 2021 08:06:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzB55PqZ+EcRjTPmnEKYJdh2C7Wh9UTwfVjNOoEAdmIkbA9ceUr6vcr1sJBVKD90XDiauZoLA== X-Received: by 2002:a05:6214:248e:: with SMTP id gi14mr10461232qvb.7.1620399961952; Fri, 07 May 2021 08:06:01 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id c141sm950456qke.12.2021.05.07.08.06.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 08:06:01 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Jan Kara , John Hubbard , peterx@redhat.com, Linus Torvalds , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Oleg Nesterov , Andrew Morton , Jann Horn , Andrea Arcangeli , Jason Gunthorpe , Matthew Wilcox , Hugh Dickins Subject: [PATCH v2 2/3] mm: gup: allow FOLL_PIN to scale in SMP Date: Fri, 7 May 2021 11:05:52 -0400 Message-Id: <20210507150553.208763-3-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210507150553.208763-1-peterx@redhat.com> References: <20210507150553.208763-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QDyK1KS+; spf=none (imf16.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A17EA8019389 X-Stat-Signature: ory84f3j76xgrzp7wq8m3uwu5751mf47 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf16; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620399955-902508 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: From: Andrea Arcangeli has_pinned cannot be written by each pin-fast or it won't scale in SMP. This isn't "false sharing" strictly speaking (it's more like "true non-sharing"), but it creates the same SMP scalability bottleneck of "false sharing". To verify the improvement, below test is done on 40 cpus host with Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (must be with CONFIG_GUP_TEST=y): $ sudo chrt -f 1 ./gup_test -a -m 512 -j 40 Where we can get (average value for 40 threads): Old kernel: 477729.97 (+- 3.79%) New kernel: 89144.65 (+-11.76%) On a similar condition with 256 cpus, this commits increases the SMP scalability of pin_user_pages_fast() executed by different threads of the same process by more than 4000%. Signed-off-by: Andrea Arcangeli [peterx: rewrite commit message, add parentheses against "(A & B)"] Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/gup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 63a079e361a3d..9933bc5c2eff2 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1292,7 +1292,7 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm, BUG_ON(*locked != 1); } - if (flags & FOLL_PIN) + if ((flags & FOLL_PIN) && !atomic_read(&mm->has_pinned)) atomic_set(&mm->has_pinned, 1); /* @@ -2617,7 +2617,7 @@ static int internal_get_user_pages_fast(unsigned long start, FOLL_FAST_ONLY))) return -EINVAL; - if (gup_flags & FOLL_PIN) + if ((gup_flags & FOLL_PIN) && !atomic_read(¤t->mm->has_pinned)) atomic_set(¤t->mm->has_pinned, 1); if (!(gup_flags & FOLL_FAST_ONLY)) From patchwork Fri May 7 15:05:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12244859 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEDFAC43461 for ; Fri, 7 May 2021 15:06:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 179C261415 for ; Fri, 7 May 2021 15:06:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 179C261415 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9CA218D0019; Fri, 7 May 2021 11:06:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A6C28D0014; Fri, 7 May 2021 11:06:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86E0C8D0019; Fri, 7 May 2021 11:06:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0170.hostedemail.com [216.40.44.170]) by kanga.kvack.org (Postfix) with ESMTP id 6372E8D0014 for ; Fri, 7 May 2021 11:06:11 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 22852A2D4 for ; Fri, 7 May 2021 15:06:11 +0000 (UTC) X-FDA: 78114760542.38.B1EBB99 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf29.hostedemail.com (Postfix) with ESMTP id DCFEA135 for ; Fri, 7 May 2021 15:06:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620399970; 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=xdcz1iiMJ5gfAfI4nfvenmAExZGLAkLPxmpCCshgl2M=; b=ic1qgjGV1P5N6y5WEibq0aGBDBr5syQ4KX/QG9Ays++Qy61aJ2IhLs9bgVz3GLuF2f00uR KidAuFbhqOflCemdyubElpASuohttO6ucaBuQUiybbvCb0vGk4yYIQDInuPKC4/KrIG2KW Nm5M57FmX+6TptmLeqJIUQDIRCcHHhs= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-603-4zvuW-40MdOjHKTy3UB6WA-1; Fri, 07 May 2021 11:06:07 -0400 X-MC-Unique: 4zvuW-40MdOjHKTy3UB6WA-1 Received: by mail-qv1-f72.google.com with SMTP id y24-20020a0ca9180000b02901d367101f9dso6858700qva.1 for ; Fri, 07 May 2021 08:06:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xdcz1iiMJ5gfAfI4nfvenmAExZGLAkLPxmpCCshgl2M=; b=EIz13l5g2EZhSNPKrrpPAWI31hZC+NbEJ99Rt2zHrzCvFzn9XTnLCp0rkROx7TrPn1 SdyXBd1Zm+PlJUM+o8mc3aKxqEbfhyQ8vGEdesw84Hbpz3/KxaZZqp0lSDIZMDKW5vDj dilM0fWPCkqEsQjv6dZ602GELEOpnxrDmAhOvR9b1Da+9V5IdZc41mVN5vZqvhvsHqZ3 8oPuX0u/9vPusogo/gn+7aB3vbGzHW2kUWFIsaC5mL7jXI9VVeJoXvTn9DmDnpCfLnOp nAd8+TUJ01KPnAv0u08k7GC+8idrCEcA5JO8/we6aSWhbGY3rkXVmR6EFY0IGhC+HOai veZA== X-Gm-Message-State: AOAM533t4iLlYYvet4JPEVC18fS2uKXK+lscwcLjqPJRfRcpGeLgZDMv /1kdHc3KSqRysmksfRe6LJNrfflVCqtJaazjFrDGAL2gUwISztWDS4wXEQlnGqfAZLYOrVqesdK vxX6nb+H6dtTAX3sN326nq+TabtdzIcTcMobzEwSAymZShbGk8OHlo4umOwgi X-Received: by 2002:a05:620a:133b:: with SMTP id p27mr9923963qkj.354.1620399964275; Fri, 07 May 2021 08:06:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmMkOVQJS6gjZ491s2ZTwn1A0qXX91bKFd/CY+lfU6/IeIUokPpEPo9SfwpZ8hf+ONImU0pg== X-Received: by 2002:a05:620a:133b:: with SMTP id p27mr9923906qkj.354.1620399963820; Fri, 07 May 2021 08:06:03 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id c141sm950456qke.12.2021.05.07.08.06.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 08:06:03 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Jan Kara , John Hubbard , peterx@redhat.com, Linus Torvalds , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Oleg Nesterov , Andrew Morton , Jann Horn , Andrea Arcangeli , Jason Gunthorpe , Matthew Wilcox , Hugh Dickins Subject: [PATCH v2 3/3] mm: gup: pack has_pinned in MMF_HAS_PINNED Date: Fri, 7 May 2021 11:05:53 -0400 Message-Id: <20210507150553.208763-4-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210507150553.208763-1-peterx@redhat.com> References: <20210507150553.208763-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DCFEA135 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ic1qgjGV; spf=none (imf29.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: oj5oqoaheyditznf7b1m314f9s414jsg Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf29; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620399964-983564 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: From: Andrea Arcangeli has_pinned 32bit can be packed in the MMF_HAS_PINNED bit as a noop cleanup. Any atomic_inc/dec to the mm cacheline shared by all threads in pin-fast would reintroduce a loss of SMP scalability to pin-fast, so there's no future potential usefulness to keep an atomic in the mm for this. set_bit(MMF_HAS_PINNED) will be theoretically a bit slower than WRITE_ONCE (atomic_set is equivalent to WRITE_ONCE), but the set_bit (just like atomic_set after this commit) has to be still issued only once per "mm", so the difference between the two will be lost in the noise. will-it-scale "mmap2" shows no change in performance with enterprise config as expected. will-it-scale "pin_fast" retains the > 4000% SMP scalability performance improvement against upstream as expected. This is a noop as far as overall performance and SMP scalability are concerned. Signed-off-by: Andrea Arcangeli [peterx: Fix build for task_mmu.c, introduce mm_set_has_pinned_flag, fix comment here and there] Signed-off-by: Peter Xu Reviewed-by: John Hubbard Reported-by: Naresh Kamboju --- fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- include/linux/mm_types.h | 10 ---------- include/linux/sched/coredump.h | 8 ++++++++ kernel/fork.c | 1 - mm/gup.c | 19 +++++++++++++++---- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 4c95cc57a66a8..6144571942db9 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1049,7 +1049,7 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, return false; if (!is_cow_mapping(vma->vm_flags)) return false; - if (likely(!atomic_read(&vma->vm_mm->has_pinned))) + if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) return false; page = vm_normal_page(vma, addr, pte); if (!page) diff --git a/include/linux/mm.h b/include/linux/mm.h index d6790ab0cf575..94dc84f6d8658 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1331,7 +1331,7 @@ static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, if (!is_cow_mapping(vma->vm_flags)) return false; - if (!atomic_read(&vma->vm_mm->has_pinned)) + if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)) return false; return page_maybe_dma_pinned(page); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 6613b26a88946..15d79858fadbd 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -435,16 +435,6 @@ struct mm_struct { */ atomic_t mm_count; - /** - * @has_pinned: Whether this mm has pinned any pages. This can - * be either replaced in the future by @pinned_vm when it - * becomes stable, or grow into a counter on its own. We're - * aggresive on this bit now - even if the pinned pages were - * unpinned later on, we'll still keep this bit set for the - * lifecycle of this mm just for simplicity. - */ - atomic_t has_pinned; - /** * @write_protect_seq: Locked when any thread is write * protecting pages mapped by this mm to enforce a later COW, diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index dfd82eab29025..4d9e3a6568758 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -73,6 +73,14 @@ static inline int get_dumpable(struct mm_struct *mm) #define MMF_OOM_VICTIM 25 /* mm is the oom victim */ #define MMF_OOM_REAP_QUEUED 26 /* mm was queued for oom_reaper */ #define MMF_MULTIPROCESS 27 /* mm is shared between processes */ +/* + * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either + * replaced in the future by mm.pinned_vm when it becomes stable, or grow into + * a counter on its own. We're aggresive on this bit for now: even if the + * pinned pages were unpinned later on, we'll still keep this bit set for the + * lifecycle of this mm, just for simplicity. + */ +#define MMF_HAS_PINNED 28 /* FOLL_PIN has run, never cleared */ #define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ diff --git a/kernel/fork.c b/kernel/fork.c index 502dc046fbc62..a71e73707ef59 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1026,7 +1026,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm_pgtables_bytes_init(mm); mm->map_count = 0; mm->locked_vm = 0; - atomic_set(&mm->has_pinned, 0); atomic64_set(&mm->pinned_vm, 0); memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); spin_lock_init(&mm->page_table_lock); diff --git a/mm/gup.c b/mm/gup.c index 9933bc5c2eff2..bb130723a6717 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1270,6 +1270,17 @@ int fixup_user_fault(struct mm_struct *mm, } EXPORT_SYMBOL_GPL(fixup_user_fault); +/* + * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's + * lifecycle. Avoid setting the bit unless necessary, or it might cause write + * cache bouncing on large SMP machines for concurrent pinned gups. + */ +static inline void mm_set_has_pinned_flag(unsigned long *mm_flags) +{ + if (!test_bit(MMF_HAS_PINNED, mm_flags)) + set_bit(MMF_HAS_PINNED, mm_flags); +} + /* * Please note that this function, unlike __get_user_pages will not * return 0 for nr_pages > 0 without FOLL_NOWAIT @@ -1292,8 +1303,8 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm, BUG_ON(*locked != 1); } - if ((flags & FOLL_PIN) && !atomic_read(&mm->has_pinned)) - atomic_set(&mm->has_pinned, 1); + if (flags & FOLL_PIN) + mm_set_has_pinned_flag(&mm->flags); /* * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior @@ -2617,8 +2628,8 @@ static int internal_get_user_pages_fast(unsigned long start, FOLL_FAST_ONLY))) return -EINVAL; - if ((gup_flags & FOLL_PIN) && !atomic_read(¤t->mm->has_pinned)) - atomic_set(¤t->mm->has_pinned, 1); + if (gup_flags & FOLL_PIN) + mm_set_has_pinned_flag(¤t->mm->flags); if (!(gup_flags & FOLL_FAST_ONLY)) might_lock_read(¤t->mm->mmap_lock);