From patchwork Sat Oct 28 21:33:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13439606 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 4FE4AC4167D for ; Sat, 28 Oct 2023 21:34:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C95E36B030F; Sat, 28 Oct 2023 17:34:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C46816B033B; Sat, 28 Oct 2023 17:34:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5C516B033C; Sat, 28 Oct 2023 17:34:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A7B296B030F for ; Sat, 28 Oct 2023 17:34:00 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 719AAA0295 for ; Sat, 28 Oct 2023 21:34:00 +0000 (UTC) X-FDA: 81396173040.25.CC842BB Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf24.hostedemail.com (Postfix) with ESMTP id A2891180023 for ; Sat, 28 Oct 2023 21:33:58 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Cdy6mk5q; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 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=1698528838; 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:references:dkim-signature; bh=yoFfUtvNfDfR75EmvJSn4tShJ7LE+zwtBrZG5IlewZU=; b=kdsRFcBci4jLwaOzXaf1hkZebUH6n+v17FASOtFYeF6rvI2JG9mL2HnCIyVy1wdZTKvOCU y03v02oREzwGVny97e6SPSbddfPsLNq8FLn4ZEfEfMTSK6COyM/9hpuG4ba71j/tFvjrSd bZFYN21gWo/uNXGhxU4P1o75fdrfGOU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Cdy6mk5q; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698528838; a=rsa-sha256; cv=none; b=6OMJPPchZw4mLWYsFq5Bfbe9g3dwPSS81GNHqijtvsMRrPD6ZXXe/41yYs8Y1pqMGn0+v4 tBYu/gODQw1e+OKp3ZyM2s3tyKs/hNCmBkAW6EVMOIQTUX/ADzRedeC4fz9L1ezRIRPQl1 +Y2jeoBG405UG+swAcr/VJ8VfBYQEI4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 404EFB80AC2; Sat, 28 Oct 2023 21:33:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 695EDC433C8; Sat, 28 Oct 2023 21:33:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698528835; bh=UtSutLXi7E+qkhfIMryx3kSyfkKokutnxEvlL8G6ZwA=; h=From:To:Cc:Subject:Date:From; b=Cdy6mk5q5CB8HhSyv2aPmAM70gKtHkfpfDCtkkkDC5fX/jw5zKTMfRQkqWBI8O62p mFYUccWMuUNF6g0GHPJg6lYG6pOrSfRjULFgYMDlWw15Cn8C7z6BlcA20OweGuuVIP Pqzqd77W1wj8VIs+wMymqQGBcg6CfTSnUb4X+rjxB5zJfrSx/OcDCjLbkTAZYRmC+6 xX4l6nhOz4I7bnSLYwZgIcC65cCoEefsYf6sb9JCzdnviCc5QCwrKPp+WNpVRJuO1L ATWP2+fNh7YpUoOr05YRN1dxgFBPlK08idaWzlC0UGftj6jE2iubR6Gg7BJKr/bFDJ ERO1Po8Tb/qQg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH mm-hotfixes-unstable] mm/damon/sysfs: fix unnecessary monitoring results removal when online-commit inputs Date: Sat, 28 Oct 2023 21:33:53 +0000 Message-Id: <20231028213353.45397-1-sj@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A2891180023 X-Stat-Signature: 5wzqrkbgxbamnw4jzwor39bmzdn1t6x4 X-HE-Tag: 1698528838-514692 X-HE-Meta: U2FsdGVkX1+JK8nvuJJIqCZuvRRjMi/ZeIURSCcyTg+rYhSSGJhNAiqr9oCRlmSn/gu10LSBLUCQJLupZJXjQDOL7McnJFFWZdS7AR9M5uQhlyrcIw3UsPw+B/5CkPawzatIivM7Ew7vzf0g8L9eb+D6fGR/GB7mAdEpzCe9O5pE/6Vi/EZqmUqzlyfRWx2ykMFLhY+1GC90W3YnN1XJ9izdoOORYEXNVFSetru8B9c6U45vOteu1DJ+7WL+5UZzzRRRSoTl71Ey7hVXs6TKvVJSw1vqA0ftE+o8eIZRbC2cronEXIYW40pMRCc7LQeDHuBbPIl2b6PtNHRSgs+HgIO+9mLwe68EDX+KTm4ao1ipaOriqCHtW50lE9h321SMxHto5f0K4vOvBZmli27taKfu1CIaHy9wRyqDUFflASC5rOqutDQAMYKxlfVR1lZPiu8kkGbmGG4FEBAAriVW7XxegEeVBBT9EfxgBCRUrCczmELyglHvDhPaWiU20sJPXMXWP1Vk2FyqL8qD83HoxasUizwrbJ+HCUYYpgMJSQwcEOzsABvvoJzLIH2s6OpCyxo6xLk9ojIux9VDrzqe0i5mV74viS0/LqxvaD5E78ZlWnwMK8x6QVP3Dqod9FX3AAQdwxEz6o74LdBQN6dsB77E46UUpldshCNL4ixs4z50+QKj4H7M40qdabqQTNuvdKri+yhCvDcOK+oPipFHaaLRY60lTEmnAiahp1+lA55FbNVIXJD0iVqohd3+SbpkRpBVWVaHIwLsgXE1zJn/sneDp/U/W+JhgFRY+474IAsWRpBlazsP2DFFpmGHmmUT87//lcsWC7MUqkkyyBN0Nxy0TJ8e5dqofZiyn7Pqr6ChRwEzXn3o9I5BbmtZi4BK1McyQtdYQ4Lb3UKNLJi66fYVO6tw4R2q3qjh4N1INYivC2nnI1xUTYgp4drlq84KcqE3DkPf5Klk7RVVTSS RqGBKs6D PmKe39iWpYNGTU5xl3VTVaMIq+HBnlXHIBhmvkSBIo9SeMjq9oyzvuQvwZB7D2E0r+k4Ch4uggkS+RdBmK5f5UrPgEdcepHai4pnGT8pEO2iYebkex4ERvUabD8TrRpCWnMgmPJBFf+vqxRrrln13ek1zWwf0byoIghM4J4cL0dPO3zpMmtUYc3XnO0goJoJQ5EB66cB8N/Tc0KMyhm5oeCR9v6zRY3gH25CZISmt5Tvf+xc= 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 db27869df6ed ("mm/damon/sysfs: remove requested targets when online-commit inputs") of mm-hotfixes-unstable tree[1] makes all targets to removed and then added again based on the user input, for online-commit inputs. The commit says it is inefficient but ok as below: 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 But that's not true. 'struct target' is containing the monitoring results ('->regions_list'). As a result, the patch makes all previous monitoring results to be removed for every online-commit inputs. Fix it by removing targets only when really changed or removal is requested. [1] https://lore.kernel.org/damon/20231022210735.46409-2-sj@kernel.org/ Fixes: db27869df6ed ("mm/damon/sysfs: remove requested targets when online-commit inputs") # mm-hotfixes-unstable Signed-off-by: SeongJae Park --- Andrew, if you squash this into the original patch, please update the last paragraph as below. Fix it by doing in place updates and removals of existing targets as user requested. Note that monitoring results (->regions_list) of targets that no change has requested are maintained. mm/damon/sysfs.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index d13e353bee52..1a231bde18f9 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1150,23 +1150,57 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, return err; } +static int damon_sysfs_update_target(struct damon_target *target, + struct damon_ctx *ctx, + struct damon_sysfs_target *sys_target) +{ + struct pid *pid; + struct damon_region *r, *next; + + if (!damon_target_has_pid(ctx)) + return 0; + + pid = find_get_pid(sys_target->pid); + if (!pid) + return -EINVAL; + + /* no change to the target */ + if (pid == target->pid) { + put_pid(pid); + return 0; + } + + /* remove old monitoring results and update the target's pid */ + damon_for_each_region_safe(r, next, target) + damon_destroy_region(r, target); + put_pid(target->pid); + target->pid = pid; + return 0; +} + static int damon_sysfs_set_targets(struct damon_ctx *ctx, struct damon_sysfs_targets *sysfs_targets) { struct damon_target *t, *next; - int i, err; + int i = 0, 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); + if (i < sysfs_targets->nr) { + damon_sysfs_update_target(t, ctx, + sysfs_targets->targets_arr[i]); + } else { + if (damon_target_has_pid(ctx)) + put_pid(t->pid); + damon_destroy_target(t); + } + i++; } - for (i = 0; i < sysfs_targets->nr; i++) { + for (; i < sysfs_targets->nr; i++) { struct damon_sysfs_target *st = sysfs_targets->targets_arr[i]; err = damon_sysfs_add_target(st, ctx);