From nobody Sun Jun 21 13:59:04 2026 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 6D4483D6491; Fri, 3 Apr 2026 19:40:28 +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=1775245228; cv=none; b=o6ZyGLonlw2Y7N1KquGCCorWYhUcb532zSusYqL92CidoL8xirYKVcE+U91ggokBFhGZhL+4CPsZrWX2YQJ9oIMOBsc0j51Kkg8kGpn9sh8JBTNUtkGNiJIBOVaUbL3Hx3Cy6iJOaLvLY7y3FH1IP6NZ60Q594uJCI8u09YaeMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245228; c=relaxed/simple; bh=VK1HcPC/3wExF1FoSkZi5ZUUe6b5gm8f6ww+D0kO+o8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IHJovJFDk8YbKOmDIz87kfn2o4sLlnfKGfcarciWZim848LXcHYbO7HXxcSfa9jcjh/ZesocVneCWQNLPYm7LqgtTx0DmEGP4O6+0r3XpUiaCuvsK26jGps1dst6VHy+8fqTKRror9y1IV2A+f2QTct18aIJjLnbhNogUh7X/A4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZoTiwGe1; 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="ZoTiwGe1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AD8DC19424; Fri, 3 Apr 2026 19:40:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245228; bh=VK1HcPC/3wExF1FoSkZi5ZUUe6b5gm8f6ww+D0kO+o8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZoTiwGe1QmwvyLreS4Iwrj0r8ah0hPBXnXkDtoGieRWHthFimFGI/8HyOb0NfseHn U3Rfy1sk15IcwoJLlpi32RaU6sx0F/WIfS49RxgHFJmI2SxvbSHZm5RmndtmsNjjy5 9JIiG6ShZO2+U8SlhLW2qp5Wz8yI6r6dhBeJP+RSJae8qbTkIhHVjLhE2QHfY6aWaa C4lj3ERuasJB+zfR4KTOLQqnwPHge8yRiNX736KKPZ3jlpcDYTuNkk+HAo3Q3X6n5h pztj4dQayog8j5amMJ2fQgbBklDTkb0hQVeL0ucoCZF8M2p6iXp7ZJWxrIrUZlnYVp v2TCiAAEh4yYg== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 1/6] selftests/liveupdate: add framework for memfd tests Date: Fri, 3 Apr 2026 19:40:07 +0000 Message-ID: <20260403194014.3704180-2-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" Currently memfd preservation using LUO is only tested indirectly via the luo_multi_session or luo_kexec_simple tests. Their main purpose is to test other live update functionality. Add a framework for writing memfd tests. The framework hooks into the kselftest harness, but adds some things on top to make it suitable for live update. The LUO FD (/dev/liveupdate) can only be opened by one process at a time. Each test runs in its own process. This means the LUO FD must be owned by the main process and shared to children. main() opens the LUO FD and shares it to child runners using a global variable. Live update tests run in two stages. One before kexec and one after. Detect the stage using a special state session. If the session is present, it means the test is in post-kexec state. Additionally, take in an optional --stage argument that lets callers specify expected stage. This is useful as a safety net to catch LUO core failures. If LUO core fails to preserve the state session properly, this option can help detect this and fail early. Since the option is not recognized by the kselftest harness, remove it from argv before calling test_harness_run(). Reviewed-by: Mike Rapoport (Microsoft) Signed-off-by: Pratyush Yadav (Google) --- tools/testing/selftests/liveupdate/Makefile | 1 + .../testing/selftests/liveupdate/luo_memfd.c | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.c diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile index 080754787ede..051daae55eec 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -6,6 +6,7 @@ TEST_GEN_PROGS +=3D liveupdate =20 TEST_GEN_PROGS_EXTENDED +=3D luo_kexec_simple TEST_GEN_PROGS_EXTENDED +=3D luo_multi_session +TEST_GEN_PROGS_EXTENDED +=3D luo_memfd =20 TEST_FILES +=3D do_kexec.sh =20 diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing= /selftests/liveupdate/luo_memfd.c new file mode 100644 index 000000000000..47d2c3163b62 --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_memfd.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2026, Google LLC. + * Pratyush Yadav (Google) + */ + +/* + * Selftests for memfd preservation via LUO. + */ + +#include +#include +#include +#include +#include + +#include + +#include "../kselftest.h" +#include "../kselftest_harness.h" + +#include "luo_test_utils.h" + +#define STATE_SESSION_NAME "luo-state" +#define STATE_MEMFD_TOKEN 1 + +static int luo_fd =3D -1; +static int stage; + +int main(int argc, char *argv[]) +{ + int expected_stage =3D 0; + int session; + + /* + * The test takes an optional --stage argument. This lets callers + * provide the expected stage, and if that doesn't match the test errors + * out. + * + * Look for the stage. Since test_harness_run() doesn't recognize it, + * once found, remove it from argv. + */ + for (int i =3D 1; i < argc; i++) { + if (strcmp(argv[i], "--stage") =3D=3D 0) { + if (i + 1 < argc) { + expected_stage =3D atoi(argv[i + 1]); + memmove(&argv[i], &argv[i + 2], (argc - i - 1) * sizeof(char *)); + argc -=3D 2; + i--; + } else { + ksft_exit_fail_msg("Option --stage requires an argument\n"); + } + } + } + + luo_fd =3D luo_open_device(); + if (luo_fd < 0) + ksft_exit_skip("Failed to open %s (%s) device. Is LUO enabled?\n", + LUO_DEVICE, strerror(errno)); + + session =3D luo_retrieve_session(luo_fd, STATE_SESSION_NAME); + if (session =3D=3D -ENOENT) + stage =3D 1; + else if (session >=3D 0) + stage =3D 2; + else + ksft_exit_fail_perror("Failed to check for state session"); + + if (expected_stage && expected_stage !=3D stage) + ksft_exit_fail_msg("Stage mismatch: expected %d, got %d\n", + expected_stage, stage); + + if (stage =3D=3D 1) + create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2); + + test_harness_run(argc, argv); +} --=20 2.53.0.1213.gd9a14994de-goog From nobody Sun Jun 21 13:59:04 2026 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 914FE3D6CC1; Fri, 3 Apr 2026 19:40:30 +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=1775245230; cv=none; b=FskJadxKVeskSdegDMOVAVW46qUdHjZWfVuX9s8NUa2DJrQUNqlo19dcGp5pebSAdOSDiMs2ZlxdY+LqQnsmQT199IY6ZZQ1nAoG4k/DiVfWMhdNz1cCvz+ajKxYb/PPCysBGKmpxWuWZP8aiocDF8nAIAbZCFlF/KiZ4qLs/os= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245230; c=relaxed/simple; bh=ewW/SXYu+Y2DgtLtkPP3OYPb7SH30gYzCUlOzRzYtZg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AM9jTbyZ3z/tFfQvOXEI3bb8dXkYrSUStqa00D09uIaBnhKaw2WbRmJAfi9dUpljoyqZiXPDrIpxgfqopuSdAdjbobXTNHPEDuk70peOj0uNJy/iGOt7jeW2qBDmBV/my6cNdxMqku5Tx1DzvdhO8JHVPo5CxBMxKBTiBnO8uQw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qnlOGk11; 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="qnlOGk11" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F6F5C4CEF7; Fri, 3 Apr 2026 19:40:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245230; bh=ewW/SXYu+Y2DgtLtkPP3OYPb7SH30gYzCUlOzRzYtZg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qnlOGk11o5AS6MsumHXaD8u37mQ9YU0Zd+bZcK/NLVLf4k6QhzvOgOuuBKj06f4Bh HUpfkcFnLTpgNWZeZFc8fleEiuDx8ZdLBJk1tDgykW47T+pscombNbOhGUSjPAJILD vsru0LfvsEB+6bnjIlSbBb/qer7wlMEEsjI3rzLmrTJbNVuqIVecURE9ThBWRPwxGX Yvdpi7sy8WKU56ukloUUpQRV2sRkuUWMhL+cYBod79vvI0627n8XIGgSAm8xTVzrCs tpDfbeRNaQgZnafwgWOEK702D3pWf3Xiy0tBpAcO62+hssN9wVAgwZA3jhwNOK+ZfL PbjOM3ZpHd12g== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Pratyush Yadav Subject: [PATCH v2 2/6] selftests/liveupdate: add helper functions for memfd tests Date: Fri, 3 Apr 2026 19:40:08 +0000 Message-ID: <20260403194014.3704180-3-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" Add some helper functions that will be used by memfd tests. This moves some of the complexity out of the test itself, which results in better test readability and less code duplication. Reviewed-by: Mike Rapoport (Microsoft) Signed-off-by: Pratyush Yadav Signed-off-by: Pratyush Yadav (Google) --- .../selftests/liveupdate/luo_test_utils.c | 189 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 198 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c b/tools/te= sting/selftests/liveupdate/luo_test_utils.c index 3c8721c505df..585add102ac2 100644 --- a/tools/testing/selftests/liveupdate/luo_test_utils.c +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c @@ -1,8 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-only =20 /* - * Copyright (c) 2025, Google LLC. + * Copyright (c) 2025-2026, Google LLC. * Pasha Tatashin + * Pratyush Yadav (Google) + * + * Copyright (C) 2025 Amazon.com Inc. or its affiliates. + * Pratyush Yadav */ =20 #define _GNU_SOURCE @@ -20,9 +24,192 @@ #include #include #include +#include +#include =20 #include "luo_test_utils.h" =20 +int cwd_is_tmpfs(void) +{ + struct statfs buf; + + if (statfs(".", &buf) < 0) + return -errno; + + return buf.f_type =3D=3D TMPFS_MAGIC; +} + +/* Read exactly specified size from fd. Any less results in error. */ +int read_size(int fd, char *buffer, size_t size) +{ + size_t remain =3D size; + ssize_t bytes_read; + + while (remain) { + bytes_read =3D read(fd, buffer, remain); + if (bytes_read =3D=3D 0) + return -ENODATA; + if (bytes_read < 0) + return -errno; + + remain -=3D bytes_read; + buffer +=3D bytes_read; + } + + return 0; +} + +/* Write exactly specified size from fd. Any less results in error. */ +int write_size(int fd, const char *buffer, size_t size) +{ + size_t remain =3D size; + ssize_t written; + + while (remain) { + written =3D write(fd, buffer, remain); + if (written =3D=3D 0) + return -EIO; + if (written < 0) + return -errno; + + remain -=3D written; + buffer +=3D written; + } + + return 0; +} + +int generate_random_data(char *buffer, size_t size) +{ + int fd, ret; + + fd =3D open("/dev/urandom", O_RDONLY); + if (fd < 0) + return -errno; + + ret =3D read_size(fd, buffer, size); + close(fd); + return ret; +} + +int save_test_data(const char *filename, const char *buffer, size_t size) +{ + int fd, ret; + + fd =3D open(filename, O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -errno; + + ret =3D write_size(fd, buffer, size); + fsync(fd); + close(fd); + return ret; +} + +int load_test_data(const char *filename, char *buffer, size_t size) +{ + int fd, ret; + + fd =3D open(filename, O_RDONLY); + if (fd < 0) + return -errno; + + ret =3D read_size(fd, buffer, size); + close(fd); + return ret; +} + +/* Create and initialize a memfd with random data. */ +int create_random_memfd(const char *memfd_name, char *buffer, size_t size) +{ + int fd; + int ret; + + fd =3D memfd_create(memfd_name, 0); + if (fd < 0) + return -errno; + + ret =3D generate_random_data(buffer, size); + if (ret < 0) { + close(fd); + return ret; + } + + if (write_size(fd, buffer, size) < 0) { + close(fd); + return -errno; + } + + /* Reset file position to beginning */ + if (lseek(fd, 0, SEEK_SET) < 0) { + close(fd); + return -errno; + } + + return fd; +} + +/* + * Make sure fd contains expected data up to size. Returns 0 on success, 1= on + * data mismatch, -errno on error. + */ +int verify_fd_content_read(int fd, const char *expected_data, size_t size) +{ + char *buffer; + int ret; + + buffer =3D malloc(size); + if (!buffer) + return -ENOMEM; + + /* Reset file position to beginning */ + if (lseek(fd, 0, SEEK_SET) < 0) { + ret =3D -errno; + goto out; + } + + ret =3D read_size(fd, buffer, size); + if (ret < 0) + goto out; + + if (memcmp(buffer, expected_data, size) !=3D 0) { + ret =3D 1; + goto out; + } + + ret =3D 0; + +out: + free(buffer); + return ret; +} + +/* + * Verify fd content using mmap. Returns 0 on success, 1 on data mismatch, + * -errno on error. + */ +int verify_fd_content_mmap(int fd, const char *expected_data, size_t size) +{ + char *mapped_mem; + int ret; + + mapped_mem =3D mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (mapped_mem =3D=3D MAP_FAILED) + return -errno; + + /* ret =3D memcmp(mapped_mem, expected_data, size) ? 1 : 0; */ + ret =3D 0; + for (size_t i =3D 0; i < size; i++) { + if (mapped_mem[i] !=3D expected_data[i]) { + ret =3D 1; + break; + } + } + + munmap(mapped_mem, size); + return ret; +} + int luo_open_device(void) { return open(LUO_DEVICE, O_RDWR); diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.h b/tools/te= sting/selftests/liveupdate/luo_test_utils.h index 90099bf49577..a69dce2649b0 100644 --- a/tools/testing/selftests/liveupdate/luo_test_utils.h +++ b/tools/testing/selftests/liveupdate/luo_test_utils.h @@ -35,6 +35,16 @@ void restore_and_read_stage(int state_session_fd, int to= ken, int *stage); =20 void daemonize_and_wait(void); =20 +int cwd_is_tmpfs(void); +int read_size(int fd, char *buffer, size_t size); +int write_size(int fd, const char *buffer, size_t size); +int generate_random_data(char *buffer, size_t size); +int save_test_data(const char *filename, const char *buffer, size_t size); +int load_test_data(const char *filename, char *buffer, size_t size); +int create_random_memfd(const char *memfd_name, char *buffer, size_t size); +int verify_fd_content_read(int fd, const char *expected_data, size_t size); +int verify_fd_content_mmap(int fd, const char *expected_data, size_t size); + typedef void (*luo_test_stage1_fn)(int luo_fd); typedef void (*luo_test_stage2_fn)(int luo_fd, int state_session_fd); =20 --=20 2.53.0.1213.gd9a14994de-goog From nobody Sun Jun 21 13:59:04 2026 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 442173D75BF; Fri, 3 Apr 2026 19:40:33 +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=1775245233; cv=none; b=UykVadA5R/p5gPM6dMyVKoEraSOwPzYXaasG9dVZvG4ihcSGoIDCoOxt39fhg200PUbo9c5Q7IGeQEhoW3W8ZfjljC+khu70F9aT59G7SxxgZnbAuOvFI0o5mAlpWkB/H4+DJSemrLcAuqHymcaHacXqtcT6QEQ9zCteiphj0vo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245233; c=relaxed/simple; bh=4sQ2am7P1fIKmx4JakwbC3BEjgsHp2lmwc8hACFKRSQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mCXzH2FJ+gh5LXe9mX3Muw3+gAVelKdTAf9JGWddifm8D3/gw12SvSd266eT882zZmvLGNkyyY8f4gr0r2dt6QQsD7l9lSXCHJvflMVuCEu5Yyqp5C4+pA4UwTltfC2p60D/n7ntOZpdXQJEl+o+g0U8cnI+Kf7hzEzSf68igrA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Go2tvX9v; 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="Go2tvX9v" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE2E5C2BCB3; Fri, 3 Apr 2026 19:40:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245232; bh=4sQ2am7P1fIKmx4JakwbC3BEjgsHp2lmwc8hACFKRSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Go2tvX9vIXO8EQ7FnchajHLDwJAyDeUd2ba9pglp4R8p4TfPwtbTRxvwWW8nAex8z riJFtzogQZha0TsAVk6oEpL5fa6Q1IDjFFZirOhY2UxemaonhgAqvut8CQ/gE4f7Ne SNMgJq17zo4XkmuluRT1n2FCChbhIcTWBPfI4smS2mjYs+MYHr+t6PhRWSL5lbEoMB SSpZZ4DBKE6SvQPew1OX2ROC5OgUOLteIWDz+pX4rV5+DgXIFzIDztY/h21VJNhvNB reMVhu3ewxbXcTdsYDbJvmYi0jwjpJX4QNxMcI4tvAP7WwQGRDBznGTwfm8N7onmLv 3C3Pz6cKJsRPg== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Pratyush Yadav Subject: [PATCH v2 3/6] selftests/liveupdate: add test for memfd content preservation Date: Fri, 3 Apr 2026 19:40:09 +0000 Message-ID: <20260403194014.3704180-4-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" Add a selftest that makes sure the contents of a memfd are correctly preserved across a live update. In stage 1, create a memfd and fill it with random data, and preserve it. Save the random data to the file system. This will be used by stage 2 to verify the contents are correct. In stage 2, retrieve the memfd and compare its contents with the contents saved on the file system. Signed-off-by: Pratyush Yadav Signed-off-by: Pratyush Yadav (Google) --- tools/testing/selftests/liveupdate/Makefile | 1 + .../testing/selftests/liveupdate/luo_memfd.c | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile index 051daae55eec..8e2eb6500c07 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -13,6 +13,7 @@ TEST_FILES +=3D do_kexec.sh include ../lib.mk =20 CFLAGS +=3D $(KHDR_INCLUDES) +CFLAGS +=3D -I$(top_srcdir)/tools/include CFLAGS +=3D -Wall -O2 -Wno-unused-function CFLAGS +=3D -MD =20 diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing= /selftests/liveupdate/luo_memfd.c index 47d2c3163b62..70eb96950f04 100644 --- a/tools/testing/selftests/liveupdate/luo_memfd.c +++ b/tools/testing/selftests/liveupdate/luo_memfd.c @@ -3,6 +3,9 @@ /* * Copyright (c) 2026, Google LLC. * Pratyush Yadav (Google) + * + * Copyright (C) 2025 Amazon.com Inc. or its affiliates. + * Pratyush Yadav */ =20 /* @@ -11,11 +14,14 @@ =20 #include #include +#include #include #include +#include #include =20 #include +#include =20 #include "../kselftest.h" #include "../kselftest_harness.h" @@ -25,9 +31,83 @@ #define STATE_SESSION_NAME "luo-state" #define STATE_MEMFD_TOKEN 1 =20 +#define MEMFD_DATA_SESSION_NAME "memfd_data_session" +#define MEMFD_DATA_TOKEN 1 +#define MEMFD_DATA_BUFFER_SIZE SZ_1M +#define MEMFD_DATA_FS_COPY "memfd_data_fs_copy.bin" + static int luo_fd =3D -1; static int stage; =20 +static void memfd_data_stage_1(struct __test_metadata *_metadata) +{ + int fd, session; + char *buffer; + struct liveupdate_session_preserve_fd preserve_arg =3D { .size =3D sizeof= (preserve_arg) }; + + buffer =3D malloc(MEMFD_DATA_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session =3D luo_create_session(luo_fd, MEMFD_DATA_SESSION_NAME); + ASSERT_GE(session, 0); + + fd =3D create_random_memfd("memfd_data", buffer, MEMFD_DATA_BUFFER_SIZE); + ASSERT_GE(fd, 0); + + ASSERT_EQ(save_test_data(MEMFD_DATA_FS_COPY, buffer, MEMFD_DATA_BUFFER_SI= ZE), 0); + + preserve_arg.fd =3D fd; + preserve_arg.token =3D MEMFD_DATA_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), = 0); + + daemonize_and_wait(); +} + +static void memfd_data_stage_2(struct __test_metadata *_metadata) +{ + int fd, session; + char *buffer; + struct liveupdate_session_retrieve_fd retrieve_arg =3D { .size =3D sizeof= (retrieve_arg) }; + + buffer =3D malloc(MEMFD_DATA_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session =3D luo_retrieve_session(luo_fd, MEMFD_DATA_SESSION_NAME); + ASSERT_GE(session, 0); + + ASSERT_EQ(load_test_data(MEMFD_DATA_FS_COPY, buffer, MEMFD_DATA_BUFFER_SI= ZE), 0); + + retrieve_arg.token =3D MEMFD_DATA_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), = 0); + fd =3D retrieve_arg.fd; + ASSERT_GE(fd, 0); + + ASSERT_EQ(verify_fd_content_read(fd, buffer, MEMFD_DATA_BUFFER_SIZE), 0); + + ASSERT_EQ(luo_session_finish(session), 0); +} + +/* + * Test that a memfd with its data is preserved across live update. + */ +TEST(memfd_data) +{ + if (cwd_is_tmpfs()) + SKIP(return, "test saves data to rootfs, cannot run on tmpfs"); + + switch (stage) { + case 1: + memfd_data_stage_1(_metadata); + break; + case 2: + memfd_data_stage_2(_metadata); + break; + default: + TH_LOG("Unknown stage %d\n", stage); + ASSERT_FALSE(true); + } +} + int main(int argc, char *argv[]) { int expected_stage =3D 0; --=20 2.53.0.1213.gd9a14994de-goog From nobody Sun Jun 21 13:59:04 2026 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 A1F063D6CA4; Fri, 3 Apr 2026 19:40:35 +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=1775245235; cv=none; b=p2PjBLAT5R7n4c35XgG+uiganc8cUAxo4zk6Y2taRZcddPoXvSkXtPVCUHYXhrF4wb0aYUHyD10LiHIix3pBod/SO+VeFEPZ8S5H8kdBiH2JqLzag/dnidMaMOwNV3BU2rrSE/sBqgzXsTkKd+hdWyZaHFtNeeByfSkWyq6rZbs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245235; c=relaxed/simple; bh=FJIlVUEHAp7ZJEKT0idl4M4VyELOXTlHEmJHYyO6eBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gj8JOfti0DjTYbbVZsacdEQxa6dUBrRkfKBnX3ZGl4UnSwMayzccqhQLuVJJ8i97lzPvi5guu1AvtV5lTD79XuILdlzikavVK9YyneWggrF/ABhOEvwEV3nRvDf8vQPFNb0DdvjFteY7PSeuRot+bXzMeBGRu76UcgJMGmShemo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ITI6lWTo; 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="ITI6lWTo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59E04C2BCAF; Fri, 3 Apr 2026 19:40:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245235; bh=FJIlVUEHAp7ZJEKT0idl4M4VyELOXTlHEmJHYyO6eBY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ITI6lWToNau7UFzXYD5NG6S04w5VvF0y9PiRgP8aOTF8SaPWU0ntqT+ztUJHwCQsQ lFVEWyHtVOSgin1AwMzWxwxHaXokWKEr12q0OSjUSg64SQFwpnOf8f4y3a8YQ4/On2 9yaFxndaJx+FCSG0610AJQfawQNYrkDmAnqVHH4s4WpQ+6vQcm4/g3kESjOm6eBVlU WY017tHR10glhlBpnfRshzKObMqgQ1XA677drxqveJMqzq4K/Km8bRQ2QDrxabyOxj emAl0D4bLmYM+V3qsYgwFV5rQhev7Zxg26TKN4Yo+0IJF+3314bz1X3YBlDBSQLSk4 dw7KVccZMB7ZQ== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 4/6] selftests/liveupdate: add test for zero-size memfd preservation Date: Fri, 3 Apr 2026 19:40:10 +0000 Message-ID: <20260403194014.3704180-5-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" A zero-size memfd is a special case of memfd preservation. It takes a different path from normal both during preservation and during restore. In the serialization structure, the number of folios is zero and the vmalloc array with folios is empty. The restore logic should check for this and make sure to not touch the invalid array. Add a test to make sure this path works as expected. In stage 1, the test creates and preserves a memfd without any data. In stage 2, the test retrieves the memfd and makes sure it is still without data. Reviewed-by: Mike Rapoport (Microsoft) Signed-off-by: Pratyush Yadav (Google) --- .../testing/selftests/liveupdate/luo_memfd.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing= /selftests/liveupdate/luo_memfd.c index 70eb96950f04..3ba0709f2c61 100644 --- a/tools/testing/selftests/liveupdate/luo_memfd.c +++ b/tools/testing/selftests/liveupdate/luo_memfd.c @@ -36,6 +36,9 @@ #define MEMFD_DATA_BUFFER_SIZE SZ_1M #define MEMFD_DATA_FS_COPY "memfd_data_fs_copy.bin" =20 +#define ZERO_SESSION_NAME "zero_session" +#define ZERO_MEMFD_TOKEN 1 + static int luo_fd =3D -1; static int stage; =20 @@ -108,6 +111,60 @@ TEST(memfd_data) } } =20 +static void zero_memfd_stage_1(struct __test_metadata *_metadata) +{ + int zero_fd, session; + struct liveupdate_session_preserve_fd preserve_arg =3D { .size =3D sizeof= (preserve_arg) }; + + session =3D luo_create_session(luo_fd, ZERO_SESSION_NAME); + ASSERT_GE(session, 0); + + zero_fd =3D memfd_create("zero_memfd", 0); + ASSERT_GE(zero_fd, 0); + + preserve_arg.fd =3D zero_fd; + preserve_arg.token =3D ZERO_MEMFD_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), = 0); + + daemonize_and_wait(); +} + +static void zero_memfd_stage_2(struct __test_metadata *_metadata) +{ + int zero_fd, session; + struct liveupdate_session_retrieve_fd retrieve_arg =3D { .size =3D sizeof= (retrieve_arg) }; + + session =3D luo_retrieve_session(luo_fd, ZERO_SESSION_NAME); + ASSERT_GE(session, 0); + + retrieve_arg.token =3D ZERO_MEMFD_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), = 0); + zero_fd =3D retrieve_arg.fd; + ASSERT_GE(zero_fd, 0); + + ASSERT_EQ(lseek(zero_fd, 0, SEEK_END), 0); + + ASSERT_EQ(luo_session_finish(session), 0); +} + +/* + * Test that a zero-sized memfd is preserved across live update. + */ +TEST(zero_memfd) +{ + switch (stage) { + case 1: + zero_memfd_stage_1(_metadata); + break; + case 2: + zero_memfd_stage_2(_metadata); + break; + default: + TH_LOG("Unknown stage %d\n", stage); + ASSERT_FALSE(true); + } +} + int main(int argc, char *argv[]) { int expected_stage =3D 0; --=20 2.53.0.1213.gd9a14994de-goog From nobody Sun Jun 21 13:59:04 2026 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 161DB3D7D8C; Fri, 3 Apr 2026 19:40:37 +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=1775245238; cv=none; b=onKxdzuuT2T4gg4US6DsPjRkxYC5ERcqTBXvAkYydVqsvdeYwsFVOKqSEsfaeS9ecJG2X6dHO9xA/09CHJr3v4B3AfI3g5Jpqc7Qlwp8gnpUUMZkpdhWHFDIhfSkGbsjZJgkM2hNmiI9v6e//ACukDLaE1FGm5HLqMWEz6htjbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245238; c=relaxed/simple; bh=hx4LuheWBNtlwVbJuz9IzDnvXJqSAD9ip7Q7EdlOqnY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aj9++dW4lX0SnFdR9GTNS7sR9HvhmS7s8lckEUAOm5msGlfqHKGqsPQR2tHF9gHz1/kljuqpILL+dDQjQfr7AfalXWKkL0+G6T24TZ4QJUStQlL8gKpD/bdVPcY2rtBg/adWjLzGKaZjVdIlslDntmlgMbEk08XIbPWov5y6gz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a3ej2wEl; 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="a3ej2wEl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EB6FC19424; Fri, 3 Apr 2026 19:40:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245237; bh=hx4LuheWBNtlwVbJuz9IzDnvXJqSAD9ip7Q7EdlOqnY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a3ej2wElP+3pm9krk6g11dT5QXKQSXJCjHbUiold3i0U+NJB8RlKfgZcxGbiLUSz7 A6VoexLAElGlVS2Q4t7lSFX28GrDvLxgGRkqVuqsTgAmsofO5O+ryPwnOZk/HW7fWY KwYGJGnuhroSM2GPlktidOMiaKIFBeV5JpM2e1nFGFlW+LshjuEP19yqngZgIufq2U /+KfF/d4qQRm5/iMnkyw6OYn2Q8i97CTHv8HthTlYdZD930cYZzZQUn7Bm42GPFema Jf0HRYsmBhlwfgob+j81QzJIMm/OGdp72nhgv9wbjES9lkOMRMvklpnv4jKZZ0f/aC UEMcEsbWjtupA== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Pratyush Yadav Subject: [PATCH v2 5/6] selftests/liveupdate: add test for operations on a preserved memfd Date: Fri, 3 Apr 2026 19:40:11 +0000 Message-ID: <20260403194014.3704180-6-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" Once a memfd is preserved, certain operations are not allowed to succeed since they might make the state of the memfd inconsistent with the serialized state. Among these operations are truncating or growing the memfd. Writes and reads to already existing memfd should succeed. Add a test that makes sure a preserved memfd does not allow growing or shrinking, but does allow reads and writes to existing memory to go thorough. Reviewed-by: Mike Rapoport (Microsoft) Signed-off-by: Pratyush Yadav Signed-off-by: Pratyush Yadav (Google) --- .../testing/selftests/liveupdate/luo_memfd.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing= /selftests/liveupdate/luo_memfd.c index 3ba0709f2c61..a1e9f34da006 100644 --- a/tools/testing/selftests/liveupdate/luo_memfd.c +++ b/tools/testing/selftests/liveupdate/luo_memfd.c @@ -39,6 +39,10 @@ #define ZERO_SESSION_NAME "zero_session" #define ZERO_MEMFD_TOKEN 1 =20 +#define PRESERVED_SESSION_NAME "preserved_session" +#define PRESERVED_MEMFD_TOKEN 1 +#define PRESERVED_BUFFER_SIZE SZ_1M + static int luo_fd =3D -1; static int stage; =20 @@ -165,6 +169,59 @@ TEST(zero_memfd) } } =20 +/* + * Test that preserved memfd can't grow or shrink, but reads and writes st= ill + * work. + */ +TEST(preserved_ops) +{ + char write_buffer[128] =3D {'A'}; + int fd, session; + char *buffer; + struct liveupdate_session_preserve_fd preserve_arg =3D { .size =3D sizeof= (preserve_arg) }; + + if (stage !=3D 1) + SKIP(return, "test only expected to run on stage 1"); + + buffer =3D malloc(PRESERVED_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session =3D luo_create_session(luo_fd, PRESERVED_SESSION_NAME); + ASSERT_GE(session, 0); + + fd =3D create_random_memfd("preserved_memfd", buffer, PRESERVED_BUFFER_SI= ZE); + ASSERT_GE(fd, 0); + + preserve_arg.fd =3D fd; + preserve_arg.token =3D PRESERVED_MEMFD_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), = 0); + + /* + * Write to the preserved memfd (within existing size). This should + * work. + */ + ASSERT_GE(lseek(fd, 0, SEEK_SET), 0); + /* Write buffer is smaller than total file size. */ + ASSERT_EQ(write_size(fd, write_buffer, sizeof(write_buffer)), 0); + ASSERT_EQ(verify_fd_content_read(fd, write_buffer, sizeof(write_buffer)),= 0); + + /* Try to grow the file using write(). */ + + /* First, seek to one byte behind initial size. */ + ASSERT_GE(lseek(fd, PRESERVED_BUFFER_SIZE - 1, SEEK_SET), 0); + + /* + * Then, write some data that should increase the file size. This should + * fail. + */ + ASSERT_LT(write_size(fd, write_buffer, sizeof(write_buffer)), 0); + ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE); + + /* Try to shrink the file using truncate. This should also fail. */ + ASSERT_LT(ftruncate(fd, PRESERVED_BUFFER_SIZE / 2), 0); + ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE); +} + int main(int argc, char *argv[]) { int expected_stage =3D 0; --=20 2.53.0.1213.gd9a14994de-goog From nobody Sun Jun 21 13:59:04 2026 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 47EAF3D8105; Fri, 3 Apr 2026 19:40:40 +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=1775245240; cv=none; b=G6KUS1kvThzZUYkRKROaA5XlXGCt+y74SvBb40OZTIjo0n3vYRuTMmQxYcRKLLnH625iant5SDnBHD4uG4isML+QPA7cwC6wNTvKgduokTTMH0Rj9+Jdq632yRwijOg674UWqeD4G351XEUlYhZhBftoNTTUPV/UyC8Zf0OibxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775245240; c=relaxed/simple; bh=CUyr5fNZuY4RKq1OLQXkrBMqLq0EWre1SrQk+TC1ABM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ILtYs0gAnqi57qPhJ+O+xRqmNxdU7xUgz3j52w1w1FVnQBzv+yKg3KgezTd7HFxm2KbOWrEsCrKGGv/YpogQwzzluP1MhCV+w7QVmQIsYEbSJGCZQUhK1qHn35YUse+ZnVXuL9GWxfqRolP4/ZmYSOpQDicxG3exNrFaOvH2ie8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dIPziEqe; 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="dIPziEqe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19C21C2BCAF; Fri, 3 Apr 2026 19:40:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775245239; bh=CUyr5fNZuY4RKq1OLQXkrBMqLq0EWre1SrQk+TC1ABM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dIPziEqeVeRPRUf4x4dKXgETATxg/MOMCYRoLEjhgV8QqBul4R0BXSVDkR+Bj4atL 60u03+RgXPzKHlpyTtqMgHuraKDXu1UbtDtkvlz8aA2DPEAgrLXwEafafyB5n1nu5L GBmrAEGKPzhW5UxwVcfYjCPcob+fgZab75SHf9oMpcvmc1pQg5MFCil3RFvVla1/4b NHrofvyYG8y52AWHiZJyf1Chrsbs43bQaaFYTuJVTHM+W6Kk363WBZIm1MfMjZdBFp ZXrfd5PK2CsWXen6CjqDW/aFNZ6ZWw3emXkj8gD+0+zCVSCZJly7hhQaaRy5Roldry ieEgLRH/JLpqQ== From: Pratyush Yadav To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Shuah Khan , Andrew Morton , Usama Arif Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 6/6] selftests/liveupdate: add fallocate test for memfd Date: Fri, 3 Apr 2026 19:40:12 +0000 Message-ID: <20260403194014.3704180-7-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260403194014.3704180-1-pratyush@kernel.org> References: <20260403194014.3704180-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Pratyush Yadav (Google)" When memory is added to a memfd via fallocate(), it does not get zeroed immediately. This is tracked by the absence of the uptodate folio flag. Initially, memfd preservation simply saved the folio flags at preserve time. This led to a bug, where all writes to un-initialized fallocated memory after preserve were lost after live update. This is fixed by commit 50d7b4332f27 ("mm: memfd_luo: always make all folios uptodate"). Add a test that fallocates some memory in a memfd, preserves it, writes to it. Then in stage 2 it verifies the written content is still present. Reviewed-by: Mike Rapoport (Microsoft) Signed-off-by: Pratyush Yadav (Google) --- .../testing/selftests/liveupdate/luo_memfd.c | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing= /selftests/liveupdate/luo_memfd.c index a1e9f34da006..bc6ab063363f 100644 --- a/tools/testing/selftests/liveupdate/luo_memfd.c +++ b/tools/testing/selftests/liveupdate/luo_memfd.c @@ -43,6 +43,11 @@ #define PRESERVED_MEMFD_TOKEN 1 #define PRESERVED_BUFFER_SIZE SZ_1M =20 +#define FALLOCATE_SESSION_NAME "fallocate_session" +#define FALLOCATE_MEMFD_TOKEN 1 +#define FALLOCATE_BUFFER_SIZE SZ_1M +#define FALLOCATE_DATA_FS_COPY "fallocate_data_fs_copy.bin" + static int luo_fd =3D -1; static int stage; =20 @@ -222,6 +227,84 @@ TEST(preserved_ops) ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE); } =20 +static void fallocate_memfd_stage_1(struct __test_metadata *_metadata) +{ + int fd, session; + char *buffer; + struct liveupdate_session_preserve_fd preserve_arg =3D { .size =3D sizeof= (preserve_arg) }; + + buffer =3D malloc(FALLOCATE_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session =3D luo_create_session(luo_fd, FALLOCATE_SESSION_NAME); + ASSERT_GE(session, 0); + + fd =3D memfd_create("fallocate_memfd", 0); + ASSERT_GE(fd, 0); + + /* Fallocate memory but do not write to it yet */ + ASSERT_EQ(fallocate(fd, 0, 0, FALLOCATE_BUFFER_SIZE), 0); + + preserve_arg.fd =3D fd; + preserve_arg.token =3D FALLOCATE_MEMFD_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), = 0); + + /* Now write to it after preserving */ + ASSERT_GE(generate_random_data(buffer, FALLOCATE_BUFFER_SIZE), 0); + ASSERT_EQ(save_test_data(FALLOCATE_DATA_FS_COPY, buffer, FALLOCATE_BUFFER= _SIZE), 0); + + ASSERT_GE(lseek(fd, 0, SEEK_SET), 0); + ASSERT_EQ(write_size(fd, buffer, FALLOCATE_BUFFER_SIZE), 0); + + daemonize_and_wait(); +} + +static void fallocate_memfd_stage_2(struct __test_metadata *_metadata) +{ + int fd, session; + char *buffer; + struct liveupdate_session_retrieve_fd retrieve_arg =3D { .size =3D sizeof= (retrieve_arg) }; + + buffer =3D malloc(FALLOCATE_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session =3D luo_retrieve_session(luo_fd, FALLOCATE_SESSION_NAME); + ASSERT_GE(session, 0); + + ASSERT_EQ(load_test_data(FALLOCATE_DATA_FS_COPY, buffer, FALLOCATE_BUFFER= _SIZE), 0); + + retrieve_arg.token =3D FALLOCATE_MEMFD_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), = 0); + fd =3D retrieve_arg.fd; + ASSERT_GE(fd, 0); + + ASSERT_EQ(verify_fd_content_read(fd, buffer, FALLOCATE_BUFFER_SIZE), 0); + + ASSERT_EQ(luo_session_finish(session), 0); +} + +/* + * Test that an fallocated memfd is preserved across live update and can be + * written to after being preserved. + */ +TEST(fallocate_memfd) +{ + if (cwd_is_tmpfs()) + SKIP(return, "test saves data to rootfs, cannot run on tmpfs"); + + switch (stage) { + case 1: + fallocate_memfd_stage_1(_metadata); + break; + case 2: + fallocate_memfd_stage_2(_metadata); + break; + default: + TH_LOG("Unknown stage %d\n", stage); + ASSERT_FALSE(true); + } +} + int main(int argc, char *argv[]) { int expected_stage =3D 0; --=20 2.53.0.1213.gd9a14994de-goog