From mboxrd@z Thu Jan 1 00:00:00 1970 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 260FB2F5A12; Mon, 6 Apr 2026 14:20:22 +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=1775485223; cv=none; b=rEbw5i/ryZaV+Vd9loni+o8q56UHgUNg56kmvAQy4E49C5JRcMXFsbqAlOe2sApw9JfOex5MR5H2l5eCQNpfp7nmcalF6KdfF4keWD5WR5+yhp4MPjye8PmrFdOyWW1eF7oAksBGN1hKJ8am4zlYRhijX+BE9JF9y37XAtIFM2A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775485223; c=relaxed/simple; bh=R/W5pZYTUPbeFRpIiJm6n8WLPptkA9w+3mZt5IDX8BM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JD90Keu7Hy8BIe6Wr24+D1/NQxIz4T2lk39xtk++1Oujd1PADP4WbMT8txQ/il2+tzQPbxv5EE1r6icfxBCEFCXLGkTAOQHEmkh5R4JiZa2h3Qipty4dMWFmqXZiAN1l1oOlIZ3WATuJOtg2ZR9z1p8kUXB2hSydgWtXqCakzv8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e6vHFPRT; 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="e6vHFPRT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83F93C4CEF7; Mon, 6 Apr 2026 14:20:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775485222; bh=R/W5pZYTUPbeFRpIiJm6n8WLPptkA9w+3mZt5IDX8BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e6vHFPRTbwOhOPl/alglbws9iwwub5pqUgDyGZ/2xxJOmb/eULHZ5c6uM0OJrQ732 YJCqNf+S14p4OA1PP74A1tGBTSJQERtAHG2p1IpDxZb62ViZLqGaeXd+c7j7Ze2Nfz WkOacirHsCokvNz9FmfAYc0DwuvhdL7XvBV/EDeDnQVT+75MrKCTMCT6ZxdA2izz0C rMo6sG3Se1zFLkr4fv/7ExXcGeBDnlr+bK4yQjoIo23l28H88/rITX8lKt+1+LiDv4 Vs9QujcUUVxrC4ff/F6rU7Nkq6ERu4iV/CRWzCrbduXWx2UnlLu77VKTVoAwTd/4x8 C3mP34pt6V/tw== From: Mike Rapoport To: Andrew Morton , David Hildenbrand Cc: Baolin Wang , Barry Song , Dev Jain , Jason Gunthorpe , John Hubbard , "Liam R. Howlett" , Lance Yang , Leon Romanovsky , Lorenzo Stoakes , Mark Brown , Michal Hocko , Mike Rapoport , Nico Pache , Peter Xu , Ryan Roberts , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , Zi Yan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 27/53] selftests/mm: move read_file(), read_num() and write_num() to vm_util Date: Mon, 6 Apr 2026 17:17:09 +0300 Message-ID: <20260406141735.2179309-28-rppt@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260406141735.2179309-1-rppt@kernel.org> References: <20260406141735.2179309-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Mike Rapoport (Microsoft)" These are useful helpers for writing and reading sysfs and proc files. Make them available to the tests that don't use thp_settings. While on it make write_num() use "%lu" instead of "%ld" to match 'unsigned long num' argument type. Signed-off-by: Mike Rapoport (Microsoft) --- .../testing/selftests/mm/hugepage_settings.c | 41 ------------------- .../testing/selftests/mm/hugepage_settings.h | 4 -- tools/testing/selftests/mm/vm_util.c | 39 ++++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 3 ++ 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c index 3eef87e812ba..339802ecc8e4 100644 --- a/tools/testing/selftests/mm/hugepage_settings.c +++ b/tools/testing/selftests/mm/hugepage_settings.c @@ -48,47 +48,6 @@ static const char * const shmem_enabled_strings[] = { NULL }; -int read_file(const char *path, char *buf, size_t buflen) -{ - int fd; - ssize_t numread; - - fd = open(path, O_RDONLY); - if (fd == -1) - return 0; - - numread = read(fd, buf, buflen - 1); - if (numread < 1) { - close(fd); - return 0; - } - - buf[numread] = '\0'; - close(fd); - - return (unsigned int) numread; -} - -unsigned long read_num(const char *path) -{ - char buf[21]; - - if (read_file(path, buf, sizeof(buf)) < 0) { - perror("read_file()"); - exit(EXIT_FAILURE); - } - - return strtoul(buf, NULL, 10); -} - -void write_num(const char *path, unsigned long num) -{ - char buf[21]; - - sprintf(buf, "%ld", num); - write_file(path, buf, strlen(buf) + 1); -} - int thp_read_string(const char *name, const char * const strings[]) { char path[PATH_MAX]; diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h index d6a1b4e5f734..3d1a4f18be30 100644 --- a/tools/testing/selftests/mm/hugepage_settings.h +++ b/tools/testing/selftests/mm/hugepage_settings.h @@ -66,10 +66,6 @@ struct thp_settings { struct shmem_hugepages_settings shmem_hugepages[NR_ORDERS]; }; -int read_file(const char *path, char *buf, size_t buflen); -unsigned long read_num(const char *path); -void write_num(const char *path, unsigned long num); - int thp_read_string(const char *name, const char * const strings[]); void thp_write_string(const char *name, const char *val); unsigned long thp_read_num(const char *name); diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index e62e2a473123..752566f75c0b 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -699,6 +699,27 @@ int unpoison_memory(unsigned long pfn) return ret > 0 ? 0 : -errno; } +int read_file(const char *path, char *buf, size_t buflen) +{ + int fd; + ssize_t numread; + + fd = open(path, O_RDONLY); + if (fd == -1) + return 0; + + numread = read(fd, buf, buflen - 1); + if (numread < 1) { + close(fd); + return 0; + } + + buf[numread] = '\0'; + close(fd); + + return (unsigned int) numread; +} + void write_file(const char *path, const char *buf, size_t buflen) { int fd, saved_errno; @@ -722,3 +743,21 @@ void write_file(const char *path, const char *buf, size_t buflen) ksft_exit_fail_msg("%s write(%.*s) is truncated, expected %zu bytes, got %zd bytes\n", path, (int)(buflen - 1), buf, buflen - 1, numwritten); } + +unsigned long read_num(const char *path) +{ + char buf[21]; + + if (read_file(path, buf, sizeof(buf)) < 0) + ksft_exit_fail_perror("read_file()"); + + return strtoul(buf, NULL, 10); +} + +void write_num(const char *path, unsigned long num) +{ + char buf[21]; + + sprintf(buf, "%lu", num); + write_file(path, buf, strlen(buf) + 1); +} diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 195bf2e26792..5fc9707f6b9a 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -165,3 +165,6 @@ int unpoison_memory(unsigned long pfn); #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) void write_file(const char *path, const char *buf, size_t buflen); +int read_file(const char *path, char *buf, size_t buflen); +unsigned long read_num(const char *path); +void write_num(const char *path, unsigned long num); -- 2.53.0