From patchwork Tue Mar 15 14:45:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dong Aisheng X-Patchwork-Id: 12781562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 14F6BC433F5 for ; Tue, 15 Mar 2022 15:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ODamVwupVANdAhIoyHqnzk2mnU59y6qUJPuwYW+jSiI=; b=i/Fmn35EHxQByk ddm1SVtH2HrDNaVU8yDrDTvgIhQun4oR6JEfj4OhHj6NjTMFPaiQGi1FV/7kWL6YR68BED8gChZtm EQGlojm7uwxKsckRpm97cfxmGcBsYqvj+kU154DwfKJV4MC9xnLoL1kVgdEEC3oiNRafvok0gZlW1 AUaY7il92Ez5nM89F/TLB8BkQVWCOL4F52/aeHU57IMst80inMCfmPSSWthk6N1eocZhPoo1FtaNl i2y+EQREsyPY9O3Gc77FtIwseRSbtUkcl91gK7gUuxLcch51SmjqFL/GEojQ4j8t2filOR+J+P8Km Ayh1wY9vgkAEt9BLv6OA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nU8dq-009bMd-DG; Tue, 15 Mar 2022 14:59:19 +0000 Received: from mail-am5eur02on0627.outbound.protection.outlook.com ([2a01:111:f400:fe07::627] helo=EUR02-AM5-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nU8dY-009bCf-K3 for linux-arm-kernel@lists.infradead.org; Tue, 15 Mar 2022 14:59:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k5FFNQuW/gbrx9qFZ8RV75mCCc47VsLZB7llWOjbPDEm1le0oZ14Ar3hexOTjxFvaBNxgTXA2Gc/ssZwP0J4X2gqfOuYOmuYpq+4EmuCzgPV8z/f2oCsWMjvml0Fo4SHQ+tBWU2RcHq4KaRffkbxOR6jdcsqd9Q0UAhOl94giPdP/QvW//Eqf6xg7euGBtFqSth6hhMsJR08jSL5uBbb/0egeZ7XYAmfNCsK8YH0gvt0Kbn95VRK/vnZBi3PJHdvexxYm3D5FajdbYZ5m0fMqhIvoM+hgeZ805o6UJKocSQzDVOt4pnovt0wCaNmNkP2u3rdXN6tqLxvtEJx1IOh1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9mF6Bgp3L5rKpQa74lnxQEwNgjqflRId4Re63fK+mNk=; b=caGOWnTeF0BeD9fd7iQ5Vm/o23IY1gBwTFVsbMjlafOMywrkAHu0WDKnW3o0oSix/LG54DAcdl/Q/Jc8GKSVJNbGtLl/Iq1IOq8C3fHY9KZDEtkyNc2D61Mviwi8crDT1hGWgMsqQN/7Nh9yh/AJYy0W4JmDDICb6xglj8k9axeraaIqT2clAljqNkxPOyGDyiaRM7ylah0exjIBLd3ZxS5hH+uQvzJPb5Hz4Z032JQuFsSnZJs8YOqLVrBJPfu1fpF/Wz1LitYucjWtECYyh0iE6z7Sb6SKQRCUEWNOWunHwVaNVovDFsunhFgk0cjbDDq9kzQ7YFZ/eUN9mVakZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9mF6Bgp3L5rKpQa74lnxQEwNgjqflRId4Re63fK+mNk=; b=N4/CaKyBYRPPkQeHswWLJ5DrI6fY/P3EhwW3XPH4BI7upagqnzPx4cyGn+dqAHNJ88qwR0KRglyQd7NlnYvA8JVpVmLHCfzKISK0/AS96Rbuu7VsW3drhmhYBzU4ew6DRGezAFjgfRpU9kQUlBDM7e5wL748AgZ6qEvNhLrS6os= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DB9PR04MB8477.eurprd04.prod.outlook.com (2603:10a6:10:2c3::11) by AM8PR04MB7281.eurprd04.prod.outlook.com (2603:10a6:20b:1d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.29; Tue, 15 Mar 2022 14:43:43 +0000 Received: from DB9PR04MB8477.eurprd04.prod.outlook.com ([fe80::110a:dc6e:bd9c:3529]) by DB9PR04MB8477.eurprd04.prod.outlook.com ([fe80::110a:dc6e:bd9c:3529%9]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 14:43:43 +0000 From: Dong Aisheng 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 Subject: [PATCH v3 1/2] mm: cma: fix allocation may fail sometimes Date: Tue, 15 Mar 2022 22:45:20 +0800 Message-Id: <20220315144521.3810298-2-aisheng.dong@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220315144521.3810298-1-aisheng.dong@nxp.com> References: <20220315144521.3810298-1-aisheng.dong@nxp.com> X-ClientProxiedBy: SI2PR06CA0017.apcprd06.prod.outlook.com (2603:1096:4:186::15) To DB9PR04MB8477.eurprd04.prod.outlook.com (2603:10a6:10:2c3::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4adc1f6e-867f-4efe-4681-08da06923387 X-MS-TrafficTypeDiagnostic: AM8PR04MB7281:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TPW5Fx2mOB0xXqYulUzRgaMdHimteV0S3TL4vUxiA2vsusY6C2ZApr1rN1M3G7EKvz70fF+w5sff7YAGfbSaT0WaH15KYxXtW/sIX3VJsexjwyXWBqR0Fl2tf4Pvg+PxFXmmb4jBFxm/9xlgYZDghwu+rlxGZWnyTnhBkcHq9oo3IREgFtNuZlQmKYzzVeQgZ45XRJlZjZQPfOkjq7IX3cgzhLn5n6apmpk+97zlTzmQjG4HYRVBTJNqR/TpzsWPbW9jVcPUdz7a6FFnaZpPVeCmQJkOXTtDtb3P4uT6srRh+pUyPej+GXkcXekRGLe7sg6nPAPSLknyiuxwRY6Re7FberDW/Lu5FHaYpy3NDTInFpG4NveE/IvhElTKpaErlDNJyZQuYej/XGuZL+D8dMg+OXuKmgV3ACvOwhphzC3lnYFQv6bl9S6igqOHoZe3RwllLM0li+jqCvZ3WNtWXV/BuHVDw0xCVCvKId/nH+zjB/gaKjNazK0tn92DZFh5f6EfCBGzgvsovkTxKmQWN/hAod5lprXMDbJwewZpdgbuKjJc1U6xnAPsdL7I1T5bRlXo2Liy/IeaeSYCxUdz1RtvJWEPSI7O9Wmwopvw8Q9Erquz5KzE+HZJ2X5lNhzaeu6wtV0NyiDEroqVgBaqu9JTbI50fPNaso2hPDak97THE3/b+n+qWSyqhpC50tC62xi6izMtIVWw44zpkJpV3A== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR04MB8477.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(1076003)(6512007)(6486002)(2616005)(26005)(186003)(508600001)(6666004)(6506007)(52116002)(38100700002)(38350700002)(83380400001)(86362001)(316002)(66556008)(2906002)(6916009)(8936002)(4326008)(66476007)(66946007)(8676002)(36756003)(7416002)(5660300002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 48B2KY0tLa3PRIpiMeTswaaCKnxaosNoBcJ+dVN+Q7g8lFuPPBtp4F4t9t+QlCnAhVWn+N9R5NBfcMzdpqr0qINxN5ehgfXlnfx4mtT4insV9emHieBp+DdWXaVxZ1vBsYY4E63tpmNVHsgdTtMHYesVPwqOdQaLKZvx5ofqkyK21q69kYTsDnM5wpuVm5QNGPcbR83Y1a/B6N7F4RU0nSQ0nU9ygDHnBOdtRVx7jhc9zw5LhydW2Hm1/NjWuZ9XkE66CeFz/yqM7vDFtuKKNiBIAyVDV1LeoAt98ZLMkXAvcAIPcUdC8OG3ah+wma74hPFgACVxyj3BDCTd3cvDt335LIYeVDP8LhQIAsDuMBHHXUQQKLZVigA94In8NZdo+66Nck7NScJ0zQUowLqfCoTTs3sXRGrIzhbRFxXfkvd/sxuAO9KnGQhmI4s0bVxX9p35s3UUGrIGyP/aUmtFNfcIrDAisEW/OOG9KnxW7ePxhSAV+psZ3Ejuu+HL21ATMy2sTbBL12DxRu9kGWV6n07xuLrryBGSqLsj/J6oXhezcIO0fXYF1tmwCj6PGQbuI57Os09ONagvCiQCUi5hqG3ZshRm/iHlr+h4alsG0Gn/4XwMC9tuWstLE0Wr6Vb7AmgwwsxI6Tw63iYPxLtgr8fBf0PzhVlwM7YW4PmQAQGpu0En57sXYnuMKmlb2XrwLUrao960lZCBEzvz4kfgMvGXp/TgXg8XPjE+FYQ7zkkX/cm+sZlWJur9SSfnjo87c90qXLr3k9tD7+mNiJUlqOW+5y3Xn1XdiWXDNofmzLhUz+z9UpbgjhkuybtW8vnyHMF5v27VOR/dPGDcbVL9S7DliG7eUHeRRY5TZz8wg07zXpquWyTqK3I62ledNHTTmXzwOYbOsiQlK8TvVSo98/Ts0V/Lnj54pDe0AjnJhCqZG7ZDjPByyeFMZ/eoAVBTjsewOvaZ5NUt08dvg7Xk9oFvOg3eQbsSZcMtJTToRi0/oNSlGot3BB4RnJyaQjvcFi0hzpgFNZfD8dh6qx4U7WOQPsu6jC/3YxgyhIzpg06ZLaMIPBVW81wTHlyrQRaocFpsntHyVF2gg9ZlTagGGJLa++BDZfGOMd326JYfK45AVuHUUn28vhx62DZ4v4Dbt1jbsufyCiTUISaaN2v+hWeCfl5hq42VyfMQuPsuf2OA37AOuMxDou5IcIMoOUgBs+CuXeJAnlgZQTW9fJQjMmZMOI8EIOu9MeVGoh3e/GGkzm2Z3wzR0PyJ/14MPj2vOdettjw/VKfRM1LmUvk25cMtH+nI3jTD96PWJE0DVjIgTqLAS+koTd4cKdAqHcm12oH+O6sm1bI1yymnEcvdWCHJwJPA6LFILyU+J82kin68PTcp9JyE1B8PxFbyuP4h7/U+IJG5gQx7ApEncHgnJHh3FlyTBK34TTCf7qKdWCnCfIRZSV0FjhJAu4+KU7bYx96nSD51PfqC0YFey/NOyw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4adc1f6e-867f-4efe-4681-08da06923387 X-MS-Exchange-CrossTenant-AuthSource: DB9PR04MB8477.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Mar 2022 14:43:43.0517 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Smcucb1YRcFgyjWl+LVIVzzeZ4GYtMYZbWIsnMJRlxks83oH/UJg04zAxobW3NwTuBVdBLYFRhcgMfABOdE9Vw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR04MB7281 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220315_075900_697899_7F0132FA X-CRM114-Status: GOOD ( 19.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When there're multiple process allocing 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. If 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 a4efc174b382 ("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 introduces a retry mechanism to rescan CMA bitmap for -EBUSY error in case the target memory range may has been temporarily isolated by others and released later. Cc: Andrew Morton Cc: Marek Szyprowski Cc: Lecopzer Chen Cc: David Hildenbrand Cc: Vlastimil Babka CC: stable@vger.kernel.org # 5.11+ Fixes: a4efc174b382 ("mm/cma.c: remove redundant cma_mutex lock") Signed-off-by: Dong Aisheng --- ChangeLog: * v2->v3: Improve commit messages * v1->v2: no changes --- mm/cma.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/cma.c b/mm/cma.c index eaa4b5c920a2..46a9fd9f92c4 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -430,6 +430,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned long i; struct page *page = NULL; int ret = -ENOMEM; + int loop = 0; if (!cma || !cma->count || !cma->bitmap) goto out; @@ -457,6 +458,16 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, offset); if (bitmap_no >= bitmap_maxno) { spin_unlock_irq(&cma->lock); + pr_debug("%s(): alloc fail, retry loop %d\n", __func__, loop++); + /* + * rescan as others may finish the memory migration + * and quit if no available CMA memory found finally + */ + if (start) { + schedule(); + start = 0; + continue; + } break; } bitmap_set(cma->bitmap, bitmap_no, bitmap_count); From patchwork Tue Mar 15 14:45:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dong Aisheng X-Patchwork-Id: 12781560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8C9DAC433F5 for ; Tue, 15 Mar 2022 14:59:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=e73t4HIL1fFhBfUY+baOdzTxkVSbBaUfk45h4tNviWs=; b=Hrw/iaplPoyKkR zPiJk+TqDCTpHdzShao+po5NO0esknwMq7GIVsRTs2sYD4FQrUfpzFfojswD7WXEsnHGxheFdUwWR vbo3+BQ4KyNpvE1nQUn9kcFpIANpoXXsKZuVRIKJeTxDc9ND7+n8OS6VrErHX27hRRohrjOzA1ZpD AAYXiiY4soRjk/XXjIkrR+mHU67D997hxtXe4Iq92sIRiNksxs+awO6T3RHgb2dXdpLEFAHO5Po/h mPy+xfI6JHcTGbLPPvWeytQjx+HpA+7uEfbLG8DWowx5uIiaIPC0koe1HAnZXlxKD35d5aLAQXtx2 7wgXIk3+tVcoR3fmGPGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nU8d4-009b1X-3t; Tue, 15 Mar 2022 14:58:30 +0000 Received: from mail-am5eur02on0616.outbound.protection.outlook.com ([2a01:111:f400:fe07::616] helo=EUR02-AM5-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nU8d1-009b0X-DK for linux-arm-kernel@lists.infradead.org; Tue, 15 Mar 2022 14:58:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UKfL7F3DwEiN1xslfhWGu8/zvVUQ12oUnSTn0ccCh3re2fIPGsmMSOu53Vgij1tbwBJF6rJsWo5CYlYWyqn8T3qwvHMMNCDOGyCNmQydclyOQb9h64WXu35eeB7nM3LWOAbTvMf4slNexe9GIPRzcczHepgCVjzk6A/YleAMrhWrv8SxzMASXPV0Tgta9yYYcfrwQswFhjb4ZTP40mrdSx0yJ7Hi0TotTw6DL06zj6DYIVKfcfZ0A+cnSlzcqQR/78E8Gvk8sgVmQS5jdYwfX2KAyCF3u9Z4TD2XFvizo3gRcy35meoRj2YqlfcVN8Saf/yLNghLTaXKzKLYu1sdHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=izGVLYxZV8/3oohYdJEG6xwzGwx4SIhUIF/AD/5v1pY=; b=iT4NcOimsnCJrUCgZMJ1ExSDdq263Em+0+Kga70lXcp7xmAVLjKHuygdY8UElrf6VNCJnk2O92ylYT8j2krEVPNCVhwn8IgtDs8QobUweGRR1O8kAeIvTLE+T7pb9ZSbn4mOIt2n+LnMJ+Ay1o4tBuo0tNTqmH0vIrd+EPdzRRBFC6oyAhmdfdoZrs5xqSHga58GYJ+ymRkrm7Q0bKyvwqTNlYw8g8rHwqXkR9ynGkMcVhUXVfMq5cEk0dKVBl2y9mHmsZCcLRcSdlLVgsO90B5VZkujXU55nstwNPRmWtq9HJ2bWLMYn80/TLw9vVXtb68tq5GG6NLdyj1JAht+6w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=izGVLYxZV8/3oohYdJEG6xwzGwx4SIhUIF/AD/5v1pY=; b=P428L8A9FWH76I4k3NOxeAedMnMMY1F16C7YctnbUTtFvk8K6rB6fNez79ZCzjSuCMkRF8wQLaBy38lLa+1968Lo2a2M2FSew/LYsmwbK8fVaIl+b3gfa91g64oAQPfgieE9otijWJGWJRKO17fPNCSClyuBUVxRSXsBg3toJAE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DB9PR04MB8477.eurprd04.prod.outlook.com (2603:10a6:10:2c3::11) by AM8PR04MB7281.eurprd04.prod.outlook.com (2603:10a6:20b:1d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.29; Tue, 15 Mar 2022 14:43:50 +0000 Received: from DB9PR04MB8477.eurprd04.prod.outlook.com ([fe80::110a:dc6e:bd9c:3529]) by DB9PR04MB8477.eurprd04.prod.outlook.com ([fe80::110a:dc6e:bd9c:3529%9]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 14:43:47 +0000 From: Dong Aisheng 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 Subject: [PATCH v3 2/2] mm: cma: try next MAX_ORDER_NR_PAGES during retry Date: Tue, 15 Mar 2022 22:45:21 +0800 Message-Id: <20220315144521.3810298-3-aisheng.dong@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220315144521.3810298-1-aisheng.dong@nxp.com> References: <20220315144521.3810298-1-aisheng.dong@nxp.com> X-ClientProxiedBy: SI2PR06CA0017.apcprd06.prod.outlook.com (2603:1096:4:186::15) To DB9PR04MB8477.eurprd04.prod.outlook.com (2603:10a6:10:2c3::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8adc209e-1ea1-44e4-c3d4-08da0692365e X-MS-TrafficTypeDiagnostic: AM8PR04MB7281:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7Kn/rIeC3a4I1KeqfJ0qgHTHAVPe8e7l1Yfkaa6mWrwcFND7S2KC1Zn+zLmaallmOeGyeTMQOkYSq5L4c0+2mRpFTewmBuPBceHG0oNThBa/1+HlKW7ho4EVsrtJA9dpWZuiFOX3lxZayjsFDO3LebcSUuyhyZEX4H6isIZQIH9TdrHTOMQ/k9y3qFAvMrFupaBpcnc5QuN+I9ey0qprXpbRVEigP5ZEajgYDNkR4ie8VgIIEHcUw/4UYqfTT3BKYqFKAY2tt9BwDh4camojFnE6zdDXNPrk9u3i1jhop1qhyCtsWdNVBTB0oGgM4utlL9EVXuPFgE6xNDkpmxAfTwLXBv8usCmvM13kmLlgtdsMXGlcKClHBi6AqXT2AjVHmyqdZpmGy4Qflimu4ZOxjKF3vKcQW9kRdw6TjmEGYphaXwgPVJ4UIbF/MYXL4ldkOl7AP7AHHAxVdcH0sORNCpZgzPnCk/jw3ZH4TuNU5QzaNcGQZUoZ6hhWktsJvXCkFq16bdUOsZ/ZJxNqeBoHlmcbRAkL0ePM8kt62PhqnKd/vTCzM+kZmrPEiNlZ03X2yD4B+xXHQbz0oaeuOfrWVlv8K8axlLRZbaIz0ZvcamrpKVOBupvqbGydpR6XW9+TDVJ5hiRxfX4F9nyZP17ylpvKxEUcNVEySX1rQ/Ey8J1rYH7HzzwG4rcmCetiQmd4RflYDnUiAFK/pvI0kZztkA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR04MB8477.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(1076003)(6512007)(6486002)(2616005)(26005)(186003)(508600001)(6666004)(6506007)(52116002)(38100700002)(38350700002)(83380400001)(86362001)(316002)(66556008)(2906002)(6916009)(8936002)(4326008)(66476007)(66946007)(8676002)(36756003)(7416002)(5660300002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rmc7fOiAsNc8E1LambyW0iSa1qqtXr2CZc09cMoA9uvjHwVfIWOSNEkL4vlMXvg5NwTD0QnZ8EhVBb+oUlnF2fY8G1PLh9wQOgrIIHJu+PMq24C8jqndn5AqCEu31l4us+d6YJ7k0wNQZSgU21rC5YxXpVg9jRQV3OwJXLpcF33fJg3NT7s0QoehHhQpib7C5c6TTJJl434HH4EoY17SWTsFskuPDTQNNGpTE+Sd9h1lIyoe7zb12Io57aFT5PdBeIjiwJd9kWsrL1qCRjSSdgNQio/jAlQz+5nW99rfgRd8LAA7tGYtdLh3XmgNx/+IfZgXNczogH649NSfPdZKthCoiezDoQa15gI8DNgQXxhAq5mykvr17yVDN1d0OUtt6704OPn+trOcC5rtiW30k8G8GUv99UFqmv9tX/4LySVZu4jLo/2rn4vUV0ADNdd1NCiYHEq6iCDu777g2OlocZNvT7jX+O9xV0RlBAX5QvgZ8TBQO83TReakYBytassxQvxV0IgdTvW57VqyljlQK6bIwUcRldRNKhcrczWXo3tDOMdcjBmauubM6eJQ0LUIhlRxdAPvTHdg+TjAi729YUIZfbsMzWz/97SHlBJ6sWRqrywxk1lBYsrfJYCIpLq3Oh6dPDmx4I4GasUPyjAec/gdHqatqzFBdQ74k5LSDGhueSw7J4zIYhdW5ZjK1yIy6LIEG6LR40Fp4roIGe4SxGCccFVCzMlquygEAQmHLy8THrdISm5pVSn3Zjix9OWJWWum31cg1VrPpr+kQ7aDUhPJ5i7mvzVLPnAyNuTYrvVzazSFP2QZIcO1Uhoxfrv4SzzEqB9QzTTqYkZuCDtghvTBRGoCvyZnFdqX/KZj/nmjxzu0XThnSYEFzS2RWCkp0UEzxDysQOG0JzBe7ufwRrfGzggqkZpb0e3WxLfhJZnwGKh61toiA5F5/pXm+nsszdYjYH90h7peUs1tcZ5jTkdrPH3qNpdKcSu4MvTMrERT7vf4u8O0GBDoIz/+tT0xp7Gdhh3KjEnfl5UYe+fezENVQQRI8DXo5WxcJnxXY1hOW9TsCfVLOIq+twpRvH3yflruffXs3V/aQOKxXSLFuvEqOSwFZjctIE1vWoqt7uOa3t9OiUJjIo+owCqvNEhbtGu7aell2DIdFMRRB3XDAvw3wvhq2Y6TTidQp3vYjO6/2/Ma6A9eNVzBG5QzxDDHqPhZ06Rgk0P6QfdJaRanWBgHwuh6asZa2O4tTtmSLP4Fh4LQEWHwA2zs+1P7rGDvJY+bS6HT5vlRu07hF5w7oJpBdpq1i/74ou6x6DKyo8njkVBPla4GrRz7hzNvQ2IxFb1YKwoVVLrkjNJ4KtcopvZTH5/PF8/RWDz79ZVL4+NnA7PmU6XGf0zkl6lGvMtz3uxXP1wAjg8PmP7bau07PexpXRWxcil/oqJp/jvQy+j/2HwsWox+4k5GU+3hhv8j X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8adc209e-1ea1-44e4-c3d4-08da0692365e X-MS-Exchange-CrossTenant-AuthSource: DB9PR04MB8477.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Mar 2022 14:43:47.8205 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kyxyVO7ttJf2MoOJsPFNY45iVKO8Vb90dPQ3N/7I730l11r44CfMtQrTxPcqbTrxae1fr0bjINN2C2GU5n70KA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR04MB7281 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220315_075827_467287_89969620 X-CRM114-Status: GOOD ( 17.91 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 Cc: Marek Szyprowski Cc: Lecopzer Chen Cc: David Hildenbrand Cc: Vlastimil Babka CC: stable@vger.kernel.org # 5.11+ Fixes: a4efc174b382 ("mm/cma.c: remove redundant cma_mutex lock") Signed-off-by: Dong Aisheng --- 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);