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; }