From patchwork Sun Aug 11 21:21:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13759889 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 B19CFC3DA4A for ; Sun, 11 Aug 2024 21:21:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F4D66B0098; Sun, 11 Aug 2024 17:21:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A5B36B009A; Sun, 11 Aug 2024 17:21:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21F0F6B009E; Sun, 11 Aug 2024 17:21:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F2B5E6B0098 for ; Sun, 11 Aug 2024 17:21:40 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id ADA06A05BC for ; Sun, 11 Aug 2024 21:21:40 +0000 (UTC) X-FDA: 82441236360.09.E9311BB Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf21.hostedemail.com (Postfix) with ESMTP id DECC71C0010 for ; Sun, 11 Aug 2024 21:21:38 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="oL/R5YbU"; spf=pass (imf21.hostedemail.com: domain of 3YSu5ZgYKCNgSOTB4IAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yuzhao.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3YSu5ZgYKCNgSOTB4IAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723411288; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MiWur5WPUCFOabKPS7p8sIQsgKEHOpII1M/7n2SnGyU=; b=6OtADlrH4DBXA4ct7CEjhAdsUuOQBBWnorP0UFcZGfr1BpXkw4mVmG9XesmWlwb/q/m35o j/5OWnuICWe6bfr3uTrZ6dKbkXVBi0yqqFeuhvFsi8GzHeCqrpeO4SvPlujJxpN+XfBuq1 18U0sZrxxfA2wUmgO4fc3/5mnrvwZn4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="oL/R5YbU"; spf=pass (imf21.hostedemail.com: domain of 3YSu5ZgYKCNgSOTB4IAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yuzhao.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3YSu5ZgYKCNgSOTB4IAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723411288; a=rsa-sha256; cv=none; b=ABcUMia0KBMts+C+M+GhvYN7byNs6wCnYqWN3cC3sIvTNAOWlz4vOyw0mhYMLPqj6WrSd6 V5TgGLB4MeQwth0lMlHl8vUAMhPge3Hiu6osKAtPySSbg69D7pJAeDgjKtDwFrlatwufWR 2Dy3LEiVVX4PvbDbRlZtn3TdsRtFlWU= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6886cd07673so88556577b3.3 for ; Sun, 11 Aug 2024 14:21:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723411298; x=1724016098; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MiWur5WPUCFOabKPS7p8sIQsgKEHOpII1M/7n2SnGyU=; b=oL/R5YbUTk82yE+6Wp0uatGmzURJEh9Ec27HeI1r9tI4/1d1MEHUvKcLyPkfNAJsOa 3R3WFwF5EzLYWmPHtK/UO7q3qc3cOdK7mhn9GrMxr9y98AQBhYea406+hrrwxZYfp9lM fmEN9WtGUgpSuNaRDf+VG5PUEUTtvkqRLq+QX+YqqmcAqx56NGfEcQT7zXZFbwM1s3KV d45v+iq6q8m+heht1OiW2yrHSD5Qb1bdaDNFwQ8rlB58r9EwzwtfY1iw8V5Z5XLHxzD/ mxytDk5oFUw7fZiezifsPVnVid8U4P+rdmndrciEEQJTKyyFEEhYteoqOkjR0Yjp86Fc PVcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723411298; x=1724016098; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MiWur5WPUCFOabKPS7p8sIQsgKEHOpII1M/7n2SnGyU=; b=Zw4KGcle7XGPIo2oMbMCMA1YppTUGnKSo8hGb4KoeFAX0Tgubhuhf/+D48q+t9VlH0 ZpZJpt2XHPOAqpPMK5kIBAZGDIdlMbHX24T3Nh/nrnoTpjHygt7PAoHoas1TQZTyc4Vk cHWg1mBODI7iqzyUXWmiA3a5cqDkHmnxZihAqGPT/OE5ErYKkD619O31S8wIuYlU+IuH v2DXUnJppDyoDgyeDXz0e+OEl6wZdvSv/9EYgx0O2EROgeNS0fWhdGAxxO1LeQF/AK76 9MST6EC03NU4670nB7odUCwbMeX+AGQuy4OG2gBJ81C38UOXORguSY2lnNldkxkTiKiU 8+Ww== X-Forwarded-Encrypted: i=1; AJvYcCXWW+G6ASjvjGQ5YGgKE/XWA+eQ91kv4ShATk3y+zwFS+cvffTsNuD9Ytdhdt51WYV3d8obErV7KChqGCbWtmn0coY= X-Gm-Message-State: AOJu0Yw3qt+AEnJP5kzBcy7fSpb0W1u0c4eXHbxFj/U08uIBgjOaiJlM R58dL3QsRe94LzTMzIWenCQb0UTRNMRDof7zL1Wh+sZP89Dzknct3XbDByjqy6we2zglejVBeis Xiw== X-Google-Smtp-Source: AGHT+IEHCLQ7KUCLVKLYCjPKQE5Er4aNEmGlPQGmmKueq/o3fFhVzS87gYKaHPAinDZLXmdPberE0nnpI54= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:c9c:12b4:a1e3:7f10]) (user=yuzhao job=sendgmr) by 2002:a05:690c:2e13:b0:68e:edf4:7b80 with SMTP id 00721157ae682-69ec86beceamr2524307b3.5.1723411297967; Sun, 11 Aug 2024 14:21:37 -0700 (PDT) Date: Sun, 11 Aug 2024 15:21:28 -0600 In-Reply-To: <20240811212129.3074314-1-yuzhao@google.com> Mime-Version: 1.0 References: <20240811212129.3074314-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240811212129.3074314-3-yuzhao@google.com> Subject: [PATCH mm-unstable v1 2/3] mm/cma: add cma_alloc_folio() From: Yu Zhao To: Andrew Morton , Muchun Song Cc: "Matthew Wilcox (Oracle)" , Zi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao X-Rspam-User: X-Stat-Signature: u38ff6q5a3i7mbbtmnb5opytt3wai6de X-Rspamd-Queue-Id: DECC71C0010 X-Rspamd-Server: rspam11 X-HE-Tag: 1723411298-63873 X-HE-Meta: U2FsdGVkX19FMdv3bcBaqgXivEecQ22nhKarr6ZYSP3fj0SFNFPBnEtyCugqVSCDAu8pSaW9vMHQFtl3Qd9oAfPF4G0qXLWbo7eQ+BFSLro7+fyOK8bmlsnTkJcksICtBExyA8c4c67Bl3DwTmrul3gM/SPMRxUAmi/5ORZOCH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eEW3N7wYzePwATriScBprmIzkrCcx35Gpapb8RIMX2Gw1DdsMRVpHZMeCmT1+mFgk3qLCIC+wqAhbj7cmt3cN0V3VlXkMiFYcpPxMvI/oOZ2vndqBUA7Jp1z+KPcqX7aooqlsQW4AUs/QtbLpzH3aHVhGKNpIfZrAtj28Fb+pvQv5TI1dPeCp0dv6iFGVbZXntsNsaArG4zB9ty2wvvFPjTGCT6Pzg6SoCrm1n9HSGwwjP7RRqXBh3BIoknEe7/iCNEoU0TyHTdHOA4Grv2BW/qnZEZXcCMgIXRvK8kk/ijExJ5tHFgq0GN/F0QVgX 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: With alloc_contig_range() and free_contig_range() supporting large folios, CMA can allocate and free large folios too, by cma_alloc_folio() and cma_release(). Signed-off-by: Yu Zhao --- include/linux/cma.h | 1 + mm/cma.c | 47 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/include/linux/cma.h b/include/linux/cma.h index 9db877506ea8..086553fbda73 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -46,6 +46,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, struct cma **res_cma); extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align, bool no_warn); +extern struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp); extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count); extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count); diff --git a/mm/cma.c b/mm/cma.c index 95d6950e177b..46feb06db8e7 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -403,18 +403,8 @@ static void cma_debug_show_areas(struct cma *cma) spin_unlock_irq(&cma->lock); } -/** - * cma_alloc() - allocate pages from contiguous area - * @cma: Contiguous memory region for which the allocation is performed. - * @count: Requested number of pages. - * @align: Requested alignment of pages (in PAGE_SIZE order). - * @no_warn: Avoid printing message about failed allocation - * - * This function allocates part of contiguous memory on specific - * contiguous memory area. - */ -struct page *cma_alloc(struct cma *cma, unsigned long count, - unsigned int align, bool no_warn) +static struct page *__cma_alloc(struct cma *cma, unsigned long count, + unsigned int align, gfp_t gfp) { unsigned long mask, offset; unsigned long pfn = -1; @@ -463,8 +453,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); mutex_lock(&cma_mutex); - ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, - GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); + ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp); mutex_unlock(&cma_mutex); if (ret == 0) { page = pfn_to_page(pfn); @@ -494,7 +483,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, page_kasan_tag_reset(nth_page(page, i)); } - if (ret && !no_warn) { + if (ret && !(gfp & __GFP_NOWARN)) { pr_err_ratelimited("%s: %s: alloc failed, req-size: %lu pages, ret: %d\n", __func__, cma->name, count, ret); cma_debug_show_areas(cma); @@ -513,6 +502,34 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, return page; } +/** + * cma_alloc() - allocate pages from contiguous area + * @cma: Contiguous memory region for which the allocation is performed. + * @count: Requested number of pages. + * @align: Requested alignment of pages (in PAGE_SIZE order). + * @no_warn: Avoid printing message about failed allocation + * + * This function allocates part of contiguous memory on specific + * contiguous memory area. + */ +struct page *cma_alloc(struct cma *cma, unsigned long count, + unsigned int align, bool no_warn) +{ + return __cma_alloc(cma, count, align, GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); +} + +struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) +{ + struct page *page; + + if (WARN_ON(order && !(gfp | __GFP_COMP))) + return NULL; + + page = __cma_alloc(cma, 1 << order, order, gfp); + + return page ? page_folio(page) : NULL; +} + bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count) {