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 110423A875E; Sat, 4 Apr 2026 10:25:09 +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=1775298310; cv=none; b=ZpAOAu3gbUUWAhzYZT6IhOyRbBUDjnbU/6Us55SC0TUBxGANPATfAIQcnvVsondiLBaJkr0R8eBHoeNjTqL/qR/LbsYObJX18SltOYd3hQaW9vG/Y0K1hzrvMsuddLwo/2hJtOsRjhhIbNbNSvfjaF/E1cGjHjiYaU3WCYins1c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775298310; c=relaxed/simple; bh=jO6gii86DPoGXE2I2GMdnsjc79+Mrmki6SEokudViyo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rmF6ux3+NL5uge88IEBGwkpRmXiN7pT2DcWySldL223bAGfYvxcsIsWS5DoeYdz49g/s0duspa2Am41S23nEUURL87uc4Eow/tK5NkXNj+6L0RL3FLy2dF2H6Cv1AOJnggwEGaRUiL4IELX2capvDpa2Eu94jTGR3J4BevTsaIw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fkrLgO58; 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="fkrLgO58" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDF7BC19421; Sat, 4 Apr 2026 10:25:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775298309; bh=jO6gii86DPoGXE2I2GMdnsjc79+Mrmki6SEokudViyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fkrLgO58PB06qWQQJ4nhbCHIYS3eol7n2ImQc8zSS7GL1MFFSi/ShO+dzK3Ea+dCw XgOxyYoC35yO6hdYUdkndpKG6JcbD9eU8Jdg4IinA7vGzwxh7dk+qWzFo8NxM4E03P mOZtX6UjGrASHvgbsdeiTJZjbwgeZnGQ9smw4ErGAXJbrCQlPXvqcPuRj0Sd9tvIkU M2Kd1BVwOCDPV1mMS0GC47dCdboiHehCwwKWcq0ykc93iBPTmeZ219FmQtEu5DZe0L 9SyelQOQrCZMVtwsn3Wemrfb0K06JIcoJG5mvHtHcMRbmtM+2cjLaHZihLUfom61vh k7k1qp1zFK+0w== 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 v3 3/6] selftests/liveupdate: add test for memfd content preservation Date: Sat, 4 Apr 2026 10:24:46 +0000 Message-ID: <20260404102452.4091740-4-pratyush@kernel.org> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog In-Reply-To: <20260404102452.4091740-1-pratyush@kernel.org> References: <20260404102452.4091740-1-pratyush@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: "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/selftests/liveupdate/Makefile index 051daae55eec..8e2eb6500c07 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -13,6 +13,7 @@ TEST_FILES += do_kexec.sh include ../lib.mk CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -I$(top_srcdir)/tools/include CFLAGS += -Wall -O2 -Wno-unused-function CFLAGS += -MD diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing/selftests/liveupdate/luo_memfd.c index c1f3275963dc..068c7f30c4de 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 */ /* @@ -11,11 +14,14 @@ #include #include +#include #include #include +#include #include #include +#include #include "../kselftest.h" #include "../kselftest_harness.h" @@ -25,9 +31,83 @@ #define STATE_SESSION_NAME "luo-state" #define STATE_MEMFD_TOKEN 1 +#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 = -1; static int stage; +static void memfd_data_stage_1(struct __test_metadata *_metadata) +{ + int fd, session; + char *buffer; + struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) }; + + buffer = malloc(MEMFD_DATA_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session = luo_create_session(luo_fd, MEMFD_DATA_SESSION_NAME); + ASSERT_GE(session, 0); + + fd = 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_SIZE), 0); + + preserve_arg.fd = fd; + preserve_arg.token = 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 = { .size = sizeof(retrieve_arg) }; + + buffer = malloc(MEMFD_DATA_BUFFER_SIZE); + ASSERT_NE(buffer, NULL); + + session = 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_SIZE), 0); + + retrieve_arg.token = MEMFD_DATA_TOKEN; + ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), 0); + fd = 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 = 0; -- 2.53.0.1213.gd9a14994de-goog