From patchwork Wed Feb 7 20:31:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13549054 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 CC45BC4828D for ; Wed, 7 Feb 2024 20:31:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D82D6B0081; Wed, 7 Feb 2024 15:31:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 760F86B0082; Wed, 7 Feb 2024 15:31:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DBB96B0083; Wed, 7 Feb 2024 15:31:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4ABC86B0081 for ; Wed, 7 Feb 2024 15:31:49 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 21EF64056F for ; Wed, 7 Feb 2024 20:31:49 +0000 (UTC) X-FDA: 81766153938.14.D22F4A6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 612501C000C for ; Wed, 7 Feb 2024 20:31:47 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ou2UWhF7; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707337907; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pEYzds38qzJkO3LwswAGZ3BaHAxTO2WCf8jM4flPQbg=; b=Jo/mle7nMdmsLWOwFe94feQrKG9iQLaWumqxuyiPCbS/h8reCqicWsGk7FhE7BkApkRzsb whE9M3g7tYSOiaRr+le5ZQm1vallP/ss5W6KL2wUTNwJuTPRZqeDidyX3eWwbtXH+OQwPJ CRikLV2j37WiS723iUyKv+KBVJRQPQM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ou2UWhF7; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707337907; a=rsa-sha256; cv=none; b=WtdAQ5FNIK0gOVJ2SYDAXBlPovNxQsgnCOZAGGEhOYgA8NqBgdzYgjKxDqpE0PflCgMPlK RAJK30ceYx07gsI9Lfn+C0+kKpv8SdYkDk/uvJ7zy6tmrAuOULhr8agqPvxPrZK7/XRaQf tsTcTrHFB9zyI/CIS14LBm0UdLC5YmI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7760561AB2; Wed, 7 Feb 2024 20:31:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7C72C43601; Wed, 7 Feb 2024 20:31:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337906; bh=hilY7j3dbTrTcHQNZ5cH9hllN5vVoaZDnsr9bRpaZZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ou2UWhF7rKY/SOJPhENMHQfU07MoTtvz7MvwRRiYyi+LjIzxD5rbLHTVaXLNHZNR9 lFR6S9udTLIzBtTPoxRQGoEdVNalxZkr/elBolMd/SfH5QFGWjwos7+/sf2oAiOq3O a6wJNdJoaYJLL6Pv7h+oUyC+TN00MUk5ipl7S00Kd4NrisZHeGUSddyvn2IdWwCrMQ hdF+ZGVnRCq6QPicoYIQAGTLPPsGo5wDuRIkS4FTPC6TdAnAI6C8OEOEiHLUXNw9HY rZHgPKwBjJ/k5VGnsnvlP2zcb9HXC8V/mPlVuA6CTPFbN9CplEBU2O9p3H+PGt2+3J 356gekiMFzpVg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Shuah Khan , damon@lists.linux.dev, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/8] selftests/damon: add a test for the pid leak of dbgfs_target_ids_write() Date: Wed, 7 Feb 2024 12:31:33 -0800 Message-Id: <20240207203134.69976-8-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240207203134.69976-1-sj@kernel.org> References: <20240207203134.69976-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 612501C000C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: zu3hzdjrj3zes7z3ogxsr9sdctb4drxe X-HE-Tag: 1707337907-628590 X-HE-Meta: U2FsdGVkX1+U+utK5EhtbrYlzngQ11FHCPxChpLbFayXXYK51U6TQESqc/1pUAWNjwzwmf3uhgeCViGqclvyZmjj+abRmXtEKXg8FjIbc8OI/s+XOE+CTnWfePc74zqIv68QG1BRi2++scUKjL68d+6pJ4cqsAVyqLyXbOnJlEHaxvV78ko9mTrgCYsyPnkfszRhKyYbhyi3+KHj7X4JVH6JrsT4kkrbEdMxMAbzFy5tD9a5pVKSzkRd3TCRTUI4aoVh6fQeJCBbc3VKuqoV481OBGzDVJOtNzFEA9JXw13Isxy1cs/thDv4FqZRrIDaJgylqxPG/k8uY2lmOsVvJc0P9vRr4GV1s+WWqfl3lkw+pTu9IU6cvTrn/V17nq1R1CJUQLZpMKmQ/f1J7f1aP/Z4gq5XPDwXMr3V4B+yJcAMXZxtZ9K76pj1fJyYJxZe3chRsHKUyDGjTLea2j9yhAyZuzCAGQ3HBwR/EX/zP7zAT8is9OeYPfSRGA6bAPAzoIxj/4xHlD0Qv79ihD2BuIfSZ6OAmOSLvmcMSeLLxkbPasAD217mzYtKsDpqQVBNhzbDFbeKGvj9+trMhAygD8R8OCx5aPT8x/U0NVT66DG2OJzv26R4ehWMj/A34OdhcqZ8XeSpscB7nrFG2kD/AI9haBh9YpzcskRSJ9++0u7vbtgf7C0vzcaNh4G890IcrdjBxgnfxXwV5mEPtzOObQ8HzlfzXPSbB3jNAJBglvlYx68/lvkS9cMmD2l/kgJA+TzpDxhAtGKq7D67INslnszo8oqUAclLoWbj/W6lSYU3ulFjYfwjV+sP27x3BJSFg3/4+y5cuK9gF7o1eJeHgO+9J1IFBh/1/I4xnjHYqeVZRc/lraOuAxKWne/Om3xCEcz25uMqrMN1aGDqsz400dMXPNSX4OX++J+of1iDv08Vsbk8cEznlYjyZf05akzjnUPgRBIvz8Hm+7VEb1C 9XNxaa8F Q9emp7G7A0pR5c74fIHteO5zuhebYn4hcw84zXIEtbq/yUhPctAyb2ZrKJtNymtUvGstTEPz+xXJjfQZrBASxd9qWVZLvWXAVyNL4rHHbYwOqj2NOojaeR55/KphQSI0ZnHNP//9ho2F8Lnl8r+5z/HWp1Xg6ShkT+FOMbrOiJlzCk2kYROk7tX1dbBFZp5ngp3d+LaR0rePpX46IIr0Qim5hO+0HoFQfspCEKsg+zFeWlwLYKTWvL0KZSYqkp/OcNvY5R6BkzhkFeuBEGzHnphcFkjgArBgN7wX4PVV4W9H1JYaUNF+47BBvznH9Yaf9Amt7cMmCGv7Ks2wbqfvJDMd91g== 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: List-Subscribe: List-Unsubscribe: Commit ebb3f994dd92 ("mm/damon/dbgfs: fix 'struct pid' leaks in 'dbgfs_target_ids_write()'") fixes a pid leak bug in DAMON debugfs interface, namely dbgfs_target_ids_write() function. Add a selftest for the issue to prevent the problem from mistakenly recurring. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/.gitignore | 1 + tools/testing/selftests/damon/Makefile | 2 + .../damon/debugfs_target_ids_pid_leak.c | 68 +++++++++++++++++++ .../damon/debugfs_target_ids_pid_leak.sh | 22 ++++++ 4 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c create mode 100755 tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh diff --git a/tools/testing/selftests/damon/.gitignore b/tools/testing/selftests/damon/.gitignore index 7d6c6e062be7..d861701f0327 100644 --- a/tools/testing/selftests/damon/.gitignore +++ b/tools/testing/selftests/damon/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only huge_count_read_write debugfs_target_ids_read_before_terminate_race +debugfs_target_ids_pid_leak diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index 8a3a8df003db..789d6949c247 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -3,6 +3,7 @@ TEST_GEN_FILES += huge_count_read_write TEST_GEN_FILES += debugfs_target_ids_read_before_terminate_race +TEST_GEN_FILES += debugfs_target_ids_pid_leak TEST_GEN_FILES += access_memory TEST_FILES = _chk_dependency.sh _debugfs_common.sh @@ -11,6 +12,7 @@ TEST_PROGS += debugfs_empty_targets.sh debugfs_huge_count_read_write.sh TEST_PROGS += debugfs_duplicate_context_creation.sh TEST_PROGS += debugfs_rm_non_contexts.sh TEST_PROGS += debugfs_target_ids_read_before_terminate_race.sh +TEST_PROGS += debugfs_target_ids_pid_leak.sh TEST_PROGS += sysfs.sh sysfs_update_removed_scheme_dir.sh TEST_PROGS += sysfs_update_schemes_tried_regions_hang.py TEST_PROGS += sysfs_update_schemes_tried_regions_wss_estimation.py diff --git a/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c new file mode 100644 index 000000000000..0cc2eef7d142 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: SeongJae Park + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DBGFS_TARGET_IDS "/sys/kernel/debug/damon/target_ids" + +static void write_targetid_exit(void) +{ + int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR); + char pid_str[128]; + + snprintf(pid_str, sizeof(pid_str), "%d", getpid()); + write(target_ids_fd, pid_str, sizeof(pid_str)); + close(target_ids_fd); + exit(0); +} + +unsigned long msec_timestamp(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000UL + tv.tv_usec / 1000; +} + +int main(int argc, char *argv[]) +{ + unsigned long start_ms; + int time_to_run, nr_forks = 0; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + time_to_run = atoi(argv[1]); + + start_ms = msec_timestamp(); + while (true) { + int pid = fork(); + + if (pid < 0) { + fprintf(stderr, "fork() failed\n"); + exit(1); + } + if (pid == 0) + write_targetid_exit(); + wait(NULL); + nr_forks++; + + if (msec_timestamp() - start_ms > time_to_run) + break; + } + printf("%d\n", nr_forks); + return 0; +} diff --git a/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh new file mode 100755 index 000000000000..31fe33c2b032 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +before=$(grep "^pid " /proc/slabinfo | awk '{print $2}') + +nr_leaks=$(./debugfs_target_ids_pid_leak 1000) +expected_after_max=$((before + nr_leaks / 2)) + +after=$(grep "^pid " /proc/slabinfo | awk '{print $2}') + +echo > /sys/kernel/debug/damon/target_ids + +echo "tried $nr_leaks pid leak" +echo "number of active pid slabs: $before -> $after" +echo "(up to $expected_after_max expected)" +if [ $after -gt $expected_after_max ] +then + echo "maybe pids are leaking" + exit 1 +else + exit 0 +fi