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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C1DCC433F5 for ; Mon, 31 Jan 2022 19:23:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359870AbiAaTXg (ORCPT ); Mon, 31 Jan 2022 14:23:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22493 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359611AbiAaTX3 (ORCPT ); Mon, 31 Jan 2022 14:23:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643657009; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FSCcYBtevS72h8Rb3ThNAKipb4CG2TPJPslkwwiiaHA=; b=T7+7A5qBcT0tCDjr5jxQo9x1JJajuu+q3CWKm45Thyeg6t1c/xVWS0coILh2wH6NPoc7EI Yn5qMfB2+dVb23j/5wpyoeu1enOuIxLV0kx8fDnTLuDClBc+rRHstXeEcleA1MSUWRNxCV 4uqkB0F+VeIGxeBJo2NYKRSDGG0cyYI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-520-abdKjMwNOGWccPAZQ_acdA-1; Mon, 31 Jan 2022 14:23:25 -0500 X-MC-Unique: abdKjMwNOGWccPAZQ_acdA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 81D351006AA0; Mon, 31 Jan 2022 19:23:23 +0000 (UTC) Received: from llong.com (unknown [10.22.16.244]) by smtp.corp.redhat.com (Postfix) with ESMTP id 567BD10001AF; Mon, 31 Jan 2022 19:23:21 +0000 (UTC) From: Waiman Long To: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Ira Weiny , Mike Rapoport , David Rientjes , Roman Gushchin , Rafael Aquini , Waiman Long Subject: [PATCH v3 1/4] lib/vsprintf: Avoid redundant work with 0 size Date: Mon, 31 Jan 2022 14:23:05 -0500 Message-Id: <20220131192308.608837-2-longman@redhat.com> In-Reply-To: <20220131192308.608837-1-longman@redhat.com> References: <20220131192308.608837-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Note that vsnprintf() will never return -1 to indicate an error. So skipping the call to vsnprintf() when size is 0 will have no functional impact at all. Signed-off-by: Waiman Long Acked-by: David Rientjes Reviewed-by: Sergey Senozhatsky Acked-by: Roman Gushchin --- lib/vsprintf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3b8129dd374c..d419154b47bb 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2895,13 +2895,15 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { int i; + if (unlikely(!size)) + return 0; + i = vsnprintf(buf, size, fmt, args); if (likely(i < size)) return i; - if (size != 0) - return size - 1; - return 0; + + return size - 1; } EXPORT_SYMBOL(vscnprintf); -- 2.27.0