linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: John Hubbard <jhubbard@nvidia.com>,
	john.hubbard@gmail.com, Andrew Morton <akpm@linux-foundation.org>
Cc: devel@driverdev.osuosl.org, "Dave Chinner" <david@fromorbit.com>,
	"Christoph Hellwig" <hch@infradead.org>,
	"Dan Williams" <dan.j.williams@intel.com>,
	"Ira Weiny" <ira.weiny@intel.com>,
	x86@kernel.org, linux-mm@kvack.org,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org,
	linux-arm-kernel@lists.infradead.org,
	linux-rpi-kernel@lists.infradead.org, devel@lists.orangefs.org,
	xen-devel@lists.xenproject.org,
	"Boris Ostrovsky" <boris.ostrovsky@oracle.com>,
	rds-devel@oss.oracle.com, "Jérôme Glisse" <jglisse@redhat.com>,
	"Jan Kara" <jack@suse.cz>,
	ceph-devel@vger.kernel.org, kvm@vger.kernel.org,
	linux-block@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-fbdev@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>,
	linux-media@vger.kernel.org, linux-nfs@vger.kernel.org,
	linux-rdma@vger.kernel.org, linux-xfs@vger.kernel.org,
	netdev@vger.kernel.org, sparclinux@vger.kernel.org,
	"Jason Gunthorpe" <jgg@ziepe.ca>
Subject: Re: [PATCH 20/34] xen: convert put_page() to put_user_page*()
Date: Fri, 2 Aug 2019 08:10:07 +0200	[thread overview]
Message-ID: <d4931311-db01-e8c3-0f8c-d64685dc2143@suse.com> (raw)
In-Reply-To: <d5140833-e9ee-beb5-ff0a-2d13a4fe819f@nvidia.com>

[-- Attachment #1: Type: text/plain, Size: 2441 bytes --]

On 02.08.19 07:48, John Hubbard wrote:
> On 8/1/19 9:36 PM, Juergen Gross wrote:
>> On 02.08.19 04:19, john.hubbard@gmail.com wrote:
>>> From: John Hubbard <jhubbard@nvidia.com>
> ...
>>> diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
>>> index 2f5ce7230a43..29e461dbee2d 100644
>>> --- a/drivers/xen/privcmd.c
>>> +++ b/drivers/xen/privcmd.c
>>> @@ -611,15 +611,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]);
>>> -    }
>>> +    put_user_pages(pages, nr_pages);
>>
>> You are not handling the case where pages[i] is NULL here. Or am I
>> missing a pending patch to put_user_pages() here?
>>
> 
> Hi Juergen,
> 
> You are correct--this no longer handles the cases where pages[i]
> is NULL. It's intentional, though possibly wrong. :)
> 
> I see that I should have added my standard blurb to this
> commit description. I missed this one, but some of the other patches
> have it. It makes the following, possibly incorrect claim:
> 
> "This changes the release code slightly, because each page slot in the
> page_list[] array is no longer checked for NULL. However, that check
> was wrong anyway, because the get_user_pages() pattern of usage here
> never allowed for NULL entries within a range of pinned pages."
> 
> The way I've seen these page arrays used with get_user_pages(),
> things are either done single page, or with a contiguous range. So
> unless I'm missing a case where someone is either
> 
> a) releasing individual pages within a range (and thus likely messing
> up their count of pages they have), or
> 
> b) allocating two gup ranges within the same pages[] array, with a
> gap between the allocations,
> 
> ...then it should be correct. If so, then I'll add the above blurb
> to this patch's commit description.
> 
> If that's not the case (both here, and in 3 or 4 other patches in this
> series, then as you said, I should add NULL checks to put_user_pages()
> and put_user_pages_dirty_lock().

In this case it is not correct, but can easily be handled. The NULL case
can occur only in an error case with the pages array filled partially or
not at all.

I'd prefer something like the attached patch here.


Juergen

