From patchwork Wed Feb 7 20:31:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 198085 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2489247dyb; Wed, 7 Feb 2024 12:33:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVB7nczrL+EUe8tvRP72pMDa9WKarSxbOjYZldPZIYaHXuf4TNnULzSry8zWUS9vSuErDlaiXk0Zk7SzU3gioVw56ZM1Q== X-Google-Smtp-Source: AGHT+IG2vz1z4S96YSe/sY/RMJq81m2iDIGedRg+gLnWKaDDJ1u7drnTww7q92vXsWZXLOWmNOZ3 X-Received: by 2002:a05:6a20:a20e:b0:19e:4c43:3aa1 with SMTP id u14-20020a056a20a20e00b0019e4c433aa1mr4890903pzk.39.1707338016655; Wed, 07 Feb 2024 12:33:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707338016; cv=pass; d=google.com; s=arc-20160816; b=ho10dQYiygZv3LU3qevotVd7iusn+k1QMiBrlbdKfr1grX17gwWp3/FV3Ikorxtc1H GIXOEQZ4FCvR/I/OHBFCxKcZX+upv7s8jmdoxTOxHc4myawwpc4Rmsq70WKa4i34HZUW Sy65KxbkN//UqiAOOVR+FdweEG2tH1+9t+9xuqKzACBOtkgGUCHDlYfuW6hv+o5elxQe 5fsBAt1baOdtsho2LcKH21ylX0QFLX5WPpFSvZT5PuNvg1UZGXzqDY24agLgtURS9u1Q JLl+Ju22gqvXxRhrTQeLU7HSuT5xGbW8+Km8LrwLqwpn4dhtakG/VBnN9NVBMJfns6CA Vr0g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=rqjq/CwbHCXgPEB18xIZCSBiYr8N6Pvdhm3Ak795qwk=; fh=wOBZBW5q6BoG8F+eYO2d6ULAOcuwRam7+S2TLEznWHw=; b=UBdTTGyWrzDNJEN4q0vzH0gZxmmu8G8jdJgTT3LPZqGWyCyhh6fXqmqy4D6w37c86S sUlIKW34/7nAQKuJBYYTHti0xTUlug4hhKQWBDo4oIbJi8gyDEmALKmD9r971opocWxG TcUwIuvoqsCC5ryZLS7HsgMdMQo5lp2BNpTkxmirXSlhhY+lIQziAlBfkz31Vubj7E3x 12LEbUZhBXzEZBaQiTtJ2rfsrRHdi/1wAj4/W1f3Tw9ulemm4e9RkUZK8rF/D05Ypb1H 8st9FpAofCMfel4DqFXGsu0E4pSSlZd6xbhGBy0EmJd9MJI32A9EImhBWtVTw0TJLEMD rKjw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Kb2oXTnE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57032-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57032-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCUcVzDvqwUqUmNornnkh0Hfo/k5eIOrpOtJTGMP614Clx8F6TGI8XwGnwel+Czth/Qui1niTTNELJVSQyK5UWtdNEoSfQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e3-20020a17090a9a8300b00295f1aea266si2264131pjp.89.2024.02.07.12.33.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 12:33:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57032-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Kb2oXTnE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57032-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57032-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 704E4288A14 for ; Wed, 7 Feb 2024 20:33:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0622912D168; Wed, 7 Feb 2024 20:31:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Kb2oXTnE" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E522212A140; Wed, 7 Feb 2024 20:31:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337906; cv=none; b=l1dbTGVfBu2+1H2SdADQKFQ+0yDcgWYdtyqPzQropeIJ3f4hg2Yx6VJVAviTZZgFT08b/PSO4XqDM34DNo0HO+IvmANdfLaDaYIBYFij20W1Rr0jtuIAee2ZWaww5t/zF8aF4oddtbaxVBzGqNfOawwVPUorQm7FUFnz0gDvqns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337906; c=relaxed/simple; bh=S70M2GZNqYMqWcESwYxGcEzhW76DUgm+kbbaqJbCpdk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LeALtH/y1Xj2C73EwkSpgd+DaoRE8hKm9cFnnLDIJAFPxQXmJbdtgo+/sqoM/yvEk12M0dE+jkbpObcxwz5pYaEPLUMU5UraTM/536O6QNYFS54aY9YCVzDp9bEkqW5d9AyH+btsSn9P0fhs8S34WYkLAHynLHh87ELKjyBn3/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Kb2oXTnE; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDF18C433C7; Wed, 7 Feb 2024 20:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337905; bh=S70M2GZNqYMqWcESwYxGcEzhW76DUgm+kbbaqJbCpdk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kb2oXTnE6R6F4wSlva8PlQVqRsigPUXebwXQrnwij+JJ0C0b0PBPFATsjYGBrIqnm 9Y2oe/yFwWE4brv0188AL9S1F+hjHdIUZm34lv7g1vdMpmwnxAMQhkPbKuQbS9GeEs ejyeJRqNduFrSwmajR92vuwy8PrCVze+OK1zr9vlBtzzAdpTxqOOa4VAiSUjTKq2YC 58Rkx9c6FpQ0lTpEpryr9vph4mf6eMRtpoOS5ZCfcJqfiLSG4GSBVZQwWw87gxDwuZ SOeM4Q42U078d5I4K5zVz+Y9XOZr/6+TQ8P2g3eOdcd9gStaXDIkw9JP1H5abEw1cb BjLSZNNWyOoXw== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Shuah Khan , damon@lists.linux.dev, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/8] selftests/damon: add a test for a race between target_ids_read() and dbgfs_before_terminate() Date: Wed, 7 Feb 2024 12:31:32 -0800 Message-Id: <20240207203134.69976-7-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240207203134.69976-1-sj@kernel.org> References: <20240207203134.69976-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790273668053765185 X-GMAIL-MSGID: 1790273668053765185 commit 34796417964b ("mm/damon/dbgfs: protect targets destructions with kdamond_lock") fixed a race of DAMON debugfs interface. Specifically, the race was happening between target_ids_read() and dbgfs_before_terminate(). Add a test for the issue to prevent the problem from accidentally recurring. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/.gitignore | 1 + tools/testing/selftests/damon/Makefile | 2 + ...fs_target_ids_read_before_terminate_race.c | 80 +++++++++++++++++++ ...s_target_ids_read_before_terminate_race.sh | 14 ++++ 4 files changed, 97 insertions(+) create mode 100644 tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.c create mode 100755 tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.sh diff --git a/tools/testing/selftests/damon/.gitignore b/tools/testing/selftests/damon/.gitignore index c6c2965a6607..7d6c6e062be7 100644 --- a/tools/testing/selftests/damon/.gitignore +++ b/tools/testing/selftests/damon/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only huge_count_read_write +debugfs_target_ids_read_before_terminate_race diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index b545fedafb3b..8a3a8df003db 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -2,6 +2,7 @@ # Makefile for damon selftests TEST_GEN_FILES += huge_count_read_write +TEST_GEN_FILES += debugfs_target_ids_read_before_terminate_race TEST_GEN_FILES += access_memory TEST_FILES = _chk_dependency.sh _debugfs_common.sh @@ -9,6 +10,7 @@ TEST_PROGS = debugfs_attrs.sh debugfs_schemes.sh debugfs_target_ids.sh TEST_PROGS += debugfs_empty_targets.sh debugfs_huge_count_read_write.sh TEST_PROGS += debugfs_duplicate_context_creation.sh TEST_PROGS += debugfs_rm_non_contexts.sh +TEST_PROGS += debugfs_target_ids_read_before_terminate_race.sh TEST_PROGS += sysfs.sh sysfs_update_removed_scheme_dir.sh TEST_PROGS += sysfs_update_schemes_tried_regions_hang.py TEST_PROGS += sysfs_update_schemes_tried_regions_wss_estimation.py diff --git a/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.c b/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.c new file mode 100644 index 000000000000..b06f52a8ce2d --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: SeongJae Park + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DBGFS_MONITOR_ON "/sys/kernel/debug/damon/monitor_on_DEPRECATED" +#define DBGFS_TARGET_IDS "/sys/kernel/debug/damon/target_ids" + +static void turn_damon_on_exit(void) +{ + int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR); + int monitor_on_fd = open(DBGFS_MONITOR_ON, O_RDWR); + char pid_str[128]; + + snprintf(pid_str, sizeof(pid_str), "%d", getpid()); + write(target_ids_fd, pid_str, sizeof(pid_str)); + write(monitor_on_fd, "on\n", 3); + close(target_ids_fd); + close(monitor_on_fd); + usleep(1000); + exit(0); +} + +static void try_race(void) +{ + int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR); + int pid = fork(); + int buf[256]; + + if (pid < 0) { + fprintf(stderr, "fork() failed\n"); + exit(1); + } + if (pid == 0) + turn_damon_on_exit(); + while (true) { + int status; + + read(target_ids_fd, buf, sizeof(buf)); + if (waitpid(-1, &status, WNOHANG) == pid) + break; + } + close(target_ids_fd); +} + +static inline uint64_t ts_to_ms(struct timespec *ts) +{ + return (uint64_t)ts->tv_sec * 1000 + (uint64_t)ts->tv_nsec / 1000000; +} + +int main(int argc, char *argv[]) +{ + struct timespec start_time, now; + int runtime_ms; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + runtime_ms = atoi(argv[1]); + clock_gettime(CLOCK_MONOTONIC, &start_time); + while (true) { + try_race(); + clock_gettime(CLOCK_MONOTONIC, &now); + if (ts_to_ms(&now) - ts_to_ms(&start_time) > runtime_ms) + break; + } + return 0; +} diff --git a/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.sh b/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.sh new file mode 100755 index 000000000000..fc793c4c9aea --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_read_before_terminate_race.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +dmesg -C + +./debugfs_target_ids_read_before_terminate_race 5000 + +if dmesg | grep -q dbgfs_target_ids_read +then + dmesg + exit 1 +else + exit 0 +fi