From: Dong Aisheng <aisheng.dong@nxp.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, dongas86@gmail.com,
shawnguo@kernel.org, linux-imx@nxp.com,
akpm@linux-foundation.org, m.szyprowski@samsung.com,
lecopzer.chen@mediatek.com, david@redhat.com, vbabka@suse.cz,
stable@vger.kernel.org, shijie.qin@nxp.com,
Dong Aisheng <aisheng.dong@nxp.com>
Subject: [PATCH v3 2/2] mm: cma: try next MAX_ORDER_NR_PAGES during retry
Date: Tue, 15 Mar 2022 22:45:21 +0800 [thread overview]
Message-ID: <20220315144521.3810298-3-aisheng.dong@nxp.com> (raw)
In-Reply-To: <20220315144521.3810298-1-aisheng.dong@nxp.com>
On an ARMv7 platform with 1G memory, when MAX_ORDER_NR_PAGES
is big (e.g. 32M with max_order 14) and CMA memory is relatively small
(e.g. 128M), we observed a huge number of repeat retries of CMA
allocation (1k+) during booting when allocating one page for each
of 3 mmc instance probe.
This is caused by CMA now supports concurrent allocation since commit
a4efc174b382 ("mm/cma.c: remove redundant cma_mutex lock").
The memory range, MAX_ORDER_NR_PAGES aligned, from which we are trying
to allocate memory may have already been acquired and isolated by others
(see: alloc_contig_range()).
Current cma_alloc() will retry the next area by a small step of
bitmap_no + mask + 1 which are very likely within the same isolated range
and fail again. So when the MAX_ORDER_NR_PAGES is big (e.g. 32M),
keep retrying in a small step become meaningless because it will be known
to fail at a huge number of times due to that memory range has been isolated
by others, especially when allocating only one or two pages.
Instead of looping in the same isolated range and wasting CPU mips a lot,
especially for big MAX_ORDER systems (e.g. 16M or 32M),
we try the next MAX_ORDER_NR_PAGES directly.
Doing this way can greatly mitigate the situtation.
Below is the original error log during booting:
[ 2.004804] cma: cma_alloc(cma (ptrval), count 1, align 0)
[ 2.010318] cma: cma_alloc(cma (ptrval), count 1, align 0)
[ 2.010776] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
[ 2.010785] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
[ 2.010793] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
[ 2.010800] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
[ 2.010807] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
[ 2.010814] cma: cma_alloc(): memory range at (ptrval) is busy, retrying
.... (+1K retries)
After fix, the 1200+ reties can be reduced to 0.
Another test running 8 threads running dma_alloc_coherent() in parallel
shows that 1500+ retries dropped to ~145.
IOW this patch can improve the CMA allocation speed a lot when there're
enough CMA memory by reducing retries significantly.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Lecopzer Chen <lecopzer.chen@mediatek.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
CC: stable@vger.kernel.org # 5.11+
Fixes: a4efc174b382 ("mm/cma.c: remove redundant cma_mutex lock")
Signed-off-by: Dong Aisheng <aisheng.dong@nxp.com>
---
v2->v3:
* Improve commit messeages
v1->v2:
* change to align with MAX_ORDER_NR_PAGES instead of pageblock_nr_pages
---
mm/cma.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/mm/cma.c b/mm/cma.c
index 46a9fd9f92c4..46bc12fe28b3 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -496,8 +496,16 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,
trace_cma_alloc_busy_retry(cma->name, pfn, pfn_to_page(pfn),
count, align);
- /* try again with a bit different memory target */
- start = bitmap_no + mask + 1;
+ /*
+ * Try again with a bit different memory target.
+ * Since memory isolated in alloc_contig_range() is aligned
+ * with MAX_ORDER_NR_PAGES, instead of retrying in a small
+ * step within the same isolated range, we try the next
+ * available memory range directly.
+ */
+ start = ALIGN(bitmap_no + mask + 1,
+ MAX_ORDER_NR_PAGES >> cma->order_per_bit);
+
}
trace_cma_alloc_finish(cma->name, pfn, page, count, align);
--
2.25.1
prev parent reply other threads:[~2022-03-15 14:43 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-15 14:45 [PATCH v3 0/2] mm: fix cma allocation fail sometimes Dong Aisheng
2022-03-15 14:45 ` [PATCH v3 1/2] mm: cma: fix allocation may " Dong Aisheng
2022-03-15 22:58 ` Andrew Morton
2022-03-16 3:41 ` Dong Aisheng
2022-03-16 21:09 ` Andrew Morton
2022-03-17 3:49 ` Dong Aisheng
2022-03-17 10:55 ` David Hildenbrand
2022-03-17 14:26 ` Dong Aisheng
2022-03-17 17:12 ` Minchan Kim
2022-03-18 3:43 ` Dong Aisheng
2022-03-18 16:20 ` Minchan Kim
2022-05-04 15:52 ` Dong Aisheng
2022-05-04 23:25 ` Minchan Kim
2022-03-15 14:45 ` Dong Aisheng [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220315144521.3810298-3-aisheng.dong@nxp.com \
--to=aisheng.dong@nxp.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=dongas86@gmail.com \
--cc=lecopzer.chen@mediatek.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-imx@nxp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=m.szyprowski@samsung.com \
--cc=shawnguo@kernel.org \
--cc=shijie.qin@nxp.com \
--cc=stable@vger.kernel.org \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox