Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp875784pxb; Tue, 1 Feb 2022 12:13:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjA9wgLkmw0l2UiXtLXuxygJEJadVmh4NbM41P0t1MjRlCPCEYfYJiUJUTJiodD4Y9yeSA X-Received: by 2002:a05:6402:34c5:: with SMTP id w5mr27582016edc.226.1643746423926; Tue, 01 Feb 2022 12:13:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643746423; cv=none; d=google.com; s=arc-20160816; b=CwLR+pvhrtqe7+A/wnS13o90rHQChf/uJ3lKi7r3jWbTMwgRVkBgkEv6/J7Ow02D12 svbIUDIzu0nTdFnDtFAYcdz6/bKe5nI0CqPyYbSymm0CYfCrNW522GTzpZp+bvkB8KRn usiP6ywkgx9/WuCB44EXag0vkdFDwBWhG+1TJAnHUgTthU8yRAErLC6h23ewsRTPwZso qae8/K1SiIobd2kFB4SwbRfbnUcakYexMoYW+CIVRFv9Z1TcFjJABKXnONhnJWQiOfML l4SqCuvQsBU7QvaXHOKm2ohO4ZNLBYBUolKrcS74q78iqSDwTyG9/dawMljjXIXpdSQd yqMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:organization:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ImsjbmQyv6gQGqVZoJm/c3cCAR+edRVoFE2jy2wpN3E=; b=JIgFDIXh0gvZOd1ejWwX+ueM9BoOtbd6DUMmCChK7MN6Ft2ob3iiaNkiGJ+naPMnuh e1eLiZcBlrlpgd+/edMtVSDp40CUPCPlI2GKEDol9wRv2U9L9EAbWqx4xDDmi0bZ33kf UROWoGLONuYfxouorykEojroEMt2d775j9Y7hmTG1+lOuprm3NuL9nT133vHHEH3tZvw yR58kvgwppZjPZgAk2GJ3ZWXg+t/Z0wvyW2AP/rwoRXZkHeJiOG754BGUaJftUCz5c0G 4Mx/vmQo4Cm5xD9xU1HQKSQpY6zQ/qADr7qLBLNh4Of/G+pEavkbqhLSiGrQqP/vARMO Nofw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dYUK0g8h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oz44si6987496ejc.994.2022.02.01.12.13.17; Tue, 01 Feb 2022 12:13:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dYUK0g8h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378920AbiAaLnV (ORCPT + 99 others); Mon, 31 Jan 2022 06:43:21 -0500 Received: from mga05.intel.com ([192.55.52.43]:12238 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379732AbiAaLaj (ORCPT ); Mon, 31 Jan 2022 06:30:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643628639; x=1675164639; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=IrCfoF7eVv9DU6g4PgHgEV5cLq0Eof4NwIuHTMoZg8E=; b=dYUK0g8hidnaRro/VYR1kcNEvPpOtfYedQMtUIUXT/kSOxPuSixeQ+JH 3pawBbmGx9snj+aq7xvPMJIUgy45aQaPzGOa24Fdd8dp2+v5/O9YCQlSy Gk1cXXXJR+703azR6a/Rco1OirRU2/hnaW/1O6qnfu5/hfWQRAzWhTDnI NFeN4wc/ip6Vu4qFGcAlbpaycTj18UWgwIOiIbrLSFyls/u9m8jMR7s1K OwUwKcBr7yRYopHzLhVMkbh5dJ1hxYJYC1OD7EA80pvsR19bWGKPuVsgc aOplCg+lPJ3iEf7csgzxzeFPQk6+mLaeFKhvUoCj0XbNv+uuvcwRIWTxJ w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="333798871" X-IronPort-AV: E=Sophos;i="5.88,330,1635231600"; d="scan'208";a="333798871" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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