Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1454654yba; Thu, 4 Apr 2019 11:04:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqx9wP5upKlAnHrlPpNX3rUk9cwTndjX7D1jWaRCuO7KZje1NU8o+S0o9xNSumAAV9wGKlWS X-Received: by 2002:aa7:8284:: with SMTP id s4mr7460187pfm.235.1554401054219; Thu, 04 Apr 2019 11:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554401054; cv=none; d=google.com; s=arc-20160816; b=tf+s/jJArnL6qgyn9ZflIhxpBwVn528ONWzLnsqE782AlTs58V3gdBm1mO+xW2HyZ1 twj+iRS6werpOQdwEVMGnqJm6I8VKH/WH4xUIyIaX20fXHABmPIY/72zSU1s/5Hk3BnX t6AYWwG/phvF/6jdmItfjObN3jP/JB2y0ZQREngJPHrHABe04uwVt1d79fVw4KksEb73 9zS2EFzqnyvoOE1avQIIVAVdh0tiKVjk4o2w8jhfqICldq9GTbE7mDKZih57msGTDuH1 F7DHL4yY5VDHAxd4EGRxPWSCL6MWTRBhZDzIK3ug29xv9T0gllwtwYDKfKeRxX7k4uZR +LRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=R/X4KGAOrn8p8WQqB1vXISXlABvnNdOPZ+VNYM7eKxQ=; b=tLBmiBImYQIoJ6jNgQA8grF2ND+p6ets9g2ZbX/ERpc5pdkZJ0ObgFuvSvPDY6Rj3A X4GAe1aE4OdBM0WGhUX2Z69ksyhE7KkN5MW4D6oR808IW7C5rPUnixPI5aKn5oFrdveT iFSRachltp0Yt4RAAF7VMKEEvVQFVIpW+lCx+yrcblaVxfG/WsySxPzFq4OAOhOPhJgY NbCmWUgozq2C+aTi4Db8tBVEw421PDyOUaZBfHJ2PaNImjKojq7sxneUyH5zEn6NC9SL ogIHdqgR/nOe35G45T1Qpd5zjFsneepEMh9424YKMbkfYtHFcDHI25cCG5uKWIRv+8Fr lLGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Pq/Rob6f"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t17si15554440pgv.493.2019.04.04.11.03.58; Thu, 04 Apr 2019 11:04:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Pq/Rob6f"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729286AbfDDSCh (ORCPT + 99 others); Thu, 4 Apr 2019 14:02:37 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:41531 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726418AbfDDSCh (ORCPT ); Thu, 4 Apr 2019 14:02:37 -0400 Received: by mail-wr1-f65.google.com with SMTP id r4so4918196wrq.8; Thu, 04 Apr 2019 11:02:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=R/X4KGAOrn8p8WQqB1vXISXlABvnNdOPZ+VNYM7eKxQ=; b=Pq/Rob6fl9+mgNPaqkz+hZb6gyvwIOSKFjjR4WCxyc6a4toWH35GLCqNm0qyGz6N3H jM9UsyjfHrVzppB83zup1HMTcwrae3YwQRXxrAH+nbg0cSH+c2tPS9IihZd+BqYz4pAe tTC434P1srDQXRapSvV0ASzOVg2cctTl7WLqoA+6usTwp0dqx/yRVYQ929M3QfSdc13B +uub+Z1z299qP+eXH2RjiSE+9ZtQ4RaJ1jilZHHSf97PeyiQ0dKj2D5iDODQrA/PnbpI 9f5CyXK4JZtjGu2NbXoM/aOabcpc5BQT5BaVYhqRbPaZbnZlndj3y8roXS7VnY25L/NC Qw0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=R/X4KGAOrn8p8WQqB1vXISXlABvnNdOPZ+VNYM7eKxQ=; b=ewbjDCMicTwOX8F5gQcqyrAvupPK/LF7L2HrCuG9SsLpnmmA7uleUPJw1GC/BNcrxS w/SPJPPut37jb5GZ9F6/zTm3NcP66gOAtfXbUj2uLwquBmyH1h60Ytk+RRF4P5hP4tjH IAZDRHgAdexF2WbX0uA5smN0u2qj6wKAfPgSKD8UUH5Mat6IRIziOq0CqN7PdpvVd6AD pX5j0NLZ9ZnKPtnjPulfkVBfikt5Zp8apWGRKOrq6PFWkJxRnd1lP7puhj0aGTab+oze OVQnkXP+ho75B/Uh8ISroV77d1fFuQMC7v4jiz91OR0wstz637LpBkGwe5RL1LL+AKvn 34iQ== X-Gm-Message-State: APjAAAUbd+zgiRhlr8fF00zUahDgvk65i/e/u6yw237A0kCqsSWSq8z/ xWSJOySVAp51cXL6OCfNtj+w/SU= X-Received: by 2002:adf:ee01:: with SMTP id y1mr5399467wrn.51.1554400954052; Thu, 04 Apr 2019 11:02:34 -0700 (PDT) Received: from avx2 ([46.53.242.20]) by smtp.gmail.com with ESMTPSA id 61sm68054247wre.50.2019.04.04.11.02.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Apr 2019 11:02:32 -0700 (PDT) Date: Thu, 4 Apr 2019 21:02:30 +0300 From: Alexey Dobriyan To: Peter Zijlstra Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Subject: Re: [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs Message-ID: <20190404180230.GA14800@avx2> References: <20190403200809.GA13876@avx2> <20190404084249.GS4038@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190404084249.GS4038@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 04, 2019 at 10:42:49AM +0200, Peter Zijlstra wrote: > On Wed, Apr 03, 2019 at 11:08:09PM +0300, Alexey Dobriyan wrote: > > Currently there is no easy way to get the number of CPUs on the system. > > And this patch doesn't change that :-) It does! Application or a library could do one idempotent system call in a constructor. > Still, it does the right thing and I like it. Thanks. > The point is that nr_cpu_ids is the length of the bitmap, but does not > contain information on how many CPUs are in the system. Consider the > case where the bitmap is sparse. I understand that but how do you ship number of CPUs _and_ possible mask in one go? > > Applications are divided into 2 groups: > > One group allocates buffer and call sched_getaffinity(2) once. It works > > but either underallocate or overallocates and in the future such application > > will become buggy as Linux will start working on even more SMP-ier systems. > > > > Glibc in particular shipped with 1024 CPUs support maximum at some point > > which is quite surprising as glibc maitainers should know better. > > > > Another group dynamically grow buffer until cpumask fits. This is > > inefficient as multiple system calls are done. > > > > Nobody seems to parse "/sys/devices/system/cpu/possible". > > Even if someone does, parsing sysfs is much slower than necessary. > > True; but I suppose glibc already does lots of that anyway, right? It > does contain the right information. sched_getaffinity(3) does sched_getaffinity(2) + memset() sysconf(_SC_NPROCESSORS_ONLN) does "/sys/devices/system/cpu/online" sysconf(_SC_NPROCESSORS_CONF) does readdir("/sys/devices/system/cpu") which is 5 syscalls. I'm not sure which cpumask readdir represents. > > Patch overloads sched_getaffinity(len=0) to simply return "nr_cpu_ids". > > This will make gettting CPU mask require at most 2 system calls > > and will eliminate unnecessary code. > > > > len=0 is chosen so that > > * passing zeroes is the simplest thing > > > > syscall(__NR_sched_getaffinity, 0, 0, NULL) > > > > will simply do the right thing, > > > > * old kernels returned -EINVAL unconditionally. > > > > Note: glibc segfaults upon exiting from system call because it tries to > > clear the rest of the buffer if return value is positive, so > > applications will have to use syscall(3). > > Good news is that it proves noone uses sched_getaffinity(pid, 0, NULL). > > This also needs a manpage update. And I'm missing the libc people on Cc. [nods] Shipping "man/2/" with kernel is long overdue. :^) > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -4942,6 +4942,9 @@ SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, > > int ret; > > cpumask_var_t mask; > > > > + if (len == 0) > > + return nr_cpu_ids; > > + > > if ((len * BITS_PER_BYTE) < nr_cpu_ids) > > return -EINVAL; > > if (len & (sizeof(unsigned long)-1))