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: 13549053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA63DC4828F for ; Wed, 7 Feb 2024 20:31:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCB9C6B0080; Wed, 7 Feb 2024 15:31:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D538D6B0081; Wed, 7 Feb 2024 15:31:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BCED36B0082; Wed, 7 Feb 2024 15:31:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A9F356B0080 for ; Wed, 7 Feb 2024 15:31:48 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6FE5312048D for ; Wed, 7 Feb 2024 20:31:48 +0000 (UTC) X-FDA: 81766153896.22.72B1C99 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id AEE47140037 for ; Wed, 7 Feb 2024 20:31:46 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Kb2oXTnE; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707337906; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rqjq/CwbHCXgPEB18xIZCSBiYr8N6Pvdhm3Ak795qwk=; b=2aJEK54S2SeMsNTBzGv+kT5zSZvNZHpRu5tIMS+WfJtiR2XqVpwSFQsLLHn2j1ZbNEZVr7 IasajqUiHEnnMSs4qDvsqw9kFlpJx410Ww7TP+thlvViYfWbaq5BrqFX+fXh+axPOpJc06 2XbFcaeQhVjs8rXf3tsHA7Bzho/5Lo8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Kb2oXTnE; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707337906; a=rsa-sha256; cv=none; b=1JETHNlKhJxXs0F6b5SJJ6IFGxlivBbAJREAWuz/tRRMsUY9cmX9yHmgtIihqkOzxLbGa5 1gTQDKPlfPBKG7rCQ6qoqVMCGkqac+in2Va0hNPCBbwdX/hUVkAoAoaeWpmdkyoA02r2i1 UPoCKDdck1g6HDM2JZmkcbPqHCPlQkg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D25D561AC9; Wed, 7 Feb 2024 20:31:45 +0000 (UTC) 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> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: AEE47140037 X-Stat-Signature: uiegkuqn7gnokhw6bf8x4ozzofpc6w9z X-Rspam-User: X-HE-Tag: 1707337906-556839 X-HE-Meta: U2FsdGVkX1/o3RGLAQB5wPpTb9xycqH/fG5x69zggSpmn5FeE8ZfILAbyQU60cJeMMG/2wNAFe3/Vr5/AI2dwQFEPBtYwFLl8BvsyeTd/QXW2VkBmBCwB7to9hN4km/yAGhLcGtEamFlDz9vQGjJk/yBWe+Ej/YEphal888pwJ15rtNeyM83DI1NzoLuQdsVhxhzq/sM6YRgvPnpCiMN1uxfPU4G4awWtgZ6w2eYpGxDnlEfa9YnXXsbz1t8D5Dc4YOTzLkkhO2GhajqyNoAUpeD0ACCmNsmzOJQB4U1DYZRqA4KY1ntTASXuYI1q6RyVHcAa933PGoF4wI3qi4VqfteiZ2bh53cON7ijkABFtJ3RODDzpvLqJJ6cC+wzyamMRGAW1ejmbdLF0jIJVCkqh0VFndIotGZkAaeQTiVLy/XupOrCHmwG8+2yJHbJQL0pqsT4OV/ZlsIKeZrBDn0d/QV+R/AdPvEL+XxZmncd7MFSWLEclNbpYgkkyvglt+cjPL5if85cQoeui9J+o3t1OVaSnlD/IHUQEuckhd/J1462m3wiVuNtX077E8mOQESs9A5Ucg2vcmicUI+C6khHH9NqEfbe/Vbxj9j/RahbjHhmPXoWOsaz2uQ85l4Z5pggpO1eVOwvdfzwIbBll4SzKk7nMfT+yKoYHJyuE90EI1KlfemkxXq9xyEN7fE8xlMCnHNf8eVd5PMbBd09ds6kYUgjjeFUvucNxW2TcWEY0ieugeKUf/cbCTviHGT9x6T0EGakU7IiLEYtVJ5TTD6xP28PFr4qMX05CHC6UHZa2LZVWpNc2K2M9R1aEkORtj3KKQLwZ/OT77eEp522231cc58O1m1+DBlnhVU0LFmUQ1hh9xmkh114cd9YqNIkXFYhLeM5+xRq8M1LIe5bzyrQLQBixY4t6qPVceyQJ//Mrm7mgaJuZA/B3spjZrN6gPjVTr1BoogI52Y9btEOcc OmkXG9C5 zzGiOd6O1vE60+7NyRtc/8NLGDbNEClHEOJCvCogg4zUSINeSyisrWmG9EojT0J8yhhu3yscfR6oxMKfJMna8ASgcWH/SRRBVlax8vlGNDDV4xs7qUyBSuAC/KU2u7tiL8FybhqbobkX2gA3kPZR/GsTKLBA8ejRrYLs78nlWweqLmVTP6Ip15EbaG1Q80zRi37HMO8CPlNYwxUdWNJXfhpaSkQODQSJ1J5tVaDk6cjk+0UpcGU/+OWl7Z8pWdJtRqx5cIrIu9uoUNhFuEeD3sH/n9sgwYncNaViD X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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