Message ID | e32ea9a03d0797ce2b8e7a82ed59c0dad9431f2b.1680407255.git.josh@joshtriplett.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1554567vqo; Sat, 1 Apr 2023 21:04:42 -0700 (PDT) X-Google-Smtp-Source: AKy350bzGGCYveMYdglzNZT7ezkrWyHCYyAjtePqwz8JpM1spHPuHa5ny9W85OzQcYDzfWWFKh7K X-Received: by 2002:a17:907:7244:b0:947:92c9:6aa4 with SMTP id ds4-20020a170907724400b0094792c96aa4mr9937067ejc.4.1680408282096; Sat, 01 Apr 2023 21:04:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680408282; cv=none; d=google.com; s=arc-20160816; b=LJlv23N8/9/dFY/cBJhmg7MyMTKcDlS0/GM7Faq8k5gvh6i1J75yCkUcudiTxCQ60S TEdHzCO+zpT/YtVp7Q7SyvAHKPm6vYHQPoCLOa1NtPwuOUP2oGVCV2fpC3rqjxXmgyCB 1h4z0p6dQrc9TYCe41pPMMe0c23NwTg9XoNBFFXQSfYjZq39fbsFWRuRdD2ch+5HYBHM lZI2ApNqjrBjzflq02pcMUdM+7lOCJ7zfZytSrUHqBjI557EXrmUb1f12NhR4Nq8+UQ5 fY2daYGF1JrKsvXh8kAdBimt0Xa6cphEQsZhUAoP+/sBEDsbfeQ4DcdevhJmX50SsOUT yvWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:feedback-id:dkim-signature:dkim-signature; bh=5amwOPaxC92wzRYxQ1JVx2WftbFO9nBcwXCPE0iFBO8=; b=n6rhDpZ7Jqquq5oQXN12Jmtx8JrQL2t0cwNCDru6ITpzuiviLjqzxUljUAdj7aoff8 VoQzJcRJt7cCCPKCxJoi2Oy964c+/42MnR2A7HnYtu5kh/15PuooB9Q9jmJcXNW+OsEQ A2lLfbx4qeE0JakcRKMxJskUPlpaEB9Zk8XN5HnJ6P8luvamfAxIkXlqawNyzV776E6D IKUkFj7ksf0tuygF7BtOSfDE6YOAgSRlaYxbqOVYY7pI2iNBQnqxL8oJi2Xe1XQi07cP qFXGm3lZH3yxANLDhsf0eAOPylpfiPa/gcIzMSHUuEamgS5Qv75RsY8YTJ+NxwEeqQtu kn5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joshtriplett.org header.s=fm1 header.b=VF9aqMCe; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=IzGUC9ne; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be18-20020a0564021a3200b00501dcf278b0si5675839edb.271.2023.04.01.21.04.16; Sat, 01 Apr 2023 21:04:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@joshtriplett.org header.s=fm1 header.b=VF9aqMCe; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=IzGUC9ne; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230090AbjDBD5m (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Sat, 1 Apr 2023 23:57:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229492AbjDBD5j (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 1 Apr 2023 23:57:39 -0400 Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B90B121AAD for <linux-kernel@vger.kernel.org>; Sat, 1 Apr 2023 20:57:38 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id CFBCB3200A1D; Sat, 1 Apr 2023 23:57:35 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 01 Apr 2023 23:57:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= joshtriplett.org; h=cc:cc:content-type:content-type:date:date :from:from:in-reply-to:message-id:mime-version:reply-to:sender :subject:subject:to:to; s=fm1; t=1680407855; x=1680494255; bh=5a mwOPaxC92wzRYxQ1JVx2WftbFO9nBcwXCPE0iFBO8=; b=VF9aqMCeFRQygL/ne8 MipRK84Xwz6nJAYRO9LGXXfr5Av6eDwBh9frwI1HTfoxkPWE5hmVblgSQpQ0LiH/ vWKnPWGtyHn3wUPvQa+n6gS9y36BwMBvZ2/5LaT4hcwmtm3XphOCSIsy0HYH8RSS RWtmlVGvsbKtrlCKeKDloXmlvlo0v9Pez0L9VV3ERaK0OUInO4Dj91jBA28o7yfx Lj99nr9NCeRe3Iu6Ut4SvAk3QAOh6sZKWx4DvaW9uSjiAiKZVhoDJ5BVp4dTntyV leLWcYR7GFYh3OMcv71nryXIYuBTh6LA2jgX9AAaka1DjCUpZ4/+Y1KIzBdOgf3M 4Skg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1680407855; x=1680494255; bh=5amwOPaxC92wzRYxQ1JVx2WftbFO9nBcwXC PE0iFBO8=; b=IzGUC9nefIiqZGndgS+++xFjU/94qy7T2Cy87f4p4VSOV0l+NyD 0JtwgwdFJwoP1nHMGs49ITbvioNL+nQTzk8Z43BWlWTszDb6qQHJs8NO5JUoOoHC 0WsXkHP2e66JszO1HojiI/lW01AfnOXMUzotqbQDY2YheVrUxS8CecitZiFvBycQ AfQPa9XWtxq1BqiPc28LRbd+z8gAbXJ77WR70cFnrxPQg1mAPVJa+7C9s2jNirO0 b3TFHodRXG9s9oI+EvPrkBjmY7+2GZsnDaaiq47RGA1FfLCoKBzZEKPAOdPPHAWx 8TseTYVP7sLr831475S93EeR1Kv1ecOGlXg== X-ME-Sender: <xms:L_0oZDL0RtCUlERyOY024LQwQ0pTMu_w32x-6dva63EMKnu1g9bB2A> <xme:L_0oZHIBqS8dPPzOqF-oVcKzX_vnf0rwBIHWYvgVI4b94YTVm-I2Clyf4sfIKHQJH 8aJEq_Pew1ZvSw5YFc> X-ME-Received: <xmr:L_0oZLthukp9ON2NuRSiETXAg5eKr0I0Jh0UIzP2I4O3qsfI8LQn_Ju8N8-ZvUkkNsDfu5R3PkMRXA2CON-A96etuurEeH5lPEKflxQ> X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdeigedgjeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugesthdtredttddtvdenucfhrhhomheplfhoshhhucfv rhhiphhlvghtthcuoehjohhshhesjhhoshhhthhrihhplhgvthhtrdhorhhgqeenucggtf frrghtthgvrhhnpeduvdelheettdfgvddvleegueefudegudevffekjeegffefvdeikeeh vdehleekhfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehjohhshhesjhhoshhhthhrihhplhgvthhtrdhorhhg X-ME-Proxy: <xmx:L_0oZMYKcczQN-hPxbon_f0CtcU_aor00ZiBB2uKkpUnOaLC5PRPwA> <xmx:L_0oZKY-m3qhLPWgCDPfITCdvetDYUlZI5_TlMhpPPqStBKHVeMouw> <xmx:L_0oZABtUeQixdaQDC5-E03i8ihKFzTMpllXoY0cBaYNGdV83GbLiQ> <xmx:L_0oZC7M_cU3QwL3DutT4xRHY4HRAGxsiRdH-Nokqkmv7m0UC6jckA> Feedback-ID: i83e94755:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 1 Apr 2023 23:57:31 -0400 (EDT) Date: Sun, 2 Apr 2023 12:57:29 +0900 From: Josh Triplett <josh@joshtriplett.org> To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org> Cc: Catalin Marinas <catalin.marinas@arm.com>, "Eric W. Biederman" <ebiederm@xmission.com>, Joey Gouly <joey.gouly@arm.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Alexey Gladkov <legion@kernel.org>, "Jason A. Donenfeld" <Jason@zx2c4.com>, Mark Brown <broonie@kernel.org> Subject: [PATCH] sysinfo: Saturate 16-bit procs rather than wrapping Message-ID: <e32ea9a03d0797ce2b8e7a82ed59c0dad9431f2b.1680407255.git.josh@joshtriplett.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762035794743347190?= X-GMAIL-MSGID: =?utf-8?q?1762035794743347190?= |
Series |
sysinfo: Saturate 16-bit procs rather than wrapping
|
|
Commit Message
Josh Triplett
April 2, 2023, 3:57 a.m. UTC
struct sysinfo has a 16-bit field for the number of processes. Current
systems can easily exceed this. Rather than wrapping around, saturate
the value at U16_MAX. This is still incorrect, but more likely to
help the user know what's going on; a caller can then (for instance)
parse the full value out of /proc/loadavg.
Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
Not sure what tree changes to kernel/sys.c should flow through. Andrew,
could you take this through your tree (assuming you agree with it), or
suggest what tree it should go through instead?
Comments
Josh Triplett <josh@joshtriplett.org> writes: > struct sysinfo has a 16-bit field for the number of processes. Current > systems can easily exceed this. Rather than wrapping around, saturate > the value at U16_MAX. This is still incorrect, but more likely to > help the user know what's going on; a caller can then (for instance) > parse the full value out of /proc/loadavg. > > Signed-off-by: Josh Triplett <josh@joshtriplett.org> > --- > > Not sure what tree changes to kernel/sys.c should flow through. Andrew, > could you take this through your tree (assuming you agree with it), or > suggest what tree it should go through instead? Mind if I ask what the motivation for this is? I looked at debian code search and there are a lot of uses of the sysinfo system call. Most of the uses were for load average or memory occupancy. The only use of procs that I could find was in samba. I did not trace the code far enough but it clearly had an embedded assumption that 16 bits was enough to report the number of processes on a linux system. I looked at glibc and if I read things correctly the sysinfo system call is just a pass through to the kernel. I looked because just saturating the 16bit field feels like a hack that will continue to encourage buggy programs to stay buggy. If there is real value in sysinfo returning a this information someone could go through the work and update the kernel to return the high bits of the process count in info->pad that is immediately after info->procs, and then update the apps or libc to find those high bits. Otherwise I think it makes most sense to encourage programs to use /proc/loadavg, where this information has always been returned correctly as it is a text file. We could do it like: /* * Reliably fail when there are more than 64k processes. * Userspace should use /proc/loadavg instead. */ info->procs = (nr_threads <= U16_MAX) ? nr_threads : 0; If saturating does make sense can we please have a comment documenting why saturating and encouraging confused userspace programs to stay confused makes sense? Eric > diff --git a/kernel/sys.c b/kernel/sys.c > index 495cd87d9bf4..ba05fca26927 100644 > --- a/kernel/sys.c > +++ b/kernel/sys.c > @@ -2699,7 +2699,7 @@ static int do_sysinfo(struct sysinfo *info) > > get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT); > > - info->procs = nr_threads; > + info->procs = min_t(typeof(nr_threads), nr_threads, U16_MAX); > > si_meminfo(info); > si_swapinfo(info);
On Wed, Apr 05, 2023 at 05:27:12PM -0500, Eric W. Biederman wrote: > Josh Triplett <josh@joshtriplett.org> writes: > > > struct sysinfo has a 16-bit field for the number of processes. Current > > systems can easily exceed this. Rather than wrapping around, saturate > > the value at U16_MAX. This is still incorrect, but more likely to > > help the user know what's going on; a caller can then (for instance) > > parse the full value out of /proc/loadavg. > > > > Signed-off-by: Josh Triplett <josh@joshtriplett.org> > > --- > > > > Not sure what tree changes to kernel/sys.c should flow through. Andrew, > > could you take this through your tree (assuming you agree with it), or > > suggest what tree it should go through instead? > > > Mind if I ask what the motivation for this is? [...] > I looked because just saturating the 16bit field feels like a hack > that will continue to encourage buggy programs to stay buggy. On the contrary, it seemed to me like the best way to make issues more obvious. Wrapping around and showing (say) 12 processes because the results are mod 65536 won't necessarily immediately stand out in stats, the way that saturating at 65535 does. That said, I like the idea of doing 0 even more: > If there is real value in sysinfo returning a this information someone > could go through the work and update the kernel to return the high > bits of the process count in info->pad that is immediately after > info->procs, and then update the apps or libc to find those high bits. I'd love to do that; I just thought that'd be less likely to be accepted. But yes, I think that'd be even better. That said, I think we need to return *all* the bits in the later padding, rather than just the high bits, so that we can reliably detect if the bits were provided. We can return 0 in the existing field, and then return the process count in the padding, and if the padding is 0 then it wasn't provided. (If we returned the high bits in the later padding, it'd be hard to tell if low=42 and high=0 was 42 processes or 42-mod-65536 processes on an old kernel. If we return the full bits in the later padding, we can reliably tell the difference between those.) - Josh Triplett
diff --git a/kernel/sys.c b/kernel/sys.c index 495cd87d9bf4..ba05fca26927 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2699,7 +2699,7 @@ static int do_sysinfo(struct sysinfo *info) get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT); - info->procs = nr_threads; + info->procs = min_t(typeof(nr_threads), nr_threads, U16_MAX); si_meminfo(info); si_swapinfo(info);