097-mm-remove-gup_flags-FOLL_WRITE-games-from-__get_user.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. From 1294d355881cc5c3421d24fee512f16974addb6c Mon Sep 17 00:00:00 2001
  2. From: Linus Torvalds <torvalds@linux-foundation.org>
  3. Date: Thu, 13 Oct 2016 13:07:36 -0700
  4. Subject: mm: remove gup_flags FOLL_WRITE games from __get_user_pages()
  5. commit 19be0eaffa3ac7d8eb6784ad9bdbc7d67ed8e619 upstream.
  6. This is an ancient bug that was actually attempted to be fixed once
  7. (badly) by me eleven years ago in commit 4ceb5db9757a ("Fix
  8. get_user_pages() race for write access") but that was then undone due to
  9. problems on s390 by commit f33ea7f404e5 ("fix get_user_pages bug").
  10. In the meantime, the s390 situation has long been fixed, and we can now
  11. fix it by checking the pte_dirty() bit properly (and do it better). The
  12. s390 dirty bit was implemented in abf09bed3cce ("s390/mm: implement
  13. software dirty bits") which made it into v3.9. Earlier kernels will
  14. have to look at the page state itself.
  15. Also, the VM has become more scalable, and what used a purely
  16. theoretical race back then has become easier to trigger.
  17. To fix it, we introduce a new internal FOLL_COW flag to mark the "yes,
  18. we already did a COW" rather than play racy games with FOLL_WRITE that
  19. is very fundamental, and then use the pte dirty flag to validate that
  20. the FOLL_COW flag is still valid.
  21. Reported-and-tested-by: Phil "not Paul" Oester <kernel@linuxace.com>
  22. Acked-by: Hugh Dickins <hughd@google.com>
  23. Reviewed-by: Michal Hocko <mhocko@suse.com>
  24. Cc: Andy Lutomirski <luto@kernel.org>
  25. Cc: Kees Cook <keescook@chromium.org>
  26. Cc: Oleg Nesterov <oleg@redhat.com>
  27. Cc: Willy Tarreau <w@1wt.eu>
  28. Cc: Nick Piggin <npiggin@gmail.com>
  29. Cc: Greg Thelen <gthelen@google.com>
  30. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  31. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  32. ---
  33. include/linux/mm.h | 1 +
  34. mm/gup.c | 14 ++++++++++++--
  35. 2 files changed, 13 insertions(+), 2 deletions(-)
  36. diff --git a/include/linux/mm.h b/include/linux/mm.h
  37. index cfebb74..f0ffa01 100644
  38. --- a/include/linux/mm.h
  39. +++ b/include/linux/mm.h
  40. @@ -2112,6 +2112,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,
  41. #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */
  42. #define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */
  43. #define FOLL_MLOCK 0x1000 /* lock present pages */
  44. +#define FOLL_COW 0x4000 /* internal GUP flag */
  45. typedef int (*pte_fn_t)(pte_t *pte, pgtable_t token, unsigned long addr,
  46. void *data);
  47. diff --git a/mm/gup.c b/mm/gup.c
  48. index deafa2c..4b0b7e7 100644
  49. --- a/mm/gup.c
  50. +++ b/mm/gup.c
  51. @@ -58,6 +58,16 @@ static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
  52. return -EEXIST;
  53. }
  54. +/*
  55. + * FOLL_FORCE can write to even unwritable pte's, but only
  56. + * after we've gone through a COW cycle and they are dirty.
  57. + */
  58. +static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
  59. +{
  60. + return pte_write(pte) ||
  61. + ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
  62. +}
  63. +
  64. static struct page *follow_page_pte(struct vm_area_struct *vma,
  65. unsigned long address, pmd_t *pmd, unsigned int flags)
  66. {
  67. @@ -92,7 +102,7 @@ retry:
  68. }
  69. if ((flags & FOLL_NUMA) && pte_protnone(pte))
  70. goto no_page;
  71. - if ((flags & FOLL_WRITE) && !pte_write(pte)) {
  72. + if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
  73. pte_unmap_unlock(ptep, ptl);
  74. return NULL;
  75. }
  76. @@ -352,7 +362,7 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  77. * reCOWed by userspace write).
  78. */
  79. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  80. - *flags &= ~FOLL_WRITE;
  81. + *flags |= FOLL_COW;
  82. return 0;
  83. }
  84. --
  85. cgit v0.12