Skip to contentSkip to conversation

mm/gup.c: fix the wrong comments

PATCH In review Huang Shijie 3 participants 5 messages Apr 4, 2019 – Apr 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup.c: fix the wrong comments

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.

So change the comments, and make it more clear for the driver users.

---
 mm/gup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/gup.c +4 -4
index 22acdd0f79ff..b810d15d4db9 100644
@@ -1129,10 +1129,6 @@ EXPORT_SYMBOL(get_user_pages_locked);
  *  with:
  *
  *      get_user_pages_unlocked(tsk, mm, ..., pages);
- *
- * It is functionally equivalent to get_user_pages_fast so
- * get_user_pages_fast should be used instead if specific gup_flags
- * (e.g. FOLL_FORCE) are not required.
  */
 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
 			     struct page **pages, unsigned int gup_flags)
@@ -2147,6 +2143,10 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  * If not successful, it will fall back to taking the lock and
  * calling get_user_pages().
  *
+ * This function is different from the get_user_pages_unlocked():
+ *      The @pages may has different page order with the result
+ *      got by get_user_pages_unlocked().
+ *
  * Returns number of pages pinned. This may be fewer than the number
  * requested. If nr_pages is 0 or negative, returns 0. If no pages
  * were pinned, returns -errno.
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Huang Shijie

+ * This function is different from the get_user_pages_unlocked():
+ * The @pages may has different page order with the result
+ * got by get_user_pages_unlocked().
+ *

I suggest a slight rewrite of the comment, something like:

* Note this routine may fill the pages array with entries in a
* different order than get_user_pages_unlocked(), which may cause
* issues for callers expecting the routines to be equivalent.

#2 lore raw
Quoted 8 lines
Quoted 7 lines · Huang Shijie

+ * This function is different from the get_user_pages_unlocked():
+ * The @pages may has different page order with the result
+ * got by get_user_pages_unlocked().
+ *

Quoted 5 lines

I suggest a slight rewrite of the comment, something like:

* Note this routine may fill the pages array with entries in a
* different order than get_user_pages_unlocked(), which may cause
* issues for callers expecting the routines to be equivalent.

This is good too. :-D

Ira

#3 lore raw
In reply to Huang Shijie
Quoted 10 lines · Huang Shijie

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, ...);
	    .....................
Quoted 3 lines

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 wonder if there is something we can do to change sg_alloc_table_from_pages() to work? Reading the comment for it there is no indication of this limitation. So should we update that comment as well?

Quoted 25 lines

So change the comments, and make it more clear for the driver users.

---
 mm/gup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/gup.c +0 -4
index 22acdd0f79ff..b810d15d4db9 100644
@@ -1129,10 +1129,6 @@ EXPORT_SYMBOL(get_user_pages_locked);
  *  with:
  *
  *      get_user_pages_unlocked(tsk, mm, ..., pages);
- *
- * It is functionally equivalent to get_user_pages_fast so
- * get_user_pages_fast should be used instead if specific gup_flags
- * (e.g. FOLL_FORCE) are not required.
  */
 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
 			     struct page **pages, unsigned int gup_flags)

mm/gup.c int __get_user_pages_fast(unsigned long start, int nr_pages, int write, · line 2149

Quoted 7 lines
mm/gup.c +4 -0
@@ -2147,6 +2143,10 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  * If not successful, it will fall back to taking the lock and
  * calling get_user_pages().
  *
+ * This function is different from the get_user_pages_unlocked():
+ *      The @pages may has different page order with the result
+ *      got by get_user_pages_unlocked().
+ *

I think I would word this a bit more generally. Say:

<quote>
NOTE: Because get_user_pages_fast() walks the page tables to find the pages, the order of pages returned may be different from those returned by other get_user_pages_*() calls.
</quote>

Ira

Quoted 6 lines
mm/gup.c +0 -0
  * Returns number of pages pinned. This may be fewer than the number
  * requested. If nr_pages is 0 or negative, returns 0. If no pages
  * were pinned, returns -errno.
Signature · 1 line
2.17.1
#4 lore raw
Quoted 11 lines · Ira Weiny
Quoted 10 lines · Huang Shijie

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, ...);
	    .....................
Quoted 6 lines
Quoted 3 lines

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 wonder if there is something we can do to change sg_alloc_table_from_pages() to work? Reading the comment for it there is no indication of this limitation.

The sg_alloc_table_from_pages() cannot work if the page order is wrong...

Quoted 1 line

So should we update that comment as well?

Okay.

I will create a DMA patch to add more comment for sg_alloc_table_from_pages().

Thanks
Huang Shijie