Skip to content
  1. Jun 30, 2021
    • Hugh Dickins's avatar
      mm: page_vma_mapped_walk(): crossing page table boundary · e56bdb39
      Hugh Dickins authored
      commit 44828248
      
       upstream.
      
      page_vma_mapped_walk() cleanup: adjust the test for crossing page table
      boundary - I believe pvmw->address is always page-aligned, but nothing
      else here assumed that; and remember to reset pvmw->pte to NULL after
      unmapping the page table, though I never saw any bug from that.
      
      Link: https://lkml.kernel.org/r/799b3f9c-2a9e-dfef-5d89-26e9f76fd97@google.com
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Will Deacon <will@kernel.org>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      e56bdb39
    • Hugh Dickins's avatar
      mm: page_vma_mapped_walk(): prettify PVMW_MIGRATION block · 8dc191ed
      Hugh Dickins authored
      commit e2e1d407
      
       upstream.
      
      page_vma_mapped_walk() cleanup: rearrange the !pmd_present() block to
      follow the same "return not_found, return not_found, return true"
      pattern as the block above it (note: returning not_found there is never
      premature, since existence or prior existence of huge pmd guarantees
      good alignment).
      
      Link: https://lkml.kernel.org/r/378c8650-1488-2edf-9647-32a53cf2e21@google.com
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Will Deacon <will@kernel.org>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      8dc191ed
    • Hugh Dickins's avatar
      mm: page_vma_mapped_walk(): use pmde for *pvmw->pmd · 7b55a4bc
      Hugh Dickins authored
      commit 3306d311
      
       upstream.
      
      page_vma_mapped_walk() cleanup: re-evaluate pmde after taking lock, then
      use it in subsequent tests, instead of repeatedly dereferencing pointer.
      
      Link: https://lkml.kernel.org/r/53fbc9d-891e-46b2-cb4b-468c3b19238e@google.com
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Will Deacon <will@kernel.org>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      7b55a4bc
    • Hugh Dickins's avatar
      mm: page_vma_mapped_walk(): settle PageHuge on entry · 1cb0b905
      Hugh Dickins authored
      commit 6d0fd598
      
       upstream.
      
      page_vma_mapped_walk() cleanup: get the hugetlbfs PageHuge case out of
      the way at the start, so no need to worry about it later.
      
      Link: https://lkml.kernel.org/r/e31a483c-6d73-a6bb-26c5-43c3b880a2@google.com
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Will Deacon <will@kernel.org>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      1cb0b905
    • Hugh Dickins's avatar
      mm: page_vma_mapped_walk(): use page for pvmw->page · 65febb41
      Hugh Dickins authored
      commit f003c03b
      
       upstream.
      
      Patch series "mm: page_vma_mapped_walk() cleanup and THP fixes".
      
      I've marked all of these for stable: many are merely cleanups, but I
      think they are much better before the main fix than after.
      
      This patch (of 11):
      
      page_vma_mapped_walk() cleanup: sometimes the local copy of pvwm->page
      was used, sometimes pvmw->page itself: use the local copy "page"
      throughout.
      
      Link: https://lkml.kernel.org/r/589b358c-febc-c88e-d4c2-7834b37fa7bf@google.com
      Link: https://lkml.kernel.org/r/88e67645-f467-c279-bf5e-af4b5c6b13eb@google.com
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Reviewed-by: default avatarAlistair Popple <apopple@nvidia.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Will Deacon <will@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      65febb41
    • Yang Shi's avatar
      mm: thp: replace DEBUG_VM BUG with VM_WARN when unmap fails for split · 825c2805
      Yang Shi authored
      [ Upstream commit 504e070d
      
       ]
      
      When debugging the bug reported by Wang Yugui [1], try_to_unmap() may
      fail, but the first VM_BUG_ON_PAGE() just checks page_mapcount() however
      it may miss the failure when head page is unmapped but other subpage is
      mapped.  Then the second DEBUG_VM BUG() that check total mapcount would
      catch it.  This may incur some confusion.
      
      As this is not a fatal issue, so consolidate the two DEBUG_VM checks
      into one VM_WARN_ON_ONCE_PAGE().
      
      [1] https://lore.kernel.org/linux-mm/20210412180659.B9E3.409509F4@e16-tech.com/
      
      Link: https://lkml.kernel.org/r/d0f0db68-98b8-ebfb-16dc-f29df24cf012@google.com
      Signed-off-by: default avatarYang Shi <shy828301@gmail.com>
      Reviewed-by: default avatarZi Yan <ziy@nvidia.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jue Wang <juew@google.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      
      Note on stable backport: fixed up variables in split_huge_page_to_list().
      
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      825c2805
    • Hugh Dickins's avatar
      mm/thp: unmap_mapping_page() to fix THP truncate_cleanup_page() · 0010275c
      Hugh Dickins authored
      [ Upstream commit 22061a1f ]
      
      There is a race between THP unmapping and truncation, when truncate sees
      pmd_none() and skips the entry, after munmap's zap_huge_pmd() cleared
      it, but before its page_remove_rmap() gets to decrement
      compound_mapcount: generating false "BUG: Bad page cache" reports that
      the page is still mapped when deleted.  This commit fixes that, but not
      in the way I hoped.
      
      The first attempt used try_to_unmap(page, TTU_SYNC|TTU_IGNORE_MLOCK)
      instead of unmap_mapping_range() in truncate_cleanup_page(): it has
      often been an annoyance that we usually call unmap_mapping_range() with
      no pages locked, but there apply it to a single locked page.
      try_to_unmap() looks more suitable for a single locked page.
      
      However, try_to_unmap_one() contains a VM_BUG_ON_PAGE(!pvmw.pte,page):
      it is used to insert THP migration entries, but not used to unmap THPs.
      Copy zap_huge_pmd() and add THP handling now? Perhaps, but their TLB
      needs are different, I'm too ignorant of the DAX cases, and couldn't
      decide how far to go for anon+swap.  Set that aside.
      
      The second attempt took a different tack: make no change in truncate.c,
      but modify zap_huge_pmd() to insert an invalidated huge pmd instead of
      clearing it initially, then pmd_clear() between page_remove_rmap() and
      unlocking at the end.  Nice.  But powerpc blows that approach out of the
      water, with its serialize_against_pte_lookup(), and interesting pgtable
      usage.  It would need serious help to get working on powerpc (with a
      minor optimization issue on s390 too).  Set that aside.
      
      Just add an "if (page_mapped(page)) synchronize_rcu();" or other such
      delay, after unmapping in truncate_cleanup_page()? Perhaps, but though
      that's likely to reduce or eliminate the number of incidents, it would
      give less assurance of whether we had identified the problem correctly.
      
      This successful iteration introduces "unmap_mapping_page(page)" instead
      of try_to_unmap(), and goes the usual unmap_mapping_range_tree() route,
      with an addition to details.  Then zap_pmd_range() watches for this
      case, and does spin_unlock(pmd_lock) if so - just like
      page_vma_mapped_walk() now does in the PVMW_SYNC case.  Not pretty, but
      safe.
      
      Note that unmap_mapping_page() is doing a VM_BUG_ON(!PageLocked) to
      assert its interface; but currently that's only used to make sure that
      page->mapping is stable, and zap_pmd_range() doesn't care if the page is
      locked or not.  Along these lines, in invalidate_inode_pages2_range()
      move the initial unmap_mapping_range() out from under page lock, before
      then calling unmap_mapping_page() under page lock if still mapped.
      
      Link: https://lkml.kernel.org/r/a2a4a148-cdd8-942c-4ef8-51b77f643dbe@google.com
      Fixes: fc127da0
      
       ("truncate: handle file thp")
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jue Wang <juew@google.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      
      Note on stable backport: fixed up call to truncate_cleanup_page()
      in truncate_inode_pages_range().
      
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      0010275c
    • Jue Wang's avatar
      mm/thp: fix page_address_in_vma() on file THP tails · 38cda6b5
      Jue Wang authored
      commit 31657170 upstream.
      
      Anon THP tails were already supported, but memory-failure may need to
      use page_address_in_vma() on file THP tails, which its page->mapping
      check did not permit: fix it.
      
      hughd adds: no current usage is known to hit the issue, but this does
      fix a subtle trap in a general helper: best fixed in stable sooner than
      later.
      
      Link: https://lkml.kernel.org/r/a0d9b53-bf5d-8bab-ac5-759dc61819c1@google.com
      Fixes: 800d8c63
      
       ("shmem: add huge pages support")
      Signed-off-by: default avatarJue Wang <juew@google.com>
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Reviewed-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      38cda6b5
    • Hugh Dickins's avatar
      mm/thp: fix vma_address() if virtual address below file offset · 37ffe9f4
      Hugh Dickins authored
      commit 494334e4 upstream.
      
      Running certain tests with a DEBUG_VM kernel would crash within hours,
      on the total_mapcount BUG() in split_huge_page_to_list(), while trying
      to free up some memory by punching a hole in a shmem huge page: split's
      try_to_unmap() was unable to find all the mappings of the page (which,
      on a !DEBUG_VM kernel, would then keep the huge page pinned in memory).
      
      When that BUG() was changed to a WARN(), it would later crash on the
      VM_BUG_ON_VMA(end < vma->vm_start || start >= vma->vm_end, vma) in
      mm/internal.h:vma_address(), used by rmap_walk_file() for
      try_to_unmap().
      
      vma_address() is usually correct, but there's a wraparound case when the
      vm_start address is unusually low, but vm_pgoff not so low:
      vma_address() chooses max(start, vma->vm_start), but that decides on the
      wrong address, because start has become almost ULONG_MAX.
      
      Rewrite vma_address() to be more careful about vm_pgoff; move the
      VM_BUG_ON_VMA() out of it, returning -EFAULT for errors, so that it can
      be safely used from page_mapped_in_vma() and page_address_in_vma() too.
      
      Add vma_address_end() to apply similar care to end address calculation,
      in page_vma_mapped_walk() and page_mkclean_one() and try_to_unmap_one();
      though it raises a question of whether callers would do better to supply
      pvmw->end to page_vma_mapped_walk() - I chose not, for a smaller patch.
      
      An irritation is that their apparent generality breaks down on KSM
      pages, which cannot be located by the page->index that page_to_pgoff()
      uses: as commit 4b0ece6f ("mm: migrate: fix remove_migration_pte()
      for ksm pages") once discovered.  I dithered over the best thing to do
      about that, and have ended up with a VM_BUG_ON_PAGE(PageKsm) in both
      vma_address() and vma_address_end(); though the only place in danger of
      using it on them was try_to_unmap_one().
      
      Sidenote: vma_address() and vma_address_end() now use compound_nr() on a
      head page, instead of thp_size(): to make the right calculation on a
      hugetlbfs page, whether or not THPs are configured.  try_to_unmap() is
      used on hugetlbfs pages, but perhaps the wrong calculation never
      mattered.
      
      Link: https://lkml.kernel.org/r/caf1c1a3-7cfb-7f8f-1beb-ba816e932825@google.com
      Fixes: a8fa41ad
      
       ("mm, rmap: check all VMAs that PTE-mapped THP can be part of")
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jue Wang <juew@google.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      37ffe9f4
    • Hugh Dickins's avatar
      mm/thp: try_to_unmap() use TTU_SYNC for safe splitting · 66be14a9
      Hugh Dickins authored
      commit 732ed558 upstream.
      
      Stressing huge tmpfs often crashed on unmap_page()'s VM_BUG_ON_PAGE
      (!unmap_success): with dump_page() showing mapcount:1, but then its raw
      struct page output showing _mapcount ffffffff i.e.  mapcount 0.
      
      And even if that particular VM_BUG_ON_PAGE(!unmap_success) is removed,
      it is immediately followed by a VM_BUG_ON_PAGE(compound_mapcount(head)),
      and further down an IS_ENABLED(CONFIG_DEBUG_VM) total_mapcount BUG():
      all indicative of some mapcount difficulty in development here perhaps.
      But the !CONFIG_DEBUG_VM path handles the failures correctly and
      silently.
      
      I believe the problem is that once a racing unmap has cleared pte or
      pmd, try_to_unmap_one() may skip taking the page table lock, and emerge
      from try_to_unmap() before the racing task has reached decrementing
      mapcount.
      
      Instead of abandoning the unsafe VM_BUG_ON_PAGE(), and the ones that
      follow, use PVMW_SYNC in try_to_unmap_one() in this case: adding
      TTU_SYNC to the options, and passing that from unmap_page().
      
      When CONFIG_DEBUG_VM, or for non-debug too? Consensus is to do the same
      for both: the slight overhead added should rarely matter, except perhaps
      if splitting sparsely-populated multiply-mapped shmem.  Once confident
      that bugs are fixed, TTU_SYNC here can be removed, and the race
      tolerated.
      
      Link: https://lkml.kernel.org/r/c1e95853-8bcd-d8fd-55fa-e7f2488e78f@google.com
      Fixes: fec89c10
      
       ("thp: rewrite freeze_page()/unfreeze_page() with generic rmap walkers")
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jue Wang <juew@google.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      66be14a9
    • Hugh Dickins's avatar
      mm/thp: make is_huge_zero_pmd() safe and quicker · 6527d8ef
      Hugh Dickins authored
      commit 3b77e8c8 upstream.
      
      Most callers of is_huge_zero_pmd() supply a pmd already verified
      present; but a few (notably zap_huge_pmd()) do not - it might be a pmd
      migration entry, in which the pfn is encoded differently from a present
      pmd: which might pass the is_huge_zero_pmd() test (though not on x86,
      since L1TF forced us to protect against that); or perhaps even crash in
      pmd_page() applied to a swap-like entry.
      
      Make it safe by adding pmd_present() check into is_huge_zero_pmd()
      itself; and make it quicker by saving huge_zero_pfn, so that
      is_huge_zero_pmd() will not need to do that pmd_page() lookup each time.
      
      __split_huge_pmd_locked() checked pmd_trans_huge() before: that worked,
      but is unnecessary now that is_huge_zero_pmd() checks present.
      
      Link: https://lkml.kernel.org/r/21ea9ca-a1f5-8b90-5e88-95fb1c49bbfa@google.com
      Fixes: e71769ae
      
       ("mm: enable thp migration for shmem thp")
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jue Wang <juew@google.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      6527d8ef
    • Hugh Dickins's avatar
      mm/thp: fix __split_huge_pmd_locked() on shmem migration entry · a8f4ea1d
      Hugh Dickins authored
      [ Upstream commit 99fa8a48 ]
      
      Patch series "mm/thp: fix THP splitting unmap BUGs and related", v10.
      
      Here is v2 batch of long-standing THP bug fixes that I had not got
      around to sending before, but prompted now by Wang Yugui's report
      https://lore.kernel.org/linux-mm/20210412180659.B9E3.409509F4@e16-tech.com/
      
      Wang Yugui has tested a rollup of these fixes applied to 5.10.39, and
      they have done no harm, but have *not* fixed that issue: something more
      is needed and I have no idea of what.
      
      This patch (of 7):
      
      Stressing huge tmpfs page migration racing hole punch often crashed on
      the VM_BUG_ON(!pmd_present) in pmdp_huge_clear_flush(), with DEBUG_VM=y
      kernel; or shortly afterwards, on a bad dereference in
      __split_huge_pmd_locked() when DEBUG_VM=n.  They forgot to allow for pmd
      migration entries in the non-anonymous case.
      
      Full disclosure: those particular experiments were on a kernel with more
      relaxed mmap_lock and i_mmap_rwsem locking, and were not repeated on the
      vanilla kernel: it is conceivable that stricter locking happens to avoid
      those cases, or makes them less likely; but __split_huge_pmd_locked()
      already allowed for pmd migration entries when handling anonymous THPs,
      so this commit brings the shmem and file THP handling into line.
      
      And while there: use old_pmd rather than _pmd, as in the following
      blocks; and make it clearer to the eye that the !vma_is_anonymous()
      block is self-contained, making an early return after accounting for
      unmapping.
      
      Link: https://lkml.kernel.org/r/af88612-1473-2eaa-903-8d1a448b26@google.com
      Link: https://lkml.kernel.org/r/dd221a99-efb3-cd1d-6256-7e646af29314@google.com
      Fixes: e71769ae
      
       ("mm: enable thp migration for shmem thp")
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Wang Yugui <wangyugui@e16-tech.com>
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Ralph Campbell <rcampbell@nvidia.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Jue Wang <juew@google.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Oscar Salvador <osalvador@suse.de>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      
      Note on stable backport: this commit made intervening cleanups in
      pmdp_huge_clear_flush() redundant: here it's rediffed to skip them.
      
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a8f4ea1d
    • Xu Yu's avatar
      mm, thp: use head page in __migration_entry_wait() · 32f954e9
      Xu Yu authored
      commit ffc90cbb upstream.
      
      We notice that hung task happens in a corner but practical scenario when
      CONFIG_PREEMPT_NONE is enabled, as follows.
      
      Process 0                       Process 1                     Process 2..Inf
      split_huge_page_to_list
          unmap_page
              split_huge_pmd_address
                                      __migration_entry_wait(head)
                                                                    __migration_entry_wait(tail)
          remap_page (roll back)
              remove_migration_ptes
                  rmap_walk_anon
                      cond_resched
      
      Where __migration_entry_wait(tail) is occurred in kernel space, e.g.,
      copy_to_user in fstat, which will immediately fault again without
      rescheduling, and thus occupy the cpu fully.
      
      When there are too many processes performing __migration_entry_wait on
      tail page, remap_page will never be done after cond_resched.
      
      This makes __migration_entry_wait operate on the compound head page,
      thus waits for remap_page to complete, whether the THP is split
      successfully or roll back.
      
      Note that put_and_wait_on_page_locked helps to drop the page reference
      acquired with get_page_unless_zero, as soon as the page is on the wait
      queue, before actually waiting.  So splitting the THP is only prevented
      for a brief interval.
      
      Link: https://lkml.kernel.org/r/b9836c1dd522e903891760af9f0c86a2cce987eb.1623144009.git.xuyu@linux.alibaba.com
      Fixes: ba988280
      
       ("thp: add option to setup migration entries during PMD split")
      Suggested-by: default avatarHugh Dickins <hughd@google.com>
      Signed-off-by: default avatarGang Deng <gavin.dg@linux.alibaba.com>
      Signed-off-by: default avatarXu Yu <xuyu@linux.alibaba.com>
      Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Acked-by: default avatarHugh Dickins <hughd@google.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      32f954e9
    • Miaohe Lin's avatar
      mm/rmap: use page_not_mapped in try_to_unmap() · bfd90b56
      Miaohe Lin authored
      [ Upstream commit b7e188ec
      
       ]
      
      page_mapcount_is_zero() calculates accurately how many mappings a hugepage
      has in order to check against 0 only.  This is a waste of cpu time.  We
      can do this via page_not_mapped() to save some possible atomic_read
      cycles.  Remove the function page_mapcount_is_zero() as it's not used
      anymore and move page_not_mapped() above try_to_unmap() to avoid
      identifier undeclared compilation error.
      
      Link: https://lkml.kernel.org/r/20210130084904.35307-1-linmiaohe@huawei.com
      Signed-off-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      bfd90b56
    • Miaohe Lin's avatar
      mm/rmap: remove unneeded semicolon in page_not_mapped() · ff81af82
      Miaohe Lin authored
      [ Upstream commit e0af87ff
      
       ]
      
      Remove extra semicolon without any functional change intended.
      
      Link: https://lkml.kernel.org/r/20210127093425.39640-1-linmiaohe@huawei.com
      Signed-off-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      ff81af82
    • Alex Shi's avatar
      mm: add VM_WARN_ON_ONCE_PAGE() macro · a0ad7ea0
      Alex Shi authored
      [ Upstream commit a4055888
      
       part ]
      
      Add VM_WARN_ON_ONCE_PAGE() macro.
      
      Link: https://lkml.kernel.org/r/1604283436-18880-3-git-send-email-alex.shi@linux.alibaba.com
      Signed-off-by: default avatarAlex Shi <alex.shi@linux.alibaba.com>
      Acked-by: default avatarMichal Hocko <mhocko@suse.com>
      Acked-by: default avatarHugh Dickins <hughd@google.com>
      Acked-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
      Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      
      Note on stable backport: original commit was titled
      mm/memcg: warning on !memcg after readahead page charged
      which included uses of this macro in mm/memcontrol.c: here omitted.
      
      Signed-off-by: default avatarHugh Dickins <hughd@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a0ad7ea0
    • Thomas Gleixner's avatar
      x86/fpu: Make init_fpstate correct with optimized XSAVE · 130a1d76
      Thomas Gleixner authored
      commit f9dfb5e3 upstream.
      
      The XSAVE init code initializes all enabled and supported components with
      XRSTOR(S) to init state. Then it XSAVEs the state of the components back
      into init_fpstate which is used in several places to fill in the init state
      of components.
      
      This works correctly with XSAVE, but not with XSAVEOPT and XSAVES because
      those use the init optimization and skip writing state of components which
      are in init state. So init_fpstate.xsave still contains all zeroes after
      this operation.
      
      There are two ways to solve that:
      
         1) Use XSAVE unconditionally, but that requires to reshuffle the buffer when
            XSAVES is enabled because XSAVES uses compacted format.
      
         2) Save the components which are known to have a non-zero init state by other
            means.
      
      Looking deeper, #2 is the right thing to do because all components the
      kernel supports have all-zeroes init state except the legacy features (FP,
      SSE). Those cannot be hard coded because the states are not identical on all
      CPUs, but they can be saved with FXSAVE which avoids all conditionals.
      
      Use FXSAVE to save the legacy FP/SSE components in init_fpstate along with
      a BUILD_BUG_ON() which reminds developers to validate that a newly added
      component has all zeroes init state. As a bonus remove the now unused
      copy_xregs_to_kernel_booting() crutch.
      
      The XSAVE and reshuffle method can still be implemented in the unlikely
      case that components are added which have a non-zero init state and no
      other means to save them. For now, FXSAVE is just simple and good enough.
      
        [ bp: Fix a typo or two in the text. ]
      
      Fixes: 6bad06b7
      
       ("x86, xsave: Use xsaveopt in context-switch path when supported")
      Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
      Reviewed-by: default avatarBorislav Petkov <bp@suse.de>
      Cc: stable@vger.kernel.org
      Link: https://lkml.kernel.org/r/20210618143444.587311343@linutronix.de
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      130a1d76
    • Thomas Gleixner's avatar
      x86/fpu: Preserve supervisor states in sanitize_restored_user_xstate() · 51d80117
      Thomas Gleixner authored
      commit 9301982c upstream.
      
      sanitize_restored_user_xstate() preserves the supervisor states only
      when the fx_only argument is zero, which allows unprivileged user space
      to put supervisor states back into init state.
      
      Preserve them unconditionally.
      
       [ bp: Fix a typo or two in the text. ]
      
      Fixes: 5d6b6a6f
      
       ("x86/fpu/xstate: Update sanitize_restored_xstate() for supervisor xstates")
      Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
      Cc: stable@vger.kernel.org
      Link: https://lkml.kernel.org/r/20210618143444.438635017@linutronix.de
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      51d80117
    • Petr Mladek's avatar
      kthread: prevent deadlock when kthread_mod_delayed_work() races with... · 2b35a4ea
      Petr Mladek authored
      kthread: prevent deadlock when kthread_mod_delayed_work() races with kthread_cancel_delayed_work_sync()
      
      commit 5fa54346 upstream.
      
      The system might hang with the following backtrace:
      
      	schedule+0x80/0x100
      	schedule_timeout+0x48/0x138
      	wait_for_common+0xa4/0x134
      	wait_for_completion+0x1c/0x2c
      	kthread_flush_work+0x114/0x1cc
      	kthread_cancel_work_sync.llvm.16514401384283632983+0xe8/0x144
      	kthread_cancel_delayed_work_sync+0x18/0x2c
      	xxxx_pm_notify+0xb0/0xd8
      	blocking_notifier_call_chain_robust+0x80/0x194
      	pm_notifier_call_chain_robust+0x28/0x4c
      	suspend_prepare+0x40/0x260
      	enter_state+0x80/0x3f4
      	pm_suspend+0x60/0xdc
      	state_store+0x108/0x144
      	kobj_attr_store+0x38/0x88
      	sysfs_kf_write+0x64/0xc0
      	kernfs_fop_write_iter+0x108/0x1d0
      	vfs_write+0x2f4/0x368
      	ksys_write+0x7c/0xec
      
      It is caused by the following race between kthread_mod_delayed_work()
      and kthread_cancel_delayed_work_sync():
      
      CPU0				CPU1
      
      Context: Thread A		Context: Thread B
      
      kthread_mod_delayed_work()
        spin_lock()
        __kthread_cancel_work()
           spin_unlock()
           del_timer_sync()
      				kthread_cancel_delayed_work_sync()
      				  spin_lock()
      				  __kthread_cancel_work()
      				    spin_unlock()
      				    del_timer_sync()
      				    spin_lock()
      
      				  work->canceling++
      				  spin_unlock
           spin_lock()
         queue_delayed_work()
           // dwork is put into the worker->delayed_work_list
      
         spin_unlock()
      
      				  kthread_flush_work()
           // flush_work is put at the tail of the dwork
      
      				    wait_for_completion()
      
      Context: IRQ
      
        kthread_delayed_work_timer_fn()
          spin_lock()
          list_del_init(&work->node);
          spin_unlock()
      
      BANG: flush_work is not longer linked and will never get proceed.
      
      The problem is that kthread_mod_delayed_work() checks work->canceling
      flag before canceling the timer.
      
      A simple solution is to (re)check work->canceling after
      __kthread_cancel_work().  But then it is not clear what should be
      returned when __kthread_cancel_work() removed the work from the queue
      (list) and it can't queue it again with the new @delay.
      
      The return value might be used for reference counting.  The caller has
      to know whether a new work has been queued or an existing one was
      replaced.
      
      The proper solution is that kthread_mod_delayed_work() will remove the
      work from the queue (list) _only_ when work->canceling is not set.  The
      flag must be checked after the timer is stopped and the remaining
      operations can be done under worker->lock.
      
      Note that kthread_mod_delayed_work() could remove the timer and then
      bail out.  It is fine.  The other canceling caller needs to cancel the
      timer as well.  The important thing is that the queue (list)
      manipulation is done atomically under worker->lock.
      
      Link: https://lkml.kernel.org/r/20210610133051.15337-3-pmladek@suse.com
      Fixes: 9a6b06c8
      
       ("kthread: allow to modify delayed kthread work")
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Reported-by: default avatarMartin Liu <liumartin@google.com>
      Cc: <jenhaochen@google.com>
      Cc: Minchan Kim <minchan@google.com>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: Nick Desaulniers <ndesaulniers@google.com>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      2b35a4ea
    • Petr Mladek's avatar
      kthread_worker: split code for canceling the delayed work timer · bfe28af7
      Petr Mladek authored
      commit 34b3d534
      
       upstream.
      
      Patch series "kthread_worker: Fix race between kthread_mod_delayed_work()
      and kthread_cancel_delayed_work_sync()".
      
      This patchset fixes the race between kthread_mod_delayed_work() and
      kthread_cancel_delayed_work_sync() including proper return value
      handling.
      
      This patch (of 2):
      
      Simple code refactoring as a preparation step for fixing a race between
      kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync().
      
      It does not modify the existing behavior.
      
      Link: https://lkml.kernel.org/r/20210610133051.15337-2-pmladek@suse.com
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Cc: <jenhaochen@google.com>
      Cc: Martin Liu <liumartin@google.com>
      Cc: Minchan Kim <minchan@google.com>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: Nick Desaulniers <ndesaulniers@google.com>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      bfe28af7
    • Jeff Layton's avatar
      ceph: must hold snap_rwsem when filling inode for async create · 02c303f3
      Jeff Layton authored
      commit 27171ae6 upstream.
      
      ...and add a lockdep assertion for it to ceph_fill_inode().
      
      Cc: stable@vger.kernel.org # v5.7+
      Fixes: 9a8d03ca
      
       ("ceph: attempt to do async create when possible")
      Signed-off-by: default avatarJeff Layton <jlayton@kernel.org>
      Reviewed-by: default avatarIlya Dryomov <idryomov@gmail.com>
      Signed-off-by: default avatarIlya Dryomov <idryomov@gmail.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      02c303f3
    • Johan Hovold's avatar
      i2c: robotfuzz-osif: fix control-request directions · de0af265
      Johan Hovold authored
      commit 4ca070ef
      
       upstream.
      
      The direction of the pipe argument must match the request-type direction
      bit or control requests may fail depending on the host-controller-driver
      implementation.
      
      Control transfers without a data stage are treated as OUT requests by
      the USB stack and should be using usb_sndctrlpipe(). Failing to do so
      will now trigger a warning.
      
      Fix the OSIFI2C_SET_BIT_RATE and OSIFI2C_STOP requests which erroneously
      used the osif_usb_read() helper and set the IN direction bit.
      
      Reported-by: default avatar <syzbot+9d7dadd15b8819d73f41@syzkaller.appspotmail.com>
      Fixes: 83e53a8f
      
       ("i2c: Add bus driver for for OSIF USB i2c device.")
      Cc: stable@vger.kernel.org      # 3.14
      Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
      Signed-off-by: default avatarWolfram Sang <wsa@kernel.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      de0af265
    • Nicholas Piggin's avatar
      KVM: do not allow mapping valid but non-reference-counted pages · dd8ed6c9
      Nicholas Piggin authored
      commit f8be156b
      
       upstream.
      
      It's possible to create a region which maps valid but non-refcounted
      pages (e.g., tail pages of non-compound higher order allocations). These
      host pages can then be returned by gfn_to_page, gfn_to_pfn, etc., family
      of APIs, which take a reference to the page, which takes it from 0 to 1.
      When the reference is dropped, this will free the page incorrectly.
      
      Fix this by only taking a reference on valid pages if it was non-zero,
      which indicates it is participating in normal refcounting (and can be
      released with put_page).
      
      This addresses CVE-2021-22543.
      
      Signed-off-by: default avatarNicholas Piggin <npiggin@gmail.com>
      Tested-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Cc: stable@vger.kernel.org
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      dd8ed6c9
    • Heiko Carstens's avatar
      s390/stack: fix possible register corruption with stack switch helper · 5fd0c2cf
      Heiko Carstens authored
      commit 67147e96 upstream.
      
      The CALL_ON_STACK macro is used to call a C function from inline
      assembly, and therefore must consider the C ABI, which says that only
      registers 6-13, and 15 are non-volatile (restored by the called
      function).
      
      The inline assembly incorrectly marks all registers used to pass
      parameters to the called function as read-only input operands, instead
      of operands that are read and written to. This might result in
      register corruption depending on usage, compiler, and compile options.
      
      Fix this by marking all operands used to pass parameters as read/write
      operands. To keep the code simple even register 6, if used, is marked
      as read-write operand.
      
      Fixes: ff340d24
      
       ("s390: add stack switch helper")
      Cc: <stable@kernel.org> # 4.20
      Reviewed-by: default avatarVasily Gorbik <gor@linux.ibm.com>
      Signed-off-by: default avatarHeiko Carstens <hca@linux.ibm.com>
      Signed-off-by: default avatarVasily Gorbik <gor@linux.ibm.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      5fd0c2cf
    • Pavel Skripkin's avatar
      nilfs2: fix memory leak in nilfs_sysfs_delete_device_group · ab5bef97
      Pavel Skripkin authored
      [ Upstream commit 8fd0c1b0 ]
      
      My local syzbot instance hit memory leak in nilfs2.  The problem was in
      missing kobject_put() in nilfs_sysfs_delete_device_group().
      
      kobject_del() does not call kobject_cleanup() for passed kobject and it
      leads to leaking duped kobject name if kobject_put() was not called.
      
      Fail log:
      
        BUG: memory leak
        unreferenced object 0xffff8880596171e0 (size 8):
        comm "syz-executor379", pid 8381, jiffies 4294980258 (age 21.100s)
        hex dump (first 8 bytes):
          6c 6f 6f 70 30 00 00 00                          loop0...
        backtrace:
           kstrdup+0x36/0x70 mm/util.c:60
           kstrdup_const+0x53/0x80 mm/util.c:83
           kvasprintf_const+0x108/0x190 lib/kasprintf.c:48
           kobject_set_name_vargs+0x56/0x150 lib/kobject.c:289
           kobject_add_varg lib/kobject.c:384 [inline]
           kobject_init_and_add+0xc9/0x160 lib/kobject.c:473
           nilfs_sysfs_create_device_group+0x150/0x800 fs/nilfs2/sysfs.c:999
           init_nilfs+0xe26/0x12b0 fs/nilfs2/the_nilfs.c:637
      
      Link: https://lkml.kernel.org/r/20210612140559.20022-1-paskripkin@gmail.com
      Fixes: da7141fb
      
       ("nilfs2: add /sys/fs/nilfs2/<device> group")
      Signed-off-by: default avatarPavel Skripkin <paskripkin@gmail.com>
      Acked-by: default avatarRyusuke Konishi <konishi.ryusuke@gmail.com>
      Cc: Michael L. Semon <mlsemon35@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      ab5bef97
    • Christoph Hellwig's avatar
      scsi: sd: Call sd_revalidate_disk() for ioctl(BLKRRPART) · ace31c91
      Christoph Hellwig authored
      [ Upstream commit d1b7f920 ]
      
      While the disk state has nothing to do with partitions, BLKRRPART is used
      to force a full revalidate after things like a disk format for historical
      reasons. Restore that behavior.
      
      Link: https://lore.kernel.org/r/20210617115504.1732350-1-hch@lst.de
      Fixes: 471bd0af
      
       ("sd: use bdev_check_media_change")
      Reported-by: default avatarXiang Chen <chenxiang66@hisilicon.com>
      Tested-by: default avatarXiang Chen <chenxiang66@hisilicon.com>
      Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
      Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      ace31c91
    • Gabriel Knezek's avatar
      gpiolib: cdev: zero padding during conversion to gpioline_info_changed · b9e6c20d
      Gabriel Knezek authored
      [ Upstream commit cb8f63b8 ]
      
      When userspace requests a GPIO v1 line info changed event,
      lineinfo_watch_read() populates and returns the gpioline_info_changed
      structure. It contains 5 words of padding at the end which are not
      initialized before being returned to userspace.
      
      Zero the structure in gpio_v2_line_info_change_to_v1() before populating
      its contents.
      
      Fixes: aad95584
      
       ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
      Signed-off-by: default avatarGabriel Knezek <gabeknez@linux.microsoft.com>
      Reviewed-by: default avatarKent Gibson <warthog618@gmail.com>
      Signed-off-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      b9e6c20d
    • Heiner Kallweit's avatar
      i2c: i801: Ensure that SMBHSTSTS_INUSE_STS is cleared when leaving i801_access · 0221a5a4
      Heiner Kallweit authored
      [ Upstream commit 065b6211 ]
      
      As explained in [0] currently we may leave SMBHSTSTS_INUSE_STS set,
      thus potentially breaking ACPI/BIOS usage of the SMBUS device.
      
      Seems patch [0] needs a little bit more of review effort, therefore
      I'd suggest to apply a part of it as quick win. Just clearing
      SMBHSTSTS_INUSE_STS when leaving i801_access() should fix the
      referenced issue and leaves more time for discussing a more
      sophisticated locking handling.
      
      [0] https://www.spinics.net/lists/linux-i2c/msg51558.html
      
      Fixes: 01590f36
      
       ("i2c: i801: Instantiate SPD EEPROMs automatically")
      Suggested-by: default avatarHector Martin <marcan@marcan.st>
      Signed-off-by: default avatarHeiner Kallweit <hkallweit1@gmail.com>
      Reviewed-by: default avatarHector Martin <marcan@marcan.st>
      Reviewed-by: default avatarJean Delvare <jdelvare@suse.de>
      Tested-by: default avatarJean Delvare <jdelvare@suse.de>
      Signed-off-by: default avatarWolfram Sang <wsa@kernel.org>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      0221a5a4
    • Fabien Dessenne's avatar
      pinctrl: stm32: fix the reported number of GPIO lines per bank · 018d03fc
      Fabien Dessenne authored
      [ Upstream commit 67e2996f ]
      
      Each GPIO bank supports a variable number of lines which is usually 16, but
      is less in some cases : this is specified by the last argument of the
      "gpio-ranges" bank node property.
      Report to the framework, the actual number of lines, so the libgpiod
      gpioinfo command lists the actually existing GPIO lines.
      
      Fixes: 1dc9d289
      
       ("pinctrl: stm32: add possibility to use gpio-ranges to declare bank range")
      Signed-off-by: default avatarFabien Dessenne <fabien.dessenne@foss.st.com>
      Link: https://lore.kernel.org/r/20210617144629.2557693-1-fabien.dessenne@foss.st.com
      Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      018d03fc
    • Kan Liang's avatar
      perf/x86: Track pmu in per-CPU cpu_hw_events · df654cd3
      Kan Liang authored
      [ Upstream commit 61e76d53
      
       ]
      
      Some platforms, e.g. Alder Lake, have hybrid architecture. In the same
      package, there may be more than one type of CPU. The PMU capabilities
      are different among different types of CPU. Perf will register a
      dedicated PMU for each type of CPU.
      
      Add a 'pmu' variable in the struct cpu_hw_events to track the dedicated
      PMU of the current CPU.
      
      Current x86_get_pmu() use the global 'pmu', which will be broken on a
      hybrid platform. Modify it to apply the 'pmu' of the specific CPU.
      
      Initialize the per-CPU 'pmu' variable with the global 'pmu'. There is
      nothing changed for the non-hybrid platforms.
      
      The is_x86_event() will be updated in the later patch ("perf/x86:
      Register hybrid PMUs") for hybrid platforms. For the non-hybrid
      platforms, nothing is changed here.
      
      Suggested-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Signed-off-by: default avatarKan Liang <kan.liang@linux.intel.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Link: https://lkml.kernel.org/r/1618237865-33448-4-git-send-email-kan.liang@linux.intel.com
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      df654cd3
    • Esben Haabendal's avatar
      net: ll_temac: Avoid ndo_start_xmit returning NETDEV_TX_BUSY · f9e73b29
      Esben Haabendal authored
      [ Upstream commit f6396341
      
       ]
      
      As documented in Documentation/networking/driver.rst, the ndo_start_xmit
      method must not return NETDEV_TX_BUSY under any normal circumstances, and
      as recommended, we simply stop the tx queue in advance, when there is a
      risk that the next xmit would cause a NETDEV_TX_BUSY return.
      
      Signed-off-by: default avatarEsben Haabendal <esben@geanix.com>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      f9e73b29
    • Esben Haabendal's avatar
      net: ll_temac: Add memory-barriers for TX BD access · 1c9cf96f
      Esben Haabendal authored
      [ Upstream commit 28d9fab4
      
       ]
      
      Add a couple of memory-barriers to ensure correct ordering of read/write
      access to TX BDs.
      
      In xmit_done, we should ensure that reading the additional BD fields are
      only done after STS_CTRL_APP0_CMPLT bit is set.
      
      When xmit_done marks the BD as free by setting APP0=0, we need to ensure
      that the other BD fields are reset first, so we avoid racing with the xmit
      path, which writes to the same fields.
      
      Finally, making sure to read APP0 of next BD after the current BD, ensures
      that we see all available buffers.
      
      Signed-off-by: default avatarEsben Haabendal <esben@geanix.com>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      1c9cf96f
    • Mikel Rychliski's avatar
      PCI: Add AMD RS690 quirk to enable 64-bit DMA · bafb6cdd
      Mikel Rychliski authored
      [ Upstream commit cacf994a
      
       ]
      
      Although the AMD RS690 chipset has 64-bit DMA support, BIOS implementations
      sometimes fail to configure the memory limit registers correctly.
      
      The Acer F690GVM mainboard uses this chipset and a Marvell 88E8056 NIC. The
      sky2 driver programs the NIC to use 64-bit DMA, which will not work:
      
        sky2 0000:02:00.0: error interrupt status=0x8
        sky2 0000:02:00.0 eth0: tx timeout
        sky2 0000:02:00.0 eth0: transmit ring 0 .. 22 report=0 done=0
      
      Other drivers required by this mainboard either don't support 64-bit DMA,
      or have it disabled using driver specific quirks. For example, the ahci
      driver has quirks to enable or disable 64-bit DMA depending on the BIOS
      version (see ahci_sb600_enable_64bit() in ahci.c). This ahci quirk matches
      against the SB600 SATA controller, but the real issue is almost certainly
      with the RS690 PCI host that it was commonly attached to.
      
      To avoid this issue in all drivers with 64-bit DMA support, fix the
      configuration of the PCI host. If the kernel is aware of physical memory
      above 4GB, but the BIOS never configured the PCI host with this
      information, update the registers with our values.
      
      [bhelgaas: drop PCI_DEVICE_ID_ATI_RS690 definition]
      Link: https://lore.kernel.org/r/20210611214823.4898-1-mikel@mikelr.com
      Signed-off-by: default avatarMikel Rychliski <mikel@mikelr.com>
      Signed-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      bafb6cdd
    • Peter Zijlstra's avatar
      recordmcount: Correct st_shndx handling · d91c50e6
      Peter Zijlstra authored
      [ Upstream commit fb780761
      
       ]
      
      One should only use st_shndx when >SHN_UNDEF and <SHN_LORESERVE. When
      SHN_XINDEX, then use .symtab_shndx. Otherwise use 0.
      
      This handles the case: st_shndx >= SHN_LORESERVE && st_shndx != SHN_XINDEX.
      
      Link: https://lore.kernel.org/lkml/20210607023839.26387-1-mark-pk.tsai@mediatek.com/
      Link: https://lkml.kernel.org/r/20210616154126.2794-1-mark-pk.tsai@mediatek.com
      
      Reported-by: default avatarMark-PK Tsai <mark-pk.tsai@mediatek.com>
      Tested-by: default avatarMark-PK Tsai <mark-pk.tsai@mediatek.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      [handle endianness of sym->st_shndx]
      Signed-off-by: default avatarMark-PK Tsai <mark-pk.tsai@mediatek.com>
      Signed-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      d91c50e6
    • Johannes Berg's avatar
      mac80211: handle various extensible elements correctly · fb71d81c
      Johannes Berg authored
      [ Upstream commit 652e8363
      
       ]
      
      Various elements are parsed with a requirement to have an
      exact size, when really we should only check that they have
      the minimum size that we need. Check only that and therefore
      ignore any additional data that they might carry.
      
      Signed-off-by: default avatarJohannes Berg <johannes.berg@intel.com>
      Signed-off-by: default avatarLuca Coelho <luciano.coelho@intel.com>
      Link: https://lore.kernel.org/r/iwlwifi.20210618133832.cd101f8040a4.Iadf0e9b37b100c6c6e79c7b298cc657c2be9151a@changeid
      Signed-off-by: default avatarJohannes Berg <johannes.berg@intel.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      fb71d81c
    • Johannes Berg's avatar
      mac80211: reset profile_periodicity/ema_ap · 676a7cb1
      Johannes Berg authored
      [ Upstream commit bbc6f03f
      
       ]
      
      Apparently we never clear these values, so they'll remain set
      since the setting of them is conditional. Clear the values in
      the relevant other cases.
      
      Signed-off-by: default avatarJohannes Berg <johannes.berg@intel.com>
      Signed-off-by: default avatarLuca Coelho <luciano.coelho@intel.com>
      Link: https://lore.kernel.org/r/iwlwifi.20210618133832.316e32d136a9.I2a12e51814258e1e1b526103894f4b9f19a91c8d@changeid
      Signed-off-by: default avatarJohannes Berg <johannes.berg@intel.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      676a7cb1
    • Kees Cook's avatar
      net: qed: Fix memcpy() overflow of qed_dcbx_params() · ca0e1fef
      Kees Cook authored
      [ Upstream commit 1c200f83
      
       ]
      
      The source (&dcbx_info->operational.params) and dest
      (&p_hwfn->p_dcbx_info->set.config.params) are both struct qed_dcbx_params
      (560 bytes), not struct qed_dcbx_admin_params (564 bytes), which is used
      as the memcpy() size.
      
      However it seems that struct qed_dcbx_operational_params
      (dcbx_info->operational)'s layout matches struct qed_dcbx_admin_params
      (p_hwfn->p_dcbx_info->set.config)'s 4 byte difference (3 padding, 1 byte
      for "valid").
      
      On the assumption that the size is wrong (rather than the source structure
      type), adjust the memcpy() size argument to be 4 bytes smaller and add
      a BUILD_BUG_ON() to validate any changes to the structure sizes.
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      ca0e1fef
    • Fuad Tabba's avatar
      KVM: selftests: Fix kvm_check_cap() assertion · 4658a8d3
      Fuad Tabba authored
      [ Upstream commit d8ac05ea
      
       ]
      
      KVM_CHECK_EXTENSION ioctl can return any negative value on error,
      and not necessarily -1. Change the assertion to reflect that.
      
      Signed-off-by: default avatarFuad Tabba <tabba@google.com>
      Message-Id: <20210615150443.1183365-1-tabba@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      4658a8d3
    • Kees Cook's avatar
      r8169: Avoid memcpy() over-reading of ETH_SS_STATS · e83e3c5d
      Kees Cook authored
      [ Upstream commit da5ac772
      
       ]
      
      In preparation for FORTIFY_SOURCE performing compile-time and run-time
      field bounds checking for memcpy(), memmove(), and memset(), avoid
      intentionally reading across neighboring array fields.
      
      The memcpy() is copying the entire structure, not just the first array.
      Adjust the source argument so the compiler can do appropriate bounds
      checking.
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      e83e3c5d
    • Kees Cook's avatar
      sh_eth: Avoid memcpy() over-reading of ETH_SS_STATS · 992b105a
      Kees Cook authored
      [ Upstream commit 224004fb
      
       ]
      
      In preparation for FORTIFY_SOURCE performing compile-time and run-time
      field bounds checking for memcpy(), memmove(), and memset(), avoid
      intentionally reading across neighboring array fields.
      
      The memcpy() is copying the entire structure, not just the first array.
      Adjust the source argument so the compiler can do appropriate bounds
      checking.
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      992b105a