From patchwork Fri Nov 24 21:38:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 169579 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp1551784vqx; Fri, 24 Nov 2023 13:38:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcu6A/INNg/JfT8Q93rMeoImL2T/xSzBQli26c2wjf9VaEdqMRiSmtWSiaT/J32QKatM86 X-Received: by 2002:a05:6e02:1a62:b0:35b:f6a7:ce0a with SMTP id w2-20020a056e021a6200b0035bf6a7ce0amr6136238ilv.15.1700861930895; Fri, 24 Nov 2023 13:38:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700861930; cv=none; d=google.com; s=arc-20160816; b=E57gh93HdZuwU4HVLN+7On/KPFs/Xygk2avHSY9F77aC3pZK/BmRMoZHpCCQ54fYRi Kn+GTMtP6F+ejjWZ/60PmcRxaA+XtsHjbMr+Ju2L5bMmv4FnPIJsXzsC4p7kI5IB3lqD DokPaM3+BIBtRXFL6jqeiBJ58l17pxXtWtA65VdK/Tpuw/8UJCWUTgiprSF5XlrKloDU wkFHIRY+ZbJoF/xil2wAw8xQtebbFpz9EKWpVU0oZdNbsMrpa+QunBRzM82hb7vm8Ttk /l3oEkuF7Ngya6uEgwSWBylqOVNLv/FWMgogrL6C9JLbpguBAeHYqkeTftMd+R0xkpVJ 2K9A== 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=j6SnUrSk/y/V0gDJwJMOivI8vwKTdX2/2V05fqNQ/Qo=; fh=BzdeVYqZhG5iuwuKJRNLP969rvCput73lx0iwx2zu7A=; b=Ixgd06O7+Go7j+mKGjui3LCQCyNcte2VWkEubMkT29+sogmkgSvmJ+HXrJpnu+fbKs U4oRjU3pU8KcJCmiP1kJkWMvoAboazMHwh3RMrgwpHoXRZsFhiqvq/i66zJzVcOdZkVS tMITf2372CeTX8qMjYaXDCrl4ofpp21nPvh+vmpTK2EvYlY36kMxi9vH0uYnQE/60NWs FLzEC6H/9XEZIfLC43/ZU4aS9zfR1efmA0wAjolMPmegqZtsI9mQWW2jjE718PY5T7KU /XJgMCptI+VwRsEB9Ua37zhu9Qk2BkLV9bdkdkfCAgGQRG06rbzI/0qOf+MndN0erQSk JtGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WAWCwNsY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id m2-20020a056a00080200b006c33a1be028si4400499pfk.87.2023.11.24.13.38.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 13:38:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WAWCwNsY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 8C9AC802C8DC; Fri, 24 Nov 2023 13:38:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230408AbjKXVii (ORCPT + 99 others); Fri, 24 Nov 2023 16:38:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbjKXVih (ORCPT ); Fri, 24 Nov 2023 16:38:37 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92ABDD60 for ; Fri, 24 Nov 2023 13:38:43 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD367C433C8; Fri, 24 Nov 2023 21:38:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700861923; bh=ptjkAte1QXNuW1BDKm99u+0k/U6sLZF2tFKHqkC6btM=; h=From:To:Cc:Subject:Date:From; b=WAWCwNsY1fMaMcr302PWbuf4doJ/cZNDDR46mMRellmvcqfw+AE1fBzUM+XKP6w/W ZTEckTXLa3jzw433vrk0ZUlXvUeOkH/CPYzyh5vb8chikffbn3v6+6Nx5SVVol9/AP ag9lC0u5aCFRoq8B8xkjd6BEaU6udnSrYJEZw+szeuWXzkcmf4AOwVbF87rODMKuXh H2HOlG2sZxp6+hgg8qkunGc7zQUlq+R5KeYEWWRyVITNe8SNeXMPpYGDAuy2BYaggl 0d0aeVp2TRE2toLmluzDNm+GUz05Ymczjp4ozq/TWMpMD1U0aRX15nA7ZG3UDc94j2 +FLkltKFRLS0w== 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/damon/sysfs-schemes: add timeout for update_schemes_tried_regions Date: Fri, 24 Nov 2023 21:38:40 +0000 Message-Id: <20231124213840.39157-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,T_SCC_BODY_TEXT_LINE 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]); Fri, 24 Nov 2023 13:38:49 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783483000119451767 X-GMAIL-MSGID: 1783483000119451767 If a scheme is set to not applied to any monitoring target region for any reasons including the target access pattern, quota, filters, or watermarks, writing 'update_schemes_tried_regions' to 'state' DAMON sysfs file can indefinitely hang. Fix the case by implementing a timeout for the operation. The time limit is two apply intervals of each scheme. Fixes: 4d4e41b68299 ("mm/damon/sysfs-schemes: do not update tried regions more than one DAMON snapshot") Signed-off-by: SeongJae Park --- The commit that fixed by this patch is merged in v6.7-rc1. Hence not Cc-ing stable@, but this patch would better to be treated as a hotfix that will be merged in the mainline before the v6.7 release. mm/damon/sysfs-schemes.c | 49 +++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index be667236b8e6..fe0fe2562000 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -139,6 +139,13 @@ static const struct kobj_type damon_sysfs_scheme_region_ktype = { * damon_sysfs_before_damos_apply() understands the situation by showing the * 'finished' status and do nothing. * + * If DAMOS is not applied to any region due to any reasons including the + * access pattern, the watermarks, the quotas, and the filters, + * ->before_damos_apply() will not be called back. Until the situation is + * changed, the update will not be finished. To avoid this, + * damon_sysfs_after_sampling() set the status as 'finished' if more than two + * apply intervals of the scheme is passed while the state is 'idle'. + * * Finally, the tried regions request handling finisher function * (damon_sysfs_schemes_update_regions_stop()) unregisters the callbacks. */ @@ -154,6 +161,7 @@ struct damon_sysfs_scheme_regions { int nr_regions; unsigned long total_bytes; enum damos_sysfs_regions_upd_status upd_status; + unsigned long upd_timeout_jiffies; }; static struct damon_sysfs_scheme_regions * @@ -1854,7 +1862,9 @@ static int damon_sysfs_after_sampling(struct damon_ctx *ctx) for (i = 0; i < sysfs_schemes->nr; i++) { sysfs_regions = sysfs_schemes->schemes_arr[i]->tried_regions; if (sysfs_regions->upd_status == - DAMOS_TRIED_REGIONS_UPD_STARTED) + DAMOS_TRIED_REGIONS_UPD_STARTED || + time_after(jiffies, + sysfs_regions->upd_timeout_jiffies)) sysfs_regions->upd_status = DAMOS_TRIED_REGIONS_UPD_FINISHED; } @@ -1885,14 +1895,41 @@ int damon_sysfs_schemes_clear_regions( return 0; } +static struct damos *damos_sysfs_nth_scheme(int n, struct damon_ctx *ctx) +{ + struct damos *scheme; + int i = 0; + + damon_for_each_scheme(scheme, ctx) { + if (i == n) + return scheme; + i++; + } + return NULL; +} + static void damos_tried_regions_init_upd_status( - struct damon_sysfs_schemes *sysfs_schemes) + struct damon_sysfs_schemes *sysfs_schemes, + struct damon_ctx *ctx) { int i; + struct damos *scheme; + struct damon_sysfs_scheme_regions *sysfs_regions; - for (i = 0; i < sysfs_schemes->nr; i++) - sysfs_schemes->schemes_arr[i]->tried_regions->upd_status = - DAMOS_TRIED_REGIONS_UPD_IDLE; + for (i = 0; i < sysfs_schemes->nr; i++) { + sysfs_regions = sysfs_schemes->schemes_arr[i]->tried_regions; + scheme = damos_sysfs_nth_scheme(i, ctx); + if (!scheme) { + sysfs_regions->upd_status = + DAMOS_TRIED_REGIONS_UPD_FINISHED; + continue; + } + sysfs_regions->upd_status = DAMOS_TRIED_REGIONS_UPD_IDLE; + sysfs_regions->upd_timeout_jiffies = jiffies + + 2 * usecs_to_jiffies(scheme->apply_interval_us ? + scheme->apply_interval_us : + ctx->attrs.sample_interval); + } } /* Called from damon_sysfs_cmd_request_callback under damon_sysfs_lock */ @@ -1902,7 +1939,7 @@ int damon_sysfs_schemes_update_regions_start( { damon_sysfs_schemes_clear_regions(sysfs_schemes, ctx); damon_sysfs_schemes_for_damos_callback = sysfs_schemes; - damos_tried_regions_init_upd_status(sysfs_schemes); + damos_tried_regions_init_upd_status(sysfs_schemes, ctx); damos_regions_upd_total_bytes_only = total_bytes_only; ctx->callback.before_damos_apply = damon_sysfs_before_damos_apply; ctx->callback.after_sampling = damon_sysfs_after_sampling;