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: 159304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1406469vqb; Sat, 28 Oct 2023 14:34:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFH/5ZPv98vGPSnOpBNvQPrER2B2x82TXNH+y11ZL66XMEouPUlXzcdO0NYNAAmVkUMOs// X-Received: by 2002:a17:902:da84:b0:1c3:323f:f531 with SMTP id j4-20020a170902da8400b001c3323ff531mr7725141plx.20.1698528846557; Sat, 28 Oct 2023 14:34:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698528846; cv=none; d=google.com; s=arc-20160816; b=JoOmkm7ZzNFsUZXYQHvEewnsRTL9gfXhwyvSpT2PEXJVbB2r1zguV6MD3dXuSvCg6K wYcSEfsaohFmnqTR5gRNvNw9DW6rGCQ/QBCb7YyFJCNlv65BfQNgDZC9i2IgXlASgKoq Q7YehEije8hQrMVJ508R42sgVyduz/wm8mJJvfj34MJQhYe5WVB//ZdD7EDfKXzYpDHn s3JGEBQw+gCmDtDrJdT2/3kb+s5nV0FRgsxFKc5Iy5VrnCzczwELWeNxwuV4LCDbOZxE Uw7H2LwS/SWDQ89iLcm+FK1YFZMwtR31yQatCWrqb0wwCoPCwF30Ww5HNHd21LVEATkH SsmA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=yoFfUtvNfDfR75EmvJSn4tShJ7LE+zwtBrZG5IlewZU=; fh=BzdeVYqZhG5iuwuKJRNLP969rvCput73lx0iwx2zu7A=; b=gkOY4+vLTJmKRriorD4bOraRtAuxz/GT4ZZmVzZxBWmlQCm9lANQaYgqZY+tpoRIiI l2ha00okhk+9pvcTKijhafxismkRaeIDQgG6vVIt4cLEp9K2bZn3b4yZxzgSrifhIglu Kxs2qIoK6eDh/UC+dbTVCXTsT4xnEjcyXoE7S+8u9099eV1LR5eODj8YchhQD9E4OLT/ Vpm3qdSoYkYVe8Q2yjrpDVov6RFJKhMENBPROoSawHQiM+N7v0yxVEnzkFAsLtTDJcgG 2UC9tptP1vvcIcGK1b1T471ItLf9nQ9NzSPv4elhWPvQImnlyD5IXUrTfe8/eF2krccm iE/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Cdy6mk5q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bb1-20020a17090b008100b002791035445esi2792986pjb.76.2023.10.28.14.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 14:34:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Cdy6mk5q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 88605804E834; Sat, 28 Oct 2023 14:34:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229450AbjJ1VeA (ORCPT + 29 others); Sat, 28 Oct 2023 17:34:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjJ1Vd7 (ORCPT ); Sat, 28 Oct 2023 17:33:59 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CD67D3 for ; Sat, 28 Oct 2023 14:33:56 -0700 (PDT) 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Sat, 28 Oct 2023 14:34:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781036583601918810 X-GMAIL-MSGID: 1781036583601918810 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);