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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B87FC433EF for ; Mon, 31 Jan 2022 11:23:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9400B6B0071; Mon, 31 Jan 2022 06:23:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EF966B0073; Mon, 31 Jan 2022 06:23:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DF0B6B0074; Mon, 31 Jan 2022 06:23:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0043.hostedemail.com [216.40.44.43]) by kanga.kvack.org (Postfix) with ESMTP id 700C66B0071 for ; Mon, 31 Jan 2022 06:23:53 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2F17896779 for ; Mon, 31 Jan 2022 11:23:53 +0000 (UTC) X-FDA: 79090347546.16.C0136B5 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf28.hostedemail.com (Postfix) with ESMTP id 2A5A8C0002 for ; Mon, 31 Jan 2022 11:23:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643628232; x=1675164232; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=IrCfoF7eVv9DU6g4PgHgEV5cLq0Eof4NwIuHTMoZg8E=; b=Q5mia0y3WZGHY9qakzzv9qsqR4yMnxABK5NREepIYSDt/B+NYJoNQw/F n6hQbzuOc9KZlbWKEws8Ds+W2SBhLUvaFVeGCs/y718gpUuttLzQa6QEu omNrV3ulC/9OT51tteLnukcpyirbXS6B6Gkwx/yt4jBfBsm2IE62/yr6G zclMrrzQIGaEvNRW9PHxHhIYc4usWnmQIq7vm12i/dpUEUY+eFLpUPTxo M03GnagmdoJDS1+9vqJg0Lm+130mlbZ9u+g0XhEnG7a4l1q2kMN/l6J9c WdLHvGyrADm+wTTHS8F54NO+PY8kktu7Y4E6NMyHspoWfjOI+IH57B2ib Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="234832387" X-IronPort-AV: E=Sophos;i="5.88,330,1635231600"; d="scan'208";a="234832387" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2022 03:23:50 -0800 X-IronPort-AV: E=Sophos;i="5.88,330,1635231600"; d="scan'208";a="479042021" Received: from smile.fi.intel.com ([10.237.72.61]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2022 03:23:46 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.95) (envelope-from ) id 1nEUle-00GqYd-Oi; Mon, 31 Jan 2022 13:22:42 +0200 Date: Mon, 31 Jan 2022 13:22:42 +0200 From: Andy Shevchenko To: Rasmus Villemoes Cc: David Rientjes , Waiman Long , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Ira Weiny , Rafael Aquini Subject: Re: [PATCH v2 1/3] lib/vsprintf: Avoid redundant work with 0 size Message-ID: References: <20220129205315.478628-1-longman@redhat.com> <20220129205315.478628-2-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-Rspamd-Queue-Id: 2A5A8C0002 X-Rspam-User: nil Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q5mia0y3; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf28.hostedemail.com: domain of andriy.shevchenko@linux.intel.com has no SPF policy when checking 134.134.136.20) smtp.mailfrom=andriy.shevchenko@linux.intel.com X-Stat-Signature: 45f4wbpm3mhkikqwydmd119eijwqcbp5 X-Rspamd-Server: rspam08 X-HE-Tag: 1643628231-640385 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Jan 31, 2022 at 12:02:29PM +0100, Rasmus Villemoes wrote: > On 31/01/2022 11.34, Andy Shevchenko wrote: > > On Mon, Jan 31, 2022 at 12:30:33PM +0200, Andy Shevchenko wrote: > >> On Mon, Jan 31, 2022 at 12:25:09PM +0200, Andy Shevchenko wrote: > >>> On Sun, Jan 30, 2022 at 12:49:37PM -0800, David Rientjes wrote: > >>>> On Sat, 29 Jan 2022, Waiman Long wrote: > >>>> > >>>>> For *scnprintf(), vsnprintf() is always called even if the input size is > >>>>> 0. That is a waste of time, so just return 0 in this case. > >>> > >>> Why do you think it's not legit? > >> > >> I have to elaborate. > >> > >> For *nprintf() the size=0 is quite useful to have. > >> For *cnprintf() the size=0 makes less sense, but, if we read `man snprintf()`: > >> > >> The functions snprintf() and vsnprintf() do not write more than size bytes > >> (including the terminating null byte ('\0')). If the output was truncated due > >> to this limit, then the return value is the number of characters (excluding > >> the terminating null byte) which would have been written to the final string > >> if enough space had been available. Thus, a return value of size or more > >> means that the output was truncated. (See also below under NOTES.) > >> > >> If an output error is encountered, a negative value is returned. > >> > >> Note the last sentence there. You need to answer to it in the commit message > >> why your change is okay and it will show that you thought through all possible > >> scenarios. > > > > Also it seems currently the kernel documentation is not aligned with the code > > > > "If @size is == 0 the function returns 0." > > > > It should mention the (theoretical?) possibility of getting negative value, > > if vsnprintf() returns negative value. > > > > The kernel's vsnprintf _will never_ return a negative value. There is > way too much code which relies on that. It also has to work from any > context, so we'll never do any memory allocation or anything else that > could possibly force us to error out, and even if we encounter some > impossible situation, we do not return a negative value, but just stop > the output where we are. Yep, I see the code. My comments more or less are related to the (better) commit message which may include what you just said. > So yes, micro-optimizing [v]scnprintf() is completely valid, but I've > never bothered to send the patch because the use case for scnprintf() is > primarily the > > ret += scnprintf(buf + ret, size - ret, ...); > > pattern, with ret starting out at 0 and size being some non-zero number. > When given a non-zero size, scnprintf() is guaranteed to return > something _strictly less_ than that value; that invariant guarantees > that the size-ret expression never becomes 0. So if scnprintf() is > properly used, I can't think of any situation where size will be 0, > hence I see that patch as correct-but-mostly-pointless. Good remark and again commit message probably should elaborate this as well. -- With Best Regards, Andy Shevchenko