Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2682792imm; Sun, 30 Sep 2018 02:30:35 -0700 (PDT) X-Google-Smtp-Source: ACcGV601IUmhzRyrsbBfT88b4RoeC8em+SHyTFOIy2Nc/jN4hH+RFc/lApPC0ZXw8AVk6r/ppm9u X-Received: by 2002:a63:c84c:: with SMTP id l12-v6mr5703077pgi.77.1538299835244; Sun, 30 Sep 2018 02:30:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538299835; cv=none; d=google.com; s=arc-20160816; b=tTW8GFeR7Z2mYjQrAEqTsn/zoG8UW+bz/wy3JKDLfIX9jR/Npp3q/0coI5k7zdu+2G QctBDr1uE624DMvT6OBuF0rCee1EGEYiuN+DjLRK8pWvdKWRfz2XU4oL1b1tgWgiyv5R yqBLCgz2XJtbbmweDfSRGtdRUlx9HPO9shsvhDtdqHvBYCwxUEaZM5B8u1A0A/OD6xTy yeRE+dPuiPpT4b6PK8klCtg4PnwsxsgpxSMhOSB54KjXvZ6TbuuTYAd6Km35RqJ3LoAS ipmsVvmEOq5dPZJmv1MdfOd+vAMwqF2guihMEaIeJ5xKKwJRZOxLWBj1VtuhJt0xWCws kJ1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:subject:cc:to:from:date; bh=o+rk9UCljA9Yy7VKQaIRs5onhQmirXlPNrevDGPyCNg=; b=BMEO9zBEqGPtM9S6PnIaa+ZiFkOcE4u4LcEm+ZqvoddXNK/42qYolC1nwGs1Yskr5N N11M1tOol7BYiYby3L9nj/wVnxy2CdRJv2IU58yOdmQqX1BFl07IlMe5tBZUC54F/xzQ 6oxUBmxoMSZnQuii3q6lI4sx9c65U53id6w+XBYEdd5qXOlwx+Z7AElkhSrK0qXGZCS/ c/vYSe81CZb/lQklHD1nWlR907qykOi9VjCQOe24sESeT7prsgBWl/d9BLyiZljlfUx5 ha/8mOFTAZG4oOcYW0qrtNZ5WQhjMLiW7vbAENauPk2sdg1Xli7CDjZRN60ESOyuo3ly cDHQ== 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=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y11-v6si5193393pgi.121.2018.09.30.02.30.20; Sun, 30 Sep 2018 02:30:35 -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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728018AbeI3QCV (ORCPT + 99 others); Sun, 30 Sep 2018 12:02:21 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:35698 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727818AbeI3QCU (ORCPT ); Sun, 30 Sep 2018 12:02:20 -0400 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w8U9NlbR032206 for ; Sun, 30 Sep 2018 05:30:05 -0400 Received: from e06smtp04.uk.ibm.com (e06smtp04.uk.ibm.com [195.75.94.100]) by mx0b-001b2d01.pphosted.com with ESMTP id 2mtpv47ewx-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sun, 30 Sep 2018 05:30:05 -0400 Received: from localhost by e06smtp04.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sun, 30 Sep 2018 10:30:03 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp04.uk.ibm.com (192.168.101.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Sun, 30 Sep 2018 10:29:59 +0100 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w8U9TwMZ1048964 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sun, 30 Sep 2018 09:29:58 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 04DE942041; Sun, 30 Sep 2018 12:29:40 +0100 (BST) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C2EFB4203F; Sun, 30 Sep 2018 12:29:38 +0100 (BST) Received: from rapoport-lnx (unknown [9.148.204.193]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Sun, 30 Sep 2018 12:29:38 +0100 (BST) Date: Sun, 30 Sep 2018 12:29:55 +0300 From: Mike Rapoport To: Peter Xu Cc: linux-kernel@vger.kernel.org, Shuah Khan , Mike Kravetz , Jerome Glisse , 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: Re: [PATCH v2 2/3] userfaultfd: selftest: generalize read and poll References: <20180930074259.18229-1-peterx@redhat.com> <20180930074259.18229-3-peterx@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180930074259.18229-3-peterx@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-TM-AS-GCONF: 00 x-cbid: 18093009-0016-0000-0000-0000020C61B2 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18093009-0017-0000-0000-0000326393FA Message-Id: <20180930092954.GA4062@rapoport-lnx> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-09-30_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809300102 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 30, 2018 at 03:42:58PM +0800, Peter Xu wrote: > 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 Acked-by: Mike Rapoport > --- > 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 > -- Sincerely yours, Mike.