From patchwork Wed Oct 25 23:50:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 10027359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 09F40603D7 for ; Wed, 25 Oct 2017 23:50:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F185428C67 for ; Wed, 25 Oct 2017 23:50:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E647C28C6A; Wed, 25 Oct 2017 23:50:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D5A528C68 for ; Wed, 25 Oct 2017 23:50:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752085AbdJYXun (ORCPT ); Wed, 25 Oct 2017 19:50:43 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:51818 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752000AbdJYXue (ORCPT ); Wed, 25 Oct 2017 19:50:34 -0400 Received: by mail-pf0-f194.google.com with SMTP id n14so1129243pfh.8; Wed, 25 Oct 2017 16:50:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=JEb71QIYkOmu6LkoA1MdT2cI1jOYWheTL91sOZZ4++4=; b=Hgp1UTh2xxtYOfCZdaf1csn8PUKtNaQF9CzvY2Otlus4U8BCMTKuIuYybZyWc8FMZc xw0Ruxe+YnySH4q0GFIXdcskbm+UJpICMw6qW1iJhYVWm/RHGBSeC2nDCvqEVv9Mp7aD FNb3tMkdTohQyv6C35aH84cD1RPWmDSBZSpgaiC6LSfqztKGxGIj3Nn8wsUDRkS/NVy6 9sQzexq1k/Q8+ND7C9XCbD+GI72lHf2tRqjRcj+Bg6czWv5GV9rgwIdyIRn8bAREZsGh DXiTAvUxpcIuLBD59VJ73Sa9kt82UyqAe3S35pE7ZwU+2YFANvEUiaG7bV2V3sAuO3PI rnhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=JEb71QIYkOmu6LkoA1MdT2cI1jOYWheTL91sOZZ4++4=; b=pl0VgOyZGHp7DflA3KKuygPYe38OdmGayodqOyqXk0F/8Xjfw9rqJYZFwL3vwVed3a vRmZNZhmVMEIccyK1kW93Vi3Pz5Xjuoj1E3MpWjxCXJO24IdYm2m0/tM3hp3GXMBTtvC neOHEid+zKuuUy8Z0UKj9a450HRGRRrz20qC10jn6KLKWKb9Qs7XXuWalAJYqEBen3hl FWE2iCzKIdvtBE2OIrWSOCJ1+ED4NtTVXQF+FgvKwSSLoBMKnBl/9cRVwMwhzIsq3kMc LRVwGVS7Sqzbzavbq5vbryLfKJyz8XRMiNnqQAeawlhf98m30/2CFgGRA4/UdFQRbUp0 +GOg== X-Gm-Message-State: AMCzsaXm255Qm0Q/iIqSHVJ0j7oQhyKA3ZcqMkd4gjNwYYN6wPhXzQSm jN1oLDt6yLAZJ8arIDgy5zw= X-Google-Smtp-Source: ABhQp+QPN1Suira1CzikuW+7gx6OAnbTgVvHiut5RdB2IFebcfsWC15xjlLYABMKV2BFQsLXAxNbDQ== X-Received: by 10.99.124.78 with SMTP id l14mr3403838pgn.248.1508975433946; Wed, 25 Oct 2017 16:50:33 -0700 (PDT) Received: from localhost.localdomain ([65.152.152.74]) by smtp.gmail.com with ESMTPSA id h67sm7983660pfh.74.2017.10.25.16.50.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 25 Oct 2017 16:50:33 -0700 (PDT) From: Andrei Vagin To: Andrew Morton , Alexander Viro Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, criu@openvz.org, Andrei Vagin , Arnd Bergmann , Pavel Emelyanov , Michael Kerrisk , Thomas Gleixner , Josh Triplett , Jann Horn Subject: [PATCH 3/3] test: add a test for the process_vmsplice syscall Date: Wed, 25 Oct 2017 16:50:03 -0700 Message-Id: <20171025235003.24262-3-avagin@openvz.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171025235003.24262-1-avagin@openvz.org> References: <20171025235003.24262-1-avagin@openvz.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This test checks that process_vmsplice() can splice pages from a remote process and returns EFAULT, if process_vmsplice() tries to splice pages by an unaccessiable address. Signed-off-by: Andrei Vagin --- tools/testing/selftests/process_vmsplice/Makefile | 5 + .../process_vmsplice/process_vmsplice_test.c | 188 +++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 tools/testing/selftests/process_vmsplice/Makefile create mode 100644 tools/testing/selftests/process_vmsplice/process_vmsplice_test.c diff --git a/tools/testing/selftests/process_vmsplice/Makefile b/tools/testing/selftests/process_vmsplice/Makefile new file mode 100644 index 000000000000..246d5a7dfed6 --- /dev/null +++ b/tools/testing/selftests/process_vmsplice/Makefile @@ -0,0 +1,5 @@ +CFLAGS += -I../../../../usr/include/ + +TEST_GEN_PROGS := process_vmsplice_test + +include ../lib.mk diff --git a/tools/testing/selftests/process_vmsplice/process_vmsplice_test.c b/tools/testing/selftests/process_vmsplice/process_vmsplice_test.c new file mode 100644 index 000000000000..8abf59b9c567 --- /dev/null +++ b/tools/testing/selftests/process_vmsplice/process_vmsplice_test.c @@ -0,0 +1,188 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#ifndef __NR_process_vmsplice +#define __NR_process_vmsplice 333 +#endif + +#define pr_err(fmt, ...) \ + ({ \ + fprintf(stderr, "%s:%d:" fmt, \ + __func__, __LINE__, ##__VA_ARGS__); \ + KSFT_FAIL; \ + }) +#define pr_perror(fmt, ...) pr_err(fmt ": %m\n", ##__VA_ARGS__) +#define fail(fmt, ...) pr_err("FAIL:" fmt, ##__VA_ARGS__) + +static ssize_t process_vmsplice(pid_t pid, int fd, const struct iovec *iov, + unsigned long nr_segs, unsigned int flags) +{ + return syscall(__NR_process_vmsplice, pid, fd, iov, nr_segs, flags); + +} + +#define MEM_SIZE (4096 * 100) +#define MEM_WRONLY_SIZE (4096 * 10) + +int main(int argc, char **argv) +{ + char *addr, *addr_wronly; + int p[2]; + struct iovec iov[2]; + char buf[4096]; + int status, ret; + pid_t pid; + + ksft_print_header(); + + addr = mmap(0, MEM_SIZE, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (addr == MAP_FAILED) + return pr_perror("Unable to create a mapping"); + + addr_wronly = mmap(0, MEM_WRONLY_SIZE, PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (addr_wronly == MAP_FAILED) + return pr_perror("Unable to create a write-only mapping"); + + if (pipe(p)) + return pr_perror("Unable to create a pipe"); + + pid = fork(); + if (pid < 0) + return pr_perror("Unable to fork"); + + if (pid == 0) { + addr[0] = 'C'; + addr[4096 + 128] = 'A'; + addr[4096 + 128 + 4096 - 1] = 'B'; + + if (prctl(PR_SET_PDEATHSIG, SIGKILL)) + return pr_perror("Unable to set PR_SET_PDEATHSIG"); + if (write(p[1], "c", 1) != 1) + return pr_perror("Unable to write data into pipe"); + + while (1) + sleep(1); + return 1; + } + if (read(p[0], buf, 1) != 1) { + pr_perror("Unable to read data from pipe"); + kill(pid, SIGKILL); + wait(&status); + return 1; + } + + munmap(addr, MEM_SIZE); + munmap(addr_wronly, MEM_WRONLY_SIZE); + + iov[0].iov_base = addr; + iov[0].iov_len = 1; + + iov[1].iov_base = addr + 4096 + 128; + iov[1].iov_len = 4096; + + /* check one iovec */ + if (process_vmsplice(pid, p[1], iov, 1, SPLICE_F_GIFT) != 1) + return pr_perror("Unable to splice pages"); + + if (read(p[0], buf, 1) != 1) + return pr_perror("Unable to read from pipe"); + + if (buf[0] != 'C') + ksft_test_result_fail("Get wrong data\n"); + else + ksft_test_result_pass("Check process_vmsplice with one vec\n"); + + /* check two iovec-s */ + if (process_vmsplice(pid, p[1], iov, 2, SPLICE_F_GIFT) != 4097) + return pr_perror("Unable to spice pages\n"); + + if (read(p[0], buf, 1) != 1) + return pr_perror("Unable to read from pipe\n"); + + if (buf[0] != 'C') + ksft_test_result_fail("Get wrong data\n"); + + if (read(p[0], buf, 4096) != 4096) + return pr_perror("Unable to read from pipe\n"); + + if (buf[0] != 'A' || buf[4095] != 'B') + ksft_test_result_fail("Get wrong data\n"); + else + ksft_test_result_pass("check process_vmsplice with two vecs\n"); + + /* check how an unreadable region in a second vec is handled */ + iov[0].iov_base = addr; + iov[0].iov_len = 1; + + iov[1].iov_base = addr_wronly + 5; + iov[1].iov_len = 1; + + if (process_vmsplice(pid, p[1], iov, 2, SPLICE_F_GIFT) != 1) + return pr_perror("Unable to splice data"); + + if (read(p[0], buf, 1) != 1) + return pr_perror("Unable to read form pipe"); + + if (buf[0] != 'C') + ksft_test_result_fail("Get wrong data\n"); + else + ksft_test_result_pass("unreadable region in a second vec\n"); + + /* check how an unreadable region in a first vec is handled */ + errno = 0; + if (process_vmsplice(pid, p[1], iov + 1, 1, SPLICE_F_GIFT) != -1 || + errno != EFAULT) + ksft_test_result_fail("Got anexpected errno %d\n", errno); + else + ksft_test_result_pass("splice as much as possible\n"); + + iov[0].iov_base = addr; + iov[0].iov_len = 1; + + iov[1].iov_base = addr; + iov[1].iov_len = MEM_SIZE; + + /* splice as much as possible */ + ret = process_vmsplice(pid, p[1], iov, 2, + SPLICE_F_GIFT | SPLICE_F_NONBLOCK); + if (ret != 4096 * 15 + 1) /* by default a pipe can fit 16 pages */ + return pr_perror("Unable to splice pages"); + + while (ret > 0) { + int len; + + len = read(p[0], buf, 4096); + if (len < 0) + return pr_perror("Unable to read data"); + if (len > ret) + return pr_err("Read more than expected\n"); + ret -= len; + } + ksft_test_result_pass("splice as much as possible\n"); + + if (kill(pid, SIGTERM)) + return pr_perror("Unable to kill a child process"); + status = -1; + if (wait(&status) < 0) + return pr_perror("Unable to wait a child process"); + if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGTERM) + return pr_err("The child exited with an unexpected code %d\n", + status); + + if (ksft_get_fail_cnt()) + return ksft_exit_fail(); + return ksft_exit_pass(); +}