From patchwork Sun Oct 22 21:07:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13432089 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 64E1EC25B43 for ; Sun, 22 Oct 2023 21:07:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA9776B0085; Sun, 22 Oct 2023 17:07:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E30F26B0087; Sun, 22 Oct 2023 17:07:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C36986B0088; Sun, 22 Oct 2023 17:07:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A6B736B0085 for ; Sun, 22 Oct 2023 17:07:47 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7DF1B1A06A8 for ; Sun, 22 Oct 2023 21:07:47 +0000 (UTC) X-FDA: 81374334174.20.729EC86 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 46AC6180019 for ; Sun, 22 Oct 2023 21:07:44 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VPtzbp9D; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698008866; 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=vyztF8RlWiQ9YGFEDf0nkv2Iz+1sSrzYHDFSHFXmnJ0=; b=1SJp3OFd9WSNnp5+/kTuDLOgQFcoJZs+CMT9yPmQrLQ+AOum1WwnQdQYTyRNlD1699OSBv cBK6OZUufN6t5lLRJ2ZXLKSQMXHyEGHNcaaimK/fbhOnsTTgKmUq0Oyu8mxH/GbSqZaErX IRXHD5/W22A2vq+gQP2b/ugZ5+7br6k= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VPtzbp9D; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698008866; a=rsa-sha256; cv=none; b=y40pVNPbTe2sX9J3IBU0qrNIr2U/Aqgyd1vnttf5xqlkxNJ7q0lp+4Y4YOULFMfQ4rA7Ed mtC8AOOrRnlgGAFe5Y9pNb0kzX6LksciRpbHXrMETM8qpnlPOemSNvgLcWaHAEhA278gVA iYYvVvt82jJOuAdPzXkXv4IGrrx2AEk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 440DCCE1C92; Sun, 22 Oct 2023 21:07:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAD8AC433CC; Sun, 22 Oct 2023 21:07:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698008861; bh=pX1A2S2gjU4x1dZfHy4yT2f4Z5R5IGs3hrXsH8uj4kQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VPtzbp9DR4GYqSWrxjplHnqJMaf8/XbHf1DVh34biZk0X9atCutXLtohCsYEpmIgb oSE/M3l9smu1CM0WYj9uYDQeSuEjy7QE4+XhQhpqfKg7yZS7/Yc37r9tj6ofHumFQ8 y9e3/u3flDUEJ0m+lxithcFLhD+TPhUMXZ7PUNzF85LMMhoVMKvHzc+Eh+SZ+fjlzO wB9gdtBvAmnLkNztpD5YMAhv2kEjvzx9Cw3IwwoPvFwuvVT1TnioVRFrlZvkq9Uh/+ OuVGuIPTla6+EdOxYKPFZvpwWyqfL31jkE55HGMylFl0h2wZexvWGsrvRnNdvMhDUU 9LNIgOocj9Ayg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Brendan Higgins , damon@lists.linux.dev, linux-mm@kvack.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets() Date: Sun, 22 Oct 2023 21:07:34 +0000 Message-Id: <20231022210735.46409-3-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231022210735.46409-1-sj@kernel.org> References: <20231022210735.46409-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 46AC6180019 X-Rspam-User: X-Stat-Signature: mp5q5ppkrem5gp7ngcop5uo45j5chtr9 X-Rspamd-Server: rspam01 X-HE-Tag: 1698008864-155040 X-HE-Meta: U2FsdGVkX1/ux3KFg3e3L16Oxxn+BhR1MplChvjGpKzwa/BjVA7wefAUp87SDdOxHl+tk2vGBeOwjBPlgd5T9OLAB7DdGb4ItqIf2STX+84+tX3Qf9uIWDZduW37K8fswkcZdlSTabVBn7sZ6njSqBEJvIYC2XsdM5/7hEcP+9We4hEaWNK21OTq0Pzn63jlTImPtJahS3uj52453ppcXvtdX8KIYYIwon8PxNU/lrruTda+PaYo0O0x4a/UjQYRd+lQUxZMATlXI6sMz/ecyRTU9HzqEoeZXhreMIN/uBbwU6w1GAINmLdd/VHkqj69MerjcPEPlQzlqlzlJPzCKim0/hC86TjNJN7wq6aR68oPCK/TWVwGN9p2YT7lS+yCdXk20QlM6DlEYGi9CzJiBa74Xwlhpk78fHgesdXMlyHFBWnDuljIF0CG4BfAmcjhi6g+li8prH4kpnqLkBJM0M9kPdSJKGpMTNRaJ1SBDVnEV0KES9hozf59gGm6JNwpS474KzHugbt97+QQGt1qqO6BHERXuCbGgklEcEbD0acp9MMIRlC1vDx6WhdQTURnqbTWMHNeX81U50ygxnLFnDto/GRmuJnl9uQV2k1ZeNditlP35IsxRlTNzP8G4B6Ydp4zyQBiYYgTcd+Bw+ykPykh6XVGbinZI0+x2igTccClJYRAebO/XZ4l1pHqbRhrvpjQ6V6MNeNaf+OEc++uDNCgxp8e9OxZxAFnirAfZUnBU+w0/SAHsmXu/OwfROg/U3msZAkSAMxL8gpLTUswA+VpHIM1xyeb4l/4cufgmmAn+eo8KdSJ/DQVtUaZ868IxZAa5LFb3qKEx8KrU3UerTZ+MBnDgP/R5l+HUBT7w7fXEiPw+BM64xq+fFRE0mS8MqYnrtmjNCEhy61S0bt58NHoXsC0REaFL+yxhxGzmBQIG32+63wauMjcTjAxtqgdROxsWbBBt7wZkECSqST ub5gTo0R QoKGo9q9bTPDElGCaGYMPtBFiS08eXcqs4HT+8XaUfnwoFhdzjIdpeEdBkq9v1DsYOT/PLWAMUwfcglk1AY0WsKnHaKPiPnvX8HyBDX9V95YcFM0c9G0eODgGEfARO1jyoxIRBjFIFvMxoXu9SZ71yTUXAPQEP/Ip5lH1Isrd9S7iErom3OfZf4t86EknDqV3du7hER6tO9b6/tOW8ZfFaPCBR0VrzwDxXVDqXOeXusEB4fGQpCsnt51wbp872oRkddZZhsbt11PlYJab99ytoqo4/LBN4Il835VF 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: damon_sysfs_set_targets() had a bug that can result in unexpected memory usage and monitoring overhead increase. The bug has fixed by a previous commit. Add a unit test for avoiding a similar bug of future. Signed-off-by: SeongJae Park --- mm/damon/Kconfig | 12 ++++++ mm/damon/sysfs-test.h | 86 +++++++++++++++++++++++++++++++++++++++++++ mm/damon/sysfs.c | 2 + 3 files changed, 100 insertions(+) create mode 100644 mm/damon/sysfs-test.h diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig index 436c6b4cb5ec..29f43fbc2eff 100644 --- a/mm/damon/Kconfig +++ b/mm/damon/Kconfig @@ -59,6 +59,18 @@ config DAMON_SYSFS This builds the sysfs interface for DAMON. The user space can use the interface for arbitrary data access monitoring. +config DAMON_SYSFS_KUNIT_TEST + bool "Test for damon debugfs interface" if !KUNIT_ALL_TESTS + depends on DAMON_SYSFS && KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the DAMON sysfs interface Kunit test suite. + + For more information on KUnit and unit tests in general, please refer + to the KUnit documentation. + + If unsure, say N. + config DAMON_DBGFS bool "DAMON debugfs interface (DEPRECATED!)" depends on DAMON_VADDR && DAMON_PADDR && DEBUG_FS diff --git a/mm/damon/sysfs-test.h b/mm/damon/sysfs-test.h new file mode 100644 index 000000000000..73bdce2452c1 --- /dev/null +++ b/mm/damon/sysfs-test.h @@ -0,0 +1,86 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Data Access Monitor Unit Tests + * + * Author: SeongJae Park + */ + +#ifdef CONFIG_DAMON_SYSFS_KUNIT_TEST + +#ifndef _DAMON_SYSFS_TEST_H +#define _DAMON_SYSFS_TEST_H + +#include + +static unsigned int nr_damon_targets(struct damon_ctx *ctx) +{ + struct damon_target *t; + unsigned int nr_targets = 0; + + damon_for_each_target(t, ctx) + nr_targets++; + + return nr_targets; +} + +static int __damon_sysfs_test_get_any_pid(int min, int max) +{ + struct pid *pid; + int i; + + for (i = min; i <= max; i++) { + pid = find_get_pid(i); + if (pid) { + put_pid(pid); + return i; + } + } + return -1; +} + +static void damon_sysfs_test_set_targets(struct kunit *test) +{ + struct damon_sysfs_targets *sysfs_targets; + struct damon_sysfs_target *sysfs_target; + struct damon_ctx *ctx; + + sysfs_targets = damon_sysfs_targets_alloc(); + sysfs_targets->nr = 1; + sysfs_targets->targets_arr = kmalloc_array(1, + sizeof(*sysfs_targets->targets_arr), GFP_KERNEL); + + sysfs_target = damon_sysfs_target_alloc(); + sysfs_target->pid = __damon_sysfs_test_get_any_pid(12, 100); + sysfs_target->regions = damon_sysfs_regions_alloc(); + sysfs_targets->targets_arr[0] = sysfs_target; + + ctx = damon_new_ctx(); + + damon_sysfs_set_targets(ctx, sysfs_targets); + KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx)); + + sysfs_target->pid = __damon_sysfs_test_get_any_pid( + sysfs_target->pid + 1, 200); + damon_sysfs_set_targets(ctx, sysfs_targets); + KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx)); + + damon_destroy_ctx(ctx); + kfree(sysfs_targets->targets_arr); + kfree(sysfs_targets); + kfree(sysfs_target); +} + +static struct kunit_case damon_sysfs_test_cases[] = { + KUNIT_CASE(damon_sysfs_test_set_targets), + {}, +}; + +static struct kunit_suite damon_sysfs_test_suite = { + .name = "damon-sysfs", + .test_cases = damon_sysfs_test_cases, +}; +kunit_test_suite(damon_sysfs_test_suite); + +#endif /* _DAMON_SYSFS_TEST_H */ + +#endif /* CONFIG_DAMON_SYSFS_KUNIT_TEST */ diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 5268e8503722..d13e353bee52 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1804,3 +1804,5 @@ static int __init damon_sysfs_init(void) return err; } subsys_initcall(damon_sysfs_init); + +#include "sysfs-test.h"