Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp506459yba; Wed, 3 Apr 2019 13:09:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqzeXb03vfM+pBNySGjAH7ts4GNxDIj3fRCbXptcAIhMmemDdYDkxqvNF0K8Yd9PGEvArSuf X-Received: by 2002:a17:902:290b:: with SMTP id g11mr1973226plb.269.1554322150682; Wed, 03 Apr 2019 13:09:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554322150; cv=none; d=google.com; s=arc-20160816; b=iR2lvBfn2DR8spgF/TI9LlrITNwkurDsOaMky+jTaK8FXeCgTWRL1QVIMmGTMZBaJg 8W3t4hK2TK7ujcgNjnjIf2r6zh3AvAmSD71sj1cr5i9GdwZ/Aoggs+/awX97aUgQ+BVq qBe/cS5QyTq6T1zAXeKdHO2q222jvwTbdxGRlnV/bLrahVKaGJMJIRLuld/+QJh1rg8v xfkOzf/H8QP91ZzjpzJ34GqJ8FKjVqf/gbOcQqcVgKAJkOxeq5BE+9UF7f9CqTT5bmyZ pZ5MjziSnTYrhTbrp+kJxYibvHUvjxgvp8dqxAlTUhVlqPDeCru6oN4FnuRN6U+9fXE7 qIOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=4+9EzKS2xxXNQGSpxxfbuRUr2AmzKqruYi2N7NneNlg=; b=QLGr6S6DyvSUF1Rem6AFg7uTMWXrLEp74FMbxicrS/D5R2wIJHHdOwoEyVEkWBiLHC byCzJFyiF+okprtAY7cwV3mgmuC4m7obLO2Dpj0s+SY9vkWe+XyQU6JjS23/03eZCVAO EqmkKXK4+bTsIa9DEMC5t21ICndOseFeS+s3LZ7SDr6LDCD+MlI7gHnzQIceE2xyKb1k uvGHWAvkmUx5QSRLsRUNdifnXLP41zoU5TVpDngLq7Z6Kaw4lnaD3wpOcLC3R9PIHLyY eMvZdISkkb/Xn2X43bq92S1MnWGl6NZZK/tjZZ68Xb4pRC+A1l1A+id6qjfhApqHABxZ npPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=htiG74DB; 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 b3si14211524pgq.325.2019.04.03.13.08.55; Wed, 03 Apr 2019 13:09:10 -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=htiG74DB; 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 S1726905AbfDCUIP (ORCPT + 99 others); Wed, 3 Apr 2019 16:08:15 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:54530 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726168AbfDCUIP (ORCPT ); Wed, 3 Apr 2019 16:08:15 -0400 Received: by mail-wm1-f67.google.com with SMTP id c1so231104wml.4 for ; Wed, 03 Apr 2019 13:08:13 -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:mime-version:content-disposition :user-agent; bh=4+9EzKS2xxXNQGSpxxfbuRUr2AmzKqruYi2N7NneNlg=; b=htiG74DBFTQY6xPiUpUXKImSQaj+J+3IxROkQXvJ8WQRFwcE5GaAlruSXdzIWQI0Yg 03awMUffq5Q2Zv7ty3QEfdVyywPtH+JbswLfTRmkQP4oZxpeE9O+Itdd8F7By036DT0W hmcTKPuO6Vns06csGjvJS2h+v6MCyqSgFcQ5rc2R9dJ94xc+ikyJe5KOVCJBEuvn5c/U npQsPRlUme4w1CVSkUOks4sAy+Zt3RbdaTDS5Cl6SS9XFk9nbRc7Ymg+pwRQe5zT+YaB yqLAssFQ8DuKLw30j2qIrvubc9icth79Oh//rdw9+OPLj3qJe5VStywmh/i3WXEurrWA hJ8g== 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:mime-version :content-disposition:user-agent; bh=4+9EzKS2xxXNQGSpxxfbuRUr2AmzKqruYi2N7NneNlg=; b=MyG9io8UL1z2ZMOZ+MHlp4N/Icn0/Q84+BHozVR4vK3GPgKJuydyN7E1clLF8FMC+t irthgFjxnNidiWOn8Tkhuy/ytmTqiU+ucIPLCgAmmTuZ6zCx8s7sJTTJNL83wPInMUpL pbeThfGwUwOmtsyqsTXH/uDXwzKlfZLBkRRVNWIHdfVSYA6nhxA50vArxyYYa8nJpyiH 6lioJxpYxmGuLodIxe3lKDP3aSIi63jrESba4TjMN9R5UqFIsRWeOLwGemyzkhOH1ZEy RS6/bGUtUc3QrzK/hvU3XejtMhy/rTcVoGEvPuAx+0UYhCOAn4ylz5VJU6Qe8KbdJHys AnKg== X-Gm-Message-State: APjAAAWyN6Ty6c9lKT/WzKuZNnTQkQm89T+E+7qwcDaBXAE+nf69r+b7 lhDFzJ+k4hUm6KVIMNmilA== X-Received: by 2002:a1c:14:: with SMTP id 20mr1194206wma.66.1554322092874; Wed, 03 Apr 2019 13:08:12 -0700 (PDT) Received: from avx2 ([46.53.245.231]) by smtp.gmail.com with ESMTPSA id 7sm60007196wrc.81.2019.04.03.13.08.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Apr 2019 13:08:11 -0700 (PDT) Date: Wed, 3 Apr 2019 23:08:09 +0300 From: Alexey Dobriyan To: mingo@redhat.com, peterz@infradead.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs Message-ID: <20190403200809.GA13876@avx2> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 Currently there is no easy way to get the number of CPUs on the system. 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. 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). Signed-off-by: Alexey Dobriyan --- kernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+) --- 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))