From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795477194; b=V4TVw+/sBrAo5lhokT7+YnOXk72fTNt6XK8BSemt9N9/cHnOBRHoiQazIMY4rS2nMvD3 SwHMnxBdbdxOr8nTmXHUSOhMVz2qvMqOI2SjIUY7Jwt15FmOXcEmsC6UkchEI3fkr8Q8Q AdCCaIZnx2I1fYPelTnUDJ4l2qLXmt0rXgSWMDod8R+FDXoq0W2j2PmVsU6s5gVQm4JWG HLmi7hjbqttfDTjE8OUgPDFV2T1VI+CDNjuxJNTB4dd510tJIatDz9NyAoqKlbqgRaq2Q rECeNelsiB85msvPnB1HR2pkemeu4WTGaDbkzVhnCXXVjxSp8l0SakBE1uLRaFcZmjg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795477194; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=iKJ0YhG5KuD0pKI8GbolHfK4XjQIGbL51iUEGtZcrrg=; b=v6QmKnodr0WJhsQTKd1j6Dfjx0PxJ3zXdfpFCSMWgM9fLjrBQzC+v+xwBDG59xtNnDSr mDp3eHoiN14IR5qp62a/vKbQ7Gziuu/s+KPJo3rd0zWo5hUc8A7dGBaCUhv+sTmI90yfn CTcU+lV5UYq/ZOxXd/mpC/pkGrF6SX3Yoa/ekViuwxMcyxzl2wpr0LTmdFFRM2Cggc2bV kyrlLa6j9569JlBMxvSfBYTMrjRYhcUqnNOprh6XQ6orw1PcMcrvnIK4oNq4pQBmZciYz 08er9GAxj1I0TFGXjprqPbRAkXXmqUz+yJs4oN7sJu+V9u/QLnND/SZ7cC9BDeZiBdQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F6ED1zIz; arc=none smtp.remote-ip=209.85.167.199 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ED9C60A2623 for ; Mon, 23 Nov 2026 23:39:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9ED9C60A2623 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F6ED1zIz Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-4f8281066c4so4876488b6e.2 for ; Mon, 23 Nov 2026 15:39:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795477192; x=1796081992; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=iKJ0YhG5KuD0pKI8GbolHfK4XjQIGbL51iUEGtZcrrg=; b=F6ED1zIz8r1lld19IS/X338+YXxpb0n5dzmTrMI8fCaVW1/hATXclD8weCwqmzB5sn f9rDXamyaxbeSryYflUYjL5sssUoLmWHNP3z+gbb1bzhBNehre4N7M0i7Ecypd7iPJ5w ztME/AuQ7n2pYrW1PlPLj7jeb2wXtbNfXr2EJaFjpA/tYjmxYC8XZE9g2qwONiyM99bW bDuu7piEDhrl1d3Hmx+b8HNvvPmfVTtgRT72nRqZd4pazKwe02h1M/AjukNJ3Ik2o+ZW 2Vt7tp1sNbYCDKR4eANfQ1fdvTS82WgwWa1u6ZbC+1txAuCd7J24ZxtGs0/4fzKARDbk G1QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795477192; x=1796081992; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=iKJ0YhG5KuD0pKI8GbolHfK4XjQIGbL51iUEGtZcrrg=; b=hUEszUbY6sYBx0XQ8c7YlXb9CXuJjz6bQsKb+VQgY4EDTnbpMWSrzuMZgM6rnbjS3x b4sK3t4uOr+aZ6kf2+weFMiKTsjP3bmXT/Z5bcYxjix3TJyXUAYQxLhZ9uRzyQ8qVwPm caO6k6YobzzjHsi51ZRglEc1eLtJDx3/Rec/fEfIMYttzntZuWuDbjWVMrKF1Ty6PwTo 8L02NP1eIMGscB6JwKK+gRT7Im1EZ8R/HBu8ykMx1e08NeoRsW4BEqVwMIBWYZmcRnkk rnKoKTnxZSF5AohwiLxlJlAnFXhOQNwlMaL/IqSMZx+v5D8HbkVUYDDUZY/yTUOJ3RLp LlSA== X-Forwarded-Encrypted: i=1; AL+hErEIFDOq7ojpnfAym3maebTHlnSc8CbzgZcIVBgFcmyONhG+diz8ANonYMxeqW1D9A2kz2Sh2p+nkkc=@vger.kernel.org X-Gm-Message-State: APuU4Z+SWUzE0cZHJN4o1FRGgM1/FfCKkdta6zV6BlDzy7YllF74OX6K C4fWXASE8krhAF7g2DJ2P3SHsmLYOwiRgmJvOZEYeRkblD/s1ah7VafnsobbBKrHKiAszAAoFKG noPuf59C3ejTh/Q== X-Received: from ilei3-n2.prod.google.com ([2002:a05:6e02:61c3:20b0:53c:e043:6f2d]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:50a1:b0:55b:f8cc:aef1 with SMTP id 5614622812f47-57a0a930345mr13528096b6e.15.1795477192273; Mon, 23 Nov 2026 15:39:52 -0800 (PST) Date: Mon, 23 Nov 2026 23:39:22 +0000 In-Reply-To: <20261123233924.3970932-1-dmatlack@google.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261123233924.3970932-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261123233924.3970932-18-dmatlack@google.com> Subject: [PATCH v5 17/18] selftests/liveupdate: Test retrieving tmpfs files preserved read-only From: David Matlack To: kexec@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Cc: Alexander Graf , Andrew Morton , Baolin Wang , Christian Brauner , David Hildenbrand , Hugh Dickins , Jonathan Corbet , "Liam R. Howlett" , Lorenzo Stoakes , Michal Hocko , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Randy Dunlap , Samiullah Khawaja , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , David Matlack Content-Type: text/plain; charset="UTF-8" Add test cases to luo_tmpfs for source files preserved through a read-only fd, through a read-only mount, on a read-only superblock, and immutable files. Each of these cases except the first also preserves a sparse file, whose holes are filled when it is preserved. Check that the files are retrieved with their contents. Also check that the size of a preserved file is frozen, even when the file is opened by its path, and that writes within the file are preserved. Signed-off-by: David Matlack --- .../testing/selftests/liveupdate/luo_tmpfs.c | 305 ++++++++++++++++++ 1 file changed, 305 insertions(+) diff --git a/tools/testing/selftests/liveupdate/luo_tmpfs.c b/tools/testing/selftests/liveupdate/luo_tmpfs.c index 27ec750351da..7911ac28188f 100644 --- a/tools/testing/selftests/liveupdate/luo_tmpfs.c +++ b/tools/testing/selftests/liveupdate/luo_tmpfs.c @@ -462,6 +462,13 @@ static int preserve_memfd(struct test_case *tc, unsigned int flags, return ret; } +/* Skip a case whose source file couldn't be preserved in stage 1. */ +static int skip_not_preserved(struct test_case *tc) +{ + ksft_print_msg("%s: The source file was not preserved\n", tc->name); + return KSFT_SKIP; +} + static int drop_privileges(void) { CHECK_EQ(syscall(__NR_setgroups, 0, NULL), 0); @@ -838,6 +845,175 @@ static int linked_target_verify(struct test_case *tc) return KSFT_PASS; } +static int rdonly_source_preserve(struct test_case *tc) +{ + int fd, ret; + + fd = create_source("src/rdonly_source", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + + fd = open("src/rdonly_source", O_RDONLY); + CHECK(fd >= 0); + ret = preserve_fd(fd, tc->pos, tc->token); + close(fd); + + return ret; +} + +/* Create a sparse file at @path, with a hole after the first page. */ +static int create_sparse_source(const char *path, __u64 token) +{ + int fd; + + fd = create_source(path, page_size, token); + if (fd < 0) + return -1; + + if (ftruncate(fd, 2 * page_size)) { + close(fd); + return -1; + } + + return fd; +} + +/* Open @path with O_RDONLY and try to preserve it with @token. */ +static int try_preserve_path(const char *path, __u64 token) +{ + int fd, ret; + + fd = open(path, O_RDONLY); + if (fd < 0) + return -errno; + + ret = luo_session_preserve_fd(session_fd, fd, token); + close(fd); + return ret; +} + +/* Check that the file preserved with @token has the source of @tc. */ +static int check_preserved(struct test_case *tc, __u64 token, size_t size) +{ + int fd; + + fd = retrieve_new(token, "dst"); + CHECK(fd >= 0); + CHECK_EQ(file_size(fd), size); + TRY(check_contents(fd, size, tc->token)); + + close(fd); + return KSFT_PASS; +} + +/* Check that the file preserved with @token is a create_sparse_source(). */ +static int check_sparse_preserved(struct test_case *tc, __u64 token) +{ + unsigned char *p; + int fd; + + fd = retrieve_new(token, "dst"); + CHECK(fd >= 0); + CHECK_EQ(file_size(fd), 2 * page_size); + + /* Preserving the file filled the hole. */ + CHECK_EQ(file_pages(fd), 2); + p = mmap(NULL, 2 * page_size, PROT_READ, MAP_SHARED, fd, 0); + CHECK(p != MAP_FAILED); + TRY(check_range(p, 0, page_size, tc->token)); + TRY(check_range(p, page_size, 2 * page_size, 0)); + + munmap(p, 2 * page_size); + close(fd); + return KSFT_PASS; +} + +static int rdonly_mount_source_preserve(struct test_case *tc) +{ + int fd; + + fd = create_source("src/rdonly_mount_source", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + fd = create_sparse_source("src/rdonly_mount_sparse", tc->token); + CHECK(fd >= 0); + close(fd); + + CHECK_EQ(mkdir("src_ro", 0755), 0); + CHECK_EQ(do_mount("src", "src_ro", NULL, MS_BIND, NULL), 0); + CHECK_EQ(do_mount("none", "src_ro", NULL, + MS_REMOUNT | MS_BIND | MS_RDONLY, NULL), 0); + + /* + * Files can be preserved through a read-only mount. Their holes are + * filled as if by read faults, which work through read-only mounts too. + */ + CHECK_EQ(try_preserve_path("src_ro/rdonly_mount_source", tc->token), 0); + CHECK_EQ(try_preserve_path("src_ro/rdonly_mount_sparse", tc->token + 1), + 0); + return KSFT_PASS; +} + +static int rdonly_mount_source_verify(struct test_case *tc) +{ + CHECK_EQ(mount_tmpfs("dst", 16, NULL), 0); + TRY(check_preserved(tc, tc->token, tc->size)); + TRY(check_sparse_preserved(tc, tc->token + 1)); + return KSFT_PASS; +} + +static int rdonly_sb_source_child(struct test_case *tc) +{ + TRY(drop_privileges()); + + /* The owner can preserve a file whose size can't change anyway... */ + CHECK_EQ(try_preserve_path("src_rosb/own", tc->token + 1), 0); + + /* ... but nobody else can. */ + CHECK(try_preserve_path("src_rosb/other", tc->token + 2) < 0); + return KSFT_PASS; +} + +static int rdonly_sb_source_preserve(struct test_case *tc) +{ + int fd; + + CHECK_EQ(mount_tmpfs("src_rosb", 16, NULL), 0); + fd = create_source("src_rosb/root", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + fd = create_source("src_rosb/own", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + CHECK_EQ(chown("src_rosb/own", NOBODY, NOBODY), 0); + fd = create_source("src_rosb/other", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + fd = create_sparse_source("src_rosb/sparse", tc->token); + CHECK(fd >= 0); + close(fd); + + CHECK_EQ(do_mount(NULL, "src_rosb", NULL, MS_REMOUNT | MS_RDONLY, NULL), + 0); + + CHECK_EQ(try_preserve_path("src_rosb/root", tc->token), 0); + TRY(run_child(rdonly_sb_source_child, tc)); + + /* Holes are filled as if by read faults, even on a read-only sb. */ + CHECK_EQ(try_preserve_path("src_rosb/sparse", tc->token + 3), 0); + return KSFT_PASS; +} + +static int rdonly_sb_source_verify(struct test_case *tc) +{ + CHECK_EQ(mount_tmpfs("dst", 16, NULL), 0); + TRY(check_preserved(tc, tc->token, tc->size)); + TRY(check_preserved(tc, tc->token + 1, tc->size)); + CHECK_EQ(luo_session_retrieve_fd(session_fd, tc->token + 2), -ENOENT); + TRY(check_sparse_preserved(tc, tc->token + 3)); + return KSFT_PASS; +} + /* Set the inode flag @flag, e.g. FS_IMMUTABLE_FL, on the file at @path. */ static int set_inode_flag(const char *path, int flag) { @@ -859,6 +1035,104 @@ static int set_inode_flag(const char *path, int flag) return ret; } +static int immutable_source_preserve(struct test_case *tc) +{ + int fd, ret; + + fd = create_source("src/immutable_source", tc->size, tc->token); + CHECK(fd >= 0); + close(fd); + fd = create_sparse_source("src/immutable_sparse", tc->token); + CHECK(fd >= 0); + close(fd); + + ret = set_inode_flag("src/immutable_source", FS_IMMUTABLE_FL); + if (ret == -ENOTTY || ret == -EOPNOTSUPP) { + ksft_print_msg("%s: FS_IOC_SETFLAGS is not supported\n", + tc->name); + return KSFT_SKIP; + } + CHECK_EQ(ret, 0); + CHECK_EQ(set_inode_flag("src/immutable_sparse", FS_IMMUTABLE_FL), 0); + + /* + * The size of an immutable file can't change anyway, so its owner can + * preserve it. Its holes are filled as if by read faults. + */ + CHECK_EQ(try_preserve_path("src/immutable_source", tc->token), 0); + CHECK_EQ(try_preserve_path("src/immutable_sparse", tc->token + 1), 0); + return KSFT_PASS; +} + +static int immutable_source_verify(struct test_case *tc) +{ + int fd; + + CHECK_EQ(mount_tmpfs("dst", 16, NULL), 0); + fd = retrieve_new(tc->token, "dst"); + if (fd == -ENOENT) + return skip_not_preserved(tc); + CHECK(fd >= 0); + CHECK_EQ(file_size(fd), tc->size); + TRY(check_contents(fd, tc->size, tc->token)); + close(fd); + + TRY(check_sparse_preserved(tc, tc->token + 1)); + return KSFT_PASS; +} + +#define FROZEN_OFFSET 100 +#define FROZEN_DATA "frozen" + +static int frozen_source_preserve(struct test_case *tc) +{ + size_t len = strlen(FROZEN_DATA); + int fd; + + TRY(preserve_file(tc)); + + /* The size of a preserved file can't change, even through its path. */ + fd = open("src/frozen_source", O_RDWR); + CHECK(fd >= 0); + CHECK_ERRNO(ftruncate(fd, tc->size / 2), EPERM); + CHECK_ERRNO(ftruncate(fd, tc->size * 2), EPERM); + CHECK_ERRNO(do_fallocate(fd, 0, 0, tc->size * 2), EPERM); + CHECK_ERRNO(do_fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + 0, page_size), EPERM); + CHECK_EQ(lseek(fd, tc->size, SEEK_SET), tc->size); + CHECK_ERRNO(write(fd, "x", 1), EPERM); + CHECK_EQ(file_size(fd), tc->size); + + /* Writes within the file work, and the data is preserved. */ + CHECK_EQ(lseek(fd, FROZEN_OFFSET, SEEK_SET), FROZEN_OFFSET); + CHECK_EQ(write(fd, FROZEN_DATA, len), len); + + close(fd); + return KSFT_PASS; +} + +static int frozen_source_verify(struct test_case *tc) +{ + size_t len = strlen(FROZEN_DATA); + unsigned char *p; + int fd; + + CHECK_EQ(mount_tmpfs("dst", 16, NULL), 0); + fd = retrieve_new(tc->token, "dst"); + CHECK(fd >= 0); + CHECK_EQ(file_size(fd), tc->size); + + p = mmap(NULL, tc->size, PROT_READ, MAP_SHARED, fd, 0); + CHECK(p != MAP_FAILED); + TRY(check_range(p, 0, FROZEN_OFFSET, tc->token)); + CHECK(!memcmp(p + FROZEN_OFFSET, FROZEN_DATA, len)); + TRY(check_range(p, FROZEN_OFFSET + len, tc->size, tc->token)); + + munmap(p, tc->size); + close(fd); + return KSFT_PASS; +} + static int target_rdonly_verify(struct test_case *tc) { int target, rd_fd; @@ -1319,6 +1593,37 @@ static struct test_case test_cases[] = { .size = SMALL_SIZE, .verify = linked_target_verify, }, + /* Preserving files. */ + { + .name = "rdonly_source", + .size = SMALL_SIZE, + .preserve = rdonly_source_preserve, + .verify = restore_verify, + }, + { + .name = "rdonly_mount_source", + .size = SMALL_SIZE, + .preserve = rdonly_mount_source_preserve, + .verify = rdonly_mount_source_verify, + }, + { + .name = "rdonly_sb_source", + .size = SMALL_SIZE, + .preserve = rdonly_sb_source_preserve, + .verify = rdonly_sb_source_verify, + }, + { + .name = "immutable_source", + .size = SMALL_SIZE, + .preserve = immutable_source_preserve, + .verify = immutable_source_verify, + }, + { + .name = "frozen_source", + .size = SMALL_SIZE, + .preserve = frozen_source_preserve, + .verify = frozen_source_verify, + }, /* The target file. */ { .name = "target_rdonly", -- 2.56.1.809.ga0b61035b5-goog