linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Huang Shijie <sjhuang@iluvatar.ai>
To: Ira Weiny <ira.weiny@intel.com>
Cc: Matthew Wilcox <willy@infradead.org>, <akpm@linux-foundation.org>,
	<william.kucharski@oracle.com>, <palmer@sifive.com>,
	<axboe@kernel.dk>, <keescook@chromium.org>, <linux-mm@kvack.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/2] mm/gup.c: fix the wrong comments
Date: Wed, 10 Apr 2019 09:18:50 +0800	[thread overview]
Message-ID: <20190410011850.GA3640@hsj-Precision-5520> (raw)
In-Reply-To: <20190409202316.GA22989@iweiny-DESK2.sc.intel.com>

On Tue, Apr 09, 2019 at 01:23:16PM -0700, Ira Weiny wrote:
> On Tue, Apr 09, 2019 at 09:08:33AM +0800, Huang Shijie wrote:
> > On Mon, Apr 08, 2019 at 07:13:13AM -0700, Matthew Wilcox wrote:
> > > On Mon, Apr 08, 2019 at 10:37:45AM +0800, Huang Shijie wrote:
> > > > When CONFIG_HAVE_GENERIC_GUP is defined, the kernel will use its own
> > > > get_user_pages_fast().
> > > > 
> > > > In the following scenario, we will may meet the bug in the DMA case:
> > > > 	    .....................
> > > > 	    get_user_pages_fast(start,,, pages);
> > > > 	        ......
> > > > 	    sg_alloc_table_from_pages(, pages, ...);
> > > > 	    .....................
> > > > 
> > > > The root cause is that sg_alloc_table_from_pages() requires the
> > > > page order to keep the same as it used in the user space, but
> > > > get_user_pages_fast() will mess it up.
> > > 
> > > I don't understand how get_user_pages_fast() can return the pages in a
> > > different order in the array from the order they appear in userspace.
> > > Can you explain?
> > Please see the code in gup.c:
> > 
> > 	int get_user_pages_fast(unsigned long start, int nr_pages,
> > 				unsigned int gup_flags, struct page **pages)
> > 	{
> > 		.......
> > 		if (gup_fast_permitted(start, nr_pages)) {
> > 			local_irq_disable();
> > 			gup_pgd_range(addr, end, gup_flags, pages, &nr);               // The @pages array maybe filled at the first time.
> > 			local_irq_enable();
> > 			ret = nr;
> > 		}
> > 		.......
> > 		if (nr < nr_pages) {
> > 			/* Try to get the remaining pages with get_user_pages */
> > 			start += nr << PAGE_SHIFT;
> > 			pages += nr;                                                  // The @pages is moved forward.
> > 
> > 			if (gup_flags & FOLL_LONGTERM) {
> > 				down_read(&current->mm->mmap_sem);
> > 				ret = __gup_longterm_locked(current, current->mm,      // The @pages maybe filled at the second time
> >
> 
> Neither this nor the get_user_pages_unlocked is filling the pages a second
The get_user_pages_unlocked() will call the handle_mm_fault which will allocate a
new page for the empty PTE, and save the new page into the @pages array.


> time.  It is adding to the page array having moved start and the page array
> forward.

Yes. This will mess up the page order.

I will read the code again to check if I am wrong :)

> 
> Are you doing a FOLL_LONGTERM GUP?  Or are you in the else clause below when
> you get this bug?
I do not use FOLL_LONGTERM, I just use the FOLL_WRITE.

So it seems it runs into the else clause below.

Thanks
Huang Shijie

> 
> Ira
> 
> > 							    start, nr_pages - nr,
> > 							    pages, NULL, gup_flags);
> > 				up_read(&current->mm->mmap_sem);
> > 			} else {
> > 				/*
> > 				 * retain FAULT_FOLL_ALLOW_RETRY optimization if
> > 				 * possible
> > 				 */
> > 				ret = get_user_pages_unlocked(start, nr_pages - nr,    // The @pages maybe filled at the second time.
> > 							      pages, gup_flags);
> > 			}
> > 		}
> > 
> > 


  reply	other threads:[~2019-04-10  1:18 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-08  2:37 [PATCH 1/2] mm/gup.c: fix the wrong comments Huang Shijie
2019-04-08  2:37 ` [PATCH 2/2] lib/scatterlist.c: add more commit for sg_alloc_table_from_pages Huang Shijie
2019-04-08 14:13 ` [PATCH 1/2] mm/gup.c: fix the wrong comments Matthew Wilcox
2019-04-09  1:08   ` Huang Shijie
2019-04-09  2:49     ` Matthew Wilcox
2019-04-09  3:04       ` Huang Shijie
2019-04-09 11:19         ` Matthew Wilcox
2019-04-09 14:55           ` Weiny, Ira
2019-04-10  1:20             ` Huang Shijie
2019-04-10 18:04               ` Ira Weiny
2019-04-09 20:23     ` Ira Weiny
2019-04-10  1:18       ` Huang Shijie [this message]
2019-04-10 18:08         ` Ira Weiny

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=20190410011850.GA3640@hsj-Precision-5520 \
    --to=sjhuang@iluvatar.ai \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=ira.weiny@intel.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=palmer@sifive.com \
    --cc=william.kucharski@oracle.com \
    --cc=willy@infradead.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).