Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3535713ybt; Tue, 23 Jun 2020 04:54:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRppJudG+vgcXfEi4zLKuzC9WD+Do3X/e/wt8TGYj5vyN8ENbis2pMSLuVUITfmOp8u0EV X-Received: by 2002:a05:6402:206b:: with SMTP id bd11mr6873418edb.143.1592913250724; Tue, 23 Jun 2020 04:54:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592913250; cv=none; d=google.com; s=arc-20160816; b=yJ3j2RtFLTZ3JTjXuitJSXI0HjhF1F9WYTfifEZukDptNF2+wRLQ9T33vw3RO4iQbX YwKh9CHVGGG34xUa7bhMrWRmZ8MGTnpDN7UhIuo1KcMAsTce8ORJCWF6Y9tNhNiREU7D L8En0hqnZEsDxwjv8DO1XBZ7OvFencKO23mfJFmj4E2AVq+oXNVe4oK3S7wHofSuV1BE MJQ9ObmV5S8mkBKOqWMJ+pmrOA6OYf5GS3pzlZaakhl/tQehfxSPJSKr9fCXJYWR7BgG rIG16/g4q+EyvJi4dmOXIzr87DWonY1wNY0CL+xnuySaiz0peDUXnRYJTYg12oqFrdWN OqMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=70/IVEhJ02HvpdsdNd9PlWVOivEwQQMsYQTKyuJVw5w=; b=lZVjzgO/5GsqoPl0KX+Ps3iolGhSa2eUm1YJK1QLBhdSU9aghYAppjD1g+wlv1FMZK NhN7J679TOZruxW1XSjQlLP0gzxAYMjmNcySiou3Hou176cXPKANDbeDtbhCdwmDf/Us 5yZu3NxBQY9N7wtLAuMkNobP4EAXOzPcSwWdLSubHVj7ogSyRMc93JR1U0AgjfgqUxIw ohjwmJYy/Mnbo//pbC0+sOZXBpcuP7c8MudgI/Qcxn46P4Zv8PH+JBm7Y/Fh4Nd6x/z6 mCr85gj1jfefaBqKBTXvQwxpjwWqWBAQkaXPw52GZQ2iY1iBV+LZ2px4ABoNfgZF882D yIrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MppwvfZJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f12si2834525ejb.397.2020.06.23.04.53.48; Tue, 23 Jun 2020 04:54:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MppwvfZJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732568AbgFWLt7 (ORCPT + 99 others); Tue, 23 Jun 2020 07:49:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732420AbgFWLt6 (ORCPT ); Tue, 23 Jun 2020 07:49:58 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEB19C061573 for ; Tue, 23 Jun 2020 04:49:58 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id k6so9013398pll.9 for ; Tue, 23 Jun 2020 04:49:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=70/IVEhJ02HvpdsdNd9PlWVOivEwQQMsYQTKyuJVw5w=; b=MppwvfZJn8ecf5EIZhuRbEQ8l3NLLdTBNC06snahwn8RnuLn37zTW3Xh1tgQBJu5Vt zZyxaozNnvbmbkJvjzRixuvhZtfr9BzypqeE7l/aJ6+mEpn+h5IJNmHFc6l0ee/FvwOI Bl7TfpCpdRUnxhNvJmSg5FqkwoCFnVEYKOMeoe4LE1sLzSHyQqXCwHUCpL38FDtoPAOh E0mzT9Toou2nx99Ka5OdaBLolD3f2E9ypbp04kqP1nUuCWYyUKkhv/38fkxmjhwbUb0b pvjqBWy8ekO5F4RnkOQutCDq2i7behQUQ7VGXx3Sx0N6fwI8lTypVzf2fMihGCmML3cx 47wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=70/IVEhJ02HvpdsdNd9PlWVOivEwQQMsYQTKyuJVw5w=; b=iK1151WXiWNShtujGIANoXVP7sIzdFCUS90uaB2T3MkMFpSpGg0xna5WuRA3ItweFD 5WkFtXZDyYFQyqvFUoxc+/MxnXgQaZbOmoI61kwMOYWu/XHv9lsWm/jKPmXwrbnjMG7Z xkzhB8ZPdwrFXLn6IYZQLIFPo4UxiHKDYgLPvb1mEYoaklmcLz9oV+uzkr5vHAZguu2x Fe0iQBkJnTfxWQMOobJvZo/jXh4J11OVi73hpQyePDDyWqDdAQ3gawtJ/A0nYJBtzmAd nkoT7RNGHquJuho5m21mJL9ZQ7lK+IZ0mRqVQKhtYlpGuiPgH0ySBI+taos5Q29mFJDK WJCA== X-Gm-Message-State: AOAM530Igt98/U8c9LRCN0uG8KhkIKDuBp/oMTXEoz7b1sdOrLt4NH3B HU1vEJYfaXAJXGVvzJqiEFA= X-Received: by 2002:a17:902:8b8a:: with SMTP id ay10mr10745807plb.235.1592912997980; Tue, 23 Jun 2020 04:49:57 -0700 (PDT) Received: from jordon-HP-15-Notebook-PC.domain.name ([122.179.62.127]) by smtp.gmail.com with ESMTPSA id cm13sm2290720pjb.5.2020.06.23.04.49.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Jun 2020 04:49:57 -0700 (PDT) From: Souptick Joarder To: jgross@suse.com, boris.ostrovsky@oracle.com, sstabellini@kernel.org Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Souptick Joarder , John Hubbard Subject: [RFC PATCH v2] xen/privcmd: Convert get_user_pages*() to pin_user_pages*() Date: Tue, 23 Jun 2020 17:28:19 +0530 Message-Id: <1592913499-15558-1-git-send-email-jrdr.linux@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In 2019, we introduced pin_user_pages*() and now we are converting get_user_pages*() to the new API as appropriate. [1] & [2] could be referred for more information. This is case 5 as per document [1]. As discussed, pages need to be marked as dirty before unpinned it. Previously, if lock_pages() end up partially mapping pages, it used to return -ERRNO due to which unlock_pages() have to go through each pages[i] till *nr_pages* to validate them. This can be avoided by passing correct number partially mapped pages & -ERRNO separately while returning from lock_pages() due to error. With this fix unlock_pages() doesn't need to validate pages[i] till *nr_pages* for error scenario. [1] Documentation/core-api/pin_user_pages.rst [2] "Explicit pinning of user-space pages": https://lwn.net/Articles/807108/ Signed-off-by: Souptick Joarder Cc: John Hubbard Cc: Boris Ostrovsky --- drivers/xen/privcmd.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c index a250d11..013d23b 100644 --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -580,25 +580,30 @@ static long privcmd_ioctl_mmap_batch( static int lock_pages( struct privcmd_dm_op_buf kbufs[], unsigned int num, - struct page *pages[], unsigned int nr_pages) + struct page *pages[], unsigned int nr_pages, int *errno) { unsigned int i; + int pinned = 0, rc = 0; for (i = 0; i < num; i++) { unsigned int requested; - int pinned; + rc += pinned; requested = DIV_ROUND_UP( offset_in_page(kbufs[i].uptr) + kbufs[i].size, PAGE_SIZE); - if (requested > nr_pages) - return -ENOSPC; + if (requested > nr_pages) { + *errno = -ENOSPC; + return rc; + } - pinned = get_user_pages_fast( + pinned = pin_user_pages_fast( (unsigned long) kbufs[i].uptr, requested, FOLL_WRITE, pages); - if (pinned < 0) - return pinned; + if (pinned < 0) { + *errno = pinned; + return rc; + } nr_pages -= pinned; pages += pinned; @@ -609,15 +614,10 @@ static int lock_pages( static void unlock_pages(struct page *pages[], unsigned int nr_pages) { - unsigned int i; - if (!pages) return; - for (i = 0; i < nr_pages; i++) { - if (pages[i]) - put_page(pages[i]); - } + unpin_user_pages_dirty_lock(pages, nr_pages, 1); } static long privcmd_ioctl_dm_op(struct file *file, void __user *udata) @@ -630,6 +630,7 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata) struct xen_dm_op_buf *xbufs = NULL; unsigned int i; long rc; + int errno = 0; if (copy_from_user(&kdata, udata, sizeof(kdata))) return -EFAULT; @@ -683,9 +684,12 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata) goto out; } - rc = lock_pages(kbufs, kdata.num, pages, nr_pages); - if (rc) + rc = lock_pages(kbufs, kdata.num, pages, nr_pages, &errno); + if (errno < 0) { + nr_pages = rc; + rc = errno; goto out; + } for (i = 0; i < kdata.num; i++) { set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr); -- 1.9.1