From patchwork Mon Mar 25 13:41:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13602230 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FEDEC54E64 for ; Mon, 25 Mar 2024 13:41:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21F7D6B0093; Mon, 25 Mar 2024 09:41:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D2856B0095; Mon, 25 Mar 2024 09:41:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0210F6B0096; Mon, 25 Mar 2024 09:41:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E3A406B0093 for ; Mon, 25 Mar 2024 09:41:39 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9E66BA01FA for ; Mon, 25 Mar 2024 13:41:39 +0000 (UTC) X-FDA: 81935673918.28.62075A3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id CF9434001A for ; Mon, 25 Mar 2024 13:41:37 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iSZkONXR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711374097; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cpEPWSpCeTwbxNQN5JgorLS4hcdt0dP0Q44OLrNb3L4=; b=oE+6uIa2Mb33spGXu5+WxH+yTpMPDgjTVS3RwsNtuO1WpZSv9LBCTrjdMottIsjaYY/jlM AV2UQSvrZYnnO4euJP49aqCjofgqZOL71gj3IEK2tw56B0X9lJViuBDyaMjwNNLT2yZfMc amsuGs7sTAtZDukNJas36he9aRVjU4I= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iSZkONXR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711374097; a=rsa-sha256; cv=none; b=ZmjyDumpg87LKo9bMrdENFKo2mRvsmnL99WVQzWfj+An33KiFnluCXOnpLOZ4cHvXX1Du8 G6yN9SbH0VCoA6wahzx84sA4Mbpo/SZIAjLk3BdrYcredkZY4KsMwv3wYbrEVONZE5DzsQ sk0SQ4nK5n3pXQHG8dzwZi0omluWbaU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711374097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cpEPWSpCeTwbxNQN5JgorLS4hcdt0dP0Q44OLrNb3L4=; b=iSZkONXRerQZMTaJGM8WiKLxH/0EghWhtQl6tgElHySIQCM2p2IAxlOfAI3ielGGgf/3y4 hUOmwDu8HyjJpf0u4H1CiDiEwEpbEegBofatjbj42GlBoO4kRBk5v6J/I+0nXdWAMM58Fa APWkkEaJhvwMTh00yT/yAyohqTuCd8s= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-398-TiJUf4AYOGuuhRDZm9FyBA-1; Mon, 25 Mar 2024 09:41:31 -0400 X-MC-Unique: TiJUf4AYOGuuhRDZm9FyBA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 696A73CBD4E7; Mon, 25 Mar 2024 13:41:31 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.143]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0EA692166B31; Mon, 25 Mar 2024 13:41:28 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Rapoport , Miklos Szeredi , Lorenzo Stoakes , xingwei lee , yue sun Subject: [PATCH v1 2/3] selftests/memfd_secret: add vmsplice() test Date: Mon, 25 Mar 2024 14:41:13 +0100 Message-ID: <20240325134114.257544-3-david@redhat.com> In-Reply-To: <20240325134114.257544-1-david@redhat.com> References: <20240325134114.257544-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspamd-Queue-Id: CF9434001A X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 4aaz5oxrmu3t3kffhzopsre3f63pa54u X-HE-Tag: 1711374097-859320 X-HE-Meta: U2FsdGVkX1/b+Zg10PRfyDV4lvTaoTBzBk0fkQpVNuQ/l0jLTPWMl1r7my+Vfl+0fJRHIbx+FOk9M9qhvkRzlgBgTnc5TL4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OM+T/DqlCNnhAmYriRHDJBRNfZW2aSOgSm0/gXN2Y/dJYua8zMMzuuYtEYwHjPG2nTEwEyp5MFdKX8+dlvvZVS1MJEE8Bw4BxtldyHHVtlGLCxKHDJA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Let's add a simple reproducer for a scneario where GUP-fast could succeed on secretmem folios, making vmsplice() succeed instead of failing. The reproducer is based on a reproducer [1] by Miklos Szeredi. Perform the ftruncate() only once, and check the return value. For some reason, vmsplice() reliably fails (making the test succeed) when we move the test_vmsplice() call after test_process_vm_read() / test_ptrace(). Properly cleaning up in test_remote_access(), which is not part of this change, won't change that behavior. Therefore, run the vmsplice() test for now first -- something is a bit off once we involve fork(). [1] https://lkml.kernel.org/r/CAJfpegt3UCsMmxd0taOY11Uaw5U=eS1fE5dn0wZX3HF0oy8-oQ@mail.gmail.com Signed-off-by: David Hildenbrand Reviewed-by: Mike Rapoport (IBM) --- tools/testing/selftests/mm/memfd_secret.c | 44 +++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/memfd_secret.c b/tools/testing/selftests/mm/memfd_secret.c index 9b298f6a04b3..0acbdcf8230e 100644 --- a/tools/testing/selftests/mm/memfd_secret.c +++ b/tools/testing/selftests/mm/memfd_secret.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "../kselftest.h" @@ -83,6 +84,43 @@ static void test_mlock_limit(int fd) pass("mlock limit is respected\n"); } +static void test_vmsplice(int fd) +{ + ssize_t transferred; + struct iovec iov; + int pipefd[2]; + char *mem; + + if (pipe(pipefd)) { + fail("pipe failed: %s\n", strerror(errno)); + return; + } + + mem = mmap(NULL, page_size, prot, mode, fd, 0); + if (mem == MAP_FAILED) { + fail("Unable to mmap secret memory\n"); + goto close_pipe; + } + + /* + * vmsplice() may use GUP-fast, which must also fail. Prefault the + * page table, so GUP-fast could find it. + */ + memset(mem, PATTERN, page_size); + + iov.iov_base = mem; + iov.iov_len = page_size; + transferred = vmsplice(pipefd[1], &iov, 1, 0); + + ksft_test_result(transferred < 0 && errno == EFAULT, + "vmsplice is blocked as expected\n"); + + munmap(mem, page_size); +close_pipe: + close(pipefd[0]); + close(pipefd[1]); +} + static void try_process_vm_read(int fd, int pipefd[2]) { struct iovec liov, riov; @@ -187,7 +225,6 @@ static void test_remote_access(int fd, const char *name, return; } - ftruncate(fd, page_size); memset(mem, PATTERN, page_size); if (write(pipefd[1], &mem, sizeof(mem)) < 0) { @@ -258,7 +295,7 @@ static void prepare(void) strerror(errno)); } -#define NUM_TESTS 4 +#define NUM_TESTS 5 int main(int argc, char *argv[]) { @@ -277,9 +314,12 @@ int main(int argc, char *argv[]) ksft_exit_fail_msg("memfd_secret failed: %s\n", strerror(errno)); } + if (ftruncate(fd, page_size)) + ksft_exit_fail_msg("ftruncate failed: %s\n", strerror(errno)); test_mlock_limit(fd); test_file_apis(fd); + test_vmsplice(fd); test_process_vm_read(fd); test_ptrace(fd);