Skip to content
Snippets Groups Projects
  1. Apr 26, 2024
  2. Feb 22, 2024
  3. Dec 12, 2023
  4. Oct 04, 2023
    • Zi Yan's avatar
      mm/cma: use nth_page() in place of direct struct page manipulation · 2e7cfe5c
      Zi Yan authored
      Patch series "Use nth_page() in place of direct struct page manipulation",
      v3.
      
      On SPARSEMEM without VMEMMAP, struct page is not guaranteed to be
      contiguous, since each memory section's memmap might be allocated
      independently.  hugetlb pages can go beyond a memory section size, thus
      direct struct page manipulation on hugetlb pages/subpages might give wrong
      struct page.  Kernel provides nth_page() to do the manipulation properly. 
      Use that whenever code can see hugetlb pages.
      
      
      This patch (of 5):
      
      When dealing with hugetlb pages, manipulating struct page pointers
      directly can get to wrong struct page, since struct page is not guaranteed
      to be contiguous on SPARSEMEM without VMEMMAP.  Use nth_page() to handle
      it properly.
      
      Without the fix, page_kasan_tag_reset() could reset wrong page tags,
      causing a wrong kasan result.  No related bug is reported.  The fix
      comes from code inspection.
      
      Link: https://lkml.kernel.org/r/20230913201248.452081-1-zi.yan@sent.com
      Link: https://lkml.kernel.org/r/20230913201248.452081-2-zi.yan@sent.com
      
      
      Fixes: 2813b9c0 ("kasan, mm, arm64: tag non slab memory allocated via pagealloc")
      Signed-off-by: default avatarZi Yan <ziy@nvidia.com>
      Reviewed-by: default avatarMuchun Song <songmuchun@bytedance.com>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Mike Rapoport (IBM) <rppt@kernel.org>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      2e7cfe5c
  5. Aug 18, 2023
  6. Jul 31, 2023
    • Yajun Deng's avatar
      dma-contiguous: support numa CMA for specified node · bf29bfaa
      Yajun Deng authored
      
      The kernel parameter 'cma_pernuma=' only supports reserving the same
      size of CMA area for each node. We need to reserve different sizes of
      CMA area for specified nodes if these devices belong to different nodes.
      
      Adding another kernel parameter 'numa_cma=' to reserve CMA area for
      the specified node. If we want to use one of these parameters, we need to
      enable DMA_NUMA_CMA.
      
      At the same time, print the node id in cma_declare_contiguous_nid() if
      CONFIG_NUMA is enabled.
      
      Signed-off-by: default avatarYajun Deng <yajun.deng@linux.dev>
      Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
      bf29bfaa
  7. Jun 19, 2023
  8. Apr 06, 2023
  9. Feb 03, 2023
    • Levi Yun's avatar
      mm/cma: fix potential memory loss on cma_declare_contiguous_nid · 148aa87e
      Levi Yun authored
      Suppose memblock_alloc_range_nid() with highmem_start succeeds when
      cma_declare_contiguous_nid is called with !fixed on a 32-bit system with
      PHYS_ADDR_T_64BIT enabled with memblock.bottom_up == false.
      
      But the next trial to memblock_alloc_range_nid() to allocate in [SIZE_4G,
      limits) nullifies former successfully allocated addr and it retries
      memblock_alloc_ragne_nid().
      
      In this situation, the first successfully allocated address area is lost.
      
      Change the order of allocation (SIZE_4G, high_memory and base) and check
      whether the allocated succeeded to prevent potential memory loss.
      
      Link: https://lkml.kernel.org/r/20230118080523.44522-1-ppbuk5246@gmail.com
      
      
      Signed-off-by: default avatarLevi Yun <ppbuk5246@gmail.com>
      Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
      Cc: Marek Szyprowski <m.szyprowski@samsung.com>
      Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      148aa87e
  10. Jan 19, 2023
  11. May 13, 2022
    • Dong Aisheng's avatar
      Revert "mm/cma.c: remove redundant cma_mutex lock" · 60a60e32
      Dong Aisheng authored
      This reverts commit a4efc174 which introduced a regression issue
      that when there're multiple processes allocating dma memory in parallel by
      calling dma_alloc_coherent(), it may fail sometimes as follows:
      
      Error log:
      cma: cma_alloc: linux,cma: alloc failed, req-size: 148 pages, ret: -16
      cma: number of available pages:
      3@125+20@172+12@236+4@380+32@736+17@2287+23@2473+20@36076+99@40477+108@40852+44@41108+20@41196+108@41364+108@41620+
      108@42900+108@43156+483@44061+1763@45341+1440@47712+20@49324+20@49388+5076@49452+2304@55040+35@58141+20@58220+20@58284+
      7188@58348+84@66220+7276@66452+227@74525+6371@75549=> 33161 free of 81920 total pages
      
      When issue happened, we saw there were still 33161 pages (129M) free CMA
      memory and a lot available free slots for 148 pages in CMA bitmap that we
      want to allocate.
      
      When dumping memory info, we found that there was also ~342M normal
      memory, but only 1352K CMA memory left in buddy system while a lot of
      pageblocks were isolated.
      
      Memory info log:
      Normal free:351096kB min:30000kB low:37500kB high:45000kB reserved_highatomic:0KB
      	    active_anon:98060kB inactive_anon:98948kB active_file:60864kB inactive_file:31776kB
      	    unevictable:0kB writepending:0kB present:1048576kB managed:1018328kB mlocked:0kB
      	    bounce:0kB free_pcp:220kB local_pcp:192kB free_cma:1352kB lowmem_reserve[]: 0 0 0
      Normal: 78*4kB (UECI) 1772*8kB (UMECI) 1335*16kB (UMECI) 360*32kB (UMECI) 65*64kB (UMCI)
      	36*128kB (UMECI) 16*256kB (UMCI) 6*512kB (EI) 8*1024kB (UEI) 4*2048kB (MI) 8*4096kB (EI)
      	8*8192kB (UI) 3*16384kB (EI) 8*32768kB (M) = 489288kB
      
      The root cause of this issue is that since commit a4efc174 ("mm/cma.c:
      remove redundant cma_mutex lock"), CMA supports concurrent memory
      allocation.  It's possible that the memory range process A trying to alloc
      has already been isolated by the allocation of process B during memory
      migration.
      
      The problem here is that the memory range isolated during one allocation
      by start_isolate_page_range() could be much bigger than the real size we
      want to alloc due to the range is aligned to MAX_ORDER_NR_PAGES.
      
      Taking an ARMv7 platform with 1G memory as an example, when
      MAX_ORDER_NR_PAGES is big (e.g.  32M with max_order 14) and CMA memory is
      relatively small (e.g.  128M), there're only 4 MAX_ORDER slot, then it's
      very easy that all CMA memory may have already been isolated by other
      processes when one trying to allocate memory using dma_alloc_coherent(). 
      Since current CMA code will only scan one time of whole available CMA
      memory, then dma_alloc_coherent() may easy fail due to contention with
      other processes.
      
      This patch simply falls back to the original method that using cma_mutex
      to make alloc_contig_range() run sequentially to avoid the issue.
      
      Link: https://lkml.kernel.org/r/20220509094551.3596244-1-aisheng.dong@nxp.com
      Link: https://lore.kernel.org/all/20220315144521.3810298-2-aisheng.dong@nxp.com/
      
      
      Fixes: a4efc174 ("mm/cma.c: remove redundant cma_mutex lock")
      Signed-off-by: default avatarDong Aisheng <aisheng.dong@nxp.com>
      Acked-by: default avatarMinchan Kim <minchan@kernel.org>
      Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
      Cc: Marek Szyprowski <m.szyprowski@samsung.com>
      Cc: Lecopzer Chen <lecopzer.chen@mediatek.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: <stable@vger.kernel.org>	[5.11+]
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      60a60e32
  12. Mar 22, 2022
  13. Nov 06, 2021
  14. May 05, 2021
  15. Feb 26, 2021
  16. Dec 15, 2020
  17. Aug 12, 2020
  18. Jul 03, 2020
    • Barry Song's avatar
      mm/cma.c: use exact_nid true to fix possible per-numa cma leak · 40366bd7
      Barry Song authored
      
      Calling cma_declare_contiguous_nid() with false exact_nid for per-numa
      reservation can easily cause cma leak and various confusion.  For example,
      mm/hugetlb.c is trying to reserve per-numa cma for gigantic pages.  But it
      can easily leak cma and make users confused when system has memoryless
      nodes.
      
      In case the system has 4 numa nodes, and only numa node0 has memory.  if
      we set hugetlb_cma=4G in bootargs, mm/hugetlb.c will get 4 cma areas for 4
      different numa nodes.  since exact_nid=false in current code, all 4 numa
      nodes will get cma successfully from node0, but hugetlb_cma[1 to 3] will
      never be available to hugepage will only allocate memory from
      hugetlb_cma[0].
      
      In case the system has 4 numa nodes, both numa node0&2 has memory, other
      nodes have no memory.  if we set hugetlb_cma=4G in bootargs, mm/hugetlb.c
      will get 4 cma areas for 4 different numa nodes.  since exact_nid=false in
      current code, all 4 numa nodes will get cma successfully from node0 or 2,
      but hugetlb_cma[1] and [3] will never be available to hugepage as
      mm/hugetlb.c will only allocate memory from hugetlb_cma[0] and
      hugetlb_cma[2].  This causes permanent leak of the cma areas which are
      supposed to be used by memoryless node.
      
      Of cource we can workaround the issue by letting mm/hugetlb.c scan all cma
      areas in alloc_gigantic_page() even node_mask includes node0 only.  that
      means when node_mask includes node0 only, we can get page from
      hugetlb_cma[1] to hugetlb_cma[3].  But this will cause kernel crash in
      free_gigantic_page() while it wants to free page by:
      cma_release(hugetlb_cma[page_to_nid(page)], page, 1 << order)
      
      On the other hand, exact_nid=false won't consider numa distance, it might
      be not that useful to leverage cma areas on remote nodes.  I feel it is
      much simpler to make exact_nid true to make everything clear.  After that,
      memoryless nodes won't be able to reserve per-numa CMA from other nodes
      which have memory.
      
      Fixes: cf11e85f ("mm: hugetlb: optionally allocate gigantic hugepages using cma")
      Signed-off-by: default avatarBarry Song <song.bao.hua@hisilicon.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Acked-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
      Cc: Aslan Bakirov <aslan@fb.com>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Andreas Schaufler <andreas.schaufler@gmx.de>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Rik van Riel <riel@surriel.com>
      Cc: Joonsoo Kim <js1304@gmail.com>
      Cc: Robin Murphy <robin.murphy@arm.com>
      Cc: <stable@vger.kernel.org>
      Link: http://lkml.kernel.org/r/20200628074345.27228-1-song.bao.hua@hisilicon.com
      
      
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      40366bd7
  19. Apr 10, 2020
  20. Dec 01, 2019
  21. Jul 17, 2019
  22. May 24, 2019
  23. May 14, 2019
Loading