From patchwork Wed Jan 29 22:41:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank van der Linden X-Patchwork-Id: 13954221 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 CBB65C02190 for ; Wed, 29 Jan 2025 22:43:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2687280271; Wed, 29 Jan 2025 17:42:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9ACD628026C; Wed, 29 Jan 2025 17:42:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7FF9F280271; Wed, 29 Jan 2025 17:42:57 -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 5D4C428026C for ; Wed, 29 Jan 2025 17:42:57 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1C8F61C545F for ; Wed, 29 Jan 2025 22:42:57 +0000 (UTC) X-FDA: 83061965994.14.69E5749 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf28.hostedemail.com (Postfix) with ESMTP id 539A1C0003 for ; Wed, 29 Jan 2025 22:42:55 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rAIsAgcQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 37q6aZwQKCPkgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37q6aZwQKCPkgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738190575; a=rsa-sha256; cv=none; b=xPwrl+aZaGZkhY6NFzj3pJ+4KpeCO6jC2uSzThePdXi4Ov3kqO/CnbNjmLXt+uhZDfk1uk JC8pvZlAU8/r+SU5QVO8fmP9GhHB0+FVVkObevKJFJfAOIwvWaG/QRU6sCWtclIERueKBV vj3WUaYHzeLgcgwVKlmk8K5qXvccY1s= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rAIsAgcQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 37q6aZwQKCPkgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37q6aZwQKCPkgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738190575; 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=1345eHEo/n5m5FCY0Prqgv+HivXDQh8NAfYom1ZqdDA=; b=5sXtrsQ2tlZC/J6Z6DsjVxJEbK+eUVzR6T+YF1JY809XyNiurnUeugx4Obnf5oQjmxBUmE UiQBrgvh8qu18UwghDSIFPL3dQqhIah6UGIVIZMTFa8b0atCZWL48ouXASGluGsgUdsDBd LfRe12S1W6LbWJFybZnm+IDkth4oavI= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2167141e00eso3113495ad.2 for ; Wed, 29 Jan 2025 14:42:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190574; x=1738795374; 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=1345eHEo/n5m5FCY0Prqgv+HivXDQh8NAfYom1ZqdDA=; b=rAIsAgcQpIGgvFiaTT3uDQ2Lg7yElABf1JwGfuaGC0uo/TyMC1+sAFBZJLKpC4/n7c OyXMpRArBPMjArDGskOqM3NCFh6pqplT6NmxQmoId97EOVU58g5xzDVqr9k4vX1iKLYT rhARRu02/KqVOslwR1pNpZUQlZP36HU5B6262GmzOMFIunnTFWMVg/MI0tKoKLZ7FyO+ o6ZS3qWuqYhIkob9WmqClU1+HVojWdhg1/yjzRf2kRJbYMr20diWCLCCI5jdH20RYN9p 037NiqdZUN/D5lEdwdlZfmxadxMs0iO3rxJcpA65REEl26evMx9BolfWscZpv0DnJiJe 128A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190574; x=1738795374; 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=1345eHEo/n5m5FCY0Prqgv+HivXDQh8NAfYom1ZqdDA=; b=j2lyFgChwPI+Uhs6PQgKZFRJ/Ax/mHVHMAOMNTUnxbwBHyiH20jGFdt8Ujh9C68q3D Q6OLx/JV3omRxkhdFV8ac4UAM/GC57B27wBI+casWbdtAP9h/utJw82/5NmzyzEWUAmT XczSHrktfgbwZlTYoYCOrh+/nv9HQttvyr+qZES42uvxy5iHa9WbJBzCkzWRgrATmIJe qyadNSzap9PbufWfxsZdwYBX0XSj17Z3muB3ZZXmY3OktGW+t73wY1Y3KFVRj/INFKLK SWOXm4ljlf1iKV/QKUpm57fd6CE81bT329uRnG94+GN4TkxNdPIFnZNK7evAHOC9ZLya O6EQ== X-Forwarded-Encrypted: i=1; AJvYcCXTaB8NuWAzYGcQP/Ekrh0aWxd4UhdNx0A11GT4suaMhgHEOyZC/EYkseUEGSF5SRTmQfleHF0zmA==@kvack.org X-Gm-Message-State: AOJu0Yy5IRcMskPqNeUUB+RdM7C8Prt0OH8rQ+Ned4n/CSFYIAlQns5s bSyTqkD+3xDiTlcmuahrW2f+WMkueXh4Z77vaUgRZh7IOaSLZ3tZec/Y1FP8lee/YE+H0Q== X-Google-Smtp-Source: AGHT+IFgKpf48L7TtwmSEw8t26tIVwiTmSEC5MTe4TDZk31dErMHXlLcuwwt7qrIclDuDAgLXqefYm/Q X-Received: from pfu1.prod.google.com ([2002:a05:6a00:a381:b0:725:f324:ad1c]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:1508:b0:1e0:dc7b:4ee9 with SMTP id adf61e73a8af0-1ed7a5b66c4mr8200655637.8.1738190574200; Wed, 29 Jan 2025 14:42:54 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:53 +0000 In-Reply-To: <20250129224157.2046079-1-fvdl@google.com> Mime-Version: 1.0 References: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-25-fvdl@google.com> Subject: [PATCH v2 24/28] mm/cma: introduce a cma validate function From: Frank van der Linden To: akpm@linux-foundation.org, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: yuzhao@google.com, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden X-Rspam-User: X-Rspamd-Queue-Id: 539A1C0003 X-Rspamd-Server: rspam10 X-Stat-Signature: 9fc9ht14dtt4ab8j5nbz9crogjo8ok3h X-HE-Tag: 1738190575-992066 X-HE-Meta: U2FsdGVkX1/fVnZVfuZJv43lTte56MPiW4FQRe1lm5R2tVKzxItgCv4O995vOhqmZ6POWS27GqOKyFS4hz3O7dszN8uR6MxWBr/wf710ahOap9iBl7toN+OMcZuvdJheUElXzCvAgSMmsuRpTDjDlyBjLgxZMu9scGHtFJuwsmQd9mIggvkMf9GX1qaHgdVv7oKiqJ8viDox4rr5SMfCwVadm+AMU8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m+6XPQmI2v2QIV6zDRZrj8qnlXOCO6YQO6jFZRIaITJTS1iDsl2E9n1I+hdTUKO0WHqn0Xmz/TxBDUHleZ2Hdcqb4CNSTHn4STFi1mHoyqUzmbx6+coqNPQ01UsDPO0KMD6pd7fZ+TNi0wpX8+afh4F+wLDaJEpT0bdHbCH2Sv0nbAejM09FQeMa7I0GvwTpgQD/ITDuBYNBBdO6+wfu8CE7Z+J4f6yVuQrjAZmw== 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: Define a function to check if a CMA area is valid, which means: do its ranges not cross any zone boundaries. Store the result in the newly created flags for each CMA area, so that multiple calls are dealt with. This allows for checking the validity of a CMA area early, which is needed later in order to be able to allocate hugetlb bootmem pages from it with pre-HVO. Signed-off-by: Frank van der Linden --- include/linux/cma.h | 5 ++++ mm/cma.c | 60 ++++++++++++++++++++++++++++++++++++--------- mm/cma.h | 8 +++++- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/include/linux/cma.h b/include/linux/cma.h index 03d85c100dcc..62d9c1cf6326 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -60,6 +60,7 @@ extern void cma_reserve_pages_on_error(struct cma *cma); #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); +bool cma_validate_zones(struct cma *cma); #else static inline struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) { @@ -70,6 +71,10 @@ static inline bool cma_free_folio(struct cma *cma, const struct folio *folio) { return false; } +static inline bool cma_validate_zones(struct cma *cma) +{ + return false; +} #endif #endif diff --git a/mm/cma.c b/mm/cma.c index 6ad631c9fdca..41248dee7197 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -99,6 +99,49 @@ static void cma_clear_bitmap(struct cma *cma, const struct cma_memrange *cmr, spin_unlock_irqrestore(&cma->lock, flags); } +/* + * Check if a CMA area contains no ranges that intersect with + * multiple zones. Store the result in the flags in case + * this gets called more than once. + */ +bool cma_validate_zones(struct cma *cma) +{ + int r; + unsigned long base_pfn; + struct cma_memrange *cmr; + bool valid_bit_set; + + /* + * If already validated, return result of previous check. + * Either the valid or invalid bit will be set if this + * check has already been done. If neither is set, the + * check has not been performed yet. + */ + valid_bit_set = test_bit(CMA_ZONES_VALID, &cma->flags); + if (valid_bit_set || test_bit(CMA_ZONES_INVALID, &cma->flags)) + return valid_bit_set; + + for (r = 0; r < cma->nranges; r++) { + cmr = &cma->ranges[r]; + base_pfn = cmr->base_pfn; + + /* + * alloc_contig_range() requires the pfn range specified + * to be in the same zone. Simplify by forcing the entire + * CMA resv range to be in the same zone. + */ + WARN_ON_ONCE(!pfn_valid(base_pfn)); + if (pfn_range_intersects_zones(cma->nid, base_pfn, cmr->count)) { + set_bit(CMA_ZONES_INVALID, &cma->flags); + return false; + } + } + + set_bit(CMA_ZONES_VALID, &cma->flags); + + return true; +} + static void __init cma_activate_area(struct cma *cma) { unsigned long pfn, base_pfn; @@ -113,19 +156,12 @@ static void __init cma_activate_area(struct cma *cma) goto cleanup; } + if (!cma_validate_zones(cma)) + goto cleanup; + for (r = 0; r < cma->nranges; r++) { cmr = &cma->ranges[r]; base_pfn = cmr->base_pfn; - - /* - * alloc_contig_range() requires the pfn range specified - * to be in the same zone. Simplify by forcing the entire - * CMA resv range to be in the same zone. - */ - WARN_ON_ONCE(!pfn_valid(base_pfn)); - if (pfn_range_intersects_zones(cma->nid, base_pfn, cmr->count)) - goto cleanup; - for (pfn = base_pfn; pfn < base_pfn + cmr->count; pfn += pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn)); @@ -145,7 +181,7 @@ static void __init cma_activate_area(struct cma *cma) bitmap_free(cma->ranges[r].bitmap); /* Expose all pages to the buddy, they are useless for CMA. */ - if (!cma->reserve_pages_on_error) { + if (!test_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags)) { for (r = 0; r < allocrange; r++) { cmr = &cma->ranges[r]; for (pfn = cmr->base_pfn; @@ -172,7 +208,7 @@ core_initcall(cma_init_reserved_areas); void __init cma_reserve_pages_on_error(struct cma *cma) { - cma->reserve_pages_on_error = true; + set_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags); } static int __init cma_new_area(const char *name, phys_addr_t size, diff --git a/mm/cma.h b/mm/cma.h index ff79dba5508c..bddc84b3cd96 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -49,11 +49,17 @@ struct cma { /* kobject requires dynamic object */ struct cma_kobject *cma_kobj; #endif - bool reserve_pages_on_error; + unsigned long flags; /* NUMA node (NUMA_NO_NODE if unspecified) */ int nid; }; +enum cma_flags { + CMA_RESERVE_PAGES_ON_ERROR, + CMA_ZONES_VALID, + CMA_ZONES_INVALID, +}; + extern struct cma cma_areas[MAX_CMA_AREAS]; extern unsigned int cma_area_count;