Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1455967yba; Sat, 6 Apr 2019 12:49:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqwiO0iZSIm41qM9nYHNzVgPeMAW8cha+YSIBuG9QWYJXYk5EPsTROneQJPeb7zVKfotfSPf X-Received: by 2002:a17:902:9341:: with SMTP id g1mr21088737plp.81.1554580198212; Sat, 06 Apr 2019 12:49:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554580198; cv=none; d=google.com; s=arc-20160816; b=ZV78nLLiPN7ieFmnw8RMGkeWuaTITDhOSmDgvpFugJjse0D/fvvrjB1oJDBEx2bXDj lXAfUcKDd6v1VJ8nHBpZrwJPvDWknPbKb/s3Ms2wUTEmpPzWCNG9xfRnDMOXL0U30qe6 SeIZVxLCrZPYUHe1YSJ71lZZLi9R+2eB/lsvFDGEAvFLyJjXN72oE0me5/NmYZov5Zu8 Ee4PlApcKoywVZQcgZs3HG15NOqGVJNkMwx+jUt27mm3lqNysuLexo0wu+xdafFMDTN+ xfzmtiGKR9ExID+yVcXHKxIcHQRHCKZmSX/YJNaRl5Lp2iv+a+JrcutKUoUmVPGtFtAD 3BbA== 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=x8lMqENYrJ4vm1/Mum3IAFiymhyS6XWsQD5WiguLpDg=; b=EMKaIhmvnzIdmd18cbaywF8ZAw20drUQqGAzIhFkcmd7f6BXzcm4gsU+hQOzliErxs ZQYpKoOY53mKiKyms5zcpPXb+FQpgAC0SRTv1DZ1tyRxbKajiWCDvGGC+1d3FFtSn8uC c2wMKFOhEwRb0zAYHGjBhabc6/gv5tUOsPDzslAbR4f1oduEhPoVcHu3IjC31noYrtaE Sf9HuXH40mVidvaKmn0ZlYYx5Xfi//UBDpz+miT59KemgPvGnMdVQwXV+phQ4JqIQGgo T7ZBkc62TxbWZ1Daohhrftp5wg/UwSlYIeu6xsQttFqOGN+KlVcBmH2xc9F2a09/KVXR 2yEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=g5jcKS2B; 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 i4si21192295pgs.408.2019.04.06.12.49.30; Sat, 06 Apr 2019 12:49:58 -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=g5jcKS2B; 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 S1726653AbfDFTsa (ORCPT + 99 others); Sat, 6 Apr 2019 15:48:30 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:33174 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726452AbfDFTsa (ORCPT ); Sat, 6 Apr 2019 15:48:30 -0400 Received: by mail-wr1-f68.google.com with SMTP id q1so11827042wrp.0; Sat, 06 Apr 2019 12:48:28 -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=x8lMqENYrJ4vm1/Mum3IAFiymhyS6XWsQD5WiguLpDg=; b=g5jcKS2BlUf6aJfJQfhwZOIklbVCxDBT/1whYX8cP5TBj1i86JvEB9YzUwZssr3/UH JlfGvRJ62V1ZcxSuN9C2TBaJ9T1+7r/idRUgfg6gBZuKZXLDpa3G8pcmdnNDRLZy0Eqc LJrWY/EUKlkCSe5IBEzzF88DD59rTfZOC7YKhI+W5Vg2b71Y+gr6siSneJ2PR4KpWq6x +iqrKvFTiULhJPaXFJ7B5qrHbvLGgwIuRfXSemau8TsL5FBiTbsYGbD5i8eVfkf7qG2x k7ISdnscuqYZzNTK4V8XFcCXNjhI/5kDglpShgXTCCmM3KLvfmzQ0NkHn7EXB9fZzhQd p+eA== 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=x8lMqENYrJ4vm1/Mum3IAFiymhyS6XWsQD5WiguLpDg=; b=i3AbffF/IWfV81Ixb299CQ2imDcHrLMmHelriCoQbnmNFmbkEFQhrBoRp1v95JIlso SVPUXw+AwlSbAnyuLEXdIzDm034QEzq/qwhBYm3Ms9cnhm9n+yYy5XK6C2+kMop3wCIw WqU47lVTxUlt9C0+HovTEsvKfhdcNquAiG4lUPTVLImi+xhDN4ZODopC72tZbzCHKcH0 R9dURkWTwCzfm2ZdrU/a2RGgVmaqScbU+/3VBW74cPoP5UKpVAPTtrRAgfq49IJCciom bfZY8rdGdrzKFn4O0gTiZ5IYsv2Nx68SkiBIYtYFeZBkHqhaMO/JnD8ju3Zh1s4SuXZ3 kA4g== X-Gm-Message-State: APjAAAUK6zLfRxVunNM0KqHXqdtHLOzM08LOuJgAO9nSUUMzCU+BHTYT cRXy8v4HqYaQ5MICixtALA== X-Received: by 2002:adf:f3ce:: with SMTP id g14mr12653957wrp.129.1554580108223; Sat, 06 Apr 2019 12:48:28 -0700 (PDT) Received: from avx2 ([46.53.240.21]) by smtp.gmail.com with ESMTPSA id t17sm19402587wrr.26.2019.04.06.12.48.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Apr 2019 12:48:27 -0700 (PDT) Date: Sat, 6 Apr 2019 22:48:25 +0300 From: Alexey Dobriyan To: Florian Weimer Cc: Peter Zijlstra , 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: <20190406194825.GA5106@avx2> References: <20190403200809.GA13876@avx2> <20190404084249.GS4038@hirez.programming.kicks-ass.net> <87wok83gfs.fsf@oldenburg2.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <87wok83gfs.fsf@oldenburg2.str.redhat.com> 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 Fri, Apr 05, 2019 at 12:16:39PM +0200, Florian Weimer wrote: > * Peter Zijlstra: > > > 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. > > The size of the affinity mask is only related to the number of CPUs in > the system in such a way that the number of CPUs cannot be larger than > the number of bits in the affinity mask. > > >> Glibc in particular shipped with 1024 CPUs support maximum at some point > >> which is quite surprising as glibc maitainers should know better. > > This dates back to a time when the kernel was never going to support > more than 1024 CPUs. > > A lot of distribution kernels still enforce a hard limit, which papers > over firmware bugs which tell the kernel that the system can be > hot-plugged to a ridiculous number of sockets/CPUs. > > >> 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. > > If I recall correctly my last investigation, > /sys/devices/system/cpu/possible does not reflect the size of the > affinity mask, either. > > >> 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). > > Given that old kernels fail with EINVAL, that evidence is fairly > restricted. > > I'm not sure if it's a good idea to overload this interface. I expect > that users will want to call sched_getaffinity (the system call wrapper) > with cpusetsize == 0 to query the value, so there will be pressure on > glibc to remove the memset. At that point we have an API that obscurely > fails with old glibc versions, but suceeds with newer ones, which isn't > great. I can do "if (len == 536870912)" so that bit count overflows on old kernels into EINVAL and is unlikely to be used ever.