[-- Attachment #2: gup.patch --]
[-- Type: text/x-patch, Size: 2001 bytes --]

diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
index 2f5ce7230a43..12bd3154126d 100644
--- a/drivers/xen/privcmd.c
+++ b/drivers/xen/privcmd.c
@@ -582,10 +582,11 @@ 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)
 {
-	unsigned int i;
+	unsigned int i, free = *nr_pages;
 
+	*nr_pages = 0;
 	for (i = 0; i < num; i++) {
 		unsigned int requested;
 		int pinned;
@@ -593,35 +594,22 @@ static int lock_pages(
 		requested = DIV_ROUND_UP(
 			offset_in_page(kbufs[i].uptr) + kbufs[i].size,
 			PAGE_SIZE);
-		if (requested > nr_pages)
+		if (requested > free)
 			return -ENOSPC;
 
 		pinned = get_user_pages_fast(
 			(unsigned long) kbufs[i].uptr,
-			requested, FOLL_WRITE, pages);
+			requested, FOLL_WRITE, pages + *nr_pages);
 		if (pinned < 0)
 			return pinned;
 
-		nr_pages -= pinned;
-		pages += pinned;
+		free -= pinned;
+		*nr_pages += pinned;
 	}
 
 	return 0;
 }
 
-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]);
-	}
-}
-
 static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
 {
 	struct privcmd_data *data = file->private_data;
@@ -681,11 +669,12 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
 
 	xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
 	if (!xbufs) {
+		nr_pages = 0;
 		rc = -ENOMEM;
 		goto out;
 	}
 
-	rc = lock_pages(kbufs, kdata.num, pages, nr_pages);
+	rc = lock_pages(kbufs, kdata.num, pages, &nr_pages);
 	if (rc)
 		goto out;
 
@@ -699,7 +688,8 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
 	xen_preemptible_hcall_end();
 
 out:
-	unlock_pages(pages, nr_pages);
+	if (pages)
+		put_user_pages(pages, nr_pages);
 	kfree(xbufs);
 	kfree(pages);
 	kfree(kbufs);

  reply	other threads:[~2019-08-02  6:10 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-02  2:19 [PATCH 00/34] put_user_pages(): miscellaneous call sites john.hubbard
2019-08-02  2:19 ` [PATCH 01/34] mm/gup: add make_dirty arg to put_user_pages_dirty_lock() john.hubbard
2019-08-02  2:19 ` [PATCH 02/34] net/rds: convert put_page() to put_user_page*() john.hubbard
2019-08-02  2:19 ` [PATCH 03/34] net/ceph: " john.hubbard
2019-08-02 22:32   ` Jeff Layton
2019-08-02  2:19 ` [PATCH 04/34] x86/kvm: " john.hubbard
2019-08-02  2:19 ` [PATCH 05/34] drm/etnaviv: convert release_pages() to put_user_pages() john.hubbard
2019-08-02  2:19 ` [PATCH 06/34] drm/i915: convert put_page() to put_user_page*() john.hubbard
2019-08-02  9:19   ` Joonas Lahtinen
2019-08-02 18:48     ` John Hubbard
2019-08-03 20:03       ` John Hubbard
2019-08-02  2:19 ` [PATCH 07/34] drm/radeon: " john.hubbard
2019-08-02  2:19 ` [PATCH 08/34] media/ivtv: " john.hubbard
2019-08-02  2:19 ` [PATCH 09/34] media/v4l2-core/mm: " john.hubbard
2019-08-02  2:19 ` [PATCH 10/34] genwqe: " john.hubbard
2019-08-03  7:06   ` Greg Kroah-Hartman
2019-08-02  2:19 ` [PATCH 11/34] scif: " john.hubbard
2019-08-02  2:19 ` [PATCH 12/34] vmci: " john.hubbard
2019-08-02  2:19 ` [PATCH 13/34] rapidio: " john.hubbard
2019-08-02  2:19 ` [PATCH 14/34] oradax: " john.hubbard
2019-08-02  2:19 ` [PATCH 15/34] staging/vc04_services: " john.hubbard
2019-08-03  7:06   ` Greg Kroah-Hartman
2019-08-02  2:19 ` [PATCH 16/34] drivers/tee: " john.hubbard
2019-08-02  6:29   ` Jens Wiklander
2019-08-02 18:51     ` John Hubbard
2019-08-02  2:19 ` [PATCH 17/34] vfio: " john.hubbard
2019-08-02  2:19 ` [PATCH 18/34] fbdev/pvr2fb: " john.hubbard
2019-08-02  2:19 ` [PATCH 19/34] fsl_hypervisor: " john.hubbard
2019-08-02  2:19 ` [PATCH 20/34] xen: " john.hubbard
2019-08-02  4:36   ` Juergen Gross
2019-08-02  5:48     ` John Hubbard
2019-08-02  6:10       ` Juergen Gross [this message]
2019-08-02 16:09         ` Weiny, Ira
2019-08-02 19:25           ` John Hubbard
2019-08-02  2:19 ` [PATCH 21/34] fs/exec.c: " john.hubbard
2019-08-02  2:19 ` [PATCH 22/34] orangefs: " john.hubbard
2019-08-02  2:19 ` [PATCH 23/34] uprobes: " john.hubbard
2019-08-02  2:19 ` [PATCH 24/34] futex: " john.hubbard
2019-08-02  2:19 ` [PATCH 25/34] mm/frame_vector.c: " john.hubbard
2019-08-02  2:19 ` [PATCH 26/34] mm/gup_benchmark.c: " john.hubbard
2019-08-02 14:19   ` Keith Busch
2019-08-02  2:19 ` [PATCH 27/34] mm/memory.c: " john.hubbard
2019-08-02  2:19 ` [PATCH 28/34] mm/madvise.c: " john.hubbard
2019-08-02  2:20 ` [PATCH 29/34] mm/process_vm_access.c: " john.hubbard
2019-08-02  2:20 ` [PATCH 30/34] crypt: " john.hubbard
2019-08-02  2:20 ` [PATCH 31/34] nfs: " john.hubbard
2019-08-03  1:27   ` Calum Mackay
2019-08-03  1:41     ` John Hubbard
2019-08-04 23:28       ` Calum Mackay
2019-08-02  2:20 ` [PATCH 32/34] goldfish_pipe: " john.hubbard
2019-08-02  2:20 ` [PATCH 33/34] kernel/events/core.c: " john.hubbard
2019-08-02  2:20 ` [PATCH 34/34] fs/binfmt_elf: " john.hubbard
2019-08-02  9:12 ` [PATCH 00/34] put_user_pages(): miscellaneous call sites Michal Hocko
2019-08-02 12:41   ` Jan Kara
2019-08-02 14:24     ` Matthew Wilcox
2019-08-02 14:52       ` Jan Kara
2019-08-02 19:14         ` John Hubbard
2019-08-07  8:37           ` Jan Kara
2019-08-07  8:46             ` Michal Hocko
2019-08-08  2:36               ` Ira Weiny
2019-08-08  3:46                 ` John Hubbard
2019-08-08 16:25                   ` Weiny, Ira
2019-08-08 18:18                     ` John Hubbard
2019-08-09  8:34                 ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d4931311-db01-e8c3-0f8c-d64685dc2143@suse.com \
    --to=jgross@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=boris.ostrovsky@oracle.com \
    --cc=ceph-devel@vger.kernel.org \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@fromorbit.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=devel@lists.orangefs.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hch@infradead.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=ira.weiny@intel.com \
    --cc=jack@suse.cz \
    --cc=jgg@ziepe.ca \
    --cc=jglisse@redhat.com \
    --cc=jhubbard@nvidia.com \
    --cc=john.hubbard@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=rds-devel@oss.oracle.com \
    --cc=sparclinux@vger.kernel.org \
    --cc=x86@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).