Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2789405lqp; Mon, 25 Mar 2024 09:14:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVKFZ+qzjPgk90DOmnJEdCHJ+NHN0dfL7jHzRoLEo0V3soyKwpRq9M7eTVjisrnIKNM0mTqqlWlZvotkUxjBIduZ8vh7zRDUbHwCfUT7A== X-Google-Smtp-Source: AGHT+IFobjCPAWNEz/C/1GwSbVleeIPIiHkuqsL1AlS4PrJDrAfyPKyEVqoR3HqtOhcd6PsgL1NR X-Received: by 2002:a17:90b:4382:b0:29d:f581:3c60 with SMTP id in2-20020a17090b438200b0029df5813c60mr5727528pjb.36.1711383271698; Mon, 25 Mar 2024 09:14:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711383271; cv=pass; d=google.com; s=arc-20160816; b=0FrpWy3w1GSk/hIkCqG101NutsCzu8eeH4aAABo5LjeQOKuwwmWBNT35U1mJIEi4kS FFSCoh34dKuJ6JmAPZTG27/TOScfxzwOrxbCF35lk+nIrCkrOvAg9PJz4SAqz0FM8TmV zYHmi4js+mZVHXQsDH4uEA+sd4G39uVTdhVkEvPIrlHWiESiYPGpvbYyEfS++e3vKCU5 JZaoFTwERCrey55xIcADfSPvUUSHqPhxC8z+N7xjV1AffdyCIRnXt4nEaP1X634RJczV NBDbEZe8R2gyJk0G6MUcOZM37U1q1rjSLuPb55bIE9NTSgJadRzccg/DAyUkXzQFsEBt BVXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cpEPWSpCeTwbxNQN5JgorLS4hcdt0dP0Q44OLrNb3L4=; fh=rWcW5SCaRpCC57PFTQcoQDfIJzSkNevcPBdi/zC66HA=; b=feDHymWCvCxpb07XS7YmJTxSnr1sg6STeaf85u/XbDA0lKwXqp0QkPcdXI0tOKSy/X H7kLcRBSNqR7ssIsOPzSNl6IwYhiTvrGrRP96911pTU6lU5rdR7VoSHeXT1ttjjs4ZBy 88KmgUmHdyfpJK9PTFUwYuFIU76nrQCI/+FP4zF0aatc7guSD7mFBKFSsfcJfgCaG1dP aIogJ/8P8tAmuPLdS+zBetyiup9oSh4QRu+3PpEYb5QZhu036oa0Ifet+GxD0o8lyT3P 9s9d2SeZswq3l3kcCp2Ff7Tqu4Cc0r0lVPk1DB0DzO5sEtJH95qYclydxt0+iQ0HBAE6 XRlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iSZkONXR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-117164-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117164-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m2-20020a17090ade0200b0029bb297c136si7817769pjv.67.2024.03.25.09.14.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 09:14:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117164-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iSZkONXR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-117164-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117164-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 039E1341F79 for ; Mon, 25 Mar 2024 16:02:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C6B5512F582; Mon, 25 Mar 2024 13:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iSZkONXR" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 32F1412F36E for ; Mon, 25 Mar 2024 13:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711374099; cv=none; b=saYcXHWbinDY+HvzYPdg9VtcM7cuianlD60crQRlVIBNxsGB3bs5/V9114lGm+mnCnRZJ0Bemx4sw3Yi7SoYjACbx/NuQmfHgS8G5QrfaJURChTJy0F/L4qN2OK+4p5UCO8iKSZP0PjKfNpInueFHkK8K69m6Z9b3WGDcPkJCvY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711374099; c=relaxed/simple; bh=rZ16SU0IrCGHRKMpmlvaaUlMrrEbSltSWDadSv6s3XM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NGCXMkOLM2qqOeJhXRQbSyenNy7Q5VqZEYbtc4K3ZwqhKsdLE0bC8LVAX3OOA1rBm+RlVdFDsgQptlCaeDH3H4mi+dOrivGvI6vhOznPpZ3WFZR8XHUgYtBFOwDGFuuKI98LfvfgoCUpQdeq6ZJW6gy3QGS+J5i0xSSe/MzIObc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iSZkONXR; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 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 --- 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); -- 2.43.2