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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECCC1C433F5 for ; Thu, 21 Oct 2021 08:56:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D57E060EFE for ; Thu, 21 Oct 2021 08:56:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231433AbhJUI6j (ORCPT ); Thu, 21 Oct 2021 04:58:39 -0400 Received: from out30-56.freemail.mail.aliyun.com ([115.124.30.56]:39071 "EHLO out30-56.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231321AbhJUI6d (ORCPT ); Thu, 21 Oct 2021 04:58:33 -0400 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; } -- 2.31.0