From mboxrd@z Thu Jan 1 00:00:00 1970 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=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable 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 61268C63697 for ; Thu, 19 Nov 2020 00:42:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 007CC22266 for ; Thu, 19 Nov 2020 00:42:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=axtens.net header.i=@axtens.net header.b="lhpPTrHG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727007AbgKSAmo (ORCPT ); Wed, 18 Nov 2020 19:42:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726162AbgKSAmn (ORCPT ); Wed, 18 Nov 2020 19:42:43 -0500 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B146DC0613D4 for ; Wed, 18 Nov 2020 16:42:43 -0800 (PST) Received: by mail-pl1-x643.google.com with SMTP id bj5so1168695plb.4 for ; Wed, 18 Nov 2020 16:42:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vwvOdNltri8QeG/8NJREwvHkCBGEECJcvZZE736hY4g=; b=lhpPTrHGYU5nVBFNX5ArJFc05EAOTtLgysWxOa4ciurgWUviyxGN4iZV78F5KktPe6 J4MCAKsgOFCcyJ2VT/JZ6qLQzJG2+we59Dz3bsFjpDv9ABcKElxQLjX7Vt9Bn3ob86gh ok5v0yLYNsdq+N0xo7w+un75v7trNkUBSw0TY= 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:mime-version :content-transfer-encoding; bh=vwvOdNltri8QeG/8NJREwvHkCBGEECJcvZZE736hY4g=; b=ShUoTq2B6BJRp7KS7pcGAH6Z1Yg7oIsm58BDWLhvmx42+LjMgnFl5FJMf8Oq5WxkI6 xmugWNtKIj1GbveU9Cf6uxRPIAWPwNnwFtRlCIb8wec2zXs7lrUtCwC5AHWHrQgQq88e lZ475R2184Sr0psJms1iUcbbLH9FXiN13eevykJ4UeKOBJkQdv8Mm208qxaPqdeIL1WO YpnoSOG5/xXEuLFSGthd8qvnX7L4nNKLoX4IZBwz/VZnpJWbBEsgBLwKarL+PYw6h2Xs Nx5iy9+STzWf/1tKxrnVOFQEi/1Ws26AthBoEqRksRf1WB0lDfOUxpaCVr5/TSvVSACj 3NYg== X-Gm-Message-State: AOAM5319vFrFAXDk6+hlhy0yVaWcagmCojVSkA7brABExQFVrp2XYMaV zeKb7EuiN52tDve/9ElSN30JRg== X-Google-Smtp-Source: ABdhPJyTII3cKUvBoUVCBDit8YdMBKcwl776/hYEqYpBNBBnaf7gugKq37j+rUmyQ9DOz0Nk5sJ4bg== X-Received: by 2002:a17:902:9689:b029:d8:e310:2fa2 with SMTP id n9-20020a1709029689b02900d8e3102fa2mr7104334plp.42.1605746562426; Wed, 18 Nov 2020 16:42:42 -0800 (PST) Received: from localhost (2001-44b8-111e-5c00-a5b9-f4da-efe6-5d34.static.ipv6.internode.on.net. [2001:44b8:111e:5c00:a5b9:f4da:efe6:5d34]) by smtp.gmail.com with ESMTPSA id y5sm3754343pja.52.2020.11.18.16.42.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2020 16:42:41 -0800 (PST) From: Daniel Axtens To: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: David.Laight@ACULAB.COM, hch@infradead.org, Daniel Axtens Subject: [PATCH RESEND v2] fs/select.c: batch user writes in do_sys_poll Date: Thu, 19 Nov 2020 11:42:35 +1100 Message-Id: <20201119004235.173373-1-dja@axtens.net> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When returning results to userspace, do_sys_poll repeatedly calls put_user() - once per fd that it's watching. This means that on architectures that support some form of kernel-to-userspace access protection, we end up enabling and disabling access once for each file descripter we're watching. This is inefficent and we can improve things. We could do careful batching of the opening and closing of the access window, or we could just copy the entire walk entries structure. While that copies more data, it potentially does so more efficiently, and the overhead is much less than the lock/unlock overhead. Unscientific benchmarking with the poll2_threads microbenchmark from will-it-scale, run as `./poll2_threads -t 1 -s 15`: - Bare-metal Power9 with KUAP: ~49% speed-up - VM on amd64 laptop with SMAP: ~25% speed-up Signed-off-by: Daniel Axtens --- fs/select.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/fs/select.c b/fs/select.c index ebfebdfe5c69..4a74d1353ccb 100644 --- a/fs/select.c +++ b/fs/select.c @@ -1012,12 +1012,10 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, poll_freewait(&table); for (walk = head; walk; walk = walk->next) { - struct pollfd *fds = walk->entries; - int j; - - for (j = 0; j < walk->len; j++, ufds++) - if (__put_user(fds[j].revents, &ufds->revents)) - goto out_fds; + if (copy_to_user(ufds, walk->entries, + sizeof(struct pollfd) * walk->len)) + goto out_fds; + ufds += walk->len; } err = fdcount; -- 2.25.1