Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp876322rda; Sun, 22 Oct 2023 14:08:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYTottamPUtprbSwJ5BoeLaUS9mMAGe6C35p9PEn3CkTEE6W5WIecII1AF0uxE1vyjpSfO X-Received: by 2002:a17:903:124e:b0:1c6:1ab1:93eb with SMTP id u14-20020a170903124e00b001c61ab193ebmr5956374plh.17.1698008903010; Sun, 22 Oct 2023 14:08:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698008902; cv=none; d=google.com; s=arc-20160816; b=jUjpPrq9nfUdWVxgk7l7aQVoGJ3kjl0NpwBzG11dI/04ldwWgct3a4+j1sHCJe8G9s bpHyJf691+b1kU2TYKWu+4yG+Z/fM8PTPAfHqs1IwDAjzYbt9SwMWGIh/WVtiVjTmCFa qXObISIc/aK1AmrF9jz+BI1OZJmBPGykK8mRhqy1TSlr7XqLEsfTU/wscmftyzT/tEwj KCezqIUcRbBv9vTK910Swv6svBn+ycRQM4rFeAwpVEpZBplZOw2tNGPlZZBgpNJHSH2j pF+na/SvH6lI0RSpm7G2+m/eki00YI45Lid3q93t30P9sf656Ilymd63CERgIKW9nwKF 50Nw== 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:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id n4-20020a170902968400b001c589ba4a04si4935379plp.24.2023.10.22.14.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 14:08:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; 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:3 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 908818057E13; Sun, 22 Oct 2023 14:08:20 -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 S232803AbjJVVHo (ORCPT + 99 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 Content-Transfer-Encoding: 8bit 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:20 -0700 (PDT) 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; } -- 2.34.1