From patchwork Tue Feb 8 00:05:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 12737955 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 CFBF5C433F5 for ; Tue, 8 Feb 2022 00:06:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D5A46B0075; Mon, 7 Feb 2022 19:06:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 170ED6B007D; Mon, 7 Feb 2022 19:06:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E647C6B0075; Mon, 7 Feb 2022 19:06:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0135.hostedemail.com [216.40.44.135]) by kanga.kvack.org (Postfix) with ESMTP id C6F316B0075 for ; Mon, 7 Feb 2022 19:06:08 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 78ED8181D6CE1 for ; Tue, 8 Feb 2022 00:06:08 +0000 (UTC) X-FDA: 79117670016.14.F5DEB9B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 103F21C0008 for ; Tue, 8 Feb 2022 00:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644278767; 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=EpG/a8UGSgxQkJKVhwJeqMQ36JzcPE9kOn8DL2mEYFQ=; b=eDKUE59FpC1+5QgRtpO6EV2jYS5iR5EYxSM7ImFOv12Ba4p/iuJRPvtUHQPjzjTJpkNTpx iWzaBlk7hDfbcF3UdsYsuGtX14bJzk4OKHIJKmxSYPRieLXnHarS/7emoVeWACXGPAtu8h FYaq4YLFdfMxTMe6WpHlPOgLIuJoKzs= 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-613-NqDoYO8jPY-zlN6dzI7rmQ-1; Mon, 07 Feb 2022 19:06:02 -0500 X-MC-Unique: NqDoYO8jPY-zlN6dzI7rmQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ADEFD18397A7; Tue, 8 Feb 2022 00:05:59 +0000 (UTC) Received: from llong.com (unknown [10.22.32.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0498B5C2EF; Tue, 8 Feb 2022 00:05:57 +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 , Mike Rapoport Subject: [PATCH v5 1/4] lib/vsprintf: Avoid redundant work with 0 size Date: Mon, 7 Feb 2022 19:05:29 -0500 Message-Id: <20220208000532.1054311-2-longman@redhat.com> In-Reply-To: <20220208000532.1054311-1-longman@redhat.com> References: <20220208000532.1054311-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eDKUE59F; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf20.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 103F21C0008 X-Stat-Signature: 4z9e8rhgddfx6ybjont31uixtfobshss X-HE-Tag: 1644278767-75918 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: 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 Acked-by: Mike Rapoport --- 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);