From patchwork Wed Feb 7 20:31:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 771701 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 DB3B01DFCF; Wed, 7 Feb 2024 20:31:41 +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=1707337902; cv=none; b=Y5ptnmAGeCePnxTmXtRU8u8RyVkGEjksqNZlfqFICbHsfKxU9ik/PTMZZzRxzhDZ4POb3wsQ4F+Iq8BBWDklO6DbGvQMuGujjgr6yUnHq74vhYW67VoGZiqE87Ie7PubU6rNnP37Nchog1uYkVQYsnKDfWlPAGCzfrMcod/67M0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337902; c=relaxed/simple; bh=JQERS+AD7eJcXPmEIi+Gcjav4/4TJSgTRdHhSGiTeC0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a48RB+kQZnQ9//V/P3YOPiUZX8bp9UlB4FP2BgHyWRkecDdXk6kDaSBt1Kh0RnHjH8Bw6j9KtvUN7iDDbFxTgfZu+XEpMn+wqu6KeWa9BdmJ/pP1+QcGuT7e33Bp49W8bCf8zCJsOvDQZvku7OfJO2tC655xKmqXh/I8zegAipo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lx8mnNCB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lx8mnNCB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27297C43390; Wed, 7 Feb 2024 20:31:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337901; bh=JQERS+AD7eJcXPmEIi+Gcjav4/4TJSgTRdHhSGiTeC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lx8mnNCB3FWWVgO2+jRkTgucZGTLeSzoBIZ/EFuBxNFxGuPQ0D8MFrtTw7iwrE1Na aFQEjvUUK4Yev4Dy6G++TsGO2kIx4pcqN7D3BLsUKIALU3zTZalx1xNnZcvJZPrvsF H8gT8OKhTA4A5tPW6EPXK+ZEpjk546ZmS104vhzh4148PSKRj4FOzL6qNZZDwX/Vyh kMtuD1aZQm/PWZwzbxHibyvmi6bSLIQiPQhoQniGkMI61KXBripkvkHUukfFQGYqvm AlXLfX0IgWrj5f4KRCZMdu1nEmvG5U82kPY3ZAZt+zPUu8QkR+FMe1jcOaIyNuRbYm +QowLnrkH/NMQ== 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 1/8] selftests/damon/_damon_sysfs: support DAMOS quota Date: Wed, 7 Feb 2024 12:31:27 -0800 Message-Id: <20240207203134.69976-2-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Update the test-purpose DAMON sysfs control Python module to support DAMOS quota. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/_damon_sysfs.py | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/damon/_damon_sysfs.py b/tools/testing/selftests/damon/_damon_sysfs.py index e98cf4b6a4b7..b4f6e385c564 100644 --- a/tools/testing/selftests/damon/_damon_sysfs.py +++ b/tools/testing/selftests/damon/_damon_sysfs.py @@ -70,18 +70,48 @@ class DamosAccessPattern: if err != None: return err +class DamosQuota: + sz = None # size quota, in bytes + ms = None # time quota + reset_interval_ms = None # quota reset interval + scheme = None # owner scheme + + def __init__(self, sz=0, ms=0, reset_interval_ms=0): + self.sz = sz + self.ms = ms + self.reset_interval_ms = reset_interval_ms + + def sysfs_dir(self): + return os.path.join(self.scheme.sysfs_dir(), 'quotas') + + def stage(self): + err = write_file(os.path.join(self.sysfs_dir(), 'bytes'), self.sz) + if err != None: + return err + err = write_file(os.path.join(self.sysfs_dir(), 'ms'), self.ms) + if err != None: + return err + err = write_file(os.path.join(self.sysfs_dir(), 'reset_interval_ms'), + self.reset_interval_ms) + if err != None: + return err + class Damos: action = None access_pattern = None - # todo: Support quotas, watermarks, stats, tried_regions + quota = None + # todo: Support watermarks, stats, tried_regions idx = None context = None tried_bytes = None - def __init__(self, action='stat', access_pattern=DamosAccessPattern()): + def __init__(self, action='stat', access_pattern=DamosAccessPattern(), + quota=DamosQuota()): self.action = action self.access_pattern = access_pattern self.access_pattern.scheme = self + self.quota = quota + self.quota.scheme = self def sysfs_dir(self): return os.path.join( @@ -94,13 +124,7 @@ class Damos: err = self.access_pattern.stage() if err != None: return err - - # disable quotas - err = write_file(os.path.join(self.sysfs_dir(), 'quotas', 'ms'), '0') - if err != None: - return err - err = write_file( - os.path.join(self.sysfs_dir(), 'quotas', 'bytes'), '0') + err = self.quota.stage() if err != None: return err From patchwork Wed Feb 7 20:31:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 770707 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 0A31B76039; Wed, 7 Feb 2024 20:31:42 +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=1707337903; cv=none; b=dvfrwedygiv5+6KCTzhCxKmQ8xA0VYgLya953AQehl0DfIAc9AVzDArJLCVva6PHGwL9QxsQcq8hE48etBbo82gSCfVgyuOSiI3yfwiBnAnG/aM9qJre1mZOg/5Tw9viPcFMtlh4vCTQM4VTUjPxJFFpoNNd63GInZLKvphzSmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337903; c=relaxed/simple; bh=SwdeOXExaKQGPJVJ2NaT/lednGj9CW7yt27zl0ck87s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ecfLvRA1TEPEQhSQSWbXbCecjgtRQ7W0AuPau8kYu3ox5XFJSQtB0Iv7z/aaG8S6ePyd+ZPi+u3B+VWZYRfHLxsHVhG8S25syQbBrWErKYJKcsIBCESnUPI1r0D5lr5+SS1MED4+jRtiQujQRxFCbA5JJ8HgRtv5aWj/aJbI8/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=XdziAUn9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XdziAUn9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA011C433A6; Wed, 7 Feb 2024 20:31:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337902; bh=SwdeOXExaKQGPJVJ2NaT/lednGj9CW7yt27zl0ck87s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XdziAUn989qZYGouOke5VJZROLyvwJMYpwiWYmq66BUEXenv4+YEFtDUWAJ+A9e46 4RcITM/aROpBnV+7byqwfQVSsuyZPtFLLVl+f8ctcCaXnoDBxj77Kj7brTuY7MxQzf 6NO9ac8gHvUjq74gTlJ4q47izd+Hf6ebBXti6tL+WXPNCl37Qsm8WS036hCLnt6xHx 4B0sBDrpLG1W2uOj69vF8f5540kveMiYpR8ER1drvalND7Cz/HlPbut0+TCVD/d7yH EAs7LM4X6EwP+4OXCl44FY2ZUcPhdn5aGcueatWZCkqpFk+6X66NXx0kXHlRCjKbgK O5Yym670dAtVA== 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 2/8] selftests/damon/_damon_sysfs: support DAMOS stats Date: Wed, 7 Feb 2024 12:31:28 -0800 Message-Id: <20240207203134.69976-3-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Update the test-purpose DAMON sysfs control Python module to support DAMOS stats. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/_damon_sysfs.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/testing/selftests/damon/_damon_sysfs.py b/tools/testing/selftests/damon/_damon_sysfs.py index b4f6e385c564..a75244451684 100644 --- a/tools/testing/selftests/damon/_damon_sysfs.py +++ b/tools/testing/selftests/damon/_damon_sysfs.py @@ -96,6 +96,20 @@ class DamosQuota: if err != None: return err +class DamosStats: + nr_tried = None + sz_tried = None + nr_applied = None + sz_applied = None + qt_exceeds = None + + def __init__(self, nr_tried, sz_tried, nr_applied, sz_applied, qt_exceeds): + self.nr_tried = nr_tried + self.sz_tried = sz_tried + self.nr_applied = nr_applied + self.sz_applied = sz_applied + self.qt_exceeds = qt_exceeds + class Damos: action = None access_pattern = None @@ -104,6 +118,7 @@ class Damos: idx = None context = None tried_bytes = None + stats = None def __init__(self, action='stat', access_pattern=DamosAccessPattern(), quota=DamosQuota()): @@ -322,6 +337,23 @@ class Kdamond: return err scheme.tried_bytes = int(content) + def update_schemes_stats(self): + err = write_file(os.path.join(self.sysfs_dir(), 'state'), + 'update_schemes_stats') + if err != None: + return err + for context in self.contexts: + for scheme in context.schemes: + stat_values = [] + for stat in ['nr_tried', 'sz_tried', 'nr_applied', + 'sz_applied', 'qt_exceeds']: + content, err = read_file( + os.path.join(scheme.sysfs_dir(), 'stats', stat)) + if err != None: + return err + stat_values.append(int(content)) + scheme.stats = DamosStats(*stat_values) + class Kdamonds: kdamonds = [] From patchwork Wed Feb 7 20:31:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 771700 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 ACF48127B6A; Wed, 7 Feb 2024 20:31:43 +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=1707337903; cv=none; b=ReMf41TxJt02g82c2JRvTso401EjwAhHDCMVEmqzBMv0pFNs/0pcIiVjTVRLUzySmPRr2IpQTtqX+y3SzQlGU0FGWmIPm7yd8D6UvBRRDmoTa+RGIl0iQYEYvTmN3n3o2+kfUa0xd4MYc30bbq/7+78CMaJ8fxFEgToz8UouPws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337903; c=relaxed/simple; bh=9lLmi3bov7hQSKSz6Wm19r7FnAY+g94q4B/k/Jgsk9k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZFGEBVLFnu2VjfV2vR7xw8Js9krah8gzCdvnU/21bDhhF6bgdNT7FZHAVAjSVrcmUXyFg/abJ8M+otrhYKH7U5DSgq+lrXUqIV7qIrrdNZQFFVFhjKrH5mnF/rQAwg08RpS6cdFhoZC+J7/cPuyw+MBG6YCq5l/0Z1UQ3TTiErU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fi0iIujP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fi0iIujP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8F1EC43141; Wed, 7 Feb 2024 20:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337903; bh=9lLmi3bov7hQSKSz6Wm19r7FnAY+g94q4B/k/Jgsk9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fi0iIujPz1cj5u9k8DV6O6k6jYHVEQy6PRbsmappiAAOUQnYijCrUO8m+mPpuZQ/X /Qo1XhekVUHY0pnANoHsgiT0y8eK/BMSLC0y9cvyLOUdXjSE4Z5pGSRSMoGYHhyAim 6ciDGu9LMatd++0cRpr5guC30UwMHR2ZkSRBnXrH+P2hqXseTOV9V0AYm4ptQ+1oR8 WyrAKtkKkKNbHEftXNZBqMKYhKIrCO073GUohgJLzwsRXP5cFQ37mbbhO3MOD+Hjea vWnP8dF05TL/cauWKBUB90aZFB1nilt2afMb5jOh3Dw+y0IppRqhHOpKyNog663RqN X4qIwvmvgRO/A== 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 3/8] selftests/damon/_damon_sysfs: support DAMOS apply interval Date: Wed, 7 Feb 2024 12:31:29 -0800 Message-Id: <20240207203134.69976-4-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Update the test-purpose DAMON sysfs control Python module to support DAMOS apply interval. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/_damon_sysfs.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/damon/_damon_sysfs.py b/tools/testing/selftests/damon/_damon_sysfs.py index a75244451684..d23d7398a27a 100644 --- a/tools/testing/selftests/damon/_damon_sysfs.py +++ b/tools/testing/selftests/damon/_damon_sysfs.py @@ -114,6 +114,7 @@ class Damos: action = None access_pattern = None quota = None + apply_interval_us = None # todo: Support watermarks, stats, tried_regions idx = None context = None @@ -121,12 +122,13 @@ class Damos: stats = None def __init__(self, action='stat', access_pattern=DamosAccessPattern(), - quota=DamosQuota()): + quota=DamosQuota(), apply_interval_us=0): self.action = action self.access_pattern = access_pattern self.access_pattern.scheme = self self.quota = quota self.quota.scheme = self + self.apply_interval_us = apply_interval_us def sysfs_dir(self): return os.path.join( @@ -139,6 +141,11 @@ class Damos: err = self.access_pattern.stage() if err != None: return err + err = write_file(os.path.join(self.sysfs_dir(), 'apply_interval_us'), + '%d' % self.apply_interval_us) + if err != None: + return err + err = self.quota.stage() if err != None: return err From patchwork Wed Feb 7 20:31:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 770706 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 7903012881A; Wed, 7 Feb 2024 20:31:44 +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=1707337904; cv=none; b=ITU4Vo46XdOZgVOjjUCowmSgklietYGtXcOt01sLPG7/v2dPeR0cp9wfA/7Oq4cZ3OsqleZVpvOD3cRq+mXP/QuKueSF5mMDX2acYGlfPmLkBDgq8OVOWdQzYanHC022gJmpRUGYXS3i2j7KAazcFtZ/aGVJKpndxBEyTdz0ex0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337904; c=relaxed/simple; bh=tz9uTb84Z42Yizp5Acw7rjIK4AMaxjqMzKQOqT/pm1M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N/7h+7c4CkVjmy8ecwRqas+Ush5ZWxiMb1lKWqmF+9/PUkOZ9CVkSqFbP2nGMqdniYJiokikrGLMB/WD5rhCsogl5tJ6E9u3NPkx/AqoNRNX8sN7jct0hmmxQQaAdGZaVssytySOhgmwKt4uAfM8w1RxaSl0t6pfuqWPqongbMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DJJijCfm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DJJijCfm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68A8FC43390; Wed, 7 Feb 2024 20:31:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337904; bh=tz9uTb84Z42Yizp5Acw7rjIK4AMaxjqMzKQOqT/pm1M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DJJijCfmw4GrlAMLBl2nP9yduJUcln0mxAT/q50sI+tVVLy2pIDCclwCxcNfCEWOG 3uKNB+jqzsufuPDqf9L25wuWkXRB3clhwftLcRVBOtVOUEmEO9S8Q181dKzi37GUII aqtGfAyzwMiz0iE/qwfGcVHcQG99augNwfn8zblNS3xRUJtTnuyvWkcp3DDkMVQTLs 2F6ltet+/rUSxJuNO9aQjEUVy5vntT50wmjVCQ5ejj5fIqXFLj17s8lHTiCjCMpop+ bFWBaRWR7yfvnKot97njjwyosyXavwoeSgfyjAKNiOO1EH1z03lsyKV/Ew16tLZBML aePPCZfS9eubA== 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 4/8] selftests/damon: add a test for DAMOS quota Date: Wed, 7 Feb 2024 12:31:30 -0800 Message-Id: <20240207203134.69976-5-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a selftest for verifying the DAMOS quota feature. The test is very similar to sysfs_update_schemes_tried_regions_wss_estimation.py. It starts an artificial workload of 20 MiB working set, run DAMON to find the working set size, but with 1 MiB/100 ms size quota. Then, it collect the DAMON-found working set size every 100 ms and check if the quota was always applied as expected. For the confirmation, the tests shows the stat-applied region size and the qt_exceeds stat. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/Makefile | 1 + tools/testing/selftests/damon/damos_quota.py | 67 ++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 tools/testing/selftests/damon/damos_quota.py diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index 8a1cc2bf1864..9c3783f1a39d 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -12,6 +12,7 @@ TEST_PROGS += debugfs_rm_non_contexts.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 +TEST_PROGS += damos_quota.py TEST_PROGS += reclaim.sh lru_sort.sh include ../lib.mk diff --git a/tools/testing/selftests/damon/damos_quota.py b/tools/testing/selftests/damon/damos_quota.py new file mode 100755 index 000000000000..7d4c6bb2e3cd --- /dev/null +++ b/tools/testing/selftests/damon/damos_quota.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import subprocess +import time + +import _damon_sysfs + +def main(): + # access two 10 MiB memory regions, 2 second per each + sz_region = 10 * 1024 * 1024 + proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000']) + + # Set quota up to 1 MiB per 100 ms + sz_quota = 1024 * 1024 # 1 MiB + quota_reset_interval = 100 # 100 ms + kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond( + contexts=[_damon_sysfs.DamonCtx( + ops='vaddr', + targets=[_damon_sysfs.DamonTarget(pid=proc.pid)], + schemes=[_damon_sysfs.Damos( + access_pattern=_damon_sysfs.DamosAccessPattern( + # >= 25% access rate, >= 200ms age + nr_accesses=[5, 20], age=[2, 2**64 - 1]), + quota=_damon_sysfs.DamosQuota( + sz=sz_quota, reset_interval_ms=quota_reset_interval) + )] # schemes + )] # contexts + )]) # kdamonds + + err = kdamonds.start() + if err != None: + print('kdamond start failed: %s' % err) + exit(1) + + wss_collected = [] + nr_quota_exceeds = 0 + while proc.poll() == None: + time.sleep(0.1) + err = kdamonds.kdamonds[0].update_schemes_tried_bytes() + if err != None: + print('tried bytes update failed: %s' % err) + exit(1) + err = kdamonds.kdamonds[0].update_schemes_stats() + if err != None: + print('stats update failed: %s' % err) + exit(1) + + scheme = kdamonds.kdamonds[0].contexts[0].schemes[0] + wss_collected.append(scheme.tried_bytes) + nr_quota_exceeds = scheme.stats.qt_exceeds + + wss_collected.sort() + for wss in wss_collected: + if wss > sz_quota: + print('quota is not kept: %s > %s' % (wss, sz_quota)) + print('collected samples are as below') + print('\n'.join(['%d' % wss for wss in wss_collected])) + exit(1) + + if nr_quota_exceeds < len(wss_collected): + print('quota is not always exceeded: %d > %d' % + (len(wss_collected), nr_quota_exceeds)) + exit(1) + +if __name__ == '__main__': + main() From patchwork Wed Feb 7 20:31:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 771699 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 55337129A68; Wed, 7 Feb 2024 20:31:44 +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=1707337905; cv=none; b=awO4IAA7rrCQJGXGv+rhbdSqduL24SZfTY9EHHeof/iAlJn40jU2JviX8innoJbCh9Vu9Tw5MQKhWmS3Ceql+i08C3ul7ph7W5ml9UJ6zYxmXPHcfVlWmXbmbl2xkZsAm+vQJ9+s1QoJhxEass8L0P8EZf9Rh0ygQCJhH+DqNlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337905; c=relaxed/simple; bh=eTfAT8I3Zrsng6f7SuRxQKWrP7lIEivzKE3OpfIwwxI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NNNZ/v6HpOXAaHXJM3QzUFepWuf/A15ZJo7OyimKO9JELZ03TPL00eWZ+nVp1J+sqOaxyKmllJ3fsQPso9dkFu3+KaNz/CAOiyotME3GfWZF/DDW8THfaW4sWIZm6n3UgVHBVYlL0iPdsssUAWI4G+5qemv5DPXKolLuEiqHk+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JKgp/zT8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JKgp/zT8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 374E9C4166D; 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=1707337904; bh=eTfAT8I3Zrsng6f7SuRxQKWrP7lIEivzKE3OpfIwwxI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JKgp/zT8soGxMNVpEjgN4T11q/PH3dSEMmOCuxrZk1KHb+x4aO+vHKx3WghdJTjjI U1vxJoRUqSA7K4mUYmEwBW/w7cVZ/AzEVXOVi2WJGSqr85PnQHEVDdbCphy53Hiobe ewQwBW7Pj5BnELlzpz1x6bHuaUUXU1pXc7qQN322bFOVRh4Xb0IFrPID3NY1DlUp7b SnTkVHx91YfY8ZxZosB/ENWyTggk4AGpTLXidmrJmcjX6q21CdrSENztmqzzCi4WBs AUnNKvucq+cG0+WD5BN7QIV7fUw/7zlR91K8CILvWsb1THqfz3lx4WtJWh7OFb7FSi 3SpnJ3QJj9rXQ== 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 5/8] selftests/damon: add a test for DAMOS apply intervals Date: Wed, 7 Feb 2024 12:31:31 -0800 Message-Id: <20240207203134.69976-6-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a selftest for DAMOS apply intervals. It runs two schemes having different apply interval agains an artificial memory access workload, and check if the scheme with smaller apply interval was applied more frequently. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/Makefile | 2 +- .../selftests/damon/damos_apply_interval.py | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/damon/damos_apply_interval.py diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index 9c3783f1a39d..b545fedafb3b 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -12,7 +12,7 @@ TEST_PROGS += debugfs_rm_non_contexts.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 -TEST_PROGS += damos_quota.py +TEST_PROGS += damos_quota.py damos_apply_interval.py TEST_PROGS += reclaim.sh lru_sort.sh include ../lib.mk diff --git a/tools/testing/selftests/damon/damos_apply_interval.py b/tools/testing/selftests/damon/damos_apply_interval.py new file mode 100755 index 000000000000..f04d43702481 --- /dev/null +++ b/tools/testing/selftests/damon/damos_apply_interval.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import subprocess +import time + +import _damon_sysfs + +def main(): + # access two 10 MiB memory regions, 2 second per each + sz_region = 10 * 1024 * 1024 + proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000']) + + # Set quota up to 1 MiB per 100 ms + kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond( + contexts=[_damon_sysfs.DamonCtx( + ops='vaddr', + targets=[_damon_sysfs.DamonTarget(pid=proc.pid)], + schemes=[ + _damon_sysfs.Damos( + access_pattern=_damon_sysfs.DamosAccessPattern( + # >= 25% access rate, >= 200ms age + nr_accesses=[5, 20], age=[2, 2**64 - 1]), + # aggregation interval (100 ms) is used + apply_interval_us=0), + # use 10ms apply interval + _damon_sysfs.Damos( + access_pattern=_damon_sysfs.DamosAccessPattern( + # >= 25% access rate, >= 200ms age + nr_accesses=[5, 20], age=[2, 2**64 - 1]), + # explicitly set 10 ms apply interval + apply_interval_us=10 * 1000) + ] # schemes + )] # contexts + )]) # kdamonds + + err = kdamonds.start() + if err != None: + print('kdamond start failed: %s' % err) + exit(1) + + wss_collected = [] + nr_quota_exceeds = 0 + while proc.poll() == None: + time.sleep(0.1) + err = kdamonds.kdamonds[0].update_schemes_stats() + if err != None: + print('stats update failed: %s' % err) + exit(1) + schemes = kdamonds.kdamonds[0].contexts[0].schemes + nr_tried_stats = [s.stats.nr_tried for s in schemes] + if nr_tried_stats[0] == 0 or nr_tried_stats[1] == 0: + print('scheme(s) are not tried') + exit(1) + + # Because the second scheme was having the apply interval that is ten times + # lower than that of the first scheme, the second scheme should be tried + # about ten times more frequently than the first scheme. For possible + # timing errors, check if it was at least nine times more freuqnetly tried. + ratio = nr_tried_stats[1] / nr_tried_stats[0] + if ratio < 9: + print('%d / %d = %f (< 9)' % + (nr_tried_stats[1], nr_tried_stats[0], ratio)) + exit(1) + +if __name__ == '__main__': + main() 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: 770705 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Kb2oXTnE" 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 From patchwork Wed Feb 7 20:31:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 771698 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 B668E12AAF6; Wed, 7 Feb 2024 20:31:46 +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=FpN/Y+Dbp93F6yCbjnWwAonJiA1I6SgiE84hsOYbUTu8YFg9mu8LMLJo1usvpT72/CypjwdJg5gaYLVAYFtRXeuGmLlvXE9wgM07j4h1Cafb2s/E9JkjASsCqo5sFOsJxnAPKnGvOeWNZlBJSTTT1a6uzjkesfx0vYjVrFWnul8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337906; c=relaxed/simple; bh=hilY7j3dbTrTcHQNZ5cH9hllN5vVoaZDnsr9bRpaZZI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZBGxHFtfjyI5itmAHYVftt62DVXZP7vD23DVCBbnsVI7FkImex4WfYF/gPR+X+EzI8eOnlCXa7TUGsnoH0QhwYR4lUwnWXNREsXEWjZPiDBkvG5jdmygQJawY0SSOcY5JUiIsa6z3B2INiZUIdBqmpjDrzvy3C1qGxeUNCGBK+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ou2UWhF7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ou2UWhF7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7C72C43601; Wed, 7 Feb 2024 20:31:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337906; bh=hilY7j3dbTrTcHQNZ5cH9hllN5vVoaZDnsr9bRpaZZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ou2UWhF7rKY/SOJPhENMHQfU07MoTtvz7MvwRRiYyi+LjIzxD5rbLHTVaXLNHZNR9 lFR6S9udTLIzBtTPoxRQGoEdVNalxZkr/elBolMd/SfH5QFGWjwos7+/sf2oAiOq3O a6wJNdJoaYJLL6Pv7h+oUyC+TN00MUk5ipl7S00Kd4NrisZHeGUSddyvn2IdWwCrMQ hdF+ZGVnRCq6QPicoYIQAGTLPPsGo5wDuRIkS4FTPC6TdAnAI6C8OEOEiHLUXNw9HY rZHgPKwBjJ/k5VGnsnvlP2zcb9HXC8V/mPlVuA6CTPFbN9CplEBU2O9p3H+PGt2+3J 356gekiMFzpVg== 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 7/8] selftests/damon: add a test for the pid leak of dbgfs_target_ids_write() Date: Wed, 7 Feb 2024 12:31:33 -0800 Message-Id: <20240207203134.69976-8-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Commit ebb3f994dd92 ("mm/damon/dbgfs: fix 'struct pid' leaks in 'dbgfs_target_ids_write()'") fixes a pid leak bug in DAMON debugfs interface, namely dbgfs_target_ids_write() function. Add a selftest for the issue to prevent the problem from mistakenly recurring. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/.gitignore | 1 + tools/testing/selftests/damon/Makefile | 2 + .../damon/debugfs_target_ids_pid_leak.c | 68 +++++++++++++++++++ .../damon/debugfs_target_ids_pid_leak.sh | 22 ++++++ 4 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c create mode 100755 tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh diff --git a/tools/testing/selftests/damon/.gitignore b/tools/testing/selftests/damon/.gitignore index 7d6c6e062be7..d861701f0327 100644 --- a/tools/testing/selftests/damon/.gitignore +++ b/tools/testing/selftests/damon/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only huge_count_read_write debugfs_target_ids_read_before_terminate_race +debugfs_target_ids_pid_leak diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index 8a3a8df003db..789d6949c247 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -3,6 +3,7 @@ TEST_GEN_FILES += huge_count_read_write TEST_GEN_FILES += debugfs_target_ids_read_before_terminate_race +TEST_GEN_FILES += debugfs_target_ids_pid_leak TEST_GEN_FILES += access_memory TEST_FILES = _chk_dependency.sh _debugfs_common.sh @@ -11,6 +12,7 @@ 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 += debugfs_target_ids_pid_leak.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_pid_leak.c b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c new file mode 100644 index 000000000000..0cc2eef7d142 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: SeongJae Park + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DBGFS_TARGET_IDS "/sys/kernel/debug/damon/target_ids" + +static void write_targetid_exit(void) +{ + int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR); + char pid_str[128]; + + snprintf(pid_str, sizeof(pid_str), "%d", getpid()); + write(target_ids_fd, pid_str, sizeof(pid_str)); + close(target_ids_fd); + exit(0); +} + +unsigned long msec_timestamp(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000UL + tv.tv_usec / 1000; +} + +int main(int argc, char *argv[]) +{ + unsigned long start_ms; + int time_to_run, nr_forks = 0; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + time_to_run = atoi(argv[1]); + + start_ms = msec_timestamp(); + while (true) { + int pid = fork(); + + if (pid < 0) { + fprintf(stderr, "fork() failed\n"); + exit(1); + } + if (pid == 0) + write_targetid_exit(); + wait(NULL); + nr_forks++; + + if (msec_timestamp() - start_ms > time_to_run) + break; + } + printf("%d\n", nr_forks); + return 0; +} diff --git a/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh new file mode 100755 index 000000000000..31fe33c2b032 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_target_ids_pid_leak.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +before=$(grep "^pid " /proc/slabinfo | awk '{print $2}') + +nr_leaks=$(./debugfs_target_ids_pid_leak 1000) +expected_after_max=$((before + nr_leaks / 2)) + +after=$(grep "^pid " /proc/slabinfo | awk '{print $2}') + +echo > /sys/kernel/debug/damon/target_ids + +echo "tried $nr_leaks pid leak" +echo "number of active pid slabs: $before -> $after" +echo "(up to $expected_after_max expected)" +if [ $after -gt $expected_after_max ] +then + echo "maybe pids are leaking" + exit 1 +else + exit 0 +fi From patchwork Wed Feb 7 20:31:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 770704 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 7F23512B17F; Wed, 7 Feb 2024 20:31:47 +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=1707337907; cv=none; b=Ld9jZNUPmjqGaUia2x5O3i7BSZLejcelEe9MRHOibqQVs0VtZgQROFBSZxV9g9BmEjQFar9DQNXqUCWQF4zO0v4okCMOHHyz8IJlMeg0DuYO+xPIvoonZXW4VhXpITWodXkSuRlW7Pipq2qhdo5SVw94iREazeEfTTR2YCm83uM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337907; c=relaxed/simple; bh=bo48Sw4QueorxjSh+Ck9B6Mrnc7pi7WnA/Re94O6ZTo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UkGcCbrRz3Bmwg9aO8HjpxCS5sKst6esVmfucd2uAw28n3x1loMo05ivyXZzxat9uCOAPgm3W1Ja6aY66E9INT6FMP/3MXBzZ54uUQZPIE9OmVDAsjpF5e80ChL0I1nmop6eBjPnyaby7X2yLGCzReWu8pAXxBCQ1GLrkVNaWIg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bZjKuePx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bZjKuePx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BA0CC43142; Wed, 7 Feb 2024 20:31:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707337907; bh=bo48Sw4QueorxjSh+Ck9B6Mrnc7pi7WnA/Re94O6ZTo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bZjKuePxNwzjVi/Qv2DXVeStIOUrZJHJYuqVTzb+TgaleSHCdDc57bdrDp2DxAX3q tRduOaVNfL1p6H56MRwykS5lAAwln08xpqJTJn8U1LGaVj6v8Db2XM9nwyrpve4Yu8 zMqsG+ifH1Z5Zo9j7NpyGkqJBhe1nZv2ZrGOSUQxJrqtNZYdBHNOIoDC4yEUOrdBDU TWkYBjx1+scE9tW43zJLFEApIyP2qJQRoBwtAzZMKV3eaqJHrkrFio8N8hlKJTpB4Y 2PAJKPUeIiNECiNtD0y1VJ5z3hBfdLm+eqHH24vbuI+smXyAFJaXUuwPzkLoORW1rb vBYeGFwVYMdZg== 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 8/8] selftests/damon/_chk_dependency: get debugfs mount point from /proc/mounts Date: Wed, 7 Feb 2024 12:31:34 -0800 Message-Id: <20240207203134.69976-9-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DAMON debugfs selftests dependency checker assumes debugfs would be mounted at /sys/kernel/debug. That would be ok for many cases, but some systems might mounted the file system on some different places. Parse the real mount point using /proc/mounts file. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/_chk_dependency.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/damon/_chk_dependency.sh b/tools/testing/selftests/damon/_chk_dependency.sh index 350f8c2b071d..dda3a87dc00a 100644 --- a/tools/testing/selftests/damon/_chk_dependency.sh +++ b/tools/testing/selftests/damon/_chk_dependency.sh @@ -4,7 +4,14 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 -DBGFS=/sys/kernel/debug/damon +DBGFS=$(grep debugfs /proc/mounts --max-count 1 | awk '{print $2}') +if [ "$DBGFS" = "" ] +then + echo "debugfs not mounted" + exit $ksft_skip +fi + +DBGFS+="/damon" if [ $EUID -ne 0 ]; then