Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1953658rdh; Sat, 28 Oct 2023 14:34:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFT/AJmDkO2jUEqxYzVB1z+dGKa5RJaKmFMxCSfpxLs9lzO0tG+Zeks7HnhK/PMXFGKNwLN X-Received: by 2002:a05:6a21:35c2:b0:15e:1486:1e08 with SMTP id ba2-20020a056a2135c200b0015e14861e08mr7050895pzc.19.1698528843633; Sat, 28 Oct 2023 14:34:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698528843; cv=none; d=google.com; s=arc-20160816; b=SmFn1BqFpgU3rRUQiFttZgFSDwFyNbUDtcs1igdYqVPQ0LUks6pl4QppZfBX9vGg3Q J721wxe4E5NBBzs+J52foBHDaq43YZwlIA/DbM2NlQgZ1XiQjE6DhACMXcT45fI42chU mI9HTKFR6TKAyg6TQDZOlkbyVjnx96Zfx60QV+UgG+NHOV7lQYo5oez3/hfiWIzcagod T+ujiD3Iz1dSUp0fk6zPAg9haF1CIOUz7NU6H0OXUJ+FOQ3T7PAd2o/CW1OK0FErvJKB 3I7EmMO7A+SkHB6EtXCmzm4uf1Mbz12YRjVJb4cmOixySSajf74iPWMGZtbK84kVCL9e Bl3A== 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 Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id x2-20020a636302000000b005b927f53914si2879874pgb.59.2023.10.28.14.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 14:34:03 -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 A9BC1804E832; Sat, 28 Oct 2023 14:34:02 -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 S229870AbjJ1VeA (ORCPT + 99 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 Content-Transfer-Encoding: 8bit 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:02 -0700 (PDT) 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); -- 2.34.1