From patchwork Thu Oct 21 08:56:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: haoxin X-Patchwork-Id: 12574345 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B0B7C433EF for ; Thu, 21 Oct 2021 08:56:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 00A16610CB for ; Thu, 21 Oct 2021 08:56:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 00A16610CB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 951AB6B0073; Thu, 21 Oct 2021 04:56:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 902166B0074; Thu, 21 Oct 2021 04:56:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F1606B0075; Thu, 21 Oct 2021 04:56:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0060.hostedemail.com [216.40.44.60]) by kanga.kvack.org (Postfix) with ESMTP id 710A76B0073 for ; Thu, 21 Oct 2021 04:56:21 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3BE9026DCF for ; Thu, 21 Oct 2021 08:56:21 +0000 (UTC) X-FDA: 78719838162.12.74CD26B Received: from out30-56.freemail.mail.aliyun.com (out30-56.freemail.mail.aliyun.com [115.124.30.56]) by imf13.hostedemail.com (Postfix) with ESMTP id DFB9E104300B for ; Thu, 21 Oct 2021 08:56:15 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R681e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=xhao@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0Ut84NVG_1634806575; Received: from localhost.localdomain(mailfrom:xhao@linux.alibaba.com fp:SMTPD_---0Ut84NVG_1634806575) by smtp.aliyun-inc.com(127.0.0.1); Thu, 21 Oct 2021 16:56:16 +0800 From: Xin Hao To: sjpark@amazon.de Cc: xhao@linux.alibaba.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/damon/dbgfs: Optimize target_ids interface write operation Date: Thu, 21 Oct 2021 16:56:11 +0800 Message-Id: <20211021085611.81211-1-xhao@linux.alibaba.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DFB9E104300B X-Stat-Signature: d8o6thzcntqkb66wzyoejxtn6zrrekmr Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf13.hostedemail.com: domain of xhao@linux.alibaba.com designates 115.124.30.56 as permitted sender) smtp.mailfrom=xhao@linux.alibaba.com X-HE-Tag: 1634806575-925366 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: When writing some pids to target_ids interface, calling scanf() to get 'id' may be failed. If the value of '*nr_ids' is 0 at this time, there is no need to return 'ids' here, we just need to release it and return NULL pointer to improve related code operation efficiency. Signed-off-by: Xin Hao --- mm/damon/dbgfs.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index a02cf6bee8e8..2d77bf579ffb 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -308,21 +308,25 @@ static unsigned long *str_to_target_ids(const char *str, ssize_t len, unsigned long *ids; const int max_nr_ids = 32; unsigned long id; - int pos = 0, parsed, ret; + int pos = 0, parsed; *nr_ids = 0; ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL); if (!ids) return NULL; while (*nr_ids < max_nr_ids && pos < len) { - ret = sscanf(&str[pos], "%lu%n", &id, &parsed); - pos += parsed; - if (ret != 1) + if (sscanf(&str[pos], "%lu%n", &id, &parsed) != 1) break; + pos += parsed; ids[*nr_ids] = id; *nr_ids += 1; } + if (!*nr_ids) { + kfree(ids); + return NULL; + } + return ids; }