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: 156607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp945962vqx; Sun, 22 Oct 2023 14:08:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgguwoEILoAqi0amhQYRbS9Zbo2K9le5mZvLq7j0JGUpetG5KO0T7njW5w7eF5waYew7Mw X-Received: by 2002:a05:6a00:2292:b0:6bd:7cbd:15a2 with SMTP id f18-20020a056a00229200b006bd7cbd15a2mr6190663pfe.26.1698008911353; Sun, 22 Oct 2023 14:08:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698008911; cv=none; d=google.com; s=arc-20160816; b=CL5WvMMrl+bqbU+9+GzqDd2AN2VrEtj4srfrBc8LAq8unNkS3FXwUM47s5AQ4Iq05k /Y27ms8JiCKgy29dUN0wht40sgokzK2GBiSoTYVfpphS3A7Mbv2E/IfUyiUYxwtrbP7F 0r/40H0BYYe8QdKwFzUlX8L1BQv2QOlrS9Baf7vqPc57lsnte4CNRBpRi2r4Xq2Uvzq8 eR+ebRy0UL9kB0mJ8IpTyp6LlwpXs/hJ5mwJer1CRDscCKyzuRmC7Y1iKM+Ar3kDw3tV fNnrebCrC04LcdAaEAPmvaCIfjRHPisG2j6Tx3g5xKz6P4ThXGYp5Zta6G4wqAE9l/8a FNoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=v6VMuy7DmJ/BI7xwog1cv/QRtJyZDLvlSGkxffQ4BkA=; fh=WXAAPMfSxLmsB2WJ8A0s7FPd+eTotx5bYas/XF5lzN4=; b=luuZ8GaboEmF/QIMphv9S5PtapsViD1IWxu0UTo9Ma6zasJ7CHhWvvuyYm6TSkBevG 5WIjO3HsPlmqwZ0dH6CzHAmieNINZUhVRMxN6MzKlkMtohSz9IDZ3Ykv4YJGrTIhlzXp hOkMLL2oyuOhyldqvR1B14Djl1KDbESTtQWw54DAE5a0QQn8Xvi02SwXD/7ZP/13ubid p6Uy4692LB8uSvNej1wvUkHdAbA+tDWEWEfiPzRYHMrDEQLXz2+o2dWL3xAlEW3uMvNh F5Iy7FOeV2nTSMSdhxbeXdRDspMln9D27GBOjopAeOfGpWd3Z/WiYDmQx0y+vMbNVnoG KT7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NnueFOFQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id w15-20020a63934f000000b00578db71451fsi5272318pgm.501.2023.10.22.14.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 14:08:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NnueFOFQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 1107280622A6; Sun, 22 Oct 2023 14:08:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232804AbjJVVHo (ORCPT + 27 others); Sun, 22 Oct 2023 17:07:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbjJVVHm (ORCPT ); Sun, 22 Oct 2023 17:07:42 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11688E9; Sun, 22 Oct 2023 14:07:41 -0700 (PDT) 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-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 22 Oct 2023 14:08:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780491392116353051 X-GMAIL-MSGID: 1780491392116353051 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: 156606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp945961vqx; Sun, 22 Oct 2023 14:08:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEs56Kc+jfp5KpwEckTY4ke2Qt+8Wmz6TDx+QrMhASEB8C2yqORIMdt/qwC23ixCNaWo9W5 X-Received: by 2002:a05:6a00:158c:b0:690:fe13:1d28 with SMTP id u12-20020a056a00158c00b00690fe131d28mr9559016pfk.33.1698008911313; Sun, 22 Oct 2023 14:08:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698008911; cv=none; d=google.com; s=arc-20160816; b=soEtQ/244PiifJcG/1Gr5BBM6N9IksHc0+Pgk/SfsC05fZDuQQieCwwNmx2XkEide4 xcCiW/PnbcF9PKZGsyBAFtoiTvYxsL++O1QExYyE3inFvhKxIailmSl1jqi+Co/45cZi GgY3+47/rWJlrgGnijwjW/0kuNNroCbn8CugRcEkukIEGLV1NFdss/OFnrugr90ikuKx rO+AXnnSsCw7f+y4sJSbqjMZ4jgQxYypcI2pnlBdRdtUqkw728ej9ZHxh+UFKEyEUIrE LXjx54eHHzKj5e+cM5jIEH8eEMf4jx/J84IibqRmFZXJKIFeaRLG85ItU3sFF+ke9aTs l5nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vyztF8RlWiQ9YGFEDf0nkv2Iz+1sSrzYHDFSHFXmnJ0=; fh=KYrqrUZLyrFtzsRGU6oAoSUmY0DAP9CMMvmnxe+fdzY=; b=zRZoHEL+lFofdhZ4iyI/abBvH6icrntO6lc3iykFAB4jztqAkeBXva80lQuuNJ4WQz xcMReQyZuq1oy+Y4G90TjvOarZsqPPIigpKZ9q+2H9+qvtvgSP0D1j/v03aG6myqve7S sm265JvGwWY1RJl8EiqzGcxYZvgYKq7xfzGLGF7a7DLRZ9hNVSPvrs+DWUfUUzKvqodB AK2yMTJ4UPdsqzgBOnuVe8ExL+fHxUplamB8/AeUl27wJOnPjRibBXDomUihFAs1yr6V OGB6/DvjinzwC6pl5laRURv+4zJONYcH69UlwcJKphmSd5V4GK93Dzlvn4VH5XXzaCkS 8CLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VPtzbp9D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id t4-20020a63f344000000b00563de199314si5371360pgj.896.2023.10.22.14.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 14:08:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VPtzbp9D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 36D0A8057E26; Sun, 22 Oct 2023 14:08:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232847AbjJVVHq (ORCPT + 27 others); Sun, 22 Oct 2023 17:07:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232724AbjJVVHn (ORCPT ); Sun, 22 Oct 2023 17:07:43 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13B95DD; Sun, 22 Oct 2023 14:07:42 -0700 (PDT) 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-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sun, 22 Oct 2023 14:08:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780491392008177176 X-GMAIL-MSGID: 1780491392008177176 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"