linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/damon/dbgfs: Optimize target_ids interface write operation
@ 2021-10-21  8:56 Xin Hao
  2021-10-21  9:45 ` SeongJae Park
  0 siblings, 1 reply; 3+ messages in thread
From: Xin Hao @ 2021-10-21  8:56 UTC (permalink / raw)
  To: sjpark; +Cc: xhao, akpm, linux-mm, linux-kernel

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 <xhao@linux.alibaba.com>
---
 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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-21 16:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-21  8:56 [PATCH] mm/damon/dbgfs: Optimize target_ids interface write operation Xin Hao
2021-10-21  9:45 ` SeongJae Park
2021-10-21 16:42   ` Xin Hao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox