From patchwork Fri Jan 24 11:21:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13949297 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52B39C02181 for ; Fri, 24 Jan 2025 11:21:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D947C6B00AB; Fri, 24 Jan 2025 06:21:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D447D6B00AC; Fri, 24 Jan 2025 06:21:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3328280047; Fri, 24 Jan 2025 06:21:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A43426B00AB for ; Fri, 24 Jan 2025 06:21:43 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 25A25A1177 for ; Fri, 24 Jan 2025 11:21:43 +0000 (UTC) X-FDA: 83042105286.14.863F8EF Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.7]) by imf03.hostedemail.com (Postfix) with ESMTP id 4EB092000B for ; Fri, 24 Jan 2025 11:21:39 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=F6zBhtxw; spf=pass (imf03.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737717701; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=xb0y0bXCsoLutq0MQVRy9Edyx98Tyx9r7Nct237BMjM=; b=G4dj5UbAZ8+bzi6B0wWncmZoZFt3eeVyccVijarlpH/zIupM21FH74ImOQHsEXXKNdpUTa rOaEPAxck+bDMzX4vXfEm0TLGH9P2N/l47EKs6KwQDwkgZlp42MjiyHEIW1cUINDTzuhXM YLze3ZGhu3oSP9frSVd0SHEqHKfkI5o= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=F6zBhtxw; spf=pass (imf03.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737717701; a=rsa-sha256; cv=none; b=NwqRMLwaxVHizhn37bC3ftP3wF+MBxTocmLKO/a7qb4ZTT3dN5S+TS6ciG102u0uLfAUGE bksgR2KVHPkYu/aixQUQZ3ne1G6gOwIB+p1/yzc0ZCXC/7NIDWvz7m11wmbLAyN/PDs+Ai RrRIY5m7F6e9KmHKzQq+z2r1Sez50x8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=xb0y0bXCsoLutq0MQV Ry9Edyx98Tyx9r7Nct237BMjM=; b=F6zBhtxw8bO95VeEPzsodhhqOcd/3FZUFn zhyZp7O7WM18UjETKwt1LyBQESKRugT7QcsVsHXRqk216eFBpCpXZDoSudc4jkXH hbBzTecPCYg5MAlI78AzeXiVHNCXKCjrpQucKfLA7fqHH88OxaacC6drnOZP/VmV 6cP8pGDZc= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzsmtp2 (Coremail) with SMTP id PSkvCgD3b665d5NnpN2BAA--.10824S2; Fri, 24 Jan 2025 19:21:30 +0800 (CST) From: yangge1116@126.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, 21cnbao@gmail.com, david@redhat.com, baolin.wang@linux.alibaba.com, aisheng.dong@nxp.com, liuzixing@hygon.cn, yangge Subject: [PATCH] mm/cma: add an API to enable/disable concurrent memory allocation for the CMA Date: Fri, 24 Jan 2025 19:21:27 +0800 Message-Id: <1737717687-16744-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: PSkvCgD3b665d5NnpN2BAA--.10824S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAr17XF17Wryrur1UJFy5urg_yoW5WF48pF 4kWw1Yk34rWrn7Zrs7Aw409an8W3s7GF4UGFyS93s3ZFW3Jr12gwn8Kw15uFy5CrWkGF9a vF4Fq34Y9F1UZ3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRoKZXUUUUU= X-Originating-IP: [112.64.138.194] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOgXeG2eTWw-7zwAAsu X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4EB092000B X-Stat-Signature: 74wfr683wt7re1mrq8yh37ww35m6njiy X-Rspam-User: X-HE-Tag: 1737717699-280190 X-HE-Meta: U2FsdGVkX1/2x3pAZPauFLra4M4bBWONGHMvo1jnrrb+SHNhiDlINfhFfRvnn3uR0qKMm85AiAxGcmEX+QkESngM3cfJw6R1AW9RyS+Zr2n17n1h/TQWpqETRAXBHCKGnStIDDOLtZpr34DOamV1MbIglrHPeViU2RT62k2c4CcXKrJ+l+T1dKRVWWIhz/CBY64zbvq+Hz1TRkVUjnsyriHwc0HJ3UUbZ7SapNZ4RmOrBvr4XRoQrQB14PJ+pU54KGr1URKPgBit2Jfihc+2FVCXZKUBJGu0tVRpnettL81jQSFaT5xa8OfLFEwV1zMgaUkZ6gjl86RkGQF37T3YInN9ue6pt4qrc9Uhg4YOPHQtprcoyglYhoyVaVHN9xEAatYg+HQR9MdBu6JG7XKg4+x9HhRFAGfrNRFPzmy0Bru9RnEtqiTp3fBMyJcmEqF8prKoJS1+1ohzw+awSeDRTI7kgdQpp20CqC7ZK2Hlof1rlP+BGwqKSbu1SKhOHJc8Fi0f5xZ/xebBtNNoVxGVNJXPqODn2A5ulf7ACmHuFynT2sgAgTxdK4w78LoJGvbtnW9CFVGefT5ms2LRi4yJiW1NByKlXxq6YhL38ecGKtWHkFFAFLgjhCQQjm1C5e/0K9iqpyk+NW4WvkqToXvKfUtrqb61ffoywr1HF0w4GdLlXAlQE18MQClaLMstr07hOke07L0gGtzi0YXrLLYmtBtYrD1pRRUmhJG5USLYYQGP6qCEIKLbQl6jHKp9sq/DSRJ/0MQOweF2vYLJeGgxQRbBWvdoqMHv5loTmppA04ykCf6zvbgXM5oK2J7bBpl4908bSu/72b41vShb1x51WuvHRuDFqM1ctRbMzGSYJAtZ6OTQ1OyORsuJ9nBijB6U0vwqocqYO6/DGpN9WouLhZFWi7GmqMaxKY3aKgyVDix7gdQQMcbgVX2zg+4eSW5d1ThXwS1025gccXjoZJs VT3hfN3R Z3YWkoNUMR7Q47OXExBu7tgUBFHaXTysz//29jCfRNzTpQ7s7MDKUVY1TYQRkjlK3eOJ8Fi+JVrzgl8I0ZTZVA5pWIEiQW/VVA2nrXtOZjxNkr4R0YnZnVC6jw9iJTDUaCNigWE6Nw+m1grLcUvbrSJQ8Y9FPQyaXg6ZjO+KW/xhDiXY+tWdKPR+4Rd6Qje6kNKkSK4wjblT53dijnr5rDZz5rWIVNZKtcWr8/OVTL4qW3fMdtd/eFa1QO7dIAO0eqm+qg71qeUUoR4Dy11lTJoEPZpP7wBzT9fI1rdtRUXcjB0I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: yangge Commit 60a60e32cf91 ("Revert "mm/cma.c: remove redundant cma_mutex lock"") simply reverts to the original method of using the cma_mutex to ensure that alloc_contig_range() runs sequentially. This change was made to avoid concurrency allocation failures. However, it can negatively impact performance when concurrent allocation of CMA memory is required. To address this issue, we could introduce an API for concurrency settings, allowing users to decide whether their CMA can perform concurrent memory allocations or not. Fixes: 60a60e32cf91 ("Revert "mm/cma.c: remove redundant cma_mutex lock"") Signed-off-by: yangge Cc: --- include/linux/cma.h | 2 ++ mm/cma.c | 22 ++++++++++++++++++++-- mm/cma.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/linux/cma.h b/include/linux/cma.h index d15b64f..2384624 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -53,6 +53,8 @@ extern int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) extern void cma_reserve_pages_on_error(struct cma *cma); +extern bool cma_set_concurrency(struct cma *cma, bool concurrency); + #ifdef CONFIG_CMA struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp); bool cma_free_folio(struct cma *cma, const struct folio *folio); diff --git a/mm/cma.c b/mm/cma.c index de5bc0c..49a7186 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -460,9 +460,17 @@ static struct page *__cma_alloc(struct cma *cma, unsigned long count, spin_unlock_irq(&cma->lock); pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); - mutex_lock(&cma_mutex); + + /* + * If the user sets the concurr_alloc of CMA to true, concurrent + * memory allocation is allowed. If the user sets it to false or + * does not set it, concurrent memory allocation is not allowed. + */ + if (!cma->concurr_alloc) + mutex_lock(&cma_mutex); ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp); - mutex_unlock(&cma_mutex); + if (!cma->concurr_alloc) + mutex_unlock(&cma_mutex); if (ret == 0) { page = pfn_to_page(pfn); break; @@ -610,3 +618,13 @@ int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) return 0; } + +bool cma_set_concurrency(struct cma *cma, bool concurrency) +{ + if (!cma) + return false; + + cma->concurr_alloc = concurrency; + + return true; +} diff --git a/mm/cma.h b/mm/cma.h index 8485ef8..30f489d 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -16,6 +16,7 @@ struct cma { unsigned long *bitmap; unsigned int order_per_bit; /* Order of pages represented by one bit */ spinlock_t lock; + bool concurr_alloc; #ifdef CONFIG_CMA_DEBUGFS struct hlist_head mem_head; spinlock_t mem_head_lock;