Skip to contentSkip to conversation

mm/madvise: report SIGBUS as -EFAULT for MADV_POPULATE_(READ|WRITE)

PATCH Applied David Hildenbrand 1 participant 2 messages Jul 26, 2021 – Jul 30, 2021

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

PATCH 1/1 mm/madvise: report SIGBUS as -EFAULT for MADV_POPULATE_(READ|WRITE)

Doing some extended tests and polishing the man page update for MADV_POPULATE_(READ|WRITE), I realized that we end up converting also SIGBUS (via -EFAULT) to -EINVAL, making it look like yet another madvise() user error.

We want to report only problematic mappings and permission problems that the user could have know as -EINVAL.

Let's not convert -EFAULT arising due to SIGBUS (or SIGSEGV) to -EINVAL, but instead indicate -EFAULT to user space. While we could also convert it to -ENOMEM, using -EFAULT looks more helpful when user space might want to troubleshoot what's going wrong: MADV_POPULATE_(READ|WRITE) is not part of an final Linux release and we can still adjust the behavior.

Fixes 4ca9b3859dac ("mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables")
Cc Arnd Bergmann <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Matthew Wilcox (Oracle) <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mike Kravetz <[email protected]>
Cc Peter Xu <[email protected]>
Cc Rolf Eike Beer <[email protected]>
Cc Ram Pai <[email protected]>
Cc Shuah Khan <[email protected]>
---
 mm/gup.c     | 7 +++++--
 mm/madvise.c | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)
mm/gup.c +5 -2
index 42b8b1fa6521..b94717977d17 100644
@@ -1558,9 +1558,12 @@ long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
 		gup_flags |= FOLL_WRITE;
 
 	/*
-	 * See check_vma_flags(): Will return -EFAULT on incompatible mappings
-	 * or with insufficient permissions.
+	 * We want to report -EINVAL instead of -EFAULT for any permission
+	 * problems or incompatible mappings.
 	 */
+	if (check_vma_flags(vma, gup_flags))
+		return -EINVAL;
+
 	return __get_user_pages(mm, start, nr_pages, gup_flags,
 				NULL, NULL, locked);
 }
mm/madvise.c +3 -1
index 6d3d348b17f4..5c065bc8b5f6 100644
@@ -862,10 +862,12 @@ static long madvise_populate(struct vm_area_struct *vma,
 			switch (pages) {
 			case -EINTR:
 				return -EINTR;
-			case -EFAULT: /* Incompatible mappings / permissions. */
+			case -EINVAL: /* Incompatible mappings / permissions. */
 				return -EINVAL;
 			case -EHWPOISON:
 				return -EHWPOISON;
+			case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
+				return -EFAULT;
 			default:
 				pr_warn_once("%s: unhandled return value: %ld\n",
 					     __func__, pages);
Signature · 1 line
2.31.1
#1 lore raw

Hi Andrew,

sorry for not CCing you, absolutely no clue why I accidentally dropped you. Can you give this patch a churn? It would be great if we could get that into 5.14, so we don't have to deal with differing behavior between Linux versions.

Cheers!

Quoted 86 lines · David Hildenbrand

Doing some extended tests and polishing the man page update for MADV_POPULATE_(READ|WRITE), I realized that we end up converting also SIGBUS (via -EFAULT) to -EINVAL, making it look like yet another madvise() user error.

We want to report only problematic mappings and permission problems that the user could have know as -EINVAL.

Let's not convert -EFAULT arising due to SIGBUS (or SIGSEGV) to -EINVAL, but instead indicate -EFAULT to user space. While we could also convert it to -ENOMEM, using -EFAULT looks more helpful when user space might want to troubleshoot what's going wrong: MADV_POPULATE_(READ|WRITE) is not part of an final Linux release and we can still adjust the behavior.

Fixes 4ca9b3859dac ("mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables")
Cc Arnd Bergmann <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Matthew Wilcox (Oracle) <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mike Kravetz <[email protected]>
Cc Peter Xu <[email protected]>
Cc Rolf Eike Beer <[email protected]>
Cc Ram Pai <[email protected]>
Cc Shuah Khan <[email protected]>
---
  mm/gup.c     | 7 +++++--
  mm/madvise.c | 4 +++-
  2 files changed, 8 insertions(+), 3 deletions(-)
mm/gup.c +5 -2
index 42b8b1fa6521..b94717977d17 100644
@@ -1558,9 +1558,12 @@ long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
  		gup_flags |= FOLL_WRITE;
 
  	/*
-	 * See check_vma_flags(): Will return -EFAULT on incompatible mappings
-	 * or with insufficient permissions.
+	 * We want to report -EINVAL instead of -EFAULT for any permission
+	 * problems or incompatible mappings.
  	 */
+	if (check_vma_flags(vma, gup_flags))
+		return -EINVAL;
+
  	return __get_user_pages(mm, start, nr_pages, gup_flags,
  				NULL, NULL, locked);
  }
mm/madvise.c +3 -1
index 6d3d348b17f4..5c065bc8b5f6 100644
@@ -862,10 +862,12 @@ static long madvise_populate(struct vm_area_struct *vma,
  			switch (pages) {
  			case -EINTR:
  				return -EINTR;
-			case -EFAULT: /* Incompatible mappings / permissions. */
+			case -EINVAL: /* Incompatible mappings / permissions. */
  				return -EINVAL;
  			case -EHWPOISON:
  				return -EHWPOISON;
+			case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
+				return -EFAULT;
  			default:
  				pr_warn_once("%s: unhandled return value: %ld\n",
  					     __func__, pages);
Signature · 3 lines
Thanks,

David / dhildenb