From patchwork Sun Oct 22 21:07:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13432087 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 B7B53C00A8F for ; Sun, 22 Oct 2023 21:07:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83EBE6B0081; Sun, 22 Oct 2023 17:07:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EEB16B0082; Sun, 22 Oct 2023 17:07:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68FB96B0083; Sun, 22 Oct 2023 17:07:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 572BB6B0081 for ; Sun, 22 Oct 2023 17:07:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2638D1A069C for ; Sun, 22 Oct 2023 21:07:44 +0000 (UTC) X-FDA: 81374334048.12.E09E19A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 7767940013 for ; Sun, 22 Oct 2023 21:07:42 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NnueFOFQ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.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=1698008862; 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=v6VMuy7DmJ/BI7xwog1cv/QRtJyZDLvlSGkxffQ4BkA=; b=2r7u4YC4GH6xnebGs0q9kLbMYoOZycftkgRWdP5QSkrTuTRQEgO+LZSml+cisTjvOjbWLf CbSFO2VNENGw8YySfqIiyNSuoK/t9JJfU89dgxSZ6Uf2OwPw1bXAxCh0OLxqP3/BMK9SI/ jwjFWFypSLlYX0UgnkFjjBVtezD9ifg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NnueFOFQ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.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=1698008862; a=rsa-sha256; cv=none; b=R7sTZNcd3fqYf/m1HoyDttjH03/9qKH1J/J9RXNG3XZrWqqmMxgIhyfPy5DvJAzRD1gWIr zBMhl/vp20oH9kdFV2mZj1E1xxVpDjpA+WFqKaDqSs3xp/o7FXFE4lnlV6I/gIjTAZSPOh 6ydbcS/Uwg3YaFofQtrw2WvS1kvPTwM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 47CAB61565; Sun, 22 Oct 2023 21:07:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33BD1C433CA; 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=1698008860; bh=yOdMqP6KZLl0RBsnjmhN2cj6fhQSH3cbP76PBzRCOAg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NnueFOFQ6TdfQk/iuPOF/OFgmETyKyueN3Q8xtoB6EEMbxvxVHfON+yY3CJBuKIRg f1yY9C/RuQcxMRhpnmwPmKVJUrrynqKh1TIniakjnBDBebuy6nXz+TWubn1wXJd8Ri 010+bl5UYfpbCkevNZLOml+RapqIGvkwqzz3lxyAuuzj2AkffQe+/Rs6lpFqe1g/4y vz4FyTcuIiHO78oGe60UG78FqYJFRUiZgnxU8cZURkIctRznFCXHqveGG5bFPIjO9+ 4bX43pRwyubtaSVX4tcbeKdOl2Tu5t2E9xGZMEzJx4bTe0+pC+JyvjamJC/usJ0bld D7ogbirT/qmAg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 1/2] mm/damon/sysfs: remove requested targets when online-commit inputs Date: Sun, 22 Oct 2023 21:07:33 +0000 Message-Id: <20231022210735.46409-2-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: 7767940013 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: syyd8c1ijp6cyhqjq5r4kapnndynmpju X-HE-Tag: 1698008862-704724 X-HE-Meta: U2FsdGVkX19ZtXnTo2ec0oXrqrr1bXYu8JWNB5QQJmXOnB58rjHMJh0lsdEjDVvqUvdMaqLbikzw8ViCINObSVLJmAIH7PzIjme790pcvKLXYPBZg4YhLM/DyelXjwhS6B4jvvO+SlH4+tb48mKXOQH+Y6IjRcxFR3x54MsovF7iX8XQsJ4c/Oq6cAq3EVJ4sjrxmOfgByflSqo7+VQ7V5C2nzZpGtFHb68TkbJn4xT7g/w45cg1e2636HQ4eqrWM+xUdw0BF703hH0Ob901lGNug+cGcLjre6oGBKKCXwr2YkhwokruryhfnJvufmy71eUArPQgH48zEz2/zIu7or7j3HjSf/k/1nM0Zl5jOFRh9BYsEY7mzBTNbAyQEqxtNQlygKQvliMbjq+dz4SIZ0eCGFRT4Vqz1NXBJzIm48PFqTLgCfUSMq6Xszz800KoUArtH4iiH1dTL1MI0tilZK4qJAL190oNNBBk0iyjAwyffvlU6To6DdiO0uDGuHcFq2j2Vs9Uj46LjeYwNsbq1JOc7jdZbPnD6dDSABXH7T4/VDpwVLawmIpwbs7pFa8X0yejWxew4OiSccPDVvxb2Ujo+nDgnJ7jZN8yuUU3mnyZ+W0M1fAaWh2zpWQzxe7sPGbx14naOOHy5pV9LWmA+qlTW0Nxa7MdddIxGMZNZt8B0WXLJH3oAZP5lv+VT0SQX1SnZqemunRf5XE+Pi6OuqOm74RYl9CbVq1w7KLYzih3ELEUGp8e496cE2dOXpTK3Wx7VGh0zMQvIUb8ShGza+OP8ibv0F/FD/z1L49UFInOMHI3B/V/UQ2q2pDLjqcv3qKYs5jagZ2lNMkK13wYJBme32mVczXUc4YzhC96YHhW7tVRLfmVEU4X6TwdVs2VGcuR1pxRoi1ypNb0lDyUIYOQrr1F0oBO8Hvv6zSykre6nm5xHYoSBrhdBmgQeRNn7BlNJtcFHiuvMKfSd8k l/nW4V1T iVKjAhqtOt1bZoHuk/dKVdsj4I0Ww9Se1AH9ZD1U0PpMB/TKzZyW2oqoIynYrcNJV/OkvO1KMuA+V5t12D+rgjcaKlwktA40d5RgAXkH7MfbMcvPD1M8q1hR1jUx7HUiH+EG6JqLgGbyGY2E4MTmZRxm11DWlpF7FH9hiChm5EUtiay8g+VmlYnzrJA7gBUuWV4ltKElTv6HFEzInm5S4hNGUk2xn9GPvxvw8+58srMkXZ0E= 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(), which updates the targets of the context for online commitment, do not remove targets that removed from the corresponding sysfs files. As a result, more than intended targets of the context can exist and hence consume memory and monitoring CPU resource more than expected. Fix it by removing all targets of the context and fill up again using the user input. This could cause unnecessary memory dealloc and realloc operations, but this is not a hot code path. Also, note that damon_target is stateless, and hence no data is lost. Fixes: da87878010e5 ("mm/damon/sysfs: support online inputs update") Cc: # 5.19.x Signed-off-by: SeongJae Park --- mm/damon/sysfs.c | 50 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index f73dc88d2d19..5268e8503722 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1150,58 +1150,26 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, return err; } -/* - * Search a target in a context that corresponds to the sysfs target input. - * - * Return: pointer to the target if found, NULL if not found, or negative - * error code if the search failed. - */ -static struct damon_target *damon_sysfs_existing_target( - struct damon_sysfs_target *sys_target, struct damon_ctx *ctx) -{ - struct pid *pid; - struct damon_target *t; - - if (!damon_target_has_pid(ctx)) { - /* Up to only one target for paddr could exist */ - damon_for_each_target(t, ctx) - return t; - return NULL; - } - - /* ops.id should be DAMON_OPS_VADDR or DAMON_OPS_FVADDR */ - pid = find_get_pid(sys_target->pid); - if (!pid) - return ERR_PTR(-EINVAL); - damon_for_each_target(t, ctx) { - if (t->pid == pid) { - put_pid(pid); - return t; - } - } - put_pid(pid); - return NULL; -} - static int damon_sysfs_set_targets(struct damon_ctx *ctx, struct damon_sysfs_targets *sysfs_targets) { + struct damon_target *t, *next; int i, err; /* Multiple physical address space monitoring targets makes no sense */ if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1) return -EINVAL; + damon_for_each_target_safe(t, next, ctx) { + if (damon_target_has_pid(ctx)) + put_pid(t->pid); + damon_destroy_target(t); + } + for (i = 0; i < sysfs_targets->nr; i++) { struct damon_sysfs_target *st = sysfs_targets->targets_arr[i]; - struct damon_target *t = damon_sysfs_existing_target(st, ctx); - - if (IS_ERR(t)) - return PTR_ERR(t); - if (!t) - err = damon_sysfs_add_target(st, ctx); - else - err = damon_sysfs_set_regions(t, st->regions); + + err = damon_sysfs_add_target(st, ctx); if (err) return err; } 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"