Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2615141imm; Sun, 30 Sep 2018 00:43:42 -0700 (PDT) X-Google-Smtp-Source: ACcGV62h07LxQqo3SIiwI0id4znFwcg0lOgQDw8XScX5Wf1Jj8VFtrNv+94uSe1HYS2/g5cYqpT0 X-Received: by 2002:a17:902:b612:: with SMTP id b18-v6mr6263865pls.92.1538293422586; Sun, 30 Sep 2018 00:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538293422; cv=none; d=google.com; s=arc-20160816; b=whQb+ENM1JZm81H3FEw745+BICdWSGUR0jk7FFpGIlqVVXtHq50Z/mOxhMr9/7ySzQ lFSenSNfBK2VMkPV486vWEphV0OFMYfLjjLeEgAzN+uOfu6t8/zlaN4i+HGsWz2AU+Fz PlckaFBsvsYGhZa5YGoxnPp/BDzbonQa77mMpSQ/VBpLCbVV9t53apPKHJ0qoROJE3t8 OyhjdMqRfu4jz3a8M2O7n3NOchxkFL3NishTPk0MAejzqkthvJG3e4LMLJ80yRNf7Ix9 sBfBJtbf/NKn4Le4qVQCl3IvWHkUylJ/YMhV8pwyXU63GfvQQ/zZ6QpK1/KmrqgwNfkb wJzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=9n1oKLJ3RGsLL5H5sw+MzDdr6BZPb2MRjost1v5zfrc=; b=eudlkcRkRyqfkvTOi+Q4hD7xS122NfryPmSv/1K5+zQ+nY5MU5HU3Eybla9nVxAVLy d3m2M4EhNefyZdCP2N4/DJ5EbVTrCAf8LMHdd2ml8dF7BcqYhMDh8SWJwSmaNo0ysCmN QeVR+a9bhUYnk1EKEFCP65CaX5wja/FQBg/sUpap0apGfw9Wh3dn3GZMN3XKNuW1V5AY d+r58zGqM7IMfzmIaIHuAs0VwzNRgZGy0jNEdcUtf6QunlY8pnxxceV31bxH1mnXkjX+ AQETM9gByV19QLi/SDxw+2Xoeror/qIMVvZn1o9hEsbrKZYH1ZBNiOy24dn8+6/4zTxW /9TQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x3-v6si8812119pgb.329.2018.09.30.00.43.28; Sun, 30 Sep 2018 00:43:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728131AbeI3OPL (ORCPT + 99 others); Sun, 30 Sep 2018 10:15:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36532 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727651AbeI3OPL (ORCPT ); Sun, 30 Sep 2018 10:15:11 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C26BA81F0B; Sun, 30 Sep 2018 07:43:16 +0000 (UTC) Received: from xz-x1.nay.redhat.com (dhcp-14-128.nay.redhat.com [10.66.14.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 786306016F; Sun, 30 Sep 2018 07:43:12 +0000 (UTC) From: Peter Xu To: linux-kernel@vger.kernel.org Cc: Shuah Khan , Mike Kravetz , Mike Rapoport , Jerome Glisse , peterx@redhat.com, linux-mm@kvack.org, Zi Yan , "Kirill A . Shutemov" , linux-kselftest@vger.kernel.org, Shaohua Li , Andrea Arcangeli , "Dr . David Alan Gilbert" , Andrew Morton Subject: [PATCH v2 2/3] userfaultfd: selftest: generalize read and poll Date: Sun, 30 Sep 2018 15:42:58 +0800 Message-Id: <20180930074259.18229-3-peterx@redhat.com> In-Reply-To: <20180930074259.18229-1-peterx@redhat.com> References: <20180930074259.18229-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Sun, 30 Sep 2018 07:43:16 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We do very similar things in read and poll modes, but we're copying the codes around. Share the codes properly on reading the message and handling the page fault to make the code cleaner. Meanwhile this solves previous mismatch of behaviors between the two modes on that the old code: - did not check EAGAIN case in read() mode - ignored BOUNCE_VERIFY check in read() mode Signed-off-by: Peter Xu --- tools/testing/selftests/vm/userfaultfd.c | 77 +++++++++++++----------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 5ff3a4f9173e..7a8c6937cc67 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -451,6 +451,43 @@ static int copy_page(int ufd, unsigned long offset) return __copy_page(ufd, offset, false); } +static int uffd_read_msg(int ufd, struct uffd_msg *msg) +{ + int ret = read(uffd, msg, sizeof(*msg)); + + if (ret != sizeof(*msg)) { + if (ret < 0) { + if (errno == EAGAIN) + return 1; + else + perror("blocking read error"), exit(1); + } else { + fprintf(stderr, "short read\n"), exit(1); + } + } + + return 0; +} + +/* Return 1 if page fault handled by us; otherwise 0 */ +static int uffd_handle_page_fault(struct uffd_msg *msg) +{ + unsigned long offset; + + if (msg->event != UFFD_EVENT_PAGEFAULT) + fprintf(stderr, "unexpected msg event %u\n", + msg->event), exit(1); + + if (bounces & BOUNCE_VERIFY && + msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) + fprintf(stderr, "unexpected write fault\n"), exit(1); + + offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; + offset &= ~(page_size-1); + + return copy_page(uffd, offset); +} + static void *uffd_poll_thread(void *arg) { unsigned long cpu = (unsigned long) arg; @@ -458,7 +495,6 @@ static void *uffd_poll_thread(void *arg) struct uffd_msg msg; struct uffdio_register uffd_reg; int ret; - unsigned long offset; char tmp_chr; unsigned long userfaults = 0; @@ -482,25 +518,15 @@ static void *uffd_poll_thread(void *arg) if (!(pollfd[0].revents & POLLIN)) fprintf(stderr, "pollfd[0].revents %d\n", pollfd[0].revents), exit(1); - ret = read(uffd, &msg, sizeof(msg)); - if (ret < 0) { - if (errno == EAGAIN) - continue; - perror("nonblocking read error"), exit(1); - } + if (uffd_read_msg(uffd, &msg)) + continue; switch (msg.event) { default: fprintf(stderr, "unexpected msg event %u\n", msg.event), exit(1); break; case UFFD_EVENT_PAGEFAULT: - if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) - fprintf(stderr, "unexpected write fault\n"), exit(1); - offset = (char *)(unsigned long)msg.arg.pagefault.address - - area_dst; - offset &= ~(page_size-1); - if (copy_page(uffd, offset)) - userfaults++; + userfaults += uffd_handle_page_fault(&msg); break; case UFFD_EVENT_FORK: close(uffd); @@ -528,8 +554,6 @@ static void *uffd_read_thread(void *arg) { unsigned long *this_cpu_userfaults; struct uffd_msg msg; - unsigned long offset; - int ret; this_cpu_userfaults = (unsigned long *) arg; *this_cpu_userfaults = 0; @@ -538,24 +562,9 @@ static void *uffd_read_thread(void *arg) /* from here cancellation is ok */ for (;;) { - ret = read(uffd, &msg, sizeof(msg)); - if (ret != sizeof(msg)) { - if (ret < 0) - perror("blocking read error"), exit(1); - else - fprintf(stderr, "short read\n"), exit(1); - } - if (msg.event != UFFD_EVENT_PAGEFAULT) - fprintf(stderr, "unexpected msg event %u\n", - msg.event), exit(1); - if (bounces & BOUNCE_VERIFY && - msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) - fprintf(stderr, "unexpected write fault\n"), exit(1); - offset = (char *)(unsigned long)msg.arg.pagefault.address - - area_dst; - offset &= ~(page_size-1); - if (copy_page(uffd, offset)) - (*this_cpu_userfaults)++; + if (uffd_read_msg(uffd, &msg)) + continue; + (*this_cpu_userfaults) += uffd_handle_page_fault(&msg); } return (void *)NULL; } -- 2.17.1