From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB0DC19F111 for ; Thu, 6 Feb 2025 18:51:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867881; cv=none; b=FJuBTpse4wvW9X+UtbleFu40DZt43O2odh1TQZ+2Pcejj7zusCGwJ37EJvYEuLKlvw1u5c4elWk871UMc1aeSIe6dcvcEZmenZaMY+Cvdff0CUacUA/TV4uaB79M8PyhsZ8toNqd07ZbS7gYBqy0KRYRLKXx/oLeSGP7Eu4Ex/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867881; c=relaxed/simple; bh=9hPlj4yoVVV9qBroz6ro/IRU562jbhtoFg2bawGSkqE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DHEw+5Xoi7ic4+FBOsvx4ZsCaLAlQtUnHmhgFZ68L+eelh8hpJuGvtAi7LhetsvHBv7M1pGO238zVkBepDmemTuDQiMv04XfqFkTuv3fkYckxt7MgNAQvAJNSTh1A21ZcjVAtUZ/p9hVaS6iY8JAGRkP8vIBdyl3L5bIuqYWLLs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FM3EDDtQ; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FM3EDDtQ" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fa1c093f12so1067931a91.2 for ; Thu, 06 Feb 2025 10:51:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867878; x=1739472678; darn=vger.kernel.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=NJPFpuaCa1L0JdfMq2mnIjylFDa72i51MvIbj3cBAAk=; b=FM3EDDtQtyyjLLy6s8Og9G4+WMur9/XFQvpYDFJxJlLwdl37yeCzYN6ZBdzYSfcamk geLAyQrwI2d0AaQNLgYGjKVYZNGMy4BkTSwMnYx0CORZp+YrCRfvux1ZD8f4vSCKP9Wy BLLL6DRaNqyZurwfya01OkQfQPRh6kmzCQs26FxyHzOUfQDewSTiWGFML6CdzAgp2Wq1 0zPtWJ2Y7bSOLHLgzjFC6Mx5YtN1dWels6dPQAYYMw1T/PqMUby1ooxy08whSp3KALZp n54LlyuoxI5lYaPE/pnsD/C5PElfAzb97KjL7al+pyWtqAHtKS4IEIDsSpaurBy7kaAP Qa5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867878; x=1739472678; 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=NJPFpuaCa1L0JdfMq2mnIjylFDa72i51MvIbj3cBAAk=; b=VGGAGNnKMf5SPPpmsa4fWrM6Jo2c8HXxZQWI/4Q2mRi1YvyPXZdj89srQdPzBqxx4f 23fKs5PWIJPga9m9IZBlfR4am44GDofbOwQkvXqfs9LxWM27Sa0lLTskOXMuLb0QXESV GQ8TX/umPAkTSBgaMT/8O/+bJ0p6zkIG3K8W2bzDHMIqbeg0UGQ4cvBMYKyVybNUC3nl OSycgkhCK1AAQFKTROgHoQVFMcrk2wwzBuQpckgWghuaKwf8lN4ZXPiCC87QXZyDONOd IraobJn4tCGEElisKNyiinzKS/Bg1GwPsNNQf1HHDb9K81JmYA4TmiCn7iB4f/MEDQEu znZg== X-Forwarded-Encrypted: i=1; AJvYcCUUxLZwpEmyClngZkmFnOlu6d1l9gpQ1SuUMpfExRwR7NBmkxcfei9wwqIRGgCVHN/z9qvGBt2zfTJgRbM=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5ng22rRmW7WdtI9U6RX3ZEISCONjuXbKl+wWN8PwtZijNK+dY 14hdD3gnfMSs0D3NW9eDzh/wb11bncMAV+WxteJriyDPensgoYt7W+GCEAosZXUz7ZVozA== X-Google-Smtp-Source: AGHT+IE32zObckxQH9j+bb16bIAOnvT1L5KvON9aMD8I/isc7mdoYBPbnHRBSRCen9t4NlEX4I7KXcS/ X-Received: from pjbpm18.prod.google.com ([2002:a17:90b:3c52:b0:2ea:448a:8cd1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d03:b0:2ea:bf1c:1e3a with SMTP id 98e67ed59e1d1-2fa24069ec6mr240439a91.12.1738867878036; Thu, 06 Feb 2025 10:51:18 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:41 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-2-fvdl@google.com> Subject: [PATCH v3 01/28] mm/cma: export total and free number of pages for CMA areas 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In addition to the number of allocations and releases, system management software may like to be aware of the size of CMA areas, and how many pages are available in it. This information is currently not available, so export it in total_page and available_pages, respectively. The name 'available_pages' was picked over 'free_pages' because 'free' implies that the pages are unused. But they might not be, they just haven't been used by cma_alloc The number of available pages is tracked regardless of CONFIG_CMA_SYSFS, allowing for a few minor shortcuts in the code, avoiding bitmap operations. Signed-off-by: Frank van der Linden Reviewed-by: Oscar Salvador --- Documentation/ABI/testing/sysfs-kernel-mm-cma | 13 +++++++++++ mm/cma.c | 22 ++++++++++++++----- mm/cma.h | 1 + mm/cma_debug.c | 5 +---- mm/cma_sysfs.c | 20 +++++++++++++++++ 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-cma b/Documentation/= ABI/testing/sysfs-kernel-mm-cma index dfd755201142..aaf2a5d8b13b 100644 --- a/Documentation/ABI/testing/sysfs-kernel-mm-cma +++ b/Documentation/ABI/testing/sysfs-kernel-mm-cma @@ -29,3 +29,16 @@ Date: Feb 2024 Contact: Anshuman Khandual Description: the number of pages CMA API succeeded to release + +What: /sys/kernel/mm/cma//total_pages +Date: Jun 2024 +Contact: Frank van der Linden +Description: + The size of the CMA area in pages. + +What: /sys/kernel/mm/cma//available_pages +Date: Jun 2024 +Contact: Frank van der Linden +Description: + The number of pages in the CMA area that are still + available for CMA allocation. diff --git a/mm/cma.c b/mm/cma.c index de5bc0c81fc2..95a8788e54d3 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -86,6 +86,7 @@ static void cma_clear_bitmap(struct cma *cma, unsigned lo= ng pfn, =20 spin_lock_irqsave(&cma->lock, flags); bitmap_clear(cma->bitmap, bitmap_no, bitmap_count); + cma->available_count +=3D count; spin_unlock_irqrestore(&cma->lock, flags); } =20 @@ -133,7 +134,7 @@ static void __init cma_activate_area(struct cma *cma) free_reserved_page(pfn_to_page(pfn)); } totalcma_pages -=3D cma->count; - cma->count =3D 0; + cma->available_count =3D cma->count =3D 0; pr_err("CMA area %s could not be activated\n", cma->name); } =20 @@ -206,7 +207,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys= _addr_t size, snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count); =20 cma->base_pfn =3D PFN_DOWN(base); - cma->count =3D size >> PAGE_SHIFT; + cma->available_count =3D cma->count =3D size >> PAGE_SHIFT; cma->order_per_bit =3D order_per_bit; *res_cma =3D cma; cma_area_count++; @@ -390,7 +391,7 @@ static void cma_debug_show_areas(struct cma *cma) { unsigned long next_zero_bit, next_set_bit, nr_zero; unsigned long start =3D 0; - unsigned long nr_part, nr_total =3D 0; + unsigned long nr_part; unsigned long nbits =3D cma_bitmap_maxno(cma); =20 spin_lock_irq(&cma->lock); @@ -402,12 +403,12 @@ static void cma_debug_show_areas(struct cma *cma) next_set_bit =3D find_next_bit(cma->bitmap, nbits, next_zero_bit); nr_zero =3D next_set_bit - next_zero_bit; nr_part =3D nr_zero << cma->order_per_bit; - pr_cont("%s%lu@%lu", nr_total ? "+" : "", nr_part, + pr_cont("%s%lu@%lu", start ? "+" : "", nr_part, next_zero_bit); - nr_total +=3D nr_part; start =3D next_zero_bit + nr_zero; } - pr_cont("=3D> %lu free of %lu total pages\n", nr_total, cma->count); + pr_cont("=3D> %lu free of %lu total pages\n", cma->available_count, + cma->count); spin_unlock_irq(&cma->lock); } =20 @@ -444,6 +445,14 @@ static struct page *__cma_alloc(struct cma *cma, unsig= ned long count, =20 for (;;) { spin_lock_irq(&cma->lock); + /* + * If the request is larger than the available number + * of pages, stop right away. + */ + if (count > cma->available_count) { + spin_unlock_irq(&cma->lock); + break; + } bitmap_no =3D bitmap_find_next_zero_area_off(cma->bitmap, bitmap_maxno, start, bitmap_count, mask, offset); @@ -452,6 +461,7 @@ static struct page *__cma_alloc(struct cma *cma, unsign= ed long count, break; } bitmap_set(cma->bitmap, bitmap_no, bitmap_count); + cma->available_count -=3D count; /* * It's safe to drop the lock here. We've marked this region for * our exclusive use. If the migration fails we will take the diff --git a/mm/cma.h b/mm/cma.h index 8485ef893e99..3dd3376ae980 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -13,6 +13,7 @@ struct cma_kobject { struct cma { unsigned long base_pfn; unsigned long count; + unsigned long available_count; unsigned long *bitmap; unsigned int order_per_bit; /* Order of pages represented by one bit */ spinlock_t lock; diff --git a/mm/cma_debug.c b/mm/cma_debug.c index 602fff89b15f..89236f22230a 100644 --- a/mm/cma_debug.c +++ b/mm/cma_debug.c @@ -34,13 +34,10 @@ DEFINE_DEBUGFS_ATTRIBUTE(cma_debugfs_fops, cma_debugfs_= get, NULL, "%llu\n"); static int cma_used_get(void *data, u64 *val) { struct cma *cma =3D data; - unsigned long used; =20 spin_lock_irq(&cma->lock); - /* pages counter is smaller than sizeof(int) */ - used =3D bitmap_weight(cma->bitmap, (int)cma_bitmap_maxno(cma)); + *val =3D cma->count - cma->available_count; spin_unlock_irq(&cma->lock); - *val =3D (u64)used << cma->order_per_bit; =20 return 0; } diff --git a/mm/cma_sysfs.c b/mm/cma_sysfs.c index f50db3973171..97acd3e5a6a5 100644 --- a/mm/cma_sysfs.c +++ b/mm/cma_sysfs.c @@ -62,6 +62,24 @@ static ssize_t release_pages_success_show(struct kobject= *kobj, } CMA_ATTR_RO(release_pages_success); =20 +static ssize_t total_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct cma *cma =3D cma_from_kobj(kobj); + + return sysfs_emit(buf, "%lu\n", cma->count); +} +CMA_ATTR_RO(total_pages); + +static ssize_t available_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct cma *cma =3D cma_from_kobj(kobj); + + return sysfs_emit(buf, "%lu\n", cma->available_count); +} +CMA_ATTR_RO(available_pages); + static void cma_kobj_release(struct kobject *kobj) { struct cma *cma =3D cma_from_kobj(kobj); @@ -75,6 +93,8 @@ static struct attribute *cma_attrs[] =3D { &alloc_pages_success_attr.attr, &alloc_pages_fail_attr.attr, &release_pages_success_attr.attr, + &total_pages_attr.attr, + &available_pages_attr.attr, NULL, }; ATTRIBUTE_GROUPS(cma); --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D41219F13B for ; Thu, 6 Feb 2025 18:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867882; cv=none; b=QMK6quByvmFdlVXTtSRJKHr2R6VB8zBEPuMZaxPzN7rgLU3+Cqn/3zFSaSdrI6NSze9Tz75fBgWjV/NlXTVesgkXhA4gHpfOGSSd84tTQhRDLeISF9oduKlL7FK5UQePiluhUhfA4k75gk67esqsTfqL8Xvq7dvQAFJSaIYaJEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867882; c=relaxed/simple; bh=AdwTMvRN0vo9gGA+ZtytBOWdriijHWhA+lwp5WApfgo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X/4a09O9psWywD33ooHSEoD042x+0d7GxcfjNt9d2fQ32scB/PBSEYZGrclChaZrjAD3rS/buZE5E/AxLUN7D4RG3FfWhSL9i6KbB4QOZ3TzTj4cMch1s/lFnwo27hxwDMTb/uu62pTInDG1DEcNU2K0pvtcgkPCm9QQVqDNjDY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3B6Dkzxg; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3B6Dkzxg" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9c02f54f2so2485994a91.3 for ; Thu, 06 Feb 2025 10:51:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867879; x=1739472679; darn=vger.kernel.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=b6zbvBC7sDTC9/vWH7N0h9lqjmBU1iu0Iy1mp9Qfvhc=; b=3B6DkzxgsqK4svee4f/T2SXZyHEPLDfkV9i1JJsHz5PemwzqYC0fQr8XRf4ocaCPsU xYQ9lIGKX2ssASXVAnuGi/E1TLQ2xQAdJeU18kTaychK0oPWQcG0GeAEBGIZ653KBp4V zcrW8CrYG0pG4jGttNHD1Dn83pSo3PY/mVZuwwvvVW/Z7T3qErxHNg5eHNoArs0/vDlf TGxTSala4O0KlOszpwXPCO9lov8A7QDWkfb79c7+sFHmAgJcFtMwHJ86iFVf0nB3eVwn Zjb+kDzdKlb1q7cP1U2y5OzFQ5dSyUEqIZF839i1bK7za1tdrFk317RqhF+Lv9cB9fyn jhMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867879; x=1739472679; 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=b6zbvBC7sDTC9/vWH7N0h9lqjmBU1iu0Iy1mp9Qfvhc=; b=p0zpcZ/5zYRZLghUP7HfkWz0ZIHqWhCThDLHWIp58cMdAysK13s8kGq08xyYalm2bQ N1PCWiBZWy0ZP/KI7MeJWQrtppmhvIE5FwlBDiQn/VQqF4Drfc1s3z6oOu0DGSc4mOoO GgN8gt8k3tpvQj/orFRjPmWIAPGVaKIqqXMZNZQlfgt3pZLYIQsdv2q3Y8fdKyDEiQ9u Oc0Upd3XDDV7/VJPxGKZeaIuEv6MpKAEtNDTpxjGTL50x0d05Dk9fEHAcBI+2TQHesyn wbm+/ell5lOmsW5/yzWWjYOCZH4nx+BiOTDn/lPTM8XT1LfoZ/KU1cCF4ktcDdzjhurF NhWQ== X-Forwarded-Encrypted: i=1; AJvYcCUgUlsUVdga9qSc+1cLQtfwRmUkm1LpwRWKw1KXqXUkiqyi5Mn4CTnUUvZ+DtugeMC/2kI5krV5ZgMJAe8=@vger.kernel.org X-Gm-Message-State: AOJu0Yx0bO1UwijXFifU+ZU3db/YiAFM+GXBC3Yfo3LBZgMSIol+upsc 1utA0EUfOU8x68OCGtlfTIcLKWZzar7N8yIiVFqplD23gw3fQCHBy1lAGsWJiY1+Low8yQ== X-Google-Smtp-Source: AGHT+IEKvPQ7lm0Rn5b7uX4UIGLM9ngAdhktQMTfPtHK7wAo3+YelYon7sVG6argp0J2+d6Xf0MHG2W9 X-Received: from pjbsi17.prod.google.com ([2002:a17:90b:5291:b0:2f9:d9fe:e723]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:19c4:b0:2fa:1e3e:9be5 with SMTP id 98e67ed59e1d1-2fa22b8bbb5mr346931a91.0.1738867879459; Thu, 06 Feb 2025 10:51:19 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:42 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-3-fvdl@google.com> Subject: [PATCH v3 02/28] mm, cma: support multiple contiguous ranges, if requested 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, CMA manages one range of physically contiguous memory. Creation of larger CMA areas with hugetlb_cma may run in to gaps in physical memory, so that they are not able to allocate that contiguous physical range from memblock when creating the CMA area. This can happen, for example, on an AMD system with > 1TB of memory, where there will be a gap just below the 1TB (40bit DMA) line. If you have set aside most of memory for potential hugetlb CMA allocation, cma_declare_contiguous_nid will fail. hugetlb_cma doesn't need the entire area to be one physically contiguous range. It just cares about being able to get physically contiguous chunks of a certain size (e.g. 1G), and it is fine to have the CMA area backed by multiple physical ranges, as long as it gets 1G contiguous allocations. Multi-range support is implemented by introducing an array of ranges, instead of just one big one. Each range has its own bitmap. Effectively, the allocate and release operations work as before, just per-range. So, instead of going through one large bitmap, they now go through a number of smaller ones. The maximum number of supported ranges is 8, as defined in CMA_MAX_RANGES. Since some current users of CMA expect a CMA area to just use one physically contiguous range, only allow for multiple ranges if a new interface, cma_declare_contiguous_nid_multi, is used. The other interfaces will work like before, creating only CMA areas with 1 range. cma_declare_contiguous_nid_multi works as follows, mimicking the default "bottom-up, above 4G" reservation approach: 0) Try cma_declare_contiguous_nid, which will use only one region. If this succeeds, return. This makes sure that for all the cases that currently work, the behavior remains unchanged even if the caller switches from cma_declare_contiguous_nid to cma_declare_contiguous_nid_multi. 1) Select the largest free memblock ranges above 4G, with a maximum number of CMA_MAX_RANGES. 2) If we did not find at most CMA_MAX_RANGES that add up to the total size requested, return -ENOMEM. 3) Sort the selected ranges by base address. 4) Reserve them bottom-up until we get what we wanted. Signed-off-by: Frank van der Linden --- Documentation/admin-guide/mm/cma_debugfs.rst | 10 +- include/linux/cma.h | 3 + mm/cma.c | 594 +++++++++++++++---- mm/cma.h | 27 +- mm/cma_debug.c | 56 +- 5 files changed, 550 insertions(+), 140 deletions(-) diff --git a/Documentation/admin-guide/mm/cma_debugfs.rst b/Documentation/a= dmin-guide/mm/cma_debugfs.rst index 7367e6294ef6..4120e9cb0cd5 100644 --- a/Documentation/admin-guide/mm/cma_debugfs.rst +++ b/Documentation/admin-guide/mm/cma_debugfs.rst @@ -12,10 +12,16 @@ its CMA name like below: =20 The structure of the files created under that directory is as follows: =20 - - [RO] base_pfn: The base PFN (Page Frame Number) of the zone. + - [RO] base_pfn: The base PFN (Page Frame Number) of the CMA area. + This is the same as ranges/0/base_pfn. - [RO] count: Amount of memory in the CMA area. - [RO] order_per_bit: Order of pages represented by one bit. - - [RO] bitmap: The bitmap of page states in the zone. + - [RO] bitmap: The bitmap of allocated pages in the area. + This is the same as ranges/0/base_pfn. + - [RO] ranges/N/base_pfn: The base PFN of contiguous range N + in the CMA area. + - [RO] ranges/N/bitmap: The bit map of allocated pages in + range N in the CMA area. - [WO] alloc: Allocate N pages from that CMA area. For example:: =20 echo 5 > /cma//alloc diff --git a/include/linux/cma.h b/include/linux/cma.h index d15b64f51336..863427c27dc2 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -40,6 +40,9 @@ static inline int __init cma_declare_contiguous(phys_addr= _t base, return cma_declare_contiguous_nid(base, size, limit, alignment, order_per_bit, fixed, name, res_cma, NUMA_NO_NODE); } +extern int __init cma_declare_contiguous_multi(phys_addr_t size, + phys_addr_t align, unsigned int order_per_bit, + const char *name, struct cma **res_cma, int nid); extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, unsigned int order_per_bit, const char *name, diff --git a/mm/cma.c b/mm/cma.c index 95a8788e54d3..6ac9173d3a7d 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -18,6 +18,7 @@ =20 #include #include +#include #include #include #include @@ -35,9 +36,16 @@ struct cma cma_areas[MAX_CMA_AREAS]; unsigned int cma_area_count; static DEFINE_MUTEX(cma_mutex); =20 +static int __init __cma_declare_contiguous_nid(phys_addr_t base, + phys_addr_t size, phys_addr_t limit, + phys_addr_t alignment, unsigned int order_per_bit, + bool fixed, const char *name, struct cma **res_cma, + int nid); + phys_addr_t cma_get_base(const struct cma *cma) { - return PFN_PHYS(cma->base_pfn); + WARN_ON_ONCE(cma->nranges !=3D 1); + return PFN_PHYS(cma->ranges[0].base_pfn); } =20 unsigned long cma_get_size(const struct cma *cma) @@ -63,9 +71,10 @@ static unsigned long cma_bitmap_aligned_mask(const struc= t cma *cma, * The value returned is represented in order_per_bits. */ static unsigned long cma_bitmap_aligned_offset(const struct cma *cma, + const struct cma_memrange *cmr, unsigned int align_order) { - return (cma->base_pfn & ((1UL << align_order) - 1)) + return (cmr->base_pfn & ((1UL << align_order) - 1)) >> cma->order_per_bit; } =20 @@ -75,46 +84,57 @@ static unsigned long cma_bitmap_pages_to_bits(const str= uct cma *cma, return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit; } =20 -static void cma_clear_bitmap(struct cma *cma, unsigned long pfn, - unsigned long count) +static void cma_clear_bitmap(struct cma *cma, const struct cma_memrange *c= mr, + unsigned long pfn, unsigned long count) { unsigned long bitmap_no, bitmap_count; unsigned long flags; =20 - bitmap_no =3D (pfn - cma->base_pfn) >> cma->order_per_bit; + bitmap_no =3D (pfn - cmr->base_pfn) >> cma->order_per_bit; bitmap_count =3D cma_bitmap_pages_to_bits(cma, count); =20 spin_lock_irqsave(&cma->lock, flags); - bitmap_clear(cma->bitmap, bitmap_no, bitmap_count); + bitmap_clear(cmr->bitmap, bitmap_no, bitmap_count); cma->available_count +=3D count; spin_unlock_irqrestore(&cma->lock, flags); } =20 static void __init cma_activate_area(struct cma *cma) { - unsigned long base_pfn =3D cma->base_pfn, pfn; + unsigned long pfn, base_pfn; + int allocrange, r; struct zone *zone; + struct cma_memrange *cmr; + + for (allocrange =3D 0; allocrange < cma->nranges; allocrange++) { + cmr =3D &cma->ranges[allocrange]; + cmr->bitmap =3D bitmap_zalloc(cma_bitmap_maxno(cma, cmr), + GFP_KERNEL); + if (!cmr->bitmap) + goto cleanup; + } =20 - cma->bitmap =3D bitmap_zalloc(cma_bitmap_maxno(cma), GFP_KERNEL); - if (!cma->bitmap) - goto out_error; + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + base_pfn =3D cmr->base_pfn; =20 - /* - * 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)); - zone =3D page_zone(pfn_to_page(base_pfn)); - for (pfn =3D base_pfn + 1; pfn < base_pfn + cma->count; pfn++) { - WARN_ON_ONCE(!pfn_valid(pfn)); - if (page_zone(pfn_to_page(pfn)) !=3D zone) - goto not_in_zone; - } + /* + * 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)); + zone =3D page_zone(pfn_to_page(base_pfn)); + for (pfn =3D base_pfn + 1; pfn < base_pfn + cmr->count; pfn++) { + WARN_ON_ONCE(!pfn_valid(pfn)); + if (page_zone(pfn_to_page(pfn)) !=3D zone) + goto cleanup; + } =20 - for (pfn =3D base_pfn; pfn < base_pfn + cma->count; - pfn +=3D pageblock_nr_pages) - init_cma_reserved_pageblock(pfn_to_page(pfn)); + for (pfn =3D base_pfn; pfn < base_pfn + cmr->count; + pfn +=3D pageblock_nr_pages) + init_cma_reserved_pageblock(pfn_to_page(pfn)); + } =20 spin_lock_init(&cma->lock); =20 @@ -125,13 +145,19 @@ static void __init cma_activate_area(struct cma *cma) =20 return; =20 -not_in_zone: - bitmap_free(cma->bitmap); -out_error: +cleanup: + for (r =3D 0; r < allocrange; r++) + bitmap_free(cma->ranges[r].bitmap); + /* Expose all pages to the buddy, they are useless for CMA. */ if (!cma->reserve_pages_on_error) { - for (pfn =3D base_pfn; pfn < base_pfn + cma->count; pfn++) - free_reserved_page(pfn_to_page(pfn)); + for (r =3D 0; r < allocrange; r++) { + cmr =3D &cma->ranges[r]; + for (pfn =3D cmr->base_pfn; + pfn < cmr->base_pfn + cmr->count; + pfn++) + free_reserved_page(pfn_to_page(pfn)); + } } totalcma_pages -=3D cma->count; cma->available_count =3D cma->count =3D 0; @@ -154,6 +180,43 @@ void __init cma_reserve_pages_on_error(struct cma *cma) cma->reserve_pages_on_error =3D true; } =20 +static int __init cma_new_area(const char *name, phys_addr_t size, + unsigned int order_per_bit, + struct cma **res_cma) +{ + struct cma *cma; + + if (cma_area_count =3D=3D ARRAY_SIZE(cma_areas)) { + pr_err("Not enough slots for CMA reserved regions!\n"); + return -ENOSPC; + } + + /* + * Each reserved area must be initialised later, when more kernel + * subsystems (like slab allocator) are available. + */ + cma =3D &cma_areas[cma_area_count]; + cma_area_count++; + + if (name) + snprintf(cma->name, CMA_MAX_NAME, name); + else + snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count); + + cma->available_count =3D cma->count =3D size >> PAGE_SHIFT; + cma->order_per_bit =3D order_per_bit; + *res_cma =3D cma; + totalcma_pages +=3D cma->count; + + return 0; +} + +static void __init cma_drop_area(struct cma *cma) +{ + totalcma_pages -=3D cma->count; + cma_area_count--; +} + /** * cma_init_reserved_mem() - create custom contiguous area from reserved m= emory * @base: Base address of the reserved area @@ -172,13 +235,9 @@ int __init cma_init_reserved_mem(phys_addr_t base, phy= s_addr_t size, struct cma **res_cma) { struct cma *cma; + int ret; =20 /* Sanity checks */ - if (cma_area_count =3D=3D ARRAY_SIZE(cma_areas)) { - pr_err("Not enough slots for CMA reserved regions!\n"); - return -ENOSPC; - } - if (!size || !memblock_is_region_reserved(base, size)) return -EINVAL; =20 @@ -195,25 +254,261 @@ int __init cma_init_reserved_mem(phys_addr_t base, p= hys_addr_t size, if (!IS_ALIGNED(base | size, CMA_MIN_ALIGNMENT_BYTES)) return -EINVAL; =20 + ret =3D cma_new_area(name, size, order_per_bit, &cma); + if (ret !=3D 0) + return ret; + + cma->ranges[0].base_pfn =3D PFN_DOWN(base); + cma->ranges[0].count =3D cma->count; + cma->nranges =3D 1; + + *res_cma =3D cma; + + return 0; +} + +/* + * Structure used while walking physical memory ranges and finding out + * which one(s) to use for a CMA area. + */ +struct cma_init_memrange { + phys_addr_t base; + phys_addr_t size; + struct list_head list; +}; + +/* + * Work array used during CMA initialization. + */ +static struct cma_init_memrange memranges[CMA_MAX_RANGES] __initdata; + +static bool __init revsizecmp(struct cma_init_memrange *mlp, + struct cma_init_memrange *mrp) +{ + return mlp->size > mrp->size; +} + +static bool __init basecmp(struct cma_init_memrange *mlp, + struct cma_init_memrange *mrp) +{ + return mlp->base < mrp->base; +} + +/* + * Helper function to create sorted lists. + */ +static void __init list_insert_sorted( + struct list_head *ranges, + struct cma_init_memrange *mrp, + bool (*cmp)(struct cma_init_memrange *lh, struct cma_init_memrange *rh)) +{ + struct list_head *mp; + struct cma_init_memrange *mlp; + + if (list_empty(ranges)) + list_add(&mrp->list, ranges); + else { + list_for_each(mp, ranges) { + mlp =3D list_entry(mp, struct cma_init_memrange, list); + if (cmp(mlp, mrp)) + break; + } + __list_add(&mrp->list, mlp->list.prev, &mlp->list); + } +} + +/* + * Create CMA areas with a total size of @total_size. A normal allocation + * for one area is tried first. If that fails, the biggest memblock + * ranges above 4G are selected, and allocated bottom up. + * + * The complexity here is not great, but this function will only be + * called during boot, and the lists operated on have fewer than + * CMA_MAX_RANGES elements (default value: 8). + */ +int __init cma_declare_contiguous_multi(phys_addr_t total_size, + phys_addr_t align, unsigned int order_per_bit, + const char *name, struct cma **res_cma, int nid) +{ + phys_addr_t start, end; + phys_addr_t size, sizesum, sizeleft; + struct cma_init_memrange *mrp, *mlp, *failed; + struct cma_memrange *cmrp; + LIST_HEAD(ranges); + LIST_HEAD(final_ranges); + struct list_head *mp, *next; + int ret, nr =3D 1; + u64 i; + struct cma *cma; + /* - * Each reserved area must be initialised later, when more kernel - * subsystems (like slab allocator) are available. + * First, try it the normal way, producing just one range. */ - cma =3D &cma_areas[cma_area_count]; + ret =3D __cma_declare_contiguous_nid(0, total_size, 0, align, + order_per_bit, false, name, res_cma, nid); + if (ret !=3D -ENOMEM) + goto out; =20 - if (name) - snprintf(cma->name, CMA_MAX_NAME, name); - else - snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count); + /* + * Couldn't find one range that fits our needs, so try multiple + * ranges. + * + * No need to do the alignment checks here, the call to + * cma_declare_contiguous_nid above would have caught + * any issues. With the checks, we know that: + * + * - @align is a power of 2 + * - @align is >=3D pageblock alignment + * - @size is aligned to @align and to @order_per_bit + * + * So, as long as we create ranges that have a base + * aligned to @align, and a size that is aligned to + * both @align and @order_to_bit, things will work out. + */ + nr =3D 0; + sizesum =3D 0; + failed =3D NULL; =20 - cma->base_pfn =3D PFN_DOWN(base); - cma->available_count =3D cma->count =3D size >> PAGE_SHIFT; - cma->order_per_bit =3D order_per_bit; + ret =3D cma_new_area(name, total_size, order_per_bit, &cma); + if (ret !=3D 0) + goto out; + + align =3D max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES); + /* + * Create a list of ranges above 4G, largest range first. + */ + for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) { + if (start < SZ_4G) + continue; + + start =3D ALIGN(start, align); + if (start >=3D end) + continue; + + end =3D ALIGN_DOWN(end, align); + if (end <=3D start) + continue; + + size =3D end - start; + size =3D ALIGN_DOWN(size, (PAGE_SIZE << order_per_bit)); + if (!size) + continue; + sizesum +=3D size; + + pr_debug("consider %016llx - %016llx\n", (u64)start, (u64)end); + + /* + * If we don't yet have used the maximum number of + * areas, grab a new one. + * + * If we can't use anymore, see if this range is not + * smaller than the smallest one already recorded. If + * not, re-use the smallest element. + */ + if (nr < CMA_MAX_RANGES) + mrp =3D &memranges[nr++]; + else { + mrp =3D list_last_entry(&ranges, + struct cma_init_memrange, list); + if (size < mrp->size) + continue; + list_del(&mrp->list); + sizesum -=3D mrp->size; + pr_debug("deleted %016llx - %016llx from the list\n", + (u64)mrp->base, (u64)mrp->base + size); + } + mrp->base =3D start; + mrp->size =3D size; + + /* + * Now do a sorted insert. + */ + list_insert_sorted(&ranges, mrp, revsizecmp); + pr_debug("added %016llx - %016llx to the list\n", + (u64)mrp->base, (u64)mrp->base + size); + pr_debug("total size now %llu\n", (u64)sizesum); + } + + /* + * There is not enough room in the CMA_MAX_RANGES largest + * ranges, so bail out. + */ + if (sizesum < total_size) { + cma_drop_area(cma); + ret =3D -ENOMEM; + goto out; + } + + /* + * Found ranges that provide enough combined space. + * Now, sorted them by address, smallest first, because we + * want to mimic a bottom-up memblock allocation. + */ + sizesum =3D 0; + list_for_each_safe(mp, next, &ranges) { + mlp =3D list_entry(mp, struct cma_init_memrange, list); + list_del(mp); + list_insert_sorted(&final_ranges, mlp, basecmp); + sizesum +=3D mlp->size; + if (sizesum >=3D total_size) + break; + } + + /* + * Walk the final list, and add a CMA range for + * each range, possibly not using the last one fully. + */ + nr =3D 0; + sizeleft =3D total_size; + list_for_each(mp, &final_ranges) { + mlp =3D list_entry(mp, struct cma_init_memrange, list); + size =3D min(sizeleft, mlp->size); + if (memblock_reserve(mlp->base, size)) { + /* + * Unexpected error. Could go on to + * the next one, but just abort to + * be safe. + */ + failed =3D mlp; + break; + } + + pr_debug("created region %d: %016llx - %016llx\n", + nr, (u64)mlp->base, (u64)mlp->base + size); + cmrp =3D &cma->ranges[nr++]; + cmrp->base_pfn =3D PHYS_PFN(mlp->base); + cmrp->count =3D size >> PAGE_SHIFT; + + sizeleft -=3D size; + if (sizeleft =3D=3D 0) + break; + } + + if (failed) { + list_for_each(mp, &final_ranges) { + mlp =3D list_entry(mp, struct cma_init_memrange, list); + if (mlp =3D=3D failed) + break; + memblock_phys_free(mlp->base, mlp->size); + } + cma_drop_area(cma); + ret =3D -ENOMEM; + goto out; + } + + cma->nranges =3D nr; *res_cma =3D cma; - cma_area_count++; - totalcma_pages +=3D cma->count; =20 - return 0; +out: + if (ret !=3D 0) + pr_err("Failed to reserve %lu MiB\n", + (unsigned long)total_size / SZ_1M); + else + pr_info("Reserved %lu MiB in %d range%s\n", + (unsigned long)total_size / SZ_1M, nr, + nr > 1 ? "s" : ""); + + return ret; } =20 /** @@ -241,6 +536,26 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, phys_addr_t alignment, unsigned int order_per_bit, bool fixed, const char *name, struct cma **res_cma, int nid) +{ + int ret; + + ret =3D __cma_declare_contiguous_nid(base, size, limit, alignment, + order_per_bit, fixed, name, res_cma, nid); + if (ret !=3D 0) + pr_err("Failed to reserve %ld MiB\n", + (unsigned long)size / SZ_1M); + else + pr_info("Reserved %ld MiB at %pa\n", + (unsigned long)size / SZ_1M, &base); + + return ret; +} + +static int __init __cma_declare_contiguous_nid(phys_addr_t base, + phys_addr_t size, phys_addr_t limit, + phys_addr_t alignment, unsigned int order_per_bit, + bool fixed, const char *name, struct cma **res_cma, + int nid) { phys_addr_t memblock_end =3D memblock_end_of_DRAM(); phys_addr_t highmem_start; @@ -273,10 +588,9 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, /* Sanitise input arguments. */ alignment =3D max_t(phys_addr_t, alignment, CMA_MIN_ALIGNMENT_BYTES); if (fixed && base & (alignment - 1)) { - ret =3D -EINVAL; pr_err("Region at %pa must be aligned to %pa bytes\n", &base, &alignment); - goto err; + return -EINVAL; } base =3D ALIGN(base, alignment); size =3D ALIGN(size, alignment); @@ -294,10 +608,9 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, * low/high memory boundary. */ if (fixed && base < highmem_start && base + size > highmem_start) { - ret =3D -EINVAL; pr_err("Region at %pa defined on low/high memory boundary (%pa)\n", &base, &highmem_start); - goto err; + return -EINVAL; } =20 /* @@ -309,18 +622,16 @@ int __init cma_declare_contiguous_nid(phys_addr_t bas= e, limit =3D memblock_end; =20 if (base + size > limit) { - ret =3D -EINVAL; pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n", &size, &base, &limit); - goto err; + return -EINVAL; } =20 /* Reserve memory */ if (fixed) { if (memblock_is_region_reserved(base, size) || memblock_reserve(base, size) < 0) { - ret =3D -EBUSY; - goto err; + return -EBUSY; } } else { phys_addr_t addr =3D 0; @@ -357,10 +668,8 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, if (!addr) { addr =3D memblock_alloc_range_nid(size, alignment, base, limit, nid, true); - if (!addr) { - ret =3D -ENOMEM; - goto err; - } + if (!addr) + return -ENOMEM; } =20 /* @@ -373,75 +682,67 @@ int __init cma_declare_contiguous_nid(phys_addr_t bas= e, =20 ret =3D cma_init_reserved_mem(base, size, order_per_bit, name, res_cma); if (ret) - goto free_mem; - - pr_info("Reserved %ld MiB at %pa on node %d\n", (unsigned long)size / SZ_= 1M, - &base, nid); - return 0; + memblock_phys_free(base, size); =20 -free_mem: - memblock_phys_free(base, size); -err: - pr_err("Failed to reserve %ld MiB on node %d\n", (unsigned long)size / SZ= _1M, - nid); return ret; } =20 static void cma_debug_show_areas(struct cma *cma) { unsigned long next_zero_bit, next_set_bit, nr_zero; - unsigned long start =3D 0; + unsigned long start; unsigned long nr_part; - unsigned long nbits =3D cma_bitmap_maxno(cma); + unsigned long nbits; + int r; + struct cma_memrange *cmr; =20 spin_lock_irq(&cma->lock); pr_info("number of available pages: "); - for (;;) { - next_zero_bit =3D find_next_zero_bit(cma->bitmap, nbits, start); - if (next_zero_bit >=3D nbits) - break; - next_set_bit =3D find_next_bit(cma->bitmap, nbits, next_zero_bit); - nr_zero =3D next_set_bit - next_zero_bit; - nr_part =3D nr_zero << cma->order_per_bit; - pr_cont("%s%lu@%lu", start ? "+" : "", nr_part, - next_zero_bit); - start =3D next_zero_bit + nr_zero; + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + + start =3D 0; + nbits =3D cma_bitmap_maxno(cma, cmr); + + pr_info("range %d: ", r); + for (;;) { + next_zero_bit =3D find_next_zero_bit(cmr->bitmap, + nbits, start); + if (next_zero_bit >=3D nbits) + break; + next_set_bit =3D find_next_bit(cmr->bitmap, nbits, + next_zero_bit); + nr_zero =3D next_set_bit - next_zero_bit; + nr_part =3D nr_zero << cma->order_per_bit; + pr_cont("%s%lu@%lu", start ? "+" : "", nr_part, + next_zero_bit); + start =3D next_zero_bit + nr_zero; + } + pr_info("\n"); } pr_cont("=3D> %lu free of %lu total pages\n", cma->available_count, cma->count); spin_unlock_irq(&cma->lock); } =20 -static struct page *__cma_alloc(struct cma *cma, unsigned long count, - unsigned int align, gfp_t gfp) +static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr, + unsigned long count, unsigned int align, + struct page **pagep, gfp_t gfp) { unsigned long mask, offset; unsigned long pfn =3D -1; unsigned long start =3D 0; unsigned long bitmap_maxno, bitmap_no, bitmap_count; - unsigned long i; + int ret =3D -EBUSY; struct page *page =3D NULL; - int ret =3D -ENOMEM; - const char *name =3D cma ? cma->name : NULL; - - trace_cma_alloc_start(name, count, align); - - if (!cma || !cma->count || !cma->bitmap) - return page; - - pr_debug("%s(cma %p, name: %s, count %lu, align %d)\n", __func__, - (void *)cma, cma->name, count, align); - - if (!count) - return page; =20 mask =3D cma_bitmap_aligned_mask(cma, align); - offset =3D cma_bitmap_aligned_offset(cma, align); - bitmap_maxno =3D cma_bitmap_maxno(cma); + offset =3D cma_bitmap_aligned_offset(cma, cmr, align); + bitmap_maxno =3D cma_bitmap_maxno(cma, cmr); bitmap_count =3D cma_bitmap_pages_to_bits(cma, count); =20 if (bitmap_count > bitmap_maxno) - return page; + goto out; =20 for (;;) { spin_lock_irq(&cma->lock); @@ -453,14 +754,14 @@ static struct page *__cma_alloc(struct cma *cma, unsi= gned long count, spin_unlock_irq(&cma->lock); break; } - bitmap_no =3D bitmap_find_next_zero_area_off(cma->bitmap, + bitmap_no =3D bitmap_find_next_zero_area_off(cmr->bitmap, bitmap_maxno, start, bitmap_count, mask, offset); if (bitmap_no >=3D bitmap_maxno) { spin_unlock_irq(&cma->lock); break; } - bitmap_set(cma->bitmap, bitmap_no, bitmap_count); + bitmap_set(cmr->bitmap, bitmap_no, bitmap_count); cma->available_count -=3D count; /* * It's safe to drop the lock here. We've marked this region for @@ -469,7 +770,7 @@ static struct page *__cma_alloc(struct cma *cma, unsign= ed long count, */ spin_unlock_irq(&cma->lock); =20 - pfn =3D cma->base_pfn + (bitmap_no << cma->order_per_bit); + pfn =3D cmr->base_pfn + (bitmap_no << cma->order_per_bit); mutex_lock(&cma_mutex); ret =3D alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp); mutex_unlock(&cma_mutex); @@ -478,7 +779,7 @@ static struct page *__cma_alloc(struct cma *cma, unsign= ed long count, break; } =20 - cma_clear_bitmap(cma, pfn, count); + cma_clear_bitmap(cma, cmr, pfn, count); if (ret !=3D -EBUSY) break; =20 @@ -490,6 +791,38 @@ static struct page *__cma_alloc(struct cma *cma, unsig= ned long count, /* try again with a bit different memory target */ start =3D bitmap_no + mask + 1; } +out: + *pagep =3D page; + return ret; +} + +static struct page *__cma_alloc(struct cma *cma, unsigned long count, + unsigned int align, gfp_t gfp) +{ + struct page *page =3D NULL; + int ret =3D -ENOMEM, r; + unsigned long i; + const char *name =3D cma ? cma->name : NULL; + + trace_cma_alloc_start(name, count, align); + + if (!cma || !cma->count) + return page; + + pr_debug("%s(cma %p, name: %s, count %lu, align %d)\n", __func__, + (void *)cma, cma->name, count, align); + + if (!count) + return page; + + for (r =3D 0; r < cma->nranges; r++) { + page =3D NULL; + + ret =3D cma_range_alloc(cma, &cma->ranges[r], count, align, + &page, gfp); + if (ret !=3D -EBUSY || page) + break; + } =20 /* * CMA can allocate multiple page blocks, which results in different @@ -508,7 +841,8 @@ static struct page *__cma_alloc(struct cma *cma, unsign= ed long count, } =20 pr_debug("%s(): returned %p\n", __func__, page); - trace_cma_alloc_finish(name, pfn, page, count, align, ret); + trace_cma_alloc_finish(name, page ? page_to_pfn(page) : 0, + page, count, align, ret); if (page) { count_vm_event(CMA_ALLOC_SUCCESS); cma_sysfs_account_success_pages(cma, count); @@ -551,20 +885,31 @@ struct folio *cma_alloc_folio(struct cma *cma, int or= der, gfp_t gfp) bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count) { - unsigned long pfn; + unsigned long pfn, end; + int r; + struct cma_memrange *cmr; + bool ret; =20 - if (!cma || !pages) + if (!cma || !pages || count > cma->count) return false; =20 pfn =3D page_to_pfn(pages); + ret =3D false; =20 - if (pfn < cma->base_pfn || pfn >=3D cma->base_pfn + cma->count) { - pr_debug("%s(page %p, count %lu)\n", __func__, - (void *)pages, count); - return false; + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + end =3D cmr->base_pfn + cmr->count; + if (pfn >=3D cmr->base_pfn && pfn < end) { + ret =3D pfn + count <=3D end; + break; + } } =20 - return true; + if (!ret) + pr_debug("%s(page %p, count %lu)\n", + __func__, (void *)pages, count); + + return ret; } =20 /** @@ -580,19 +925,32 @@ bool cma_pages_valid(struct cma *cma, const struct pa= ge *pages, bool cma_release(struct cma *cma, const struct page *pages, unsigned long count) { - unsigned long pfn; + struct cma_memrange *cmr; + unsigned long pfn, end_pfn; + int r; + + pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count); =20 if (!cma_pages_valid(cma, pages, count)) return false; =20 - pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count); - pfn =3D page_to_pfn(pages); + end_pfn =3D pfn + count; + + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + if (pfn >=3D cmr->base_pfn && + pfn < (cmr->base_pfn + cmr->count)) { + VM_BUG_ON(end_pfn > cmr->base_pfn + cmr->count); + break; + } + } =20 - VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); + if (r =3D=3D cma->nranges) + return false; =20 free_contig_range(pfn, count); - cma_clear_bitmap(cma, pfn, count); + cma_clear_bitmap(cma, cmr, pfn, count); cma_sysfs_account_release_pages(cma, count); trace_cma_release(cma->name, pfn, pages, count); =20 diff --git a/mm/cma.h b/mm/cma.h index 3dd3376ae980..5f39dd1aac91 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -10,19 +10,35 @@ struct cma_kobject { struct cma *cma; }; =20 +/* + * Multi-range support. This can be useful if the size of the allocation + * is not expected to be larger than the alignment (like with hugetlb_cma), + * and the total amount of memory requested, while smaller than the total + * amount of memory available, is large enough that it doesn't fit in a + * single physical memory range because of memory holes. + */ +struct cma_memrange { + unsigned long base_pfn; + unsigned long count; + unsigned long *bitmap; +#ifdef CONFIG_CMA_DEBUGFS + struct debugfs_u32_array dfs_bitmap; +#endif +}; +#define CMA_MAX_RANGES 8 + struct cma { - unsigned long base_pfn; unsigned long count; unsigned long available_count; - unsigned long *bitmap; unsigned int order_per_bit; /* Order of pages represented by one bit */ spinlock_t lock; #ifdef CONFIG_CMA_DEBUGFS struct hlist_head mem_head; spinlock_t mem_head_lock; - struct debugfs_u32_array dfs_bitmap; #endif char name[CMA_MAX_NAME]; + int nranges; + struct cma_memrange ranges[CMA_MAX_RANGES]; #ifdef CONFIG_CMA_SYSFS /* the number of CMA page successful allocations */ atomic64_t nr_pages_succeeded; @@ -39,9 +55,10 @@ struct cma { extern struct cma cma_areas[MAX_CMA_AREAS]; extern unsigned int cma_area_count; =20 -static inline unsigned long cma_bitmap_maxno(struct cma *cma) +static inline unsigned long cma_bitmap_maxno(struct cma *cma, + struct cma_memrange *cmr) { - return cma->count >> cma->order_per_bit; + return cmr->count >> cma->order_per_bit; } =20 #ifdef CONFIG_CMA_SYSFS diff --git a/mm/cma_debug.c b/mm/cma_debug.c index 89236f22230a..fdf899532ca0 100644 --- a/mm/cma_debug.c +++ b/mm/cma_debug.c @@ -46,17 +46,26 @@ DEFINE_DEBUGFS_ATTRIBUTE(cma_used_fops, cma_used_get, N= ULL, "%llu\n"); static int cma_maxchunk_get(void *data, u64 *val) { struct cma *cma =3D data; + struct cma_memrange *cmr; unsigned long maxchunk =3D 0; - unsigned long start, end =3D 0; - unsigned long bitmap_maxno =3D cma_bitmap_maxno(cma); + unsigned long start, end; + unsigned long bitmap_maxno; + int r; =20 spin_lock_irq(&cma->lock); - for (;;) { - start =3D find_next_zero_bit(cma->bitmap, bitmap_maxno, end); - if (start >=3D bitmap_maxno) - break; - end =3D find_next_bit(cma->bitmap, bitmap_maxno, start); - maxchunk =3D max(end - start, maxchunk); + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + bitmap_maxno =3D cma_bitmap_maxno(cma, cmr); + end =3D 0; + for (;;) { + start =3D find_next_zero_bit(cmr->bitmap, + bitmap_maxno, end); + if (start >=3D bitmap_maxno) + break; + end =3D find_next_bit(cmr->bitmap, bitmap_maxno, + start); + maxchunk =3D max(end - start, maxchunk); + } } spin_unlock_irq(&cma->lock); *val =3D (u64)maxchunk << cma->order_per_bit; @@ -159,24 +168,41 @@ DEFINE_DEBUGFS_ATTRIBUTE(cma_alloc_fops, NULL, cma_al= loc_write, "%llu\n"); =20 static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentr= y) { - struct dentry *tmp; + struct dentry *tmp, *dir, *rangedir; + int r; + char rdirname[12]; + struct cma_memrange *cmr; =20 tmp =3D debugfs_create_dir(cma->name, root_dentry); =20 debugfs_create_file("alloc", 0200, tmp, cma, &cma_alloc_fops); debugfs_create_file("free", 0200, tmp, cma, &cma_free_fops); - debugfs_create_file("base_pfn", 0444, tmp, - &cma->base_pfn, &cma_debugfs_fops); debugfs_create_file("count", 0444, tmp, &cma->count, &cma_debugfs_fops); debugfs_create_file("order_per_bit", 0444, tmp, &cma->order_per_bit, &cma_debugfs_fops); debugfs_create_file("used", 0444, tmp, cma, &cma_used_fops); debugfs_create_file("maxchunk", 0444, tmp, cma, &cma_maxchunk_fops); =20 - cma->dfs_bitmap.array =3D (u32 *)cma->bitmap; - cma->dfs_bitmap.n_elements =3D DIV_ROUND_UP(cma_bitmap_maxno(cma), - BITS_PER_BYTE * sizeof(u32)); - debugfs_create_u32_array("bitmap", 0444, tmp, &cma->dfs_bitmap); + rangedir =3D debugfs_create_dir("ranges", tmp); + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + snprintf(rdirname, sizeof(rdirname), "%d", r); + dir =3D debugfs_create_dir(rdirname, rangedir); + debugfs_create_file("base_pfn", 0444, dir, + &cmr->base_pfn, &cma_debugfs_fops); + cmr->dfs_bitmap.array =3D (u32 *)cmr->bitmap; + cmr->dfs_bitmap.n_elements =3D + DIV_ROUND_UP(cma_bitmap_maxno(cma, cmr), + BITS_PER_BYTE * sizeof(u32)); + debugfs_create_u32_array("bitmap", 0444, dir, + &cmr->dfs_bitmap); + } + + /* + * Backward compatible symlinks to range 0 for base_pfn and bitmap. + */ + debugfs_create_symlink("base_pfn", tmp, "ranges/0/base_pfn"); + debugfs_create_symlink("bitmap", tmp, "ranges/0/bitmap"); } =20 static int __init cma_debugfs_init(void) --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C694F1A00D1 for ; Thu, 6 Feb 2025 18:51:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867883; cv=none; b=C1adOwKCRwMn39h0IGUNDG3zDLc0hpVR7BLA6/XE0aAU5LpVZHmONeZb9gEDeeuyoW5rqjdh3wCBk0sQKIJ1gc9DtBC0bygPym9eXN2t7qku51PlQ3yvjsX7ntqce4Au8zjWwzcaQUFi8aKfTnjIZ0N/bEwrnVJMUTNeEU3EIIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867883; c=relaxed/simple; bh=kZMcsQE8tmWol8Wm7lSOkd2XdB59ZsBJwEwnyhHgas4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vF+DV4kP0p71AC0J95x0wHnkUki/7MZ0s01Am4Cew9iZPlUM+MhWfEJxXMtvDFdhbwlfEsFVjWe3pkJww+YDz5xAstoQViRtNDRn2mLBpOPmpd4dNQFqHyf0Nd53SJBQtQ4WTx7zFoy2RNPnD5Scg2jXxoyzV5gzYGUNhjloQRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dphu4B57; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dphu4B57" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f9cf77911eso2505594a91.1 for ; Thu, 06 Feb 2025 10:51:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867881; x=1739472681; darn=vger.kernel.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=GxP6S0l3dQ+6CkO+pIY1HsOnc8ShdfnkIOi767vYzT8=; b=dphu4B57lvnXi/LCOv7bhjTlnIDICM0nicd5OGOo3jdvqOKtFOG9b9WhBVzhIeGvB6 +HUDaVfp0zb/U+HmUr6XZztPZLkX4tA/DRA1c/d7VXxBsylkHDsPRGZr+YDYxWDFDz/r wc/mLGsmNoHCoRU4PwtXoMgUfmK+lcevjR9eJXct8FpPnlRgWDj9tdyMUipeGKBWml+5 aSZt9wl44X4NRvzTDCQAlVqrRjxLJ+xwiqPX8NLkudh1vxuYRdh+Q2e9XUSj0lzFKnRx wSZLKtAObQv0El0EkXcmwrhbz/sE3Eg+g+i5ocAxu8GwN4KS5YWxiLr92pZatKEWx16K xRag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867881; x=1739472681; 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=GxP6S0l3dQ+6CkO+pIY1HsOnc8ShdfnkIOi767vYzT8=; b=thaf44ohfaGz/VEKyBobSNT87dTdqB+ZAXVw73O+kDGC/Y5aJzwZuUxk1GhOOGOuto FOxL0O7XRdzrPrnBgLxHn6c6FLthWrK68c3yQhKxCsaGM3YX4zPVk61/NldTst2/0c4J yAPPKk9O9FfT/LEdgg/L/MZasN/D5/QKc7weK7F7dOAi+dz2Bcfo29UcOeSTdsfQozGG Q83SiGqJyV5ut/7u2+La7e8a4EMYsmGfE3Khx6k7GP6/05VNyaU7/I/MmBKvFDdKEFes BdlYzbDoUqbqprb5Wsx9TGR9165BzR0fOI0co7rtp+0JpzNebAqzqe/Xa8GR+a7TBJoB ZK8w== X-Forwarded-Encrypted: i=1; AJvYcCW4AxsE8cL34JzFPdkwbqexz00A/L3pCxf3op7BX8p1sTKapXLH1SttKpMn7sGTvDn2yue9aWD5qDvHUPs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz4cXioLod+ECruetCZYy1L0xQWZyIbaxg+GEeszyH+h6hmyvX4 Z1SO0Dtpp1G8LC4AXigTorpbkIJwWZb6AEBJp3Ng57TW9bCVr2ZvkYWiN/woGMj2wqpBBQ== X-Google-Smtp-Source: AGHT+IFhAWowVYwEoYtx6mfFOivywbt3Piw8sNnJL4ZJYNh4M1FuUx2ERPDZanvYPgCeFa64BAkgYsQp X-Received: from pjbse15.prod.google.com ([2002:a17:90b:518f:b0:2f4:3e59:8bb1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1946:b0:2fa:17e4:b1cf with SMTP id 98e67ed59e1d1-2fa247f9fcdmr44198a91.2.1738867881130; Thu, 06 Feb 2025 10:51:21 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:43 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-4-fvdl@google.com> Subject: [PATCH v3 03/28] mm/cma: introduce cma_intersects 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 , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , linux-s390@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that CMA areas can have multiple physical ranges, code can't assume a CMA struct represents a base_pfn plus a size, as returned from cma_get_base. Most cases are ok though, since they all explicitly refer to CMA areas that were created using existing interfaces (cma_declare_contiguous_nid or cma_init_reserved_mem), which guarantees they have just one physical range. An exception is the s390 code, which walks all CMA ranges to see if they intersect with a range of memory that is about to be hotremoved. So, in the future, it might run in to multi-range areas. To keep this check working, define a cma_intersects function. This just checks if a physaddr range intersects any of the ranges. Use it in the s390 check. Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: linux-s390@vger.kernel.org Signed-off-by: Frank van der Linden Acked-by: Alexander Gordeev --- arch/s390/mm/init.c | 13 +++++-------- include/linux/cma.h | 1 + mm/cma.c | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c index f2298f7a3f21..d88cb1c13f7d 100644 --- a/arch/s390/mm/init.c +++ b/arch/s390/mm/init.c @@ -239,16 +239,13 @@ struct s390_cma_mem_data { static int s390_cma_check_range(struct cma *cma, void *data) { struct s390_cma_mem_data *mem_data; - unsigned long start, end; =20 mem_data =3D data; - start =3D cma_get_base(cma); - end =3D start + cma_get_size(cma); - if (end < mem_data->start) - return 0; - if (start >=3D mem_data->end) - return 0; - return -EBUSY; + + if (cma_intersects(cma, mem_data->start, mem_data->end)) + return -EBUSY; + + return 0; } =20 static int s390_cma_mem_notifier(struct notifier_block *nb, diff --git a/include/linux/cma.h b/include/linux/cma.h index 863427c27dc2..03d85c100dcc 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -53,6 +53,7 @@ extern bool cma_pages_valid(struct cma *cma, const struct= page *pages, unsigned extern bool cma_release(struct cma *cma, const struct page *pages, unsigne= d long count); =20 extern int cma_for_each_area(int (*it)(struct cma *cma, void *data), void = *data); +extern bool cma_intersects(struct cma *cma, unsigned long start, unsigned = long end); =20 extern void cma_reserve_pages_on_error(struct cma *cma); =20 diff --git a/mm/cma.c b/mm/cma.c index 6ac9173d3a7d..c7116a5819c5 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -978,3 +978,24 @@ int cma_for_each_area(int (*it)(struct cma *cma, void = *data), void *data) =20 return 0; } + +bool cma_intersects(struct cma *cma, unsigned long start, unsigned long en= d) +{ + int r; + struct cma_memrange *cmr; + unsigned long rstart, rend; + + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + + rstart =3D PFN_PHYS(cmr->base_pfn); + rend =3D PFN_PHYS(cmr->base_pfn + cmr->count); + if (end < rstart) + continue; + if (start >=3D rend) + continue; + return true; + } + + return false; +} --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 813601A0BE1 for ; Thu, 6 Feb 2025 18:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867885; cv=none; b=CqslP8xs8OCtDmXmm7s6cido7T5blJSTE09msRDS5lcnVG1IXMb/NHRZcyTmx5ESrLZzGhUzdWUcgYinrnM3+cj+yNmjKg4kBqauuIH+8kOjqB4dylhoejNslhq9bUlWqByOmq7AaK1I8S1rN5HthOHlpO2O0ip74kpv8Fk4tTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867885; c=relaxed/simple; bh=uqif3DVqjaaHwsC4GuL0urReugnTia1gdJmb4PcI+Ig=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XH0AIsWlqqz2OQyAO0y5A7ML4N19vOBSC/Ahakh8FYcWc+EdCEnQYI/rzQZrvYKPbKfPLkCiJ+gMHutSfjwlNL+s7wwk+mt3KpCTELcaHlxd4bMEG8ysiBNoq28jH0sOqkOHVpP4vbE41qTA1qBN5v+rtHGL5y23LK/byJYmdYs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eRrfu/WJ; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eRrfu/WJ" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f9fbe0215bso2765691a91.1 for ; Thu, 06 Feb 2025 10:51:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867883; x=1739472683; darn=vger.kernel.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=RHIk1zAzNzZzzd4zq0SrZCaGEXgiIYu2uBl1eqTVpf4=; b=eRrfu/WJbJTsfszXr++2YTKRUQ8jOLuc0SedO7vCo6LHOpMR1ovRWACaFmRRaeKi1i PrglvLK3GCIy5B8v4KqGRzHJNyEi7Dlkz5kQGymMafhVQ/JgYy1xFDtWD9XIfS8odBg5 t+Uw++SnkarwEPMrKteIZeUoUfJLybFUOWDiF38GBEqqTR+cum9SGkL1gN0d9QOFOaf1 AKl4t5jzXq1ExRCAVDqe2dV+0gc0niIHbUH9D+eFR66fiidmNNt1wrV4iGTZTUM6ErWp 8PY1+rcjY+MnMGbRir5aStV/WE3pN4uwGqrqhrSmIle78LJCCayU6wI1TTZ9dGpSfcVy Oreg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867883; x=1739472683; 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=RHIk1zAzNzZzzd4zq0SrZCaGEXgiIYu2uBl1eqTVpf4=; b=MJG7dtME7Z+chRy1DQqd88sfTPWhfLLMms21iBhxT8DEBSolrwHhfhHQKZ7NyR53x4 0hetPPweVsX9Zw3pFGeeLwkSe88VhFqbemy5KGCYPHkuSMuL5JwD4rNrxBWIoVMZkK2r xUfhhemFVjbWFmFfYWWyLRhz475AnfAMttxzzLfmJuDv+rGbbSiqUmKATGDUkenda/FF +NldAU8PBeFmjX4M6W4zJngIeE8ek03zQqBRCnfH5lsRN77YTNGPuqiwY/2avgtw4q9L 9tM5vChnCBW7Tond6qhHXHHZKrIKOTFiuW7ph3usYrOlGT5MXLFBzgSwTfYCc1qFYPp3 bX6w== X-Forwarded-Encrypted: i=1; AJvYcCWP5uWYc578A9p0IzZP4QHZAaokKsug4BfehIQ8gB62WuNNjtJVD6uVML+ZL6s+qBEqwtBuBetQGkPHORA=@vger.kernel.org X-Gm-Message-State: AOJu0YxYxQ3K8QXZHQeWKeigmpoDnpibDQjXVDpx8gANeiVzgyPT/lRo qyGgq/p42w3yGn8b4OtNQAi6nlkAQ7HceMNfZQ4VifUrNx53vFyziA69zfdqWtOiXrZ7DA== X-Google-Smtp-Source: AGHT+IG2C6Tqj4tol1eUNf+3cFodAFg6bV6h55pukBTkPfnQj74WsqcAVhR4si2RwXlaV64ND0j3yoTj X-Received: from pjbsi17.prod.google.com ([2002:a17:90b:5291:b0:2f9:d9fe:e723]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4fc6:b0:2ee:cd83:8fe7 with SMTP id 98e67ed59e1d1-2fa243f666emr158067a91.35.1738867882861; Thu, 06 Feb 2025 10:51:22 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:44 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-5-fvdl@google.com> Subject: [PATCH v3 04/28] mm, hugetlb: use cma_declare_contiguous_multi 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hugetlb_cma is fine with using multiple CMA ranges, as long as it can get its gigantic pages allocated from them. So, use cma_declare_contiguous_multi to allow for multiple ranges, increasing the chances of getting what we want on systems with gaps in physical memory. Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 65068671e460..828ae0080ab5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7738,9 +7738,8 @@ void __init hugetlb_cma_reserve(int order) * may be returned to CMA allocator in the case of * huge page demotion. */ - res =3D cma_declare_contiguous_nid(0, size, 0, - PAGE_SIZE << order, - HUGETLB_PAGE_ORDER, false, name, + res =3D cma_declare_contiguous_multi(size, PAGE_SIZE << order, + HUGETLB_PAGE_ORDER, name, &hugetlb_cma[nid], nid); if (res) { pr_warn("hugetlb_cma: reservation failed: err %d, node %d", --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F2C61A23B8 for ; Thu, 6 Feb 2025 18:51:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867886; cv=none; b=RODjo4wpR+AwhMIt8jUv29h4AYgi4B6uPi8Rjp4HDwpHkNmxXfGBxEAVD0q9Dp+bMHtljY9fuFMfaa8EoBYoEzKOb3G87fUYM5jRddjB3wIc/1ZoeOEZKZioK9GLq2KljlxZwEX5B+m2cpL0VxB+T/9TmFh4jCIS493hVdRD8Xo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867886; c=relaxed/simple; bh=r7rS1CImG83/jbux0jZKZt0J0v0GE0hzBEm0JLU0NvE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=M+qoUWRu+QWUbEY+4yXrxDYj5cHW6IHPnFXojHxuDfiN8mWvEzVxjl7ZZQ2Vn1lTS3HiIYrkq+fBjhNlq8bp1DBUgcogyIU8unFaNB5igu8YBXJcWSHF3rBWg3X352+TSGlEcpG9B5OwqmowukxCRtmLFFDgnjOkUmrJr3LcRYU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=10Wxy5eb; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="10Wxy5eb" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f2a9f056a8so2425401a91.2 for ; Thu, 06 Feb 2025 10:51:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867884; x=1739472684; darn=vger.kernel.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=hzoPqdOikoObjXWU9SvKmiO4GzZxuupFnL9M3tsuTWc=; b=10Wxy5ebIE6wyRLojkRYOFLc2VDiIAiZ61O8BVckY4YiD1SKcf3xHHQGIIkt4NaSiV ch8Wv9+YT0aHoWnlid62GY8HkvEjt3zMqhD0gdUN1wpiRkaTgssv1Q8qMiJfnrz0rkn1 arXgxydpoBXR8q/zYC07oMIi/SqNaYTQhev52F+3CdNWggLqOXUbHFclJGNpJDtnVruo 0Fnjv0jqi4HLMjbuCqorbuWDOFnq3nKWOKdb09ZSIunYz7Er0oMKQovof+tvmNEXL3wK V2/rLbH6JQ5Xifsi0fzxY/R20mYYz517zydxEhNWPhqOh8lURjyNNmBrXoqZe1KqcM0m cKOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867884; x=1739472684; 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=hzoPqdOikoObjXWU9SvKmiO4GzZxuupFnL9M3tsuTWc=; b=EiO1oSQdihWBE36QeVx+R8Ahc9kHKqc6kLh7MEzBWx2h2ZA/qZbZz2z95pFIeineTS GwpXwI3G8fQLghra0zFsb00LmKsDUeWHE20BNUStIxCGWzco8Yjpf1DoxRWQn4pnxNjn wsqBTZ8x9aOVXYJGrJB2nn4tg20/5q1M/CKsgCyJyMhwCK52g1N3MLYdKecoqKovo1tm PP1/4JeB6SvyrQ53v8bN256BfHhW4YahrlLVeqrWHTv1pmdawPbmODsniG/s3JLTwyea uXhNEcKNvEJsEG8f2Ydrte9kME3IKU9Uc2cSb6nTm3tALwHtcZmourH39sd84F0zu1SV 2bfw== X-Forwarded-Encrypted: i=1; AJvYcCVZl+f1GVRQAbSO20DsDEPAEpYgYyT/iJPSqR7oR4HsJv5byITtC6Mzi3JjVfPjqQ/7wg6Ezq5PwayhXQU=@vger.kernel.org X-Gm-Message-State: AOJu0YyTCmIhPrSrRnB2LD8X5xKkFWrufWQO9nk0gx+TmfFrTcQEae9P aWesAthRyGu8+Gx+4KFL7qKqeypvokXCx40RetN8IFaZaT/UcBFGIg7v+JxGxJO+8uD2zQ== X-Google-Smtp-Source: AGHT+IEL2yhkPaKLLzs2ZR25Ntw2PD6TefD2snZgeFGU7MfGyptfuCLCB6W/h56WjY/xARRU7WJJptnX X-Received: from pjbpl9.prod.google.com ([2002:a17:90b:2689:b0:2ea:aa56:49c]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c0e:b0:2f2:ab09:c256 with SMTP id 98e67ed59e1d1-2fa243ee52dmr179111a91.33.1738867884627; Thu, 06 Feb 2025 10:51:24 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:45 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-6-fvdl@google.com> Subject: [PATCH v3 05/28] mm/hugetlb: fix round-robin bootmem allocation 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 , Zhenguo Yao Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit b5389086ad7b ("hugetlbfs: extend the definition of hugepages paramet= er to support node allocation") changed the NUMA_NO_NODE round-robin allocation behavior in case of a failure to allocate from one NUMA node. The code originally moved on to the next node to try again, but now it immediately breaks out of the loop. Restore the original behavior. Fixes: b5389086ad7b ("hugetlbfs: extend the definition of hugepages paramet= er to support node allocation") Cc: Zhenguo Yao Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 828ae0080ab5..1d8ec21dc2c2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3156,16 +3156,13 @@ int __alloc_bootmem_huge_page(struct hstate *h, int= nid) m =3D memblock_alloc_try_nid_raw( huge_page_size(h), huge_page_size(h), 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); - /* - * Use the beginning of the huge page to store the - * huge_bootmem_page struct (until gather_bootmem - * puts them into the mem_map). - */ - if (!m) - return 0; - goto found; + if (m) + break; } =20 + if (!m) + return 0; + found: =20 /* @@ -3177,7 +3174,14 @@ int __alloc_bootmem_huge_page(struct hstate *h, int = nid) */ memblock_reserved_mark_noinit(virt_to_phys((void *)m + PAGE_SIZE), huge_page_size(h) - PAGE_SIZE); - /* Put them into a private list first because mem_map is not up yet */ + /* + * Use the beginning of the huge page to store the + * huge_bootmem_page struct (until gather_bootmem + * puts them into the mem_map). + * + * Put them into a private list first because mem_map + * is not up yet. + */ INIT_LIST_HEAD(&m->list); list_add(&m->list, &huge_boot_pages[node]); m->hstate =3D h; --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F093A1A2C0B for ; Thu, 6 Feb 2025 18:51:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867888; cv=none; b=WFV4KN4RiHDclPe8Y9/v5mAmdCDbWZt+X6owsUryS+Y0XNy+KLEBONClhJVIfUABwlC4e7vVT82UsgeVKyCmDNH+QqwCfPvfCi65OT0rEkjg5JFo5HlRjLt9xmQp/XO2nrg2tQSOPUTUOWlP1tqFDptc8DamnStd0WrWgvKbvKw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867888; c=relaxed/simple; bh=+L2iUrkIOG2McHgwXf/cmLt73VvME9sfxcwyqOSO+z0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X7daIFvG/86YBbJPkXGe2iroYkuR67meeC2aDw0tIsGLgWgUni5B8eqes9m9gl4wFomiLjucAQ2mvIYxUXZ2kaK70kwy2FW9GdphxeUL78L8e6pj62D+ng56EwXCDdRJ1OLl1z3Zs9tt6nBjMwyzXjUwEEM/HovyfXJP2aVWoOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1xpBNuJ6; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1xpBNuJ6" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa228b4143so147781a91.0 for ; Thu, 06 Feb 2025 10:51:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867886; x=1739472686; darn=vger.kernel.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=3krrgaxsXaFa4uf9iEDSe21apCBKUA0Oh8FsqZLKWjo=; b=1xpBNuJ6slJNDEtm7amz1rKxPtgnUA7K1Yq6TW9i6hho8ysFHdG63v2sWO+S45avle RqOQ5D0izI6xwN5Hz3h6PwG1jVBXCPTYVeorNqVAnVwoDoLpfmB9F3ShjfrD6dyYvXXY JTzgIuRgbD5YHmXiWR34PCglhd5uIHe3J7MR5akmahwTZeSOEtSLRHkPIaYGy6tTviEV mfOED4LtnoZ7MGYgjDUGD5JpduzXfIJc6+ycsf1u7nuOJ/bKe2HsnL/L1Ea1XKcptRZ4 Ku0nMxBwRlrqW65IZeohnXvCNVgNIkXm2uEkZKfZTmYtzbBNDTtLdY7P12odtf0mlaiI GZvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867886; x=1739472686; 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=3krrgaxsXaFa4uf9iEDSe21apCBKUA0Oh8FsqZLKWjo=; b=C4tcPWw4xdSOH41aMK2iF3LJLQttA2pu4GCM1P55k3vDgy5yO7aEGsHtfvCUZ8mgOr 0TjWoDd+2pZMdOOb8XiKG28IMprxmpnrvdB9cr8y3QZpEnZHU0UheoqERd09b8ozYxQk bcJZRgRyoUbVJu7z6b6+UABvaoAtI52D9nD4nz2Bk9nadiNw+3x0yfzA8fdhTZlDHTqk KeDvAd6tZBFkOEqbx0h7vRS3U/mhPpXXLFeczxqiA67NKopRBvJjqQ3tZ4rIRf1YM6Me pMcEgqxj7aQQvBSe5Bt7VS2kFkcLuyptdsED2GthAtr0D+2siGBne7rA1FdFp3vXD9Hz JPeA== X-Forwarded-Encrypted: i=1; AJvYcCU0Wpffnw8l2EuBImKUk2r4EPb2r89rgqqW7sJWxN72W8vE/I5fr0I2XEcQzuHZLAhMYMLJ5g2q3SUaFwo=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0Hq36YnOmAPI0rL5+bJNEFwcMnqm5XYYxuoYbqE0FpD6mN/bd mHgg9pRjZfYvyKz94EYGqY5WjtTpvsmBLeXs5nFctIsSraEaYCbq29PrxGfVwh5D2aI0Og== X-Google-Smtp-Source: AGHT+IHdn9ARX0BKU2aoxnDMJSfDfQ2JxdDOMx8aC4l0xtpEE2SmhHzHCIxuEt3YV/CF1/5TaCD9NFnV X-Received: from pjbst7.prod.google.com ([2002:a17:90b:1fc7:b0:2ef:95f4:4619]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5282:b0:2fa:9c9:20a3 with SMTP id 98e67ed59e1d1-2fa24700cbfmr107801a91.0.1738867886262; Thu, 06 Feb 2025 10:51:26 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:46 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-7-fvdl@google.com> Subject: [PATCH v3 06/28] mm/hugetlb: remove redundant __ClearPageReserved 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In hugetlb_folio_init_tail_vmemmap, the reserved flag is cleared for the tail page just before it is zeroed out, which is redundant. Remove the __ClearPageReserved call. Signed-off-by: Frank van der Linden Reviewed-by: Oscar Salvador --- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 1d8ec21dc2c2..0836e3231774 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3202,7 +3202,6 @@ static void __init hugetlb_folio_init_tail_vmemmap(st= ruct folio *folio, for (pfn =3D head_pfn + start_page_number; pfn < end_pfn; pfn++) { struct page *page =3D pfn_to_page(pfn); =20 - __ClearPageReserved(folio_page(folio, pfn - head_pfn)); __init_single_page(page, pfn, zone, nid); prep_compound_tail((struct page *)folio, pfn - head_pfn); ret =3D page_ref_freeze(page, 1); --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 949141A5B94 for ; Thu, 6 Feb 2025 18:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867890; cv=none; b=MmrrQntHxpacXVDdY8sQU9HAC1duRczgJCbuarWAHZoVL8ORJB0UaiZvllDb7R+BOOVXQic05+sdb12Fo+RhmJDPaPlekOscnRIoxKmTgBzrro8wEvjs8GmkrKUvPbKukmweJFO9esh2Wfee78h+hEF0Og4bOqogLZ7RKR9NOMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867890; c=relaxed/simple; bh=cfOUOmXD1/WjBlCaesJi7AOw5xOycmilUh7uKfCe/wY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HJWccXoULU479Jb7OuozZ3n5bVoYV2eCvF74RlM9TT/suiU5WlFuHgnHJH6wBQHHELC/SNI2XUFdSXQ23xLYIMvSJknrSqaD9pCPKNuEOaOHVjisLTbZA3RjgGBnR24vXJ1r6mSCmNITfQcnEBX9+ZanjROWoz7W4iWB9A5l3zw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0Tf/eIGU; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0Tf/eIGU" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa0204ad21so2418143a91.0 for ; Thu, 06 Feb 2025 10:51:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867888; x=1739472688; darn=vger.kernel.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=6PxACdTioptMBNslx7g+3VfoPnhQJsGOs8ZAh4yAkb8=; b=0Tf/eIGU+SYMgEAnNewFayOMwTOImjc7ETHzO5fSMppNnarJkj8Q8J43oaB+DBRjVr O0B7mAamEy2LTD3pjpgVhQsZgQvyoHhzod1gxEqQiEi02m7XlmnnkWL50vC5lma5Xt5q qM1fHz/gd1+YFp3y/8O1ONPgWuwFZx3kMkgS7vqV3c0XmsCmY7LUr8oxEBL8eq13RokC yBEFdUsaF0tiP7+G+snLhBCi9fCvSdiRru0N4SnnNj5bi01XysgWSqRYbfbwlvjHFNnW qTgnJ8HgrwG2Q9UvGfGA+VEtdTAhwOBbLbT57xA7Zm0OtVZztFEXeuhNncl8qkYCD+oI yzOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867888; x=1739472688; 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=6PxACdTioptMBNslx7g+3VfoPnhQJsGOs8ZAh4yAkb8=; b=UobzghYiuHxcAMfv79aav2vfDLuKBBepd+K9rYoWNzV0EOhkFaxASiVeDpYq9lGdKv uR57ImGxGXgOM4TNpIl0QAdcRdyKgJ4urPdrxBvs1QLoU+JWxWCfcDywssSO6EeW78TU 9LDg7VfLB7F6oLACYxsppN5O7yFKaLVImAXU2XHdQVbsvjTkWlIeqDqgaMEzMB4UkCok /ox/OB67GlQNw+y6t7nrN0NdbjQ5mAXoCarwbJq560isS5Vqym3XQJ2qa5L55qLKaEa/ ar6ogHwdTyJzx72pxB0nsEDcPCSLAtm4gP18wPIwFl9yWXoQhxaQr+d3zXXHtAMuI5xh 6Kzw== X-Forwarded-Encrypted: i=1; AJvYcCUXe2nZx9e8OLD3duFmkmkx12cSLuGWv4zVBkXE4CggXUhWjP1hni6DZNrUqkOdg6CKmQ19AS6kmb3YFTw=@vger.kernel.org X-Gm-Message-State: AOJu0YxXWIyxpTANNXWUFSIp7epCtxLFnmsLXF0nNyTr5qMYIbCbV93m VlX9nQuXOSfYn+7HxC15fnkISz/Ss8Rx/8hyB2N466sfmPZrQAccHQQvDG5TWwNQ5XU0vA== X-Google-Smtp-Source: AGHT+IHyKxzmRP4eOhwx4mD9ZcZrcL1FTQwwbx+QtNyhga2EwWZzDYY9rIMSfx1FziNod6CZsidSIYk3 X-Received: from pjbpq3.prod.google.com ([2002:a17:90b:3d83:b0:2ef:701e:21c1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a86:b0:2f9:c144:9d13 with SMTP id 98e67ed59e1d1-2fa242e5dc0mr174642a91.24.1738867887870; Thu, 06 Feb 2025 10:51:27 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:47 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-8-fvdl@google.com> Subject: [PATCH v3 07/28] mm/hugetlb: use online nodes for bootmem allocation 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later commits will move hugetlb bootmem allocation to earlier in init, when N_MEMORY has not yet been set on nodes. Use online nodes instead. At most, this wastes just a few cycles once during boot (and most likely none). Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 0836e3231774..b4de3bbd010d 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3152,7 +3152,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int n= id) goto found; } /* allocate from next node when distributing huge pages */ - for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, &node_= states[N_MEMORY]) { + for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, &node_= states[N_ONLINE]) { m =3D memblock_alloc_try_nid_raw( huge_page_size(h), huge_page_size(h), 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); @@ -4550,8 +4550,8 @@ void __init hugetlb_add_hstate(unsigned int order) for (i =3D 0; i < MAX_NUMNODES; ++i) INIT_LIST_HEAD(&h->hugepage_freelists[i]); INIT_LIST_HEAD(&h->hugepage_activelist); - h->next_nid_to_alloc =3D first_memory_node; - h->next_nid_to_free =3D first_memory_node; + h->next_nid_to_alloc =3D first_online_node; + h->next_nid_to_free =3D first_online_node; snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", huge_page_size(h)/SZ_1K); =20 --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A6F71A83FB for ; Thu, 6 Feb 2025 18:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867892; cv=none; b=qS5Pp8c43vbAV0CF3Wg68EM2PgSVOJHM0FaEXQNv1/jV+eXHPCSs+XngWi+mFggEQkB61lFlxZ7KcZodT5PP3XEc1L+Kmlr4Fu+HWs+SpGZKjKO09VifHHa6sjtgYccGOhXyFOIxWBTRonGk3BPVWYKwu+FCTG+rTmKSuxQwz5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867892; c=relaxed/simple; bh=RDfG0pFSr3X3Q9IKXCGuOFfzEP876n6PEgPL6g1GO+4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eI3vCjjwaxhrFiv8KgEIfNc+iCl/n3oaeYyJkb5/fm6leozdaTtFjcDNjzRiqWc3vnrr5bBt7HfJLp0wtS5q0Ou6LkFsWy3Aa+XS+bI6cM8qNIzvySnC36N9Egw7Bj3uGBCGLWHNv92JlEP3KhpsPUVvpQgrD1lnYFKNR+sLRlI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Szx3XdMw; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Szx3XdMw" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f81a0d0a18so2562106a91.3 for ; Thu, 06 Feb 2025 10:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867889; x=1739472689; darn=vger.kernel.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=nTJNRf6LTiMkMlXPEOIwPpP06vsojDmaDWHwE95+Iic=; b=Szx3XdMwWxmmFu1xKVpmjvp0ghb2Yw1IyRi8LxQIOfcI3C/wIZ0HZKX1XSaPOnSiRA +0u4dD96SMpLj1Vo/cdnSiIYH1hgZD6zeYgA0yL0GaEBZJqbzHVg+/MtN4qqIgOLs9F9 5hPtrW7/iMI+uHIJpaH7wQ9rnQqcUcT26RBgUWvE8sHljV8Hi+pnRoy+HpzwwtDmPyS9 iza4y8/TmWlPBIHI2mECvp+Jhpg/V+gxlL7fWeXJFqqBG+kqFliZ6V5afvhdCNjNgqMV ka6fo+8HORYtxhycKRl3CMWt/MQrGOqLHBF54WbbKCpZfqx24yV2oeSqMaBk1kyLqDQm ULyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867889; x=1739472689; 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=nTJNRf6LTiMkMlXPEOIwPpP06vsojDmaDWHwE95+Iic=; b=kc7qQrxHikjLehwjbbI/EsU8uTfUaU0R2+PL4e/S7FB+dTWBQ+l02x92nZcJXd1Mna yf5BP621xJjcIbIgNrfTR/+7gp9wfC0fUzf+HZoWKFfKAlwphh5gdq5RaM/8Zqhefjus bpkca1zitpyDSCsbV+3IgRiZlZVsygCiNozlRUnE4y1yx7joVarJlrgetELupB4if0Vt YaL4vz7ZRmPf1inTBKNlXcVgFMvD2AjsNKt/K5lUULrceCWVcnFUL7gr7jAsNlulZ1Im YkasOUTX+ahQ4ylRGx6V3AOdFed9RhTebrY12lIye/XF9p5m/7zW90aWHLa8u6FI0rWU 2U1A== X-Forwarded-Encrypted: i=1; AJvYcCW6nsan5pnq41bV7DMsdZIxR1Bv2dB021qKPATyjtK9prvoiD6b4FedZBm5v91MmOrEr7BD+gRNCTrXSXE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzyqb4YLCMA1Jxcg3aDtHVPQcTXrRwQQKjHzMvOKrgjcVp502Wu 07GILYVIEFMPpCRZNVngO45FUI3WdpZLZlJwrLjeawyba75Q2b64CquuKFfJOGesB0lxfw== X-Google-Smtp-Source: AGHT+IH3ZOjV9MQ9vVNRnnO7zW4cnOheRu/dHPje2DoECTojb4J2x2zX0jaHZ8pQaiYA+/kja/RHuYZU X-Received: from pjbpl16.prod.google.com ([2002:a17:90b:2690:b0:2e9:ee22:8881]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2ccf:b0:2ee:b666:d14a with SMTP id 98e67ed59e1d1-2fa24177fa6mr225873a91.17.1738867889615; Thu, 06 Feb 2025 10:51:29 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:48 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-9-fvdl@google.com> Subject: [PATCH v3 08/28] mm/hugetlb: convert cmdline parameters from setup to early 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert the cmdline parameters (hugepagesz, hugepages, default_hugepagesz and hugetlb_free_vmemmap) to early parameters. Since parse_early_param might run before MMU setups on some platforms (powerpc), validation of huge page sizes as specified in command line parameters would fail. So instead, for the hstate-related values, just record the them and parse them on demand, from hugetlb_bootmem_alloc. The allocation of hugetlb bootmem pages is now done in hugetlb_bootmem_alloc, which is called explicitly at the start of mm_core_init(). core_initcall would be too late, as that happens with memblock already torn down. This change will allow earlier allocation and initialization of bootmem hugetlb pages later on. No functional change intended. Signed-off-by: Frank van der Linden --- include/linux/hugetlb.h | 6 ++ mm/hugetlb.c | 133 +++++++++++++++++++++++++++++++--------- mm/hugetlb_vmemmap.c | 6 +- mm/mm_init.c | 3 + 4 files changed, 119 insertions(+), 29 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index ec8c0ccc8f95..9cd7c9dacb88 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -174,6 +174,8 @@ struct address_space *hugetlb_folio_mapping_lock_write(= struct folio *folio); extern int sysctl_hugetlb_shm_group; extern struct list_head huge_boot_pages[MAX_NUMNODES]; =20 +void hugetlb_bootmem_alloc(void); + /* arch callbacks */ =20 #ifndef CONFIG_HIGHPTE @@ -1250,6 +1252,10 @@ static inline bool hugetlbfs_pagecache_present( { return false; } + +static inline void hugetlb_bootmem_alloc(void) +{ +} #endif /* CONFIG_HUGETLB_PAGE */ =20 static inline spinlock_t *huge_pte_lock(struct hstate *h, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b4de3bbd010d..5a4b322e2bb2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -40,6 +40,7 @@ #include #include #include +#include =20 #include #include @@ -62,6 +63,24 @@ static unsigned long hugetlb_cma_size __initdata; =20 __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; =20 +/* + * Due to ordering constraints across the init code for various + * architectures, hugetlb hstate cmdline parameters can't simply + * be early_param. early_param might call the setup function + * before valid hugetlb page sizes are determined, leading to + * incorrect rejection of valid hugepagesz=3D options. + * + * So, record the parameters early and consume them whenever the + * init code is ready for them, by calling hugetlb_parse_params(). + */ + +/* one (hugepagesz=3D,hugepages=3D) pair per hstate, one default_hugepages= z */ +#define HUGE_MAX_CMDLINE_ARGS (2 * HUGE_MAX_HSTATE + 1) +struct hugetlb_cmdline { + char *val; + int (*setup)(char *val); +}; + /* for command line parsing */ static struct hstate * __initdata parsed_hstate; static unsigned long __initdata default_hstate_max_huge_pages; @@ -69,6 +88,20 @@ static bool __initdata parsed_valid_hugepagesz =3D true; static bool __initdata parsed_default_hugepagesz; static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata; =20 +static char hstate_cmdline_buf[COMMAND_LINE_SIZE] __initdata; +static int hstate_cmdline_index __initdata; +static struct hugetlb_cmdline hugetlb_params[HUGE_MAX_CMDLINE_ARGS] __init= data; +static int hugetlb_param_index __initdata; +static __init int hugetlb_add_param(char *s, int (*setup)(char *val)); +static __init void hugetlb_parse_params(void); + +#define hugetlb_early_param(str, func) \ +static __init int func##args(char *s) \ +{ \ + return hugetlb_add_param(s, func); \ +} \ +early_param(str, func##args) + /* * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pa= ges, * free_huge_pages, and surplus_huge_pages. @@ -3488,6 +3521,8 @@ static void __init hugetlb_hstate_alloc_pages(struct = hstate *h) =20 for (i =3D 0; i < MAX_NUMNODES; i++) INIT_LIST_HEAD(&huge_boot_pages[i]); + h->next_nid_to_alloc =3D first_online_node; + h->next_nid_to_free =3D first_online_node; initialized =3D true; } =20 @@ -4550,8 +4585,6 @@ void __init hugetlb_add_hstate(unsigned int order) for (i =3D 0; i < MAX_NUMNODES; ++i) INIT_LIST_HEAD(&h->hugepage_freelists[i]); INIT_LIST_HEAD(&h->hugepage_activelist); - h->next_nid_to_alloc =3D first_online_node; - h->next_nid_to_free =3D first_online_node; snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", huge_page_size(h)/SZ_1K); =20 @@ -4576,6 +4609,42 @@ static void __init hugepages_clear_pages_in_node(voi= d) } } =20 +static __init int hugetlb_add_param(char *s, int (*setup)(char *)) +{ + size_t len; + char *p; + + if (hugetlb_param_index >=3D HUGE_MAX_CMDLINE_ARGS) + return -EINVAL; + + len =3D strlen(s) + 1; + if (len + hstate_cmdline_index > sizeof(hstate_cmdline_buf)) + return -EINVAL; + + p =3D &hstate_cmdline_buf[hstate_cmdline_index]; + memcpy(p, s, len); + hstate_cmdline_index +=3D len; + + hugetlb_params[hugetlb_param_index].val =3D p; + hugetlb_params[hugetlb_param_index].setup =3D setup; + + hugetlb_param_index++; + + return 0; +} + +static __init void hugetlb_parse_params(void) +{ + int i; + struct hugetlb_cmdline *hcp; + + for (i =3D 0; i < hugetlb_param_index; i++) { + hcp =3D &hugetlb_params[i]; + + hcp->setup(hcp->val); + } +} + /* * hugepages command line processing * hugepages normally follows a valid hugepagsz or default_hugepagsz @@ -4595,7 +4664,7 @@ static int __init hugepages_setup(char *s) if (!parsed_valid_hugepagesz) { pr_warn("HugeTLB: hugepages=3D%s does not follow a valid hugepagesz, ign= oring\n", s); parsed_valid_hugepagesz =3D true; - return 1; + return -EINVAL; } =20 /* @@ -4649,24 +4718,16 @@ static int __init hugepages_setup(char *s) } } =20 - /* - * Global state is always initialized later in hugetlb_init. - * But we need to allocate gigantic hstates here early to still - * use the bootmem allocator. - */ - if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate)) - hugetlb_hstate_alloc_pages(parsed_hstate); - last_mhp =3D mhp; =20 - return 1; + return 0; =20 invalid: pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p); hugepages_clear_pages_in_node(); - return 1; + return -EINVAL; } -__setup("hugepages=3D", hugepages_setup); +hugetlb_early_param("hugepages", hugepages_setup); =20 /* * hugepagesz command line processing @@ -4685,7 +4746,7 @@ static int __init hugepagesz_setup(char *s) =20 if (!arch_hugetlb_valid_size(size)) { pr_err("HugeTLB: unsupported hugepagesz=3D%s\n", s); - return 1; + return -EINVAL; } =20 h =3D size_to_hstate(size); @@ -4700,7 +4761,7 @@ static int __init hugepagesz_setup(char *s) if (!parsed_default_hugepagesz || h !=3D &default_hstate || default_hstate.max_huge_pages) { pr_warn("HugeTLB: hugepagesz=3D%s specified twice, ignoring\n", s); - return 1; + return -EINVAL; } =20 /* @@ -4710,14 +4771,14 @@ static int __init hugepagesz_setup(char *s) */ parsed_hstate =3D h; parsed_valid_hugepagesz =3D true; - return 1; + return 0; } =20 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); parsed_valid_hugepagesz =3D true; - return 1; + return 0; } -__setup("hugepagesz=3D", hugepagesz_setup); +hugetlb_early_param("hugepagesz", hugepagesz_setup); =20 /* * default_hugepagesz command line input @@ -4731,14 +4792,14 @@ static int __init default_hugepagesz_setup(char *s) parsed_valid_hugepagesz =3D false; if (parsed_default_hugepagesz) { pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n"= , s); - return 1; + return -EINVAL; } =20 size =3D (unsigned long)memparse(s, NULL); =20 if (!arch_hugetlb_valid_size(size)) { pr_err("HugeTLB: unsupported default_hugepagesz=3D%s\n", s); - return 1; + return -EINVAL; } =20 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); @@ -4755,17 +4816,33 @@ static int __init default_hugepagesz_setup(char *s) */ if (default_hstate_max_huge_pages) { default_hstate.max_huge_pages =3D default_hstate_max_huge_pages; - for_each_online_node(i) - default_hstate.max_huge_pages_node[i] =3D - default_hugepages_in_node[i]; - if (hstate_is_gigantic(&default_hstate)) - hugetlb_hstate_alloc_pages(&default_hstate); + /* + * Since this is an early parameter, we can't check + * NUMA node state yet, so loop through MAX_NUMNODES. + */ + for (i =3D 0; i < MAX_NUMNODES; i++) { + if (default_hugepages_in_node[i] !=3D 0) + default_hstate.max_huge_pages_node[i] =3D + default_hugepages_in_node[i]; + } default_hstate_max_huge_pages =3D 0; } =20 - return 1; + return 0; +} +hugetlb_early_param("default_hugepagesz", default_hugepagesz_setup); + +void __init hugetlb_bootmem_alloc(void) +{ + struct hstate *h; + + hugetlb_parse_params(); + + for_each_hstate(h) { + if (hstate_is_gigantic(h)) + hugetlb_hstate_alloc_pages(h); + } } -__setup("default_hugepagesz=3D", default_hugepagesz_setup); =20 static unsigned int allowed_mems_nr(struct hstate *h) { diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 7735972add01..5b484758f813 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -444,7 +444,11 @@ DEFINE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key); EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key); =20 static bool vmemmap_optimize_enabled =3D IS_ENABLED(CONFIG_HUGETLB_PAGE_OP= TIMIZE_VMEMMAP_DEFAULT_ON); -core_param(hugetlb_free_vmemmap, vmemmap_optimize_enabled, bool, 0); +static int __init hugetlb_vmemmap_optimize_param(char *buf) +{ + return kstrtobool(buf, &vmemmap_optimize_enabled); +} +early_param("hugetlb_free_vmemmap", hugetlb_vmemmap_optimize_param); =20 static int __hugetlb_vmemmap_restore_folio(const struct hstate *h, struct folio *folio, unsigned long flags) diff --git a/mm/mm_init.c b/mm/mm_init.c index 2630cc30147e..d2dee53e95dd 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "internal.h" #include "slab.h" #include "shuffle.h" @@ -2641,6 +2642,8 @@ static void __init mem_init_print_info(void) */ void __init mm_core_init(void) { + hugetlb_bootmem_alloc(); + /* Initializations relying on SMP setup */ BUILD_BUG_ON(MAX_ZONELISTS > 2); build_all_zonelists(NULL); --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D910E1AA1C9 for ; Thu, 6 Feb 2025 18:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867895; cv=none; b=sowRZ3tQD/QPPoLN8NtOlQmC6ayLgRUFF+psSPciuSjHRDm5R+Lcp1WlKHUUcBe2tzQ36QFoSWmpt9Gg2jkPL2O9LWTj9eO5YDMkZAmpCP9aEEn5ZdqAxZjDZj/fGSOcwuZ+BcaUS9i8XdAkH80UzPeTPa/aSo/mTHn6CUMgblc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867895; c=relaxed/simple; bh=4I6z/LNscwuvV7r5B+sccqHhY2UFpLlyeFFA+UED/14=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B9Hl0ciJFGvczxMHnVvAIAusBODAiUnySN6Kt8DnUzpKXXRrJ1eop53ghLSwZ11CGw+Bhloy4PjxSTo5KMpzQzwk02d92IktyWqm4Wvm9ElFSoL5FDfz5JIYODIISWmTrV8oeUaWpD8V3rLOvMzOq93B4KOTmd/eD3L64TqtOuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gQUUEdgE; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gQUUEdgE" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f9e3a00fcdso2787449a91.0 for ; Thu, 06 Feb 2025 10:51:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867891; x=1739472691; darn=vger.kernel.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=Avpy7BuEP02ffYq1Owfnvcdb9scYtoUfsZ01E274s1M=; b=gQUUEdgESUAh1mRws4vXsuHyHKSn8XtZfWOwrDA1t5XQ+XiQNgkK6nlMICVVZALt5f 7djR2LQ5ZTUmUr05w6XFmV/ZTE2Cf88Zu29me7Ie3+dV006Np6A+VGfciQnq2XE51upG XkxMnubQxzOqvWctg9pcfd20nfVqn+MoJtGijYr84DpqV9th97Lcg/DoIetJ2UaaPB/L XgBwgY6Owob0spAcXwf+5pg1a3Vgfdt40EoHO5LFsu4GwdZvQQ+jbMqKrkmUmbCLp5Q+ Ly6y9Dr43Yffnsn88v58pinzNiAQW1Dw9DJrzXB6b5C8bjHtyj4fgndogttYxJkSwEY1 kGEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867891; x=1739472691; 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=Avpy7BuEP02ffYq1Owfnvcdb9scYtoUfsZ01E274s1M=; b=o9dL/7ocfrrykGBS5A9IjOxQKqLNBtLEVIKf0NGOFcy/07+wwJOR7onEoyzkCGcmFx wFWPPDjHMMvOvxbDXnWMzdq7oEUD7ZYbJM5JV7CT3mNPQw2RtJ71GsRsEYtXM2IJFx08 LGPbAMMXLyeOeEuFFcIyTTV//7/ewfrfszLM5GKnX7B2aoabwotA5/tMNz2iDVOkTxwp usvmvZLqc7Ah+bkmQNUukjLRKY2mQkq0xyMyMz8X2ms0dH8yu9wu+gZ7vAOzeVK6i1rb eVVlDyuByVCrPVDHkMRhNX5I3msrLLDW5ui7iKEn6qCXBkkir1KyOo7UY+LYK51I4ZwC 2A8g== X-Forwarded-Encrypted: i=1; AJvYcCU1U8+bCSkPbWvLttFjMXS/Y7sv0hMGDN7QKTbLMtuaJJ70BVLOeYXkbp2w2LlHdYjPZgr03AHOhPE+Et0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0YJBtB4NUcDF1n0lNWiWp9VXFeLtSZMRJd1odlKKMfw7UrE3L duRTGw7mzuBLXQUYTRQGrmu/KfrZfWZox8G70biw6/u+Co/jJh+AUbeJrlv5F6CNVDHUHg== X-Google-Smtp-Source: AGHT+IFX3NvACBJ1xhrHGkex/mQRvyXzqKTeTXis5hJniSsUBBE3cj7dQzcaYViSr3/nKGrkfs3yIvZW X-Received: from pjbpq3.prod.google.com ([2002:a17:90b:3d83:b0:2ef:701e:21c1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:510e:b0:2ee:8430:b831 with SMTP id 98e67ed59e1d1-2fa23f55c3bmr201823a91.2.1738867891288; Thu, 06 Feb 2025 10:51:31 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:49 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-10-fvdl@google.com> Subject: [PATCH v3 09/28] x86/mm: make register_page_bootmem_memmap handle PTE mappings 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 , Dave Hansen , Andy Lutomirski , Peter Zijlstra Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" register_page_bootmem_memmap expects that vmemmap pages handed to it are PMD-mapped, and that the number of pages to call get_page_bootmem on is PMD-aligned. This is currently a correct assumption, but will no longer be true once pre-HVO of hugetlb pages is implemented. Make it handle PTE-mapped vmemmap pages and a nr_pages argument that is not necessarily PAGES_PER_SECTION. Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Signed-off-by: Frank van der Linden --- arch/x86/mm/init_64.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 01ea7c6df303..e7572af639a4 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1599,11 +1599,12 @@ void register_page_bootmem_memmap(unsigned long sec= tion_nr, } get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO); =20 - if (!boot_cpu_has(X86_FEATURE_PSE)) { + pmd =3D pmd_offset(pud, addr); + if (pmd_none(*pmd)) + continue; + + if (!boot_cpu_has(X86_FEATURE_PSE) || !pmd_leaf(*pmd)) { next =3D (addr + PAGE_SIZE) & PAGE_MASK; - pmd =3D pmd_offset(pud, addr); - if (pmd_none(*pmd)) - continue; get_page_bootmem(section_nr, pmd_page(*pmd), MIX_SECTION_INFO); =20 @@ -1614,12 +1615,7 @@ void register_page_bootmem_memmap(unsigned long sect= ion_nr, SECTION_INFO); } else { next =3D pmd_addr_end(addr, end); - - pmd =3D pmd_offset(pud, addr); - if (pmd_none(*pmd)) - continue; - - nr_pmd_pages =3D 1 << get_order(PMD_SIZE); + nr_pmd_pages =3D (next - addr) >> PAGE_SHIFT; page =3D pmd_page(*pmd); while (nr_pmd_pages--) get_page_bootmem(section_nr, page++, --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 630B61A01CC for ; Thu, 6 Feb 2025 18:51:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867894; cv=none; b=ZWcAkKG70pxkeLnei5Oh2tEAD0y73x8mYtDiQq2LBlGDYLmCL44NNsSchkiQy2jIZlUOEwfxEqq4Sp8oCTvBvL+0S7kOT05NEe54VfcW0nmvmnE59TgwKRV1If4ZiPWnNZOwmwBo5r3ThModPLxd6hfFzptEXCHV3/8DA2mm3NQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867894; c=relaxed/simple; bh=l5bPVm7m9Up4DS/ePsgdGmNzbRXRNd7MVW6ZKnyXIow=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s9LjslkGuxSLuIBHphHuBSwhGUi9k82UvAi5bbLIDkSsnSmML9SlEn4os6ozIcxTzXlH1Z10A4BfITk5x755ENI9UDrDY28Qk1IFnaQb7tsC3P+CYvOEoPJTTwkP7pdnC67dfrihV3JS32ANDbNujyZEXbQMcVdtSVb+YvkjEdM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PMPRDgzy; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PMPRDgzy" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f83e54432dso3847098a91.2 for ; Thu, 06 Feb 2025 10:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867893; x=1739472693; darn=vger.kernel.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=ZM2K7Fz2wY4XMJIGW8UIEY9GhsBMo7TuugczH48eoOc=; b=PMPRDgzyUg5N4ZExgVnHTuEFu9A3XK67IXLt7pKEOhdHKrLW0zRsfZ3ZhjpO6KxYtX 5VQOFnFgM0ZzqZPQhE+DraJfPbf8Nk3sjgaBkAdxjhEcEPpXfzT1vC3NdgYNd/43lGS0 EggksKCjFa/h5hVjmh/oaSHyog31QxYmt/4BNPmDZajRgpd1DnM6ADOhN8UDrCS7xOao Hraoe4GIfInKf0Q6IxXRMVnNt9neCswXP1V8oLZnYCRw5vdH7MUPCE2Hp++thDx3LZtM RQFM1kR+USdc2KsSeVFQFNfCc/ik1Hf+TIKc4SP5XSGG5mdcaJNnCcubW9Y6rba3YPuB ci3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867893; x=1739472693; 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=ZM2K7Fz2wY4XMJIGW8UIEY9GhsBMo7TuugczH48eoOc=; b=MXqPt6sy3v/y2w08D6dfLjg7izE3UggTKeP8VadD5SRh6yBAms9elQgjkhA9SsHHLb ESumV5CuzediPC9JzRVrinRGgm1Yewxzx3MZ2PlaoTC7eGe3u5AbRntI2Y/5wSIHUJgs 2aMv8IMBbtXyK0i7vC2sopDeXN1ENRzOBGCq+2aAY3Fae0ciDtnfOuIEXIby1Oh9yOia AeYbnFZKVp2D935FEKbt0wIk1q+mGKYkf9UxoFjmEyToM2KeDKdCrZ35QlWEvTbSapqO FrnARmlfE4+jK29nZ2H/83lGROuUwnVnxOpbVLMGOSw+PT8wees7GWyUizQZ+z8ew8M7 ro2Q== X-Forwarded-Encrypted: i=1; AJvYcCXzc/MMb2d5eqDRyfLmjWi+KdDkqZKQmcw4uzSq6ZngQbbjKhqhEobEzq5hyyCzAouQoebbt6HF2maWCug=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9/t0UhjYxzSsr7FtM0hli206I2bHdWTeoocVxoz7LfZ0YTK/T eyhNx7drpiAfm4Ds5IeDB5XaAXmc75Zk4Gb3HRzryDkfmoNFec2Tea6a7qTB11JSqJo1IQ== X-Google-Smtp-Source: AGHT+IFVHLzvQXhhOOlHiOL7BAHshrKvK37YtrOlJQtEUP1k+IFRDA6RXf7GAghzr6oe2lWRYNCvHguW X-Received: from pfbha22.prod.google.com ([2002:a05:6a00:8516:b0:728:e76c:253f]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:84c:b0:72d:80da:aff with SMTP id d2e1a72fcca58-7305d463bb9mr573392b3a.9.1738867892913; Thu, 06 Feb 2025 10:51:32 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:50 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-11-fvdl@google.com> Subject: [PATCH v3 10/28] mm/bootmem_info: export register_page_bootmem_memmap 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If other mm code wants to use this function for early memmap inialization (on the platforms that have it), it should be made available properly, not just unconditionally in mm.h Make this function available for such cases. Signed-off-by: Frank van der Linden --- arch/powerpc/mm/init_64.c | 4 ++++ include/linux/bootmem_info.h | 7 +++++++ include/linux/mm.h | 3 --- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/mm/init_64.c b/arch/powerpc/mm/init_64.c index d96bbc001e73..b6f3ae03ca9e 100644 --- a/arch/powerpc/mm/init_64.c +++ b/arch/powerpc/mm/init_64.c @@ -41,6 +41,7 @@ #include #include #include +#include =20 #include #include @@ -386,10 +387,13 @@ void __ref vmemmap_free(unsigned long start, unsigned= long end, } =20 #endif + +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void register_page_bootmem_memmap(unsigned long section_nr, struct page *start_page, unsigned long size) { } +#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ =20 #endif /* CONFIG_SPARSEMEM_VMEMMAP */ =20 diff --git a/include/linux/bootmem_info.h b/include/linux/bootmem_info.h index d8a8d245824a..4c506e76a808 100644 --- a/include/linux/bootmem_info.h +++ b/include/linux/bootmem_info.h @@ -18,6 +18,8 @@ enum bootmem_type { =20 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void __init register_page_bootmem_info_node(struct pglist_data *pgdat); +void register_page_bootmem_memmap(unsigned long section_nr, struct page *m= ap, + unsigned long nr_pages); =20 void get_page_bootmem(unsigned long info, struct page *page, enum bootmem_type type); @@ -58,6 +60,11 @@ static inline void register_page_bootmem_info_node(struc= t pglist_data *pgdat) { } =20 +static inline void register_page_bootmem_memmap(unsigned long section_nr, + struct page *map, unsigned long nr_pages) +{ +} + static inline void put_page_bootmem(struct page *page) { } diff --git a/include/linux/mm.h b/include/linux/mm.h index 7b1068ddcbb7..6dfc41b461af 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3918,9 +3918,6 @@ static inline bool vmemmap_can_optimize(struct vmem_a= ltmap *altmap, } #endif =20 -void register_page_bootmem_memmap(unsigned long section_nr, struct page *m= ap, - unsigned long nr_pages); - enum mf_flags { MF_COUNT_INCREASED =3D 1 << 0, MF_ACTION_REQUIRED =3D 1 << 1, --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43ECA1A0739 for ; Thu, 6 Feb 2025 18:51:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867897; cv=none; b=EU8AyOUKxjpfimcVHjB4ZH7TqtxgJofRwx2laOpTCZaWH4KKf38cFWY+GXzxbgl9Cr34QjleIyvFpcRVPWTynrgLkVnaXmsJLLzCXmQLfS5xMMLCZeKgwQlbqM1zQvoY1/Avj2UEWGKxJi9PhVYKVgAtg0P93t7c0xZImHLWBfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867897; c=relaxed/simple; bh=oMu1tGyn53Ze4K24SStlgpi0I/+/y0r77QLaVO4C+vs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SFF1HAveZxhyqx1xGTJgI+YIU+fDBySeNtZ39IEmnNOk7pBdPF/4QkdyYnJjLYStzE24PsJjAHGX0JdB0u3eLjp1fnIWFp9lo3SpQpz3l117Qjgfd9qLkXAh7wIlTTql/y4uBc+sK3aOpdkgKoi7LE/0JQZkrMLp0Er3tLA+dTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=q9hN6QvP; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="q9hN6QvP" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa0204ad21so2418453a91.0 for ; Thu, 06 Feb 2025 10:51:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867894; x=1739472694; darn=vger.kernel.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=laTVDvhr5jKOxEf5d1BFEkjR6DjNz8pI1Rzto2J5iEQ=; b=q9hN6QvP36Y2MbOMWCFF7VSBqkEhfZu40X0Dtfi7I7hGiQcxZ8QbZzcF+qGQkoR4XO 1aiC4vpW16GewdmkRQlcHc6YwXt1xTWEwH6Ruq2uNMrqgp1uT25sOxEWvoetph67CkGJ HRdQbH1O+lhtDb/IPF8wx02IU2HyncKUYag+MZIe3c9sePMmOmTSlCWe9JnajX1K02MV 2dHvCxLCHSd3QzCDqBtF0Fv1+oj3qut0kUS9cUbKLmyRjqCs9pTcj0UfH2gD/JQJGKdH InzFZ4uMM88v7jmZQvAeLR354kuhjvTdflPiPnLisiCrMh+ukBohOXvwwB+l6Fvk6ZXT uFNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867894; x=1739472694; 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=laTVDvhr5jKOxEf5d1BFEkjR6DjNz8pI1Rzto2J5iEQ=; b=qTDtDB23FGmZiyub+4UvPC+RkZyDd5hdc+08hUrOW+mtHfumW6g/qyHgI0yIuyc53M YsANJehIPDw45PotD1++mYbEAChwF/GxvbLkh1p6u4+5kuP/vGLpBeutB7wvMLtUxi2I TBSKzSPIAnXC0JcArDUicCA2lDEjRwUhaOex/ZoyHuxkP0kWsTsPtjW6rxlxW7u6yfy1 jYlmA44+tZnuQ67Eah0CGYkAo6ztVbK+ThEINyTNSbwbXf5+GV0fsHWjPnH7Pwmpoq3q aNIhUOKB3QdwJn+RIfrntPGhqgRnqblRkFZiNCgjz4VDNYv5JfxQcubqGAhfT4wgMXNU bw+w== X-Forwarded-Encrypted: i=1; AJvYcCWeA0t/qF1WYhYXyrp6DCZOUhI+9wsMnIgEPPILx+0H9TuA9AIMf461el9ePG/21Qh62Iis6piK92lAifs=@vger.kernel.org X-Gm-Message-State: AOJu0Yyi94QbOEqg9f7L9gGvyz5C23tOQ9lCamidiUxkIGEDTZfUHsr0 DN3+RhQxtCJr+JShFy+MGGHz/peEnGlOyxpupNIO5GCN1nhAugm6mQjlwRW2BqXZxdQZBQ== X-Google-Smtp-Source: AGHT+IFWXu22XDiYyc0nTMJwiopxxnqaKl40GlxcarOlGhTscpEPLMqJOzkRyOrxdabf2F7/pMajnLN0 X-Received: from pjur16.prod.google.com ([2002:a17:90a:d410:b0:2f2:e5c9:de99]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:38d2:b0:2ee:48bf:7dc3 with SMTP id 98e67ed59e1d1-2fa241646afmr193581a91.15.1738867894502; Thu, 06 Feb 2025 10:51:34 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:51 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-12-fvdl@google.com> Subject: [PATCH v3 11/28] mm/sparse: allow for alternate vmemmap section init at boot 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add functions that are called just before the per-section memmap is initialized and just before the memmap page structures are initialized. They are called sparse_vmemmap_init_nid_early and sparse_vmemmap_init_nid_late, respectively. This allows for mm subsystems to add calls to initialize memmap and page structures in a specific way, if using SPARSEMEM_VMEMMAP. Specifically, hugetlb can pre-HVO bootmem allocated pages that way, so that no time and resources are wasted on allocating vmemmap pages, only to free them later (and possibly unnecessarily running the system out of memory in the process). Refactor some code and export a few convenience functions for external use. In sparse_init_nid, skip any sections that are already initialized, e.g. they have been initialized by sparse_vmemmap_init_nid_early already. The hugetlb code to use these functions will be added in a later commit. Export section_map_size, as any alternate memmap init code will want to use it. THe config option to enable this is SPARSEMEM_VMEMMAP_PREINIT, which is dependent on and architecture-specific option, ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT. This is done because a section flag is used, and the number of flags available is architecture-dependent (see mmzone.h). Architecures can decide if there is room for the flag and enable the option. Fortunately, as of right now, all sparse vmemmap using architectures do have room. Signed-off-by: Frank van der Linden --- include/linux/mm.h | 1 + include/linux/mmzone.h | 35 +++++++++++++++++ mm/Kconfig | 8 ++++ mm/bootmem_info.c | 4 +- mm/mm_init.c | 3 ++ mm/sparse-vmemmap.c | 23 +++++++++++ mm/sparse.c | 87 ++++++++++++++++++++++++++++++++---------- 7 files changed, 139 insertions(+), 22 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 6dfc41b461af..df83653ed6e3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3828,6 +3828,7 @@ static inline void print_vma_addr(char *prefix, unsig= ned long rip) #endif =20 void *sparse_buffer_alloc(unsigned long size); +unsigned long section_map_size(void); struct page * __populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap); diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 9540b41894da..44ecb2f90db4 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1933,6 +1933,9 @@ enum { SECTION_IS_EARLY_BIT, #ifdef CONFIG_ZONE_DEVICE SECTION_TAINT_ZONE_DEVICE_BIT, +#endif +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT + SECTION_IS_VMEMMAP_PREINIT_BIT, #endif SECTION_MAP_LAST_BIT, }; @@ -1944,6 +1947,9 @@ enum { #ifdef CONFIG_ZONE_DEVICE #define SECTION_TAINT_ZONE_DEVICE BIT(SECTION_TAINT_ZONE_DEVICE_BIT) #endif +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT +#define SECTION_IS_VMEMMAP_PREINIT BIT(SECTION_IS_VMEMMAP_PREINIT_BIT) +#endif #define SECTION_MAP_MASK (~(BIT(SECTION_MAP_LAST_BIT) - 1)) #define SECTION_NID_SHIFT SECTION_MAP_LAST_BIT =20 @@ -1998,6 +2004,30 @@ static inline int online_device_section(struct mem_s= ection *section) } #endif =20 +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT +static inline int preinited_vmemmap_section(struct mem_section *section) +{ + return (section && + (section->section_mem_map & SECTION_IS_VMEMMAP_PREINIT)); +} + +void sparse_vmemmap_init_nid_early(int nid); +void sparse_vmemmap_init_nid_late(int nid); + +#else +static inline int preinited_vmemmap_section(struct mem_section *section) +{ + return 0; +} +static inline void sparse_vmemmap_init_nid_early(int nid) +{ +} + +static inline void sparse_vmemmap_init_nid_late(int nid) +{ +} +#endif + static inline int online_section_nr(unsigned long nr) { return online_section(__nr_to_section(nr)); @@ -2035,6 +2065,9 @@ static inline int pfn_section_valid(struct mem_sectio= n *ms, unsigned long pfn) } #endif =20 +void sparse_init_early_section(int nid, struct page *map, unsigned long pn= um, + unsigned long flags); + #ifndef CONFIG_HAVE_ARCH_PFN_VALID /** * pfn_valid - check if there is a valid memory map entry for a PFN @@ -2116,6 +2149,8 @@ void sparse_init(void); #else #define sparse_init() do {} while (0) #define sparse_index_init(_sec, _nid) do {} while (0) +#define sparse_vmemmap_init_nid_early(_nid, _use) do {} while (0) +#define sparse_vmemmap_init_nid_late(_nid) do {} while (0) #define pfn_in_present_section pfn_valid #define subsection_map_init(_pfn, _nr_pages) do {} while (0) #endif /* CONFIG_SPARSEMEM */ diff --git a/mm/Kconfig b/mm/Kconfig index 1b501db06417..f984dd928ce7 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -489,6 +489,14 @@ config SPARSEMEM_VMEMMAP SPARSEMEM_VMEMMAP uses a virtually mapped memmap to optimise pfn_to_page and page_to_pfn operations. This is the most efficient option when sufficient kernel resources are available. + +config ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT + bool + +config SPARSEMEM_VMEMMAP_PREINIT + bool "Early init of sparse memory virtual memmap" + depends on SPARSEMEM_VMEMMAP && ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT + default y # # Select this config option from the architecture Kconfig, if it is prefer= red # to enable the feature of HugeTLB/dev_dax vmemmap optimization. diff --git a/mm/bootmem_info.c b/mm/bootmem_info.c index 95f288169a38..b0e2a9fa641f 100644 --- a/mm/bootmem_info.c +++ b/mm/bootmem_info.c @@ -88,7 +88,9 @@ static void __init register_page_bootmem_info_section(uns= igned long start_pfn) =20 memmap =3D sparse_decode_mem_map(ms->section_mem_map, section_nr); =20 - register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); + if (!preinited_vmemmap_section(ms)) + register_page_bootmem_memmap(section_nr, memmap, + PAGES_PER_SECTION); =20 usage =3D ms->usage; page =3D virt_to_page(usage); diff --git a/mm/mm_init.c b/mm/mm_init.c index d2dee53e95dd..9f1e41c3dde6 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1862,6 +1862,9 @@ void __init free_area_init(unsigned long *max_zone_pf= n) } } =20 + for_each_node_state(nid, N_MEMORY) + sparse_vmemmap_init_nid_late(nid); + calc_nr_kernel_pages(); memmap_init(); =20 diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 3287ebadd167..8751c46c35e4 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -470,3 +470,26 @@ struct page * __meminit __populate_section_memmap(unsi= gned long pfn, =20 return pfn_to_page(pfn); } + +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT +/* + * This is called just before initializing sections for a NUMA node. + * Any special initialization that needs to be done before the + * generic initialization can be done from here. Sections that + * are initialized in hooks called from here will be skipped by + * the generic initialization. + */ +void __init sparse_vmemmap_init_nid_early(int nid) +{ +} + +/* + * This is called just before the initialization of page structures + * through memmap_init. Zones are now initialized, so any work that + * needs to be done that needs zone information can be done from + * here. + */ +void __init sparse_vmemmap_init_nid_late(int nid) +{ +} +#endif diff --git a/mm/sparse.c b/mm/sparse.c index 133b033d0cba..ee0234a77c7f 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -408,13 +408,13 @@ static void __init check_usemap_section_nr(int nid, #endif /* CONFIG_MEMORY_HOTREMOVE */ =20 #ifdef CONFIG_SPARSEMEM_VMEMMAP -static unsigned long __init section_map_size(void) +unsigned long __init section_map_size(void) { return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE); } =20 #else -static unsigned long __init section_map_size(void) +unsigned long __init section_map_size(void) { return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION); } @@ -495,6 +495,44 @@ void __weak __meminit vmemmap_populate_print_last(void) { } =20 +static void *sparse_usagebuf __meminitdata; +static void *sparse_usagebuf_end __meminitdata; + +/* + * Helper function that is used for generic section initialization, and + * can also be used by any hooks added above. + */ +void __init sparse_init_early_section(int nid, struct page *map, + unsigned long pnum, unsigned long flags) +{ + BUG_ON(!sparse_usagebuf || sparse_usagebuf >=3D sparse_usagebuf_end); + check_usemap_section_nr(nid, sparse_usagebuf); + sparse_init_one_section(__nr_to_section(pnum), pnum, map, + sparse_usagebuf, SECTION_IS_EARLY | flags); + sparse_usagebuf =3D (void *)sparse_usagebuf + mem_section_usage_size(); +} + +static int __init sparse_usage_init(int nid, unsigned long map_count) +{ + unsigned long size; + + size =3D mem_section_usage_size() * map_count; + sparse_usagebuf =3D sparse_early_usemaps_alloc_pgdat_section( + NODE_DATA(nid), size); + if (!sparse_usagebuf) { + sparse_usagebuf_end =3D NULL; + return -ENOMEM; + } + + sparse_usagebuf_end =3D sparse_usagebuf + size; + return 0; +} + +static void __init sparse_usage_fini(void) +{ + sparse_usagebuf =3D sparse_usagebuf_end =3D NULL; +} + /* * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_= end) * And number of present sections in this node is map_count. @@ -503,47 +541,54 @@ static void __init sparse_init_nid(int nid, unsigned = long pnum_begin, unsigned long pnum_end, unsigned long map_count) { - struct mem_section_usage *usage; unsigned long pnum; struct page *map; + struct mem_section *ms; =20 - usage =3D sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid), - mem_section_usage_size() * map_count); - if (!usage) { + if (sparse_usage_init(nid, map_count)) { pr_err("%s: node[%d] usemap allocation failed", __func__, nid); goto failed; } + sparse_buffer_init(map_count * section_map_size(), nid); + + sparse_vmemmap_init_nid_early(nid); + for_each_present_section_nr(pnum_begin, pnum) { unsigned long pfn =3D section_nr_to_pfn(pnum); =20 if (pnum >=3D pnum_end) break; =20 - map =3D __populate_section_memmap(pfn, PAGES_PER_SECTION, - nid, NULL, NULL); - if (!map) { - pr_err("%s: node[%d] memory map backing failed. Some memory will not be= available.", - __func__, nid); - pnum_begin =3D pnum; - sparse_buffer_fini(); - goto failed; + ms =3D __nr_to_section(pnum); + if (!preinited_vmemmap_section(ms)) { + map =3D __populate_section_memmap(pfn, PAGES_PER_SECTION, + nid, NULL, NULL); + if (!map) { + pr_err("%s: node[%d] memory map backing failed. Some memory will not b= e available.", + __func__, nid); + pnum_begin =3D pnum; + sparse_usage_fini(); + sparse_buffer_fini(); + goto failed; + } + sparse_init_early_section(nid, map, pnum, 0); } - check_usemap_section_nr(nid, usage); - sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage, - SECTION_IS_EARLY); - usage =3D (void *) usage + mem_section_usage_size(); } + sparse_usage_fini(); sparse_buffer_fini(); return; failed: - /* We failed to allocate, mark all the following pnums as not present */ + /* + * We failed to allocate, mark all the following pnums as not present, + * except the ones already initialized earlier. + */ for_each_present_section_nr(pnum_begin, pnum) { - struct mem_section *ms; - if (pnum >=3D pnum_end) break; ms =3D __nr_to_section(pnum); + if (!preinited_vmemmap_section(ms)) + ms->section_mem_map =3D 0; ms->section_mem_map =3D 0; } } --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E06991B21AA for ; Thu, 6 Feb 2025 18:51:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867898; cv=none; b=l3ChW1J7TsGCs2/kjt9lKecNWjHxGL8GkizHXxVkSM8GUNFYAgzk8GU0NBYZaRmkZ6wackBAgxU3xfnNInoFFyD8xlQ3CktA9KsQMX/YQEnpCnVXsuQZhVFiMSNdePgGIVFoGTNfhghXJecPPJaXIMb5Ykw5oZ/en30J1HCzBEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867898; c=relaxed/simple; bh=AqKIWs9Sv7x0CLKfPLKlMM+0qtXjdKuonIonrMdi61g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PzAWpulgvrA1d/eStPvtNnPr/K/U/oXNqKcpuM2w/vf3eU/Pg34P946mvsm373lZYodLTt6crRGkBXpaCORFk1g9lK6nPEReNf3QzdtoRe++wxS6VmBbQrxh2xE5gz6ueMckueCaqud/7ax63F/HGOZIiff6t7vpmLZUTjmBSGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kneqMn/2; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kneqMn/2" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa0204ad21so2418521a91.0 for ; Thu, 06 Feb 2025 10:51:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867896; x=1739472696; darn=vger.kernel.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=3NeQMOnzxPkjyqTJozldr6OrEIjuFMWwE6WQJOgcCAA=; b=kneqMn/2slH6JL71S3C4ueXbD3jIQx/esMr0BXvc/eNjO0rY2ZQxmBV7c8hj3lilw6 W8svj10jDcuZE4yRfVQokZIlLnIIrFmkLrSBvEvLebsscPkVtEf2ztjNmgvU2eyG9kiC FaiYhWka9TSIt/VWjeW7UZmRs7fyc+Fxr8LTbeZJtWD6vZMdLr5iNum2P/+NDSLm3r2x NpC2rhnpBWRh5S/7hjKSBeBtHcW2V3N2qeN38CKZdFCbMobQxI/seloIUo+5nH7IyI8S k0sIvLi0gyVHIYNwsgqRHjAozfTwj+JfdfDKVUT+0pJ2+GztvUgXS6zjzlRK7c6dQ8Ze zTXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867896; x=1739472696; 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=3NeQMOnzxPkjyqTJozldr6OrEIjuFMWwE6WQJOgcCAA=; b=migna7MgsJ0ayWU3GRJLDpbp9feVpg20CHdvRZKJ19puTD3XnHT73a2cJrdKbvMYRO TaSjgIyfWzpG1LTsGVVxs4oDqOBu1I/xhxHIkMyNu8Cyi1BUgw+Sl82u/8H3Xchq4o4Z FHYQ0XdguocdiI9EjhK5wrEQ3UORUfI4yMcEoZn4nttqA0DM8VmjeVjGKMTDffg8UfNJ QtZyqXWVO8LPBb1d9te66QoPbOXj/+6WpnV4XYy3lz5qQ/lXxEswNpEZXnef4DvF6j2E RjCUkXe5ZwatYF3LBZYnjLYDAAP90nxj3oasbn5iznMyQVmWJlyeK5AXGih5rMnc/7gu Glfw== X-Forwarded-Encrypted: i=1; AJvYcCVsn2g3kUHa5nMJsLFiQGsb+qbEvQuy0Ygqf1aZaivvl9eA7WmvgGA0lCZaV31vCR492/dfg3o4xail/3I=@vger.kernel.org X-Gm-Message-State: AOJu0Yyjg46Hwhy0KtRbn1ls49vagGqAUrB9WHQ/9lfzw6Ig7vxCDrzl 4guw0824D1KN2X8BSugd/MkeImXP9JduJw+dceBW/ZGxkhodBMjfp8lH6rrCXpCz4f7Y1A== X-Google-Smtp-Source: AGHT+IH7ZN2LcrXXo7ZGhTfjqav29224bEfR3b4dopinjdsKyhAOEliIZ1YG+HBMVTQXtNzOsKEFpkdd X-Received: from pjuw5.prod.google.com ([2002:a17:90a:d605:b0:2ef:eb2f:ebe]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:f94e:b0:2f2:a664:df19 with SMTP id 98e67ed59e1d1-2fa23f436d6mr217716a91.7.1738867896108; Thu, 06 Feb 2025 10:51:36 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:52 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-13-fvdl@google.com> Subject: [PATCH v3 12/28] mm/hugetlb: set migratetype for bootmem folios 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The pageblocks that back memblock allocated hugetlb folios might not have the migrate type set, in the CONFIG_DEFERRED_STRUCT_PAGE_INIT case. memblock allocated hugetlb folios might be given to the buddy allocator eventually (if nr_hugepages is lowered), so make sure that the migrate type for the pageblocks contained in them is set when initializing them. Set it to the default that memmap init also uses (MIGRATE_MOVABLE). Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 5a4b322e2bb2..de8adfb487f4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3258,6 +3258,26 @@ static void __init hugetlb_folio_init_vmemmap(struct= folio *folio, prep_compound_head((struct page *)folio, huge_page_order(h)); } =20 +/* + * memblock-allocated pageblocks might not have the migrate type set + * if marked with the 'noinit' flag. Set it to the default (MIGRATE_MOVABL= E) + * here. + * + * Note that this will not write the page struct, it is ok (and necessary) + * to do this on vmemmap optimized folios. + */ +static void __init hugetlb_bootmem_init_migratetype(struct folio *folio, + struct hstate *h) +{ + unsigned long nr_pages =3D pages_per_huge_page(h), i; + + WARN_ON_ONCE(!pageblock_aligned(folio_pfn(folio))); + + for (i =3D 0; i < nr_pages; i +=3D pageblock_nr_pages) + set_pageblock_migratetype(folio_page(folio, i), + MIGRATE_MOVABLE); +} + static void __init prep_and_add_bootmem_folios(struct hstate *h, struct list_head *folio_list) { @@ -3279,6 +3299,7 @@ static void __init prep_and_add_bootmem_folios(struct= hstate *h, HUGETLB_VMEMMAP_RESERVE_PAGES, pages_per_huge_page(h)); } + hugetlb_bootmem_init_migratetype(folio, h); /* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave(&hugetlb_lock, flags); __prep_account_new_huge_page(h, folio_nid(folio)); --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94DCC1A08BC for ; Thu, 6 Feb 2025 18:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867900; cv=none; b=MbUUL0FL+fNnW5iOWa0ptpMvHQ3pEHgEeIBKR+l5BXigN/BCOTi0/7ksbZYzQdKSysu35LbfAFUUF3JUptsRSzz1FM8QmzYsmurJC8oWh8wuHaZO1CwAJAWX6MUIpw5PxlHfMUWsVkjdSWufNIg7iJYijSuyR9R54niaYJ2vQps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867900; c=relaxed/simple; bh=pwg6S22sqSel5O2JxWwl4u1ekaJhDyAQhSEgfU5URb8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=unMQYlmWRL+FYVI9PDOOVEpZvnQQ/z50aHZBnJM3E2kPcIdjIRWRsWrO6E+oe1JSxfOTV6nCgVI9J10PQygd+OVs7ZhM7jRE65Pn8V1oN/3wghgMNNqsONhfMwJ7x61P9DimxKXYz5R7VNewLKtEYQT56SYk+tDnl81G808/62I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wt6pZVVD; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wt6pZVVD" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fa11d8e448so2482729a91.0 for ; Thu, 06 Feb 2025 10:51:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867898; x=1739472698; darn=vger.kernel.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=N1nizw8LTgFJ6Hunu1baoXowLm72wInJnvRTI+vB3dI=; b=wt6pZVVD6ZWinfdoGf83MGFXO1KN2OGkFMUevNxVUsPfOVy9uPUYq083I5oMzLczRa 0V0uEZYXiPLKriEPSuOfD8z9bOh/1VTB+uycVVBzvqJV7fVXHSGKCnnV1GX18GiyjVV/ CYwGqRkujBcZsJAHjD+qjMSntzyC13u7PDTSSFM1aB8GGbXbmRdci1x+C/pY8j2N4Y9S RkolKd9FNU0AqUt8aY8XjHCbyX5Z3q+hiMXe1dA15R13zwM4UEQhUCP5qooM/0xLXbL8 xf5/Y79qg8sgUEJg5ypeYOBI/Ypx9s6hAjLQ7Dv7HJ3JxaVOrKxVtpuiiGccsiM234+1 F6bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867898; x=1739472698; 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=N1nizw8LTgFJ6Hunu1baoXowLm72wInJnvRTI+vB3dI=; b=B6ljYS0I7xa35tjLHgR2iY/XJDkXuoGXQuvkzCw1K+pEjNGzQNli3WDvv1yDReqAOF pag0Sit5CTqotyFL4eM0KzIpFXNUpxOM7coGdBgRYlzKahqytBBIiAQe+NWXZAVoOsHp f4T6c3eUQby+A5RSQ6jxN6emhdJ/JQmddGdS0MhKdQGzJaH23s3783368W1n0mpp2Cs1 dt9BaTQuJzj9QeQExo2zJl3w6mRAhdER6FtN8u4Hm2E+nsjSlhkQEbEVv5fJU/j/IuYD xopT4ZJ+D61yiZ6cMrzi0R52ZDxyUlM3Ur+1DccWXVvzsA57r1EKLxF7FXhvQB5qMFGn Y95A== X-Forwarded-Encrypted: i=1; AJvYcCVyCzMyjhi//Sqx/djhzM9vzAUYjGyzgGFDUKNzlilmzKdIQMY6fhF3kxM6aOywCLKBpwrR6YkUYXKrb5M=@vger.kernel.org X-Gm-Message-State: AOJu0Yzg41hHdh61xl1YQGg41zrmnZdCfkUlXDU4CX5M1rSVpz7Z4Qqc 9vtyo0Rdkk73ilzr0BqKMAvsJE3Musk56hjXGFigeYuJ7lEji9IJWJUIa5ZaXLIWvz6qaA== X-Google-Smtp-Source: AGHT+IHCvMErKFRJySbjcM7qJ570UaYkF9tu8lbCYFmm8LplntCVFlwxB6ni+y+lUDS7htgcQtwDW2+L X-Received: from pfwy2.prod.google.com ([2002:a05:6a00:1c82:b0:72f:f548:3e09]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2d27:b0:1e1:b105:87b with SMTP id adf61e73a8af0-1ee03a8f413mr928165637.23.1738867897906; Thu, 06 Feb 2025 10:51:37 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:53 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-14-fvdl@google.com> Subject: [PATCH v3 13/28] mm: define __init_reserved_page_zone 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sometimes page structs must be unconditionally initialized as reserved, regardless of DEFERRED_STRUCT_PAGE_INIT. Define a function, __init_reserved_page_zone, containing code that already did all of the work in init_reserved_page, and make it available for use. Signed-off-by: Frank van der Linden --- mm/internal.h | 1 + mm/mm_init.c | 38 +++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 109ef30fee11..57662141930e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1448,6 +1448,7 @@ static inline bool pte_needs_soft_dirty_wp(struct vm_= area_struct *vma, pte_t pte =20 void __meminit __init_single_page(struct page *page, unsigned long pfn, unsigned long zone, int nid); +void __meminit __init_reserved_page_zone(unsigned long pfn, int nid); =20 /* shrinker related functions */ unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memc= g, diff --git a/mm/mm_init.c b/mm/mm_init.c index 9f1e41c3dde6..925ed6564572 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -650,6 +650,28 @@ static inline void fixup_hashdist(void) static inline void fixup_hashdist(void) {} #endif /* CONFIG_NUMA */ =20 +/* + * Initialize a reserved page unconditionally, finding its zone first. + */ +void __meminit __init_reserved_page_zone(unsigned long pfn, int nid) +{ + pg_data_t *pgdat; + int zid; + + pgdat =3D NODE_DATA(nid); + + for (zid =3D 0; zid < MAX_NR_ZONES; zid++) { + struct zone *zone =3D &pgdat->node_zones[zid]; + + if (zone_spans_pfn(zone, pfn)) + break; + } + __init_single_page(pfn_to_page(pfn), pfn, zid, nid); + + if (pageblock_aligned(pfn)) + set_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_MOVABLE); +} + #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT static inline void pgdat_set_deferred_range(pg_data_t *pgdat) { @@ -708,24 +730,10 @@ defer_init(int nid, unsigned long pfn, unsigned long = end_pfn) =20 static void __meminit init_reserved_page(unsigned long pfn, int nid) { - pg_data_t *pgdat; - int zid; - if (early_page_initialised(pfn, nid)) return; =20 - pgdat =3D NODE_DATA(nid); - - for (zid =3D 0; zid < MAX_NR_ZONES; zid++) { - struct zone *zone =3D &pgdat->node_zones[zid]; - - if (zone_spans_pfn(zone, pfn)) - break; - } - __init_single_page(pfn_to_page(pfn), pfn, zid, nid); - - if (pageblock_aligned(pfn)) - set_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_MOVABLE); + __init_reserved_page_zone(pfn, nid); } #else static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D32E91C6FEE for ; Thu, 6 Feb 2025 18:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867901; cv=none; b=F06hpbLrtmtpsHO8b4mTSE1Urb1TUhE7zmdmGQqgkr4+404Pu9d+lJOrk/K4NBMDklEZoQHRBwdiUXZgMVoFSX55v8+K5wPUGjud36xAKGAL7ndfe1kNiga4Pj92VWDhpxUUOCunA+obNFCLiOQQvHHTbCZ8rIW70KN9yPY4hJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867901; c=relaxed/simple; bh=ffwQ8Tp8XZIYbaj9AWTKAMDSaGO9X6prNZKnNfvc2aw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JPRwp9qLgOa9wVHWv5DAQwIXP4fkz3zE+DKgmQCD45ZOeX3RVHY+2dwLzaEeQTGHufs5TAjG1ysNHMflasU/q4KjzjKsY35uPLY4pX+ks9QhoYcXtcJ3fM7TZ+jCgyQp86lVJWTU/iChEQJZARBMDyRDjV6D6Xk+d41Aj1vXQwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tsRds4cb; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tsRds4cb" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2166464e236so39892535ad.1 for ; Thu, 06 Feb 2025 10:51:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867899; x=1739472699; darn=vger.kernel.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=1XzNWdK53DK5kTMFUNWfsYU5y9JEDM1DtaDFYklZD4s=; b=tsRds4cbxjkJ15Czf0Tw6y8b93BNdWBL6IPwBtWcUV7wut+WrY9Cwg0OLzikO0cVMg 9eCY3ZhEeMaFc4N3LCVmkMzJyJWi46eQVnpcaXMgiCjuLXdUdpmVB9fxf+rTmvr4+joh kaTBIfzs9LGV3zKlu4YyvM1BwCC7FxXACfvz/KhC4OUa28FUAFxEaA7gOzp+aXnNHzFz rK/Mfjo/natMOi3W2VY2pDDw6T1DEMrL7we24/rRYoQ0u9zU6UWM/6BP8EhC3QC42nIS B3mT/Bw1BfwZ9hFdecWYul4+qIsL9lO4fYPUA68wnPXRY6XC+idUezk8bzHO6bvLgJ7K tmPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867899; x=1739472699; 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=1XzNWdK53DK5kTMFUNWfsYU5y9JEDM1DtaDFYklZD4s=; b=Eb49+jPPK9Wsk8msgny4MaDzI6H8IThoR0nTfWV9pGygzRBW5Euq0OmZuZU3N027QM /M9ZfbrmZmF0cdO+VSanKEp1GsETt4eas0BQBB4cqm7LfqHKGYNlTip98wGwsA1ZI3FF gut4v1737IaZRKKcj67XQjajO/RSrdiHmCPmT4wV+8tWaVeAldyeuFXc6en64zA00oHE P6uC2CZdGmnUbruqP5cCQKXd2p0EBAxnL5oAqofH5P4zhFrVOHc5GSkS1ewQ6eT3S9+r iOeu/iSlnscdgzx74nCNovFKrKbvQlX+oJiDqKU2a19hF6sey0E4FthERpeTmqzEtba3 3eBw== X-Forwarded-Encrypted: i=1; AJvYcCWFDVcIaMjARD7QjhCrzAQAP6yPaID5fK55uQtVbsTDAMSaGZU2wIH0Gsc51szMwz2VtZeg5P4qUHBEfb8=@vger.kernel.org X-Gm-Message-State: AOJu0YxDbghS0imBO/5pX6HSL8tiOzbsAGv1cX+DYpzreeVsl5f4JOQS Ps2TBflci2xilSZhDOqIEIWBBrDp6Nx59PfS0JFjfKmLEtTJJuA92x1C+ThSXIxHHab66Q== X-Google-Smtp-Source: AGHT+IGIkx9zow3SNmjwLwkWX732+NMszmOw5UwG52/2qtVEFt/yi4VJkAhAKbmBsmMF+ToCMr1/WRbP X-Received: from pgbdl12.prod.google.com ([2002:a05:6a02:d0c:b0:ad5:1a11:9dae]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a87:b0:1ed:a565:f980 with SMTP id adf61e73a8af0-1ee03bb030cmr937467637.27.1738867899243; Thu, 06 Feb 2025 10:51:39 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:54 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-15-fvdl@google.com> Subject: [PATCH v3 14/28] mm/hugetlb: check bootmem pages for zone intersections 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Bootmem hugetlb pages are allocated using memblock, which isn't (and mostly can't be) aware of zones. So, they may end up crossing zone boundaries. This would create confusion, a hugetlb page that is part of multiple zones is bad. Worse, HVO might then end up stealthily re-assigning pages to a different zone when a hugetlb page is freed, since the tail page structures beyond the first vmemmap page would inherit the zone of the first page structures. While the chance of this happening is low, you can definitely create a configuration where this happens (especially using ZONE_MOVABLE). To avoid this issue, check if bootmem hugetlb pages intersect with multiple zones during the gather phase, and discard them, handing them to the page allocator, if they do. Record the number of invalid bootmem pages per node and subtract them from the number of available pages at the end, making it easier to do these checks in multiple places later on. Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++-- mm/internal.h | 2 ++ mm/mm_init.c | 25 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index de8adfb487f4..789341158ef6 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -62,6 +62,7 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODE= S] __initdata; static unsigned long hugetlb_cma_size __initdata; =20 __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; +static unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE] __initdata; =20 /* * Due to ordering constraints across the init code for various @@ -3308,6 +3309,44 @@ static void __init prep_and_add_bootmem_folios(struc= t hstate *h, } } =20 +static bool __init hugetlb_bootmem_page_zones_valid(int nid, + struct huge_bootmem_page *m) +{ + unsigned long start_pfn; + bool valid; + + start_pfn =3D virt_to_phys(m) >> PAGE_SHIFT; + + valid =3D !pfn_range_intersects_zones(nid, start_pfn, + pages_per_huge_page(m->hstate)); + if (!valid) + hstate_boot_nrinvalid[hstate_index(m->hstate)]++; + + return valid; +} + +/* + * Free a bootmem page that was found to be invalid (intersecting with + * multiple zones). + * + * Since it intersects with multiple zones, we can't just do a free + * operation on all pages at once, but instead have to walk all + * pages, freeing them one by one. + */ +static void __init hugetlb_bootmem_free_invalid_page(int nid, struct page = *page, + struct hstate *h) +{ + unsigned long npages =3D pages_per_huge_page(h); + unsigned long pfn; + + while (npages--) { + pfn =3D page_to_pfn(page); + __init_reserved_page_zone(pfn, nid); + free_reserved_page(page); + page++; + } +} + /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. @@ -3315,14 +3354,25 @@ static void __init prep_and_add_bootmem_folios(stru= ct hstate *h, static void __init gather_bootmem_prealloc_node(unsigned long nid) { LIST_HEAD(folio_list); - struct huge_bootmem_page *m; + struct huge_bootmem_page *m, *tm; struct hstate *h =3D NULL, *prev_h =3D NULL; =20 - list_for_each_entry(m, &huge_boot_pages[nid], list) { + list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) { struct page *page =3D virt_to_page(m); struct folio *folio =3D (void *)page; =20 h =3D m->hstate; + if (!hugetlb_bootmem_page_zones_valid(nid, m)) { + /* + * Can't use this page. Initialize the + * page structures if that hasn't already + * been done, and give them to the page + * allocator. + */ + hugetlb_bootmem_free_invalid_page(nid, page, h); + continue; + } + /* * It is possible to have multiple huge page sizes (hstates) * in this list. If so, process each size separately. @@ -3594,13 +3644,20 @@ static void __init hugetlb_init_hstates(void) static void __init report_hugepages(void) { struct hstate *h; + unsigned long nrinvalid; =20 for_each_hstate(h) { char buf[32]; =20 + nrinvalid =3D hstate_boot_nrinvalid[hstate_index(h)]; + h->max_huge_pages -=3D nrinvalid; + string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", buf, h->free_huge_pages); + if (nrinvalid) + pr_info("HugeTLB: %s page size: %lu invalid page%s discarded\n", + buf, nrinvalid, nrinvalid > 1 ? "s" : ""); pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n", hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf); } diff --git a/mm/internal.h b/mm/internal.h index 57662141930e..63fda9bb9426 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -658,6 +658,8 @@ static inline struct page *pageblock_pfn_to_page(unsign= ed long start_pfn, } =20 void set_zone_contiguous(struct zone *zone); +bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, + unsigned long nr_pages); =20 static inline void clear_zone_contiguous(struct zone *zone) { diff --git a/mm/mm_init.c b/mm/mm_init.c index 925ed6564572..f7d5b4fe1ae9 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2287,6 +2287,31 @@ void set_zone_contiguous(struct zone *zone) zone->contiguous =3D true; } =20 +/* + * Check if a PFN range intersects multiple zones on one or more + * NUMA nodes. Specify the @nid argument if it is known that this + * PFN range is on one node, NUMA_NO_NODE otherwise. + */ +bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, + unsigned long nr_pages) +{ + struct zone *zone, *izone =3D NULL; + + for_each_zone(zone) { + if (nid !=3D NUMA_NO_NODE && zone_to_nid(zone) !=3D nid) + continue; + + if (zone_intersects(zone, start_pfn, nr_pages)) { + if (izone !=3D NULL) + return true; + izone =3D zone; + } + + } + + return false; +} + static void __init mem_init_print_info(void); void __init page_alloc_init_late(void) { --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F5EA1D5AD3 for ; Thu, 6 Feb 2025 18:51:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867904; cv=none; b=bE+Fq1yjA7LzMnyAIyT6szS3vx0Dm/USMxeaFlK8t1E2/oiiaQMSa8mojtAr3m+fltc9Fj7pGdSVHbxR2pdNltYHEfmDQTptzozjQaLkMfyFHZf6TG7Cj25TzhnolS1V+nCUhtIT0l3VpwqeaVJGkW+soVmrMJgpigzMy2/Vz3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867904; c=relaxed/simple; bh=4HQP3tKyopU+CtkewFLNjyoY5jZsOQt8G3YlQTDY0eo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GO+mr+yqMbtmY8D+m13xn+zvCT2BGu9qMTo+khhCkOTLeZNXKbxawwR2/aG+l1nFfsqhx5fNS869ewIwpyxgQv5jr6hTaJbpV6MNu3z48FnLjxVuK8/Hp9OIwN385sChgbn1Zg+0sjy7PJmsef0gDjnkhq2Ve/LohV7d5eNtnEQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JqOLgQdI; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JqOLgQdI" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21f0f1af7c7so30481885ad.0 for ; Thu, 06 Feb 2025 10:51:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867901; x=1739472701; darn=vger.kernel.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=nFoWWRyCHF1Iin3pK7rG6BcJvDoZ6S/iIkIiqdRP1Hc=; b=JqOLgQdIMzKlDLeHFWJ5yvM1xA+rW1JjQnAGYKTioDXKMc6hH+hsDzzUhvLIT85Al5 zhzSYGHxguRTsqgaItab1i6SqkjAxV/hn3RHHHdhW8yW53p/DGE653b5lNLf+j5xjjXy 0JQXXpP+VzOhMWI08xGwtSPaGoFYKSkjKVwehYjqZzGoGBlIGazAV4aJJMCIpF6BkPY4 YdE9w28+hiB6E1g2kTvcurUbkPG89wqpva3LZo+h9L/9filSxfyJOyEWZLcBoDIg9bjK v9sXAzqZkRCw4MGQyX8bV+afTmFBnhqRIpuuu78ovO8RwkWZS45sFh7stOuIJUr5AXVk EWRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867901; x=1739472701; 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=nFoWWRyCHF1Iin3pK7rG6BcJvDoZ6S/iIkIiqdRP1Hc=; b=CjDcW+AgJqe6yD+LyUcAEOYSgILJZPzAWvjcGi0yhDl24JEIvEIc4uu+g64imxJdrW izKmF8COO64lfK/JcFcC6QTcS1iJfJxBdSUKNxY6DEhyileWr5T9M3J/FkjXqSWjEyYH p5MkBVkTW1qm6bRXM2/wYL7t/mAZQ600Yk/yj8blPOF/rlAg2sGtVAEtfJOBttr5uwh7 MHCJVem0kGUhIcbSygMsJPAUMD6ctwmjJ0fDQpRb0+XhBKE4H69/QDIAqJRVVKlqeAYI XMeqKGZ9CFfPlLHN9Y++LrYMHN978HJmVnM+z9lqEnrJjJnATOmpTsoNnyQAUDZSByKM AS1A== X-Forwarded-Encrypted: i=1; AJvYcCVeIzJgB1t2/KXKq5pcOP7pno0q54iBKePuOL9pUe2H18YLJw3rrZPyiWInh9tZwROMDCn8WejWQH9wKkg=@vger.kernel.org X-Gm-Message-State: AOJu0Yy85GiCt1sIOCFm0BkZSwJ7MehlmCfZrGS+TyVX/96SyD4T8Q5X HCG9aKMnCogyr+A86KBiXxs6cVJ64J6BY2RBo4nsZnwr8pj4k81s5G11E74XXcWKEm/jZA== X-Google-Smtp-Source: AGHT+IEGjRoSpQn8/xkl3lhb6Wk2y9VhZAipET65ydINQje9MB9/NNORs9N8PFdtkfHgKJaqcoUjp9yu X-Received: from pfvx11.prod.google.com ([2002:a05:6a00:270b:b0:725:dec7:dd47]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:6f91:b0:1dc:2365:4d7d with SMTP id adf61e73a8af0-1ee03a4744fmr811464637.16.1738867900833; Thu, 06 Feb 2025 10:51:40 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:55 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-16-fvdl@google.com> Subject: [PATCH v3 15/28] mm/sparse: add vmemmap_*_hvo functions 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a few functions to enable early HVO: vmemmap_populate_hvo vmemmap_undo_hvo vmemmap_wrprotect_hvo The populate and undo functions are expected to be used in early init, from the sparse_init_nid_early() function. The wrprotect function is to be used, potentially, later. To implement these functions, mostly re-use the existing compound pages vmemmap logic used by DAX. vmemmap_populate_address has its argument changed a bit in this commit: the page structure passed in to be reused in the mapping is replaced by a PFN and a flag. The flag indicates whether an extra ref should be taken on the vmemmap page containing the head page structure. Taking the ref is appropriate to for DAX / ZONE_DEVICE, but not for HugeTLB HVO. The HugeTLB vmemmap optimization maps tail page structure pages read-only. The vmemmap_wrprotect_hvo function that does this is implemented separately, because it cannot be guaranteed that reserved page structures will not be write accessed during memory initialization. Even with CONFIG_DEFERRED_STRUCT_PAGE_INIT, they might still be written to (if they are at the bottom of a zone). So, vmemmap_populate_hvo leaves the tail page structure pages RW initially, and then later during initialization, after memmap init is fully done, vmemmap_wrprotect_hvo must be called to finish the job. Subsequent commits will use these functions for early HugeTLB HVO. Signed-off-by: Frank van der Linden --- include/linux/mm.h | 9 ++- mm/sparse-vmemmap.c | 141 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 135 insertions(+), 15 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index df83653ed6e3..0463c062fd7a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3837,7 +3837,8 @@ p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long= addr, int node); pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node); pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node); pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node, - struct vmem_altmap *altmap, struct page *reuse); + struct vmem_altmap *altmap, unsigned long ptpfn, + unsigned long flags); void *vmemmap_alloc_block(unsigned long size, int node); struct vmem_altmap; void *vmemmap_alloc_block_buf(unsigned long size, int node, @@ -3853,6 +3854,12 @@ int vmemmap_populate_hugepages(unsigned long start, = unsigned long end, int node, struct vmem_altmap *altmap); int vmemmap_populate(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap); +int vmemmap_populate_hvo(unsigned long start, unsigned long end, int node, + unsigned long headsize); +int vmemmap_undo_hvo(unsigned long start, unsigned long end, int node, + unsigned long headsize); +void vmemmap_wrprotect_hvo(unsigned long start, unsigned long end, int nod= e, + unsigned long headsize); void vmemmap_populate_print_last(void); #ifdef CONFIG_MEMORY_HOTPLUG void vmemmap_free(unsigned long start, unsigned long end, diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 8751c46c35e4..8cc848c4b17c 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -30,6 +30,13 @@ =20 #include #include +#include + +/* + * Flags for vmemmap_populate_range and friends. + */ +/* Get a ref on the head page struct page, for ZONE_DEVICE compound pages = */ +#define VMEMMAP_POPULATE_PAGEREF 0x0001 =20 #include "internal.h" =20 @@ -144,17 +151,18 @@ void __meminit vmemmap_verify(pte_t *pte, int node, =20 pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int= node, struct vmem_altmap *altmap, - struct page *reuse) + unsigned long ptpfn, unsigned long flags) { pte_t *pte =3D pte_offset_kernel(pmd, addr); if (pte_none(ptep_get(pte))) { pte_t entry; void *p; =20 - if (!reuse) { + if (ptpfn =3D=3D (unsigned long)-1) { p =3D vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap); if (!p) return NULL; + ptpfn =3D PHYS_PFN(__pa(p)); } else { /* * When a PTE/PMD entry is freed from the init_mm @@ -165,10 +173,10 @@ pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, un= signed long addr, int node, * and through vmemmap_populate_compound_pages() when * slab is available. */ - get_page(reuse); - p =3D page_to_virt(reuse); + if (flags & VMEMMAP_POPULATE_PAGEREF) + get_page(pfn_to_page(ptpfn)); } - entry =3D pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL); + entry =3D pfn_pte(ptpfn, PAGE_KERNEL); set_pte_at(&init_mm, addr, pte, entry); } return pte; @@ -238,7 +246,8 @@ pgd_t * __meminit vmemmap_pgd_populate(unsigned long ad= dr, int node) =20 static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int = node, struct vmem_altmap *altmap, - struct page *reuse) + unsigned long ptpfn, + unsigned long flags) { pgd_t *pgd; p4d_t *p4d; @@ -258,7 +267,7 @@ static pte_t * __meminit vmemmap_populate_address(unsig= ned long addr, int node, pmd =3D vmemmap_pmd_populate(pud, addr, node); if (!pmd) return NULL; - pte =3D vmemmap_pte_populate(pmd, addr, node, altmap, reuse); + pte =3D vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn, flags); if (!pte) return NULL; vmemmap_verify(pte, node, addr, addr + PAGE_SIZE); @@ -269,13 +278,15 @@ static pte_t * __meminit vmemmap_populate_address(uns= igned long addr, int node, static int __meminit vmemmap_populate_range(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap, - struct page *reuse) + unsigned long ptpfn, + unsigned long flags) { unsigned long addr =3D start; pte_t *pte; =20 for (; addr < end; addr +=3D PAGE_SIZE) { - pte =3D vmemmap_populate_address(addr, node, altmap, reuse); + pte =3D vmemmap_populate_address(addr, node, altmap, + ptpfn, flags); if (!pte) return -ENOMEM; } @@ -286,7 +297,107 @@ static int __meminit vmemmap_populate_range(unsigned = long start, int __meminit vmemmap_populate_basepages(unsigned long start, unsigned lon= g end, int node, struct vmem_altmap *altmap) { - return vmemmap_populate_range(start, end, node, altmap, NULL); + return vmemmap_populate_range(start, end, node, altmap, -1, 0); +} + +/* + * Undo populate_hvo, and replace it with a normal base page mapping. + * Used in memory init in case a HVO mapping needs to be undone. + * + * This can happen when it is discovered that a memblock allocated + * hugetlb page spans multiple zones, which can only be verified + * after zones have been initialized. + * + * We know that: + * 1) The first @headsize / PAGE_SIZE vmemmap pages were individually + * allocated through memblock, and mapped. + * + * 2) The rest of the vmemmap pages are mirrors of the last head page. + */ +int __meminit vmemmap_undo_hvo(unsigned long addr, unsigned long end, + int node, unsigned long headsize) +{ + unsigned long maddr, pfn; + pte_t *pte; + int headpages; + + /* + * Should only be called early in boot, so nothing will + * be accessing these page structures. + */ + WARN_ON(!early_boot_irqs_disabled); + + headpages =3D headsize >> PAGE_SHIFT; + + /* + * Clear mirrored mappings for tail page structs. + */ + for (maddr =3D addr + headsize; maddr < end; maddr +=3D PAGE_SIZE) { + pte =3D virt_to_kpte(maddr); + pte_clear(&init_mm, maddr, pte); + } + + /* + * Clear and free mappings for head page and first tail page + * structs. + */ + for (maddr =3D addr; headpages-- > 0; maddr +=3D PAGE_SIZE) { + pte =3D virt_to_kpte(maddr); + pfn =3D pte_pfn(ptep_get(pte)); + pte_clear(&init_mm, maddr, pte); + memblock_phys_free(PFN_PHYS(pfn), PAGE_SIZE); + } + + flush_tlb_kernel_range(addr, end); + + return vmemmap_populate(addr, end, node, NULL); +} + +/* + * Write protect the mirrored tail page structs for HVO. This will be + * called from the hugetlb code when gathering and initializing the + * memblock allocated gigantic pages. The write protect can't be + * done earlier, since it can't be guaranteed that the reserved + * page structures will not be written to during initialization, + * even if CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled. + * + * The PTEs are known to exist, and nothing else should be touching + * these pages. The caller is responsible for any TLB flushing. + */ +void vmemmap_wrprotect_hvo(unsigned long addr, unsigned long end, + int node, unsigned long headsize) +{ + unsigned long maddr; + pte_t *pte; + + for (maddr =3D addr + headsize; maddr < end; maddr +=3D PAGE_SIZE) { + pte =3D virt_to_kpte(maddr); + ptep_set_wrprotect(&init_mm, maddr, pte); + } +} + +/* + * Populate vmemmap pages HVO-style. The first page contains the head + * page and needed tail pages, the other ones are mirrors of the first + * page. + */ +int __meminit vmemmap_populate_hvo(unsigned long addr, unsigned long end, + int node, unsigned long headsize) +{ + pte_t *pte; + unsigned long maddr; + + for (maddr =3D addr; maddr < addr + headsize; maddr +=3D PAGE_SIZE) { + pte =3D vmemmap_populate_address(maddr, node, NULL, -1, 0); + if (!pte) + return -ENOMEM; + } + + /* + * Reuse the last page struct page mapped above for the rest. + */ + return vmemmap_populate_range(maddr, end, node, NULL, + pte_pfn(ptep_get(pte)), 0); } =20 void __weak __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node, @@ -409,7 +520,8 @@ static int __meminit vmemmap_populate_compound_pages(un= signed long start_pfn, * with just tail struct pages. */ return vmemmap_populate_range(start, end, node, NULL, - pte_page(ptep_get(pte))); + pte_pfn(ptep_get(pte)), + VMEMMAP_POPULATE_PAGEREF); } =20 size =3D min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page)); @@ -417,13 +529,13 @@ static int __meminit vmemmap_populate_compound_pages(= unsigned long start_pfn, unsigned long next, last =3D addr + size; =20 /* Populate the head page vmemmap page */ - pte =3D vmemmap_populate_address(addr, node, NULL, NULL); + pte =3D vmemmap_populate_address(addr, node, NULL, -1, 0); if (!pte) return -ENOMEM; =20 /* Populate the tail pages vmemmap page */ next =3D addr + PAGE_SIZE; - pte =3D vmemmap_populate_address(next, node, NULL, NULL); + pte =3D vmemmap_populate_address(next, node, NULL, -1, 0); if (!pte) return -ENOMEM; =20 @@ -433,7 +545,8 @@ static int __meminit vmemmap_populate_compound_pages(un= signed long start_pfn, */ next +=3D PAGE_SIZE; rc =3D vmemmap_populate_range(next, last, node, NULL, - pte_page(ptep_get(pte))); + pte_pfn(ptep_get(pte)), + VMEMMAP_POPULATE_PAGEREF); if (rc) return -ENOMEM; } --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AB7D1D5CFF for ; Thu, 6 Feb 2025 18:51:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867904; cv=none; b=ZqFCc2aZxyx7gSHcA6iYY2IndxsRU2IFAeIsHt5cjmjrh8RJTVI1D9OxLQ/mhw3q+XC8Oz1N9amf+Xsbeb4dfMHpmKzg4vdDpKeQ9qH/7MlUk52hDuqJzMyrgp1qhAxHwlmhxBgVV6p6WHKKZHKeiWdUkehAqlTxScCPTaTdedE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867904; c=relaxed/simple; bh=1ct3sIUPt+ZB93pfiPlURgrSlIRtYGQITJSe17cwDHk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MbWz1XYBW17xVSJq9tGunXKmawXgAbzjzzIKptNxFIbaDAmohoCrLWfScTjQ2bf4g7XQLiPn24+WJ6LAZcEBMjUjNloNUEQQddqJ5atSK9hR/rllIaSlh9DK3E0bu/mb8bTb1E9I/+bX7mG6x6TXgxCyJdsJrTkOa35KMEjRJpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=J9nZji6r; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="J9nZji6r" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21f444af89fso8580555ad.1 for ; Thu, 06 Feb 2025 10:51:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867902; x=1739472702; darn=vger.kernel.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=HYCnhKfOpNHoXGLJGoUQUcx2tHg42NkQx3e7q6fph54=; b=J9nZji6rtpBxevbaBhFzjM4Ajcl0yPDlU37mCh59fFACCv66KFZQDM1O5mOAub2N44 b4Zm5ROpivxMD/Vb9qW52vp7+snzHOjxmU2yp7PZ4zL0hsgc49PlPaDG5H/mFg5R2JKt SuNnxewqortiKBzqucPTCY5GYTrYCQj9Pl5d/dXG480sHHtmUU8uwYL0HiG9hV8E75ug 32qynmTy1tW0gPwx8gj09h0AeHLCt9sawNjycrKI4d5IdhbDAmSqrfVwIo+j2nLTDODK XHiPyHvcMVhlH+fucbo0MfwIQxvV6lGT+UHb9QaP3l0ghk4S+yXDqoFnvWofaRuH5IC9 Q2CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867902; x=1739472702; 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=HYCnhKfOpNHoXGLJGoUQUcx2tHg42NkQx3e7q6fph54=; b=EvAeJkwczkq21UPsEICimjscj0A4eIxiCFUIaArxK0O8eRzx1MVceQHwaBFBnzw6LY EknJ6y5HJVg77qD0BkmB/u4QWhJ2PZHdxE3gIHFF7ZBSfF3X15jeeGZUo3dOd90Ikqkf YowmVatZQJbNVVP6dxEcLxvLf9/iA0WMN6t/P4PKlZAMYM37a6c58GT1jmFYiQF2MTd/ bA7wyOq1dr8uVUYch+DEdrZvhuNvmIvsYY+FbwASLCzN0xK7+cIjvBw8ALeVrBa7phej SIDvKXzsVuGE0mlzHa+U4q6WVydaGiGEJRWg7cBwu0Ea/ei9Fq2fo2kgiKNUqb3Mq8XW u7UQ== X-Forwarded-Encrypted: i=1; AJvYcCUIr6AZGJoRRjg/wGU0f/RzF7Pn3blFHHMXFY5TCBqZmQyPMYEPnCy1V2/mQD/sAkQ/Ob7GtoDqrVZw2D8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywh6rAahfX/kgnfTgnKj9WWslpAEYhJdWVtg4lcaXrGrFKeGRbD 06vq5q1ZccHQJKzhP98tVL+oMJTRzJrQSZyuBUcrl7qL9kl986BFBPcRpwU6D6aNRWJXEw== X-Google-Smtp-Source: AGHT+IGOwjdyUKmu33OfICAjkR5uTyQ60fkGxL3DuN6UZt1dw+rarr7Qj2spOWMme9SCtmLchvIHmNez X-Received: from pgbfq6.prod.google.com ([2002:a05:6a02:2986:b0:ad0:f8ff:b90d]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d70a:b0:1e0:dcc5:164d with SMTP id adf61e73a8af0-1ee03a21ca9mr898719637.8.1738867902197; Thu, 06 Feb 2025 10:51:42 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:56 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-17-fvdl@google.com> Subject: [PATCH v3 16/28] mm/hugetlb: deal with multiple calls to hugetlb_bootmem_alloc 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Architectures that want pre-HVO of hugetlb vmemmap pages will need to call hugetlb_bootmem_alloc from an earlier spot in boot (before sparse_init). To facilitate some architectures doing this, protect hugetlb_bootmem_alloc against multiple calls. Also provide a helper function to check if it's been called, so that the early HVO code, to be added later, can see if there is anything to do. Signed-off-by: Frank van der Linden --- include/linux/hugetlb.h | 6 ++++++ mm/hugetlb.c | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 9cd7c9dacb88..5061279e5f73 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -175,6 +175,7 @@ extern int sysctl_hugetlb_shm_group; extern struct list_head huge_boot_pages[MAX_NUMNODES]; =20 void hugetlb_bootmem_alloc(void); +bool hugetlb_bootmem_allocated(void); =20 /* arch callbacks */ =20 @@ -1256,6 +1257,11 @@ static inline bool hugetlbfs_pagecache_present( static inline void hugetlb_bootmem_alloc(void) { } + +static inline bool hugetlb_bootmem_allocated(void) +{ + return false; +} #endif /* CONFIG_HUGETLB_PAGE */ =20 static inline spinlock_t *huge_pte_lock(struct hstate *h, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 789341158ef6..3db980db15ec 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4910,16 +4910,28 @@ static int __init default_hugepagesz_setup(char *s) } hugetlb_early_param("default_hugepagesz", default_hugepagesz_setup); =20 +static bool __hugetlb_bootmem_allocated __initdata; + +bool __init hugetlb_bootmem_allocated(void) +{ + return __hugetlb_bootmem_allocated; +} + void __init hugetlb_bootmem_alloc(void) { struct hstate *h; =20 + if (__hugetlb_bootmem_allocated) + return; + hugetlb_parse_params(); =20 for_each_hstate(h) { if (hstate_is_gigantic(h)) hugetlb_hstate_alloc_pages(h); } + + __hugetlb_bootmem_allocated =3D true; } =20 static unsigned int allowed_mems_nr(struct hstate *h) --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9889819DFA7 for ; Thu, 6 Feb 2025 18:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867906; cv=none; b=fQIOyFs8VilxIYinlNb4N/V8FlS6KWBa94sfKt1KcV8TvBptj518Td2ngJDYv5SNnRR3q8pHmUlJY/N7xZioYUNb5GSFuueB+72LHVCVqQrHm+5XHJGPIezWhnZaIkNV8E+7n7DwYfJ9ncYoR/94ZRP3/4+oEBJuhuDV/Yw5W2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867906; c=relaxed/simple; bh=w163y2nRosE//joPGzINC1pFP0PswLMjSdcp4uQ9LCw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=f4dXAw1lpmD67ZRgOfNPsGF97G92uMDN0Kz0Hya07Fnfwwc1ZW0b5+pvfBqHpObVbFwd0DK+6Fz07NGoNSSz0R3u0MinoELKA4aWh6wuFVu24anm0eUJ/p6yXn14cvIVRE+CCEM9XvJG926E3MNJYncsRAvwEm6rm3ST9c6qq6I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=31dFP5CM; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="31dFP5CM" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f9f90051a2so2490043a91.2 for ; Thu, 06 Feb 2025 10:51:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867904; x=1739472704; darn=vger.kernel.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=mjgqfpoeBSlwN57iYwWF6PWmjsHMNrUorj8RHJy/6t4=; b=31dFP5CMRtAn1WnO7aYYzbznl3cBuG9xznmsvWk9tXBp565CUeCm8IPiLM4iGpQt60 T0NaOvxAg48B/b5owX0i1OkDB35L9T1CmtiWhzrKINIswNlV6kwInCAdT9fB+BR1aqgU K5EmYMcAvky3y/dYt4fB3KJPAfIIiLxoFlSr8RmVMXF83swgEywMQ8CE8jSOb4SmK66U Y4HyBGTld0fKXl8jUZ8aIVDrr//+H5Y59osJAZHc7E9GThg6GOd0JlSIJn77ur4eo/zG vH+Hemj4BuKsyGsBENQLMDQcUu7U8gwC6FmeCzKbIxeLcF0IK1NlEELlwVig1UDwinfT KoDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867904; x=1739472704; 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=mjgqfpoeBSlwN57iYwWF6PWmjsHMNrUorj8RHJy/6t4=; b=NfujLwudei7X6F5+/BPNOJ6dk+Ja19KDFG908YLP9MjkaEyokB95xtmRLrcKWG1Vp7 lTxFAd9Lw7IPlT/FMy4BDdaQQMrLuyzGvjGwcSnOcpngQUb2i1W3DCy4n/xfBY6A291T IwwxKNKrrvMbl89t+8rFrnTjwUHQqa2C83HLp8lgbUlsAl/vAeVH6iAtxFNcHXSA6hh5 5d2l7pcrt6htyXkP3N4qUDbvlxKxWehFpk4HN2BEpIp4b5pbRb95jULl4qgn2MasajF+ zodFieGKsWzov86xZ8yj2ULCF8PetVdUN7/FdXbDVQhoi96Jk5YWsxpR4VF/sW/Olv6x An3A== X-Forwarded-Encrypted: i=1; AJvYcCW6nhbxuC7sdQWll55AqE8+Suc9iD96gt8t6k/Q7nyTDbwB6csw/GfNmKsbR66lPcgaKkbYAdc7oiHSzBg=@vger.kernel.org X-Gm-Message-State: AOJu0YxGsoO/q+fQhgfGiJlM+U7EFDsPaey5YOgCuQreFOWznO69uJ32 4qDgGqPQV5bVBSj2uZop9zibG1yraKec7pIUyi99hz70boXg07IdSNLWlTEv96E6kSI9sg== X-Google-Smtp-Source: AGHT+IGsb48koloxCQu/0NbdMR5X4NhMSEmqGncLGIVHRlgshaXB4vQQkIYCmgOftKiKB7i67AnhOHrr X-Received: from pjboe12.prod.google.com ([2002:a17:90b:394c:b0:2ef:9239:aab1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d03:b0:2f6:d266:f45e with SMTP id 98e67ed59e1d1-2fa23f42672mr204592a91.2.1738867903837; Thu, 06 Feb 2025 10:51:43 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:57 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-18-fvdl@google.com> Subject: [PATCH v3 17/28] mm/hugetlb: move huge_boot_pages list init to hugetlb_bootmem_alloc 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of initializing the per-node hugetlb bootmem pages list from the alloc function, we can now do it in a somewhat cleaner way, since there is an explicit hugetlb_bootmem_alloc function. Initialize the lists there. Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3db980db15ec..52ee5514a1f2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3578,7 +3578,6 @@ static unsigned long __init hugetlb_pages_alloc_boot(= struct hstate *h) static void __init hugetlb_hstate_alloc_pages(struct hstate *h) { unsigned long allocated; - static bool initialized __initdata; =20 /* skip gigantic hugepages allocation if hugetlb_cma enabled */ if (hstate_is_gigantic(h) && hugetlb_cma_size) { @@ -3586,17 +3585,6 @@ static void __init hugetlb_hstate_alloc_pages(struct= hstate *h) return; } =20 - /* hugetlb_hstate_alloc_pages will be called many times, initialize huge_= boot_pages once */ - if (!initialized) { - int i =3D 0; - - for (i =3D 0; i < MAX_NUMNODES; i++) - INIT_LIST_HEAD(&huge_boot_pages[i]); - h->next_nid_to_alloc =3D first_online_node; - h->next_nid_to_free =3D first_online_node; - initialized =3D true; - } - /* do node specific alloc */ if (hugetlb_hstate_alloc_pages_specific_nodes(h)) return; @@ -4920,13 +4908,20 @@ bool __init hugetlb_bootmem_allocated(void) void __init hugetlb_bootmem_alloc(void) { struct hstate *h; + int i; =20 if (__hugetlb_bootmem_allocated) return; =20 + for (i =3D 0; i < MAX_NUMNODES; i++) + INIT_LIST_HEAD(&huge_boot_pages[i]); + hugetlb_parse_params(); =20 for_each_hstate(h) { + h->next_nid_to_alloc =3D first_online_node; + h->next_nid_to_free =3D first_online_node; + if (hstate_is_gigantic(h)) hugetlb_hstate_alloc_pages(h); } --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EEF01D90A9 for ; Thu, 6 Feb 2025 18:51:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867908; cv=none; b=j/I9scB+kmByvRPiU66vaT1/ucAie36zkMg6zIcQY63ACEsOjTC90DFB1RPXTs8254kLtmVDrBGrGIpHIKiN0wIErHHFQiMNYO+cyah2k5gtpyRW1Yq4THEXSNwAnpf8X+CXUg8uHzJIhmiG+ckPQVBd7GXo3e9dEXMxa8URVyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867908; c=relaxed/simple; bh=WVn7vDPfu+EjljprrRLvQ+shC+V+6zeTvZGJJUUZmjs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uKcWaGuNEf4dHAtGhh4pak9rqioIHSkdu1HTKtvWm4flmy1RMPXcNPJElrw/NLZeHjjGL2PGtEcYuLc9jaXRCmDTz0r+HN9zIXG5HjK+Bx07p/uAgYRuzpQIpU7Zt7tWBwe1MnjShhjnuievc2E6bk3DyVwvUIgVeCaQNzCFpzI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mnCY+qgt; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mnCY+qgt" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21f1a7dc0e6so46757615ad.3 for ; Thu, 06 Feb 2025 10:51:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867905; x=1739472705; darn=vger.kernel.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=1RY4Sckn7GWnsNuNzDlgUqe5LNqypC0myFrtZsSR9cE=; b=mnCY+qgtr4grN+xA1KMlx+B7FPBv+gs/+cfBa4w14pyBG7QIxF5hZk3NuH1QB97hzA NQIKVBSB5erxyX65px844xm/XXah1aLSIX87qo2eXG86GPWfBon/+n78PHXwR2HtsnrH sM+ppOd4CDuLLZeH8qQ3ZuxgXLT+cRUWQtZ4M4lj009KOcDg4DUGHqZChN0O+Sq7hTxk VUx2sE/lP9l27rnleWFdcNaLD5vzJGEeL1i9kyf5k/XroJ8zI+aDSUPEecCwkSkqcktI 20bLcAhRKJ/J+3A00hqIfOc1LAbHwrzuDS31zDwy6nuIYR/VPlnx2tSJp1vFFcZ7mt0B vfag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867905; x=1739472705; 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=1RY4Sckn7GWnsNuNzDlgUqe5LNqypC0myFrtZsSR9cE=; b=LulP01ck2z1mHwmDC5CxGwncnZ4TV50YZvJQMJkDh1PO4gIzDyNnc3AYaBFd/PgJzY rI8SI4UwAE0DT4qiD7s8JMElUWx2W967Ze5rYYZ/xQ9UDThtRbQ+9QWlr6NwHtlE3boI QcljRol/+FJozorRbciQD68Y/pFub1PsLrTFnOLcEPAzlbSjyPBzRoexXzUvZYY/sl9P KuyVzQrEclp7XKJCL6ZRXpYHTUUL40LtrZtqzGQur3S18fVl7Ebt1xqVxOoRTDVN6OA6 K0W6ZXPinUE/iZNImHG5aKwD/nktcA2HgICg18ztqjMDculAH3sgQRCcw/YrbZeZN+VO EdcQ== X-Forwarded-Encrypted: i=1; AJvYcCXlaexQMoA4GF/UtnNmElH+ZTorfDobJ2cGFdm+w64n4PiP9cJO53Bf2yCjTa6QUqUBM1B2rEpbGpKLGrM=@vger.kernel.org X-Gm-Message-State: AOJu0YzZVvB4uaUJfDyG5Xkfd+1xLi6xlaaQNRktwztMfyJnFxpsxAZf lnhZwyNhpHk3Z6brUeCY61VK5MFavZPBuR8OOefUaPcMlMKdN8CGohDr8sXyHh8AWR9PWA== X-Google-Smtp-Source: AGHT+IGv5wn5p5nXx7RKxFjYk6wjYFPCzyPzBbZhXtDcOU21vQXSokgZmbD3Gr14JvKdrzDwy2bARR7t X-Received: from pgev18.prod.google.com ([2002:a63:ac12:0:b0:abb:a12f:81a8]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9985:b0:1eb:7da4:305e with SMTP id adf61e73a8af0-1ee03b70cbcmr733791637.39.1738867905422; Thu, 06 Feb 2025 10:51:45 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:58 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-19-fvdl@google.com> Subject: [PATCH v3 18/28] mm/hugetlb: add pre-HVO framework 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define flags for pre-HVOed bootmem hugetlb pages, and act on them. The most important flag is the HVO flag, signalling that a bootmem allocated gigantic page has already been HVO-ed. If this flag is seen by the hugetlb bootmem gather code, the page is marked as HVO optimized. The HVO code will then not try to optimize it again. Instead, it will just map the tail page mirror pages read-only, completing the HVO steps. No functional change, as nothing sets the flags yet. Signed-off-by: Frank van der Linden --- arch/powerpc/mm/hugetlbpage.c | 1 + include/linux/hugetlb.h | 4 +++ mm/hugetlb.c | 24 ++++++++++++++++- mm/hugetlb_vmemmap.c | 50 +++++++++++++++++++++++++++++++++-- mm/hugetlb_vmemmap.h | 15 +++++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/mm/hugetlbpage.c b/arch/powerpc/mm/hugetlbpage.c index 6b043180220a..d3c1b749dcfc 100644 --- a/arch/powerpc/mm/hugetlbpage.c +++ b/arch/powerpc/mm/hugetlbpage.c @@ -113,6 +113,7 @@ static int __init pseries_alloc_bootmem_huge_page(struc= t hstate *hstate) gpage_freearray[nr_gpages] =3D 0; list_add(&m->list, &huge_boot_pages[0]); m->hstate =3D hstate; + m->flags =3D 0; return 1; } =20 diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 5061279e5f73..10a7ce2b95e1 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -681,8 +681,12 @@ struct hstate { struct huge_bootmem_page { struct list_head list; struct hstate *hstate; + unsigned long flags; }; =20 +#define HUGE_BOOTMEM_HVO 0x0001 +#define HUGE_BOOTMEM_ZONES_VALID 0x0002 + int isolate_or_dissolve_huge_page(struct page *page, struct list_head *lis= t); int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long en= d_pfn); struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 52ee5514a1f2..7e0810c217ce 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3219,6 +3219,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int n= id) INIT_LIST_HEAD(&m->list); list_add(&m->list, &huge_boot_pages[node]); m->hstate =3D h; + m->flags =3D 0; return 1; } =20 @@ -3286,7 +3287,7 @@ static void __init prep_and_add_bootmem_folios(struct= hstate *h, struct folio *folio, *tmp_f; =20 /* Send list for bulk vmemmap optimization processing */ - hugetlb_vmemmap_optimize_folios(h, folio_list); + hugetlb_vmemmap_optimize_bootmem_folios(h, folio_list); =20 list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { if (!folio_test_hugetlb_vmemmap_optimized(folio)) { @@ -3315,6 +3316,13 @@ static bool __init hugetlb_bootmem_page_zones_valid(= int nid, unsigned long start_pfn; bool valid; =20 + if (m->flags & HUGE_BOOTMEM_ZONES_VALID) { + /* + * Already validated, skip check. + */ + return true; + } + start_pfn =3D virt_to_phys(m) >> PAGE_SHIFT; =20 valid =3D !pfn_range_intersects_zones(nid, start_pfn, @@ -3347,6 +3355,11 @@ static void __init hugetlb_bootmem_free_invalid_page= (int nid, struct page *page, } } =20 +static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m) +{ + return (m->flags & HUGE_BOOTMEM_HVO); +} + /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. @@ -3387,6 +3400,15 @@ static void __init gather_bootmem_prealloc_node(unsi= gned long nid) hugetlb_folio_init_vmemmap(folio, h, HUGETLB_VMEMMAP_RESERVE_PAGES); init_new_hugetlb_folio(h, folio); + + if (hugetlb_bootmem_page_prehvo(m)) + /* + * If pre-HVO was done, just set the + * flag, the HVO code will then skip + * this folio. + */ + folio_set_hugetlb_vmemmap_optimized(folio); + list_add(&folio->lru, &folio_list); =20 /* diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 5b484758f813..be6b33ecbc8e 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -649,14 +649,39 @@ static int hugetlb_vmemmap_split_folio(const struct h= state *h, struct folio *fol return vmemmap_remap_split(vmemmap_start, vmemmap_end, vmemmap_reuse); } =20 -void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_list) +static void __hugetlb_vmemmap_optimize_folios(struct hstate *h, + struct list_head *folio_list, + bool boot) { struct folio *folio; + int nr_to_optimize; LIST_HEAD(vmemmap_pages); unsigned long flags =3D VMEMMAP_REMAP_NO_TLB_FLUSH | VMEMMAP_SYNCHRONIZE_= RCU; =20 + nr_to_optimize =3D 0; list_for_each_entry(folio, folio_list, lru) { - int ret =3D hugetlb_vmemmap_split_folio(h, folio); + int ret; + unsigned long spfn, epfn; + + if (boot && folio_test_hugetlb_vmemmap_optimized(folio)) { + /* + * Already optimized by pre-HVO, just map the + * mirrored tail page structs RO. + */ + spfn =3D (unsigned long)&folio->page; + epfn =3D spfn + pages_per_huge_page(h); + vmemmap_wrprotect_hvo(spfn, epfn, folio_nid(folio), + HUGETLB_VMEMMAP_RESERVE_SIZE); + register_page_bootmem_memmap(pfn_to_section_nr(spfn), + &folio->page, + HUGETLB_VMEMMAP_RESERVE_SIZE); + static_branch_inc(&hugetlb_optimize_vmemmap_key); + continue; + } + + nr_to_optimize++; + + ret =3D hugetlb_vmemmap_split_folio(h, folio); =20 /* * Spliting the PMD requires allocating a page, thus lets fail @@ -668,6 +693,16 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h,= struct list_head *folio_l break; } =20 + if (!nr_to_optimize) + /* + * All pre-HVO folios, nothing left to do. It's ok if + * there is a mix of pre-HVO and not yet HVO-ed folios + * here, as __hugetlb_vmemmap_optimize_folio() will + * skip any folios that already have the optimized flag + * set, see vmemmap_should_optimize_folio(). + */ + goto out; + flush_tlb_all(); =20 list_for_each_entry(folio, folio_list, lru) { @@ -693,10 +728,21 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h= , struct list_head *folio_l } } =20 +out: flush_tlb_all(); free_vmemmap_page_list(&vmemmap_pages); } =20 +void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_list) +{ + __hugetlb_vmemmap_optimize_folios(h, folio_list, false); +} + +void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list= _head *folio_list) +{ + __hugetlb_vmemmap_optimize_folios(h, folio_list, true); +} + static const struct ctl_table hugetlb_vmemmap_sysctls[] =3D { { .procname =3D "hugetlb_optimize_vmemmap", diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 2fcae92d3359..a6354a27e63f 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -24,6 +24,8 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *= h, struct list_head *non_hvo_folios); void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *= folio); void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_list); +void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list= _head *folio_list); + =20 static inline unsigned int hugetlb_vmemmap_size(const struct hstate *h) { @@ -64,6 +66,19 @@ static inline void hugetlb_vmemmap_optimize_folios(struc= t hstate *h, struct list { } =20 +static inline void hugetlb_vmemmap_init_early(int nid) +{ +} + +static inline void hugetlb_vmemmap_init_late(int nid) +{ +} + +static inline void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *= h, + struct list_head *folio_list) +{ +} + static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct h= state *h) { return 0; --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 912A71D9A48 for ; Thu, 6 Feb 2025 18:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867909; cv=none; b=qnUq12uS/jQwRqYsX82rAFKUKonMwSCk6qMc1ANA2yOy7nmZAImWKkPJjeAZwTuvSfMXP8JPgxc7zFxW8KgPiSQka8LrqG8w0avimD4D9ehhd+3AMBqJeT3d4OzVrEr6zVI6Jl25axNloyH/5rVSxWyrmFpOlz7LssViblHLlis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867909; c=relaxed/simple; bh=8Xr0dTsGa6vkJL7Cp0xU4nqS4A3Iel4yNhuGBUZVeug=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HVoME+zbuLh9EuQ0OkIcAycjzF13Fu9J7OW/TD6p3ZOyhWyxy9UZ0oDUf9tXX6bQe70WPnVxVzw+u2Gvw3KPli5CEl/jngBQh5q97PYqVYdEAvaRoF2nu/Vy33sgFY7PKJsCdflfRQPcuVSB8E+U1xoYUHCxcuyWdFMkjwBKxOQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eZo2QnSP; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eZo2QnSP" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f9fbe02121so2523163a91.0 for ; Thu, 06 Feb 2025 10:51:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867907; x=1739472707; darn=vger.kernel.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=PNNc4e9k2qfZPyKSSlBoWZ8VBSivbX9t64nIb//yCrE=; b=eZo2QnSP+zTLh7o/6LI3meTCqWatF/+qWxcxjaH84Pn/OEUosK7IofE+lRySROVRY8 bmtkeV7EKYFGovgblMPln6KOgDDuXz6bwUcB68ZXS9P/cGpypgbI4HXw8cgoNq16CEUP 37nSAGFhv5ZEMVMkNeDSRRIaC28W9F2nhS9Ilv0BHqk8IkpfuALwZPXd5SV3AnsN+gmy vGHiT0V22bHshHKB7ydW/3rmhPUDh4y23ZjUtIzcL3cDIUsTbojtD9jwAMJr3WN5q6mM pg27qjoe9/V2y5RTh/5PU14uyccWwgdIGkj0Gqn6bUdUZ2GWhoE2qQ3YqBMTz7XcPUX1 dMCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867907; x=1739472707; 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=PNNc4e9k2qfZPyKSSlBoWZ8VBSivbX9t64nIb//yCrE=; b=e+BB5GiSZen5eGzBMIrlv8TQN9kolDUQ43bRMrXeCFFJHlS5PadbbvdC3bUy5+wW99 q9PJxvEFWOqkisOyIDr9zZJR/CqxoIZg/TjR4dw9x856NO2dBcgCzKwdJdKdV25XrRPc Nr4/wuMQBq7FyGaBVQ81IXXEUX38iBnVnxdBxylWU5sOt7meSRXMmT5p+KjzzdzWyVT7 dA52EB6VR7N4R99XrcgxPBVM2Q+mgtPYmMg8rZyAwEKvY+t2N1bYM7Vw8+RJPF1AGhhM 1wa2uV1hM3DlQNQST1y4VFkoAWZWs1HEApq/46OP9fZnu5/svk7LaoWX5tGWFg9XXEGg 0uZg== X-Forwarded-Encrypted: i=1; AJvYcCXm3KWzP+R6WpEwlR7paJSz50QIlpp+90lxY/TPhNzqf40i/EBQJDNbuTohKl8si9O0Y2WQD2BGsKB8NoE=@vger.kernel.org X-Gm-Message-State: AOJu0YxmN58NIrEMw/y433m9vC5WnOJyayHENusGDNLcKXCciEtnvC0n pUG68mIzbjzC9pmgd0TKtw7qJAUFJDd1J3Yfx0IhrUNmlL2Gyz0/C/IQX3dAmYUVWDTP1A== X-Google-Smtp-Source: AGHT+IHGGdqW8NvChqfMo/KsvB7iHoprf/cZ/BIgo0+z0Q3eFlECPhwcrH/qX3NKeNb05ShwfwU55FNn X-Received: from pfbdh8.prod.google.com ([2002:a05:6a00:4788:b0:730:4c72:3583]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:88cb:0:b0:72f:d7ce:500f with SMTP id d2e1a72fcca58-7305d525eb4mr475558b3a.21.1738867906868; Thu, 06 Feb 2025 10:51:46 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:59 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-20-fvdl@google.com> Subject: [PATCH v3 19/28] mm/hugetlb_vmemmap: fix hugetlb_vmemmap_restore_folios definition 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make the hugetlb_vmemmap_restore_folios definition inline for the !CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP case, so that including this file in files other than hugetlb_vmemmap.c will work. Fixes: cfb8c75099db ("hugetlb: perform vmemmap restoration on a list of pag= es") Signed-off-by: Frank van der Linden --- mm/hugetlb_vmemmap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index a6354a27e63f..926b8b27b5cb 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -50,7 +50,7 @@ static inline int hugetlb_vmemmap_restore_folio(const str= uct hstate *h, struct f return 0; } =20 -static long hugetlb_vmemmap_restore_folios(const struct hstate *h, +static inline long hugetlb_vmemmap_restore_folios(const struct hstate *h, struct list_head *folio_list, struct list_head *non_hvo_folios) { --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6505F1DC9B4 for ; Thu, 6 Feb 2025 18:51:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867911; cv=none; b=skOZYLbyjkNw5j8OKdGHk1tX3JxoPIZWn7VfvEsGUVsNISq7zghJnhjr4zax4u7hVVZq0q5qtLM597++RGiPBc6Trr5NSE9nSnOk91IS3i6+aqO4IL0dFYoDgs+raGHG36uloGBXaG93Y3vnaF1yPC49v7R1UsYwE2hWAdC2mIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867911; c=relaxed/simple; bh=MwQoEMcDUUcX7Mfjoxiv5kP8Ft1uxkLcbUM1Sp/36xE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BdgsVqugDUlZMRRlo5iaXVRhkRHl6AvE7n0UxhfOB0rhclvkudN4Vf291SuHmWdqSLOYJt3An2ZMjW155oppFsdjy+u/ODRhgsJ6BowkhSWrLtsmIADZ4HYEyV+e/g1IepM6/UovzjmE88ywWRImTVUpA+DxPNRxJklwDLXWg50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cz/knLmH; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cz/knLmH" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9c02f54f2so2486922a91.3 for ; Thu, 06 Feb 2025 10:51:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867909; x=1739472709; darn=vger.kernel.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=41QLNtbtqhWSGZ/oObOknHg0NviKslOOeQUP783wlTM=; b=cz/knLmH3pBALSWrayHfVEUE05zCPIG4hClr4aInennr0TUNkLT7BzeZBS9vkTLpll Pafvbck2Ej2AeoC5o8sk6CAQuaNfyhG6PqgZ5dp2e4N4Doh9dw3jPyH+WvM52a9vsN96 wNCUqmOrylSYLIWUD4BK0EE9QUxzvFU+vLrQxnPfBGjauwOTEruoh4tcJZoD+XY4Lf10 Y71epegm3mTXLadOj7eAT2mxmoKzjVvz2rJtnVJQBxvDKvTvL89HuhOrZNgyz1JIis27 6R9HdVfT3FHEQrScZJHeLgUp6BWOI6Ahel8u3+Ps9J1U8wvgpB6b08M7cVyilhq3VOIi oFcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867909; x=1739472709; 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=41QLNtbtqhWSGZ/oObOknHg0NviKslOOeQUP783wlTM=; b=YM0HWg/k/1tPXrfwOiCsJtPuqu5wUPIReWxSUxOoTqsEY3abeHgydUp3LJi8tVW/Y3 SrFq6UTUQQG7xC/aBPzz4CkliffRe8XvBbVMgOwHxxCAVIJcASUcaugCeV7RGylSHYu1 gzS+DfpU6uqY8zSDmIozMaA8n/SU3uNn/PoZuSB2R0WaxKviLnc1KNrRJuFmV4cN+35j MAWt9CPknmR3K8ZoJAvYMCIL1+jjtVMhtzLMRTwULBpNopLA5Do6ESRB+vy+TShl5k41 0MOJlAx68pWjqq6kzxhF/drIwt96PbyYvyMHwFeKpZYAVZ+BGBHheItq5tIwmdLpsVZ3 dR3Q== X-Forwarded-Encrypted: i=1; AJvYcCW5C+a21UZsXYQWPVUSmqUVmSY6hdQuhwq9S56DY/3ezpzcvdlEgWVXDVGOj3ESPmPvxZOKImqaB0s/TN8=@vger.kernel.org X-Gm-Message-State: AOJu0YzEa9nijmk7umg33k0zMfNvsIoqw5Ud2AOAppFcBpC6rdmtfhJc +5qV4+i/vWYak0CVg+CQyQRXK9RhaigeHdZoLXc/o+TpT2+xnMaz9WbW5icICvd0IjGeSw== X-Google-Smtp-Source: AGHT+IGVxYFLO6PMMj5YOBtKJIkBsk+UPteT4nuImFK20RZLySxCL3GaHTYjR4JwUAp1YZGI3vapG7Y+ X-Received: from pfbhd4.prod.google.com ([2002:a05:6a00:6584:b0:72f:8e60:adb8]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e9c:b0:725:e405:6df7 with SMTP id d2e1a72fcca58-7305d46f7bemr390731b3a.10.1738867908791; Thu, 06 Feb 2025 10:51:48 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:00 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-21-fvdl@google.com> Subject: [PATCH v3 20/28] mm/hugetlb: do pre-HVO for bootmem allocated pages 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For large systems, the overhead of vmemmap pages for hugetlb is substantial. It's about 1.5% of memory, which is about 45G for a 3T system. If you want to configure most of that system for hugetlb (e.g. to use as backing memory for VMs), there is a chance of running out of memory on boot, even though you know that the 45G will become available later. To avoid this scenario, and since it's a waste to first allocate and then free that 45G during boot, do pre-HVO for hugetlb bootmem allocated pages ('gigantic' pages). pre-HVO is done by adding functions that are called from sparse_init_nid_early and sparse_init_nid_late. The first is called before memmap allocation, so it takes care of allocating memmap HVO-style. The second verifies that all bootmem pages look good, specifically it checks that they do not intersect with multiple zones. This can only be done from sparse_init_nid_late path, when zones have been initialized. The hugetlb page size must be aligned to the section size, and aligned to the size of memory described by the number of page structures contained in one PMD (since pre-HVO is not prepared to split PMDs). This should be true for most 'gigantic' pages, it is for 1G pages on x86, where both of these alignment requirements are 128M. This will only have an effect if hugetlb_bootmem_alloc was called early in boot. If not, it won't do anything, and HVO for bootmem hugetlb pages works as before. Signed-off-by: Frank van der Linden --- include/linux/hugetlb.h | 2 + mm/hugetlb.c | 4 +- mm/hugetlb_vmemmap.c | 143 ++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 6 ++ mm/sparse-vmemmap.c | 4 ++ 5 files changed, 157 insertions(+), 2 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 10a7ce2b95e1..2512463bca49 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -687,6 +687,8 @@ struct huge_bootmem_page { #define HUGE_BOOTMEM_HVO 0x0001 #define HUGE_BOOTMEM_ZONES_VALID 0x0002 =20 +bool hugetlb_bootmem_page_zones_valid(int nid, struct huge_bootmem_page *m= ); + int isolate_or_dissolve_huge_page(struct page *page, struct list_head *lis= t); int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long en= d_pfn); struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7e0810c217ce..29b3a6e70a53 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3310,8 +3310,8 @@ static void __init prep_and_add_bootmem_folios(struct= hstate *h, } } =20 -static bool __init hugetlb_bootmem_page_zones_valid(int nid, - struct huge_bootmem_page *m) +bool __init hugetlb_bootmem_page_zones_valid(int nid, + struct huge_bootmem_page *m) { unsigned long start_pfn; bool valid; diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index be6b33ecbc8e..9a99dfa3c495 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -743,6 +743,149 @@ void hugetlb_vmemmap_optimize_bootmem_folios(struct h= state *h, struct list_head __hugetlb_vmemmap_optimize_folios(h, folio_list, true); } =20 +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT + +/* Return true of a bootmem allocated HugeTLB page should be pre-HVO-ed */ +static bool vmemmap_should_optimize_bootmem_page(struct huge_bootmem_page = *m) +{ + unsigned long section_size, psize, pmd_vmemmap_size; + phys_addr_t paddr; + + if (!READ_ONCE(vmemmap_optimize_enabled)) + return false; + + if (!hugetlb_vmemmap_optimizable(m->hstate)) + return false; + + psize =3D huge_page_size(m->hstate); + paddr =3D virt_to_phys(m); + + /* + * Pre-HVO only works if the bootmem huge page + * is aligned to the section size. + */ + section_size =3D (1UL << PA_SECTION_SHIFT); + if (!IS_ALIGNED(paddr, section_size) || + !IS_ALIGNED(psize, section_size)) + return false; + + /* + * The pre-HVO code does not deal with splitting PMDS, + * so the bootmem page must be aligned to the number + * of base pages that can be mapped with one vmemmap PMD. + */ + pmd_vmemmap_size =3D (PMD_SIZE / (sizeof(struct page))) << PAGE_SHIFT; + if (!IS_ALIGNED(paddr, pmd_vmemmap_size) || + !IS_ALIGNED(psize, pmd_vmemmap_size)) + return false; + + return true; +} + +/* + * Initialize memmap section for a gigantic page, HVO-style. + */ +void __init hugetlb_vmemmap_init_early(int nid) +{ + unsigned long psize, paddr, section_size; + unsigned long ns, i, pnum, pfn, nr_pages; + unsigned long start, end; + struct huge_bootmem_page *m =3D NULL; + void *map; + + /* + * Noting to do if bootmem pages were not allocated + * early in boot, or if HVO wasn't enabled in the + * first place. + */ + if (!hugetlb_bootmem_allocated()) + return; + + if (!READ_ONCE(vmemmap_optimize_enabled)) + return; + + section_size =3D (1UL << PA_SECTION_SHIFT); + + list_for_each_entry(m, &huge_boot_pages[nid], list) { + if (!vmemmap_should_optimize_bootmem_page(m)) + continue; + + nr_pages =3D pages_per_huge_page(m->hstate); + psize =3D nr_pages << PAGE_SHIFT; + paddr =3D virt_to_phys(m); + pfn =3D PHYS_PFN(paddr); + map =3D pfn_to_page(pfn); + start =3D (unsigned long)map; + end =3D start + nr_pages * sizeof(struct page); + + if (vmemmap_populate_hvo(start, end, nid, + HUGETLB_VMEMMAP_RESERVE_SIZE) < 0) + continue; + + memmap_boot_pages_add(HUGETLB_VMEMMAP_RESERVE_SIZE / PAGE_SIZE); + + pnum =3D pfn_to_section_nr(pfn); + ns =3D psize / section_size; + + for (i =3D 0; i < ns; i++) { + sparse_init_early_section(nid, map, pnum, + SECTION_IS_VMEMMAP_PREINIT); + map +=3D section_map_size(); + pnum++; + } + + m->flags |=3D HUGE_BOOTMEM_HVO; + } +} + +void __init hugetlb_vmemmap_init_late(int nid) +{ + struct huge_bootmem_page *m, *tm; + unsigned long phys, nr_pages, start, end; + unsigned long pfn, nr_mmap; + struct hstate *h; + void *map; + + if (!hugetlb_bootmem_allocated()) + return; + + if (!READ_ONCE(vmemmap_optimize_enabled)) + return; + + list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) { + if (!(m->flags & HUGE_BOOTMEM_HVO)) + continue; + + phys =3D virt_to_phys(m); + h =3D m->hstate; + pfn =3D PHYS_PFN(phys); + nr_pages =3D pages_per_huge_page(h); + + if (!hugetlb_bootmem_page_zones_valid(nid, m)) { + /* + * Oops, the hugetlb page spans multiple zones. + * Remove it from the list, and undo HVO. + */ + list_del(&m->list); + + map =3D pfn_to_page(pfn); + + start =3D (unsigned long)map; + end =3D start + nr_pages * sizeof(struct page); + + vmemmap_undo_hvo(start, end, nid, + HUGETLB_VMEMMAP_RESERVE_SIZE); + nr_mmap =3D end - start - HUGETLB_VMEMMAP_RESERVE_SIZE; + memmap_boot_pages_add(DIV_ROUND_UP(nr_mmap, PAGE_SIZE)); + + memblock_phys_free(phys, huge_page_size(h)); + continue; + } else + m->flags |=3D HUGE_BOOTMEM_ZONES_VALID; + } +} +#endif + static const struct ctl_table hugetlb_vmemmap_sysctls[] =3D { { .procname =3D "hugetlb_optimize_vmemmap", diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 926b8b27b5cb..0031e49b12f7 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -9,6 +9,8 @@ #ifndef _LINUX_HUGETLB_VMEMMAP_H #define _LINUX_HUGETLB_VMEMMAP_H #include +#include +#include =20 /* * Reserve one vmemmap page, all vmemmap addresses are mapped to it. See @@ -25,6 +27,10 @@ long hugetlb_vmemmap_restore_folios(const struct hstate = *h, void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *= folio); void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_list); void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list= _head *folio_list); +#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT +void hugetlb_vmemmap_init_early(int nid); +void hugetlb_vmemmap_init_late(int nid); +#endif =20 =20 static inline unsigned int hugetlb_vmemmap_size(const struct hstate *h) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 8cc848c4b17c..fd2ab5118e13 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -32,6 +32,8 @@ #include #include =20 +#include "hugetlb_vmemmap.h" + /* * Flags for vmemmap_populate_range and friends. */ @@ -594,6 +596,7 @@ struct page * __meminit __populate_section_memmap(unsig= ned long pfn, */ void __init sparse_vmemmap_init_nid_early(int nid) { + hugetlb_vmemmap_init_early(nid); } =20 /* @@ -604,5 +607,6 @@ void __init sparse_vmemmap_init_nid_early(int nid) */ void __init sparse_vmemmap_init_nid_late(int nid) { + hugetlb_vmemmap_init_late(nid); } #endif --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F5D61DDC0B for ; Thu, 6 Feb 2025 18:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867912; cv=none; b=HzyV2qvHZTiQJEiJgBVb116eA5yxnjIFoF/V8D4AtWiy+QHZipWY2hbRHxJTOMFwd02Gq8wrtzICidekussGcBEmchdWA8wqisHUStDRaJpN3MBZpF6kwHZ7xx2Pd0bnZPCOARDivE12bmBX3wzhLOZPLwwfGgK2fxvCI370ep0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867912; c=relaxed/simple; bh=8gup9qocsMoQyKIiT/PYPMfM+/Pc3XHTTCQXaHLecVY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fD/LZ1f863Y3DPEdBLONie7S9UDbLsl30fW5KYnykWrp+XG09kmOJRAUcYapDzfm3Sk/J3vLNiGklgiLQWo7ZJs4ShpUYMuODrhTk3lh2xo0DK17YBfUNW/1URDSXeh2FrxGJ/djP0WI+9aKBKYsLrEJr1HlRO0NpiltFjy/JwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qztbxq+A; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qztbxq+A" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f83e54432dso3848147a91.2 for ; Thu, 06 Feb 2025 10:51:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867910; x=1739472710; darn=vger.kernel.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=2/3TeRATauBC6P9iV7Z+/8og+I9Z6a+H92nsoJ8VemE=; b=qztbxq+A8ebbECnBVafzxd2FcBEnbEY2L58E1btElBtYGdXGHCBz13BsGwQqs2kJKt Ac5IcPsGBUHLaoJFezudiGBQqmV1YqEfCCvw+wYsVLnJ14xDCgyAPkH6mT6N1SpYk2DQ OMvvgHYzFu/xeFjZFhnotPVf6jfc9se9rKYDpwVAX6lPsS0e83pFkPczvuj9ikSACiVQ Jp/jGDRe1joPmefNHzKx/XVu2pkRvrOYlajMkyruZ6BL/tvnQopTOGIRn9pwFwVPOHAi 4WLSEouwMO0u2qIxcqRN4sjwobKgYTNzsdbVfSZWqf9ez4h1StmBC8+YfhNeY+9722Xk I67g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867910; x=1739472710; 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=2/3TeRATauBC6P9iV7Z+/8og+I9Z6a+H92nsoJ8VemE=; b=OnF4cOZ9e4ga76vDwDjH3JviDXdxaBr4qpC9CKK1N0vBBzNF3hMVmd0KCQmpMI+2s+ 9L4hlK9a6KeFqpAB8xL6jnsNtwEupcEeqSTGaezdJ4H/oDv6/Dt5FOAZYy+eEPNgQbeQ JjNdvKjI3QooGKsFDHPs/RIYN76X0QZoIEZDIjUi1PPvN25Y2rF2wU4RoQKcnkP5HHsi 2SDRG+TZ00YyevE2cNCyhbvvkd86UWPluFw3hNwm/rLp9pcL4uB3PqK2IgUMN4d/aKt3 s27hlE6Mq5qKNQHCPeYNgaBisbykigA0mFrLRr7TNf3/DSqOmhhqNDPr7/Rqvl39PX1J HlQw== X-Forwarded-Encrypted: i=1; AJvYcCWrqcYfmQnozDnTbWBNQUGyjLtley+zyfkXcZT5yNdyLoVB5adkSgs/Y7AR9AAMUOUT76G/4/xNz927FiM=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1shr71J1R0aUiQJCp4BoQlA15/PIRO8AGnDcuB9sbQxqBI0Nr LlhxiOnzoDMVnITd8EeTFyeTcbro5vrW0d35uJ5l6K3TcvKyU0gmBFlvBc08g4hIEBrFkQ== X-Google-Smtp-Source: AGHT+IFeJiIMbGUP+s5i04ssCNNkL93EOBt0wJtECDoXfHqJC0vwkVyoIA7fvHTV0zvzaIRZ3vC9thCy X-Received: from pjbpl16.prod.google.com ([2002:a17:90b:2690:b0:2e9:ee22:8881]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3dc3:b0:2ee:edae:75e with SMTP id 98e67ed59e1d1-2fa24067d7amr209175a91.13.1738867910671; Thu, 06 Feb 2025 10:51:50 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:01 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-22-fvdl@google.com> Subject: [PATCH v3 21/28] x86/setup: call hugetlb_bootmem_alloc early 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 , Dave Hansen , Andy Lutomirski , Peter Zijlstra Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Call hugetlb_bootmem_allloc in an earlier spot in setup, after hugelb_cma_reserve. This will make vmemmap preinit of the sections covered by the allocated hugetlb pages possible. Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Signed-off-by: Frank van der Linden --- arch/x86/kernel/setup.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index cebee310e200..ff8604007b08 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1108,8 +1108,10 @@ void __init setup_arch(char **cmdline_p) initmem_init(); dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT); =20 - if (boot_cpu_has(X86_FEATURE_GBPAGES)) + if (boot_cpu_has(X86_FEATURE_GBPAGES)) { hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); + hugetlb_bootmem_alloc(); + } =20 /* * Reserve memory for crash kernel after SRAT is parsed so that it --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0EBF1DE3AF for ; Thu, 6 Feb 2025 18:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867915; cv=none; b=ALgByTnP0t7khbxP4nGNmDpNje1JV15uDbrggbQEs9FONJtnMzZWgmR8pxvkkwexF4BJGy18hCbMDIOP4bJX1ElMs04eGW6oqx2XPnp9Vvhk1eBIBINPuL9N4rM8GuyyLhckIlCoaxH/wUzho0p4eU6bJk/tdWBfoLdah1lIkJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867915; c=relaxed/simple; bh=VSTQXHUskWfH73SfXN+tCVhEcDsqnHB53YedVPhqCp8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BRobLQwZ80w7ei2zo+FlvLzQLuUW74p1WSYE9zhsQV7iKqrG9m3YKKnZJAMqhJ3mz4WFKDU+Lysm4eZpUlcedYnIKBETAgC3oL4nYke+E0nQA0v1S7sKqN883vjCafXrAA7X7YoR8fPBhtfBtTK+xWBQSeRR8puvuxnyRI8elyI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qSX7fTj+; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qSX7fTj+" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166464e236so39898325ad.1 for ; Thu, 06 Feb 2025 10:51:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867912; x=1739472712; darn=vger.kernel.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=p7TeecwDbtqxeCEQgMqbe8f8xJagmIFoeC0bQBtv3nE=; b=qSX7fTj+EnLOsWUNChn/3dUrkuiSKJ5XdS4KJkAGXf92IK4UmWN7fWmYJjgVMYF9hq yL6Pb26L463Wnqb0rje1glkSl3cxKtU1p3XhICu07+xhRXyMW/cXvIquer0F5UC/A+yC 0bFe6UBVOR8QIPymw7YbNrVj0sgwfUzHgDSTx4owiW24ub3nBbxJe7nseF4c/tknTYUu GDdnn/47qh30SnpretvVCM1/4qXeM8WgISB0RHkBgzAqtx0dq+erJu3A7MwM2vDnWxJX 8PDsREyvJQhJk08+MB95MtTB2aPHIXhoa0rXK5CkJlLCHd2pZCgrLRHTiDQEVj9cI8ff cT9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867912; x=1739472712; 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=p7TeecwDbtqxeCEQgMqbe8f8xJagmIFoeC0bQBtv3nE=; b=HHz1jrCDbp1fYjnDaUYkO7XuOvevyECJqmunVOVPxgSectxz8fqg3e95KVZw4OEDH2 B/PQ+IoKmHQ28lHRYClzSKun1spfXArG29bjjZ9teJ1jbiwHfL72fgjc9vdFQ2wHnMyZ mMSuJAHbPDM7ZjiuM98eegbDmwZh+KzhKKIkwnRbLIDo7XBDk3Omqfu/OLqXlW/W8KuF oPNH7Mxobx0RDyjHLcsmz4GgINS9Yo9FM0drXEsGZHdTne9K/3XRA6ftAX2ezS9te4vl axNm/f/9gxEZiQNpie+hLB06RcHoOv+YKTsjka3bPOuVTVskDYKIS3r4mLZ6owIZOEiL d/pQ== X-Forwarded-Encrypted: i=1; AJvYcCW+ApJa7Af2vdhCa5Y8imeXeMm1ZHqHCRozIDvIbeu+h8eYQY/MGOc07ltTRVcIJcinuHK1Jlhpzc3+DIQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxxVd3Wqh/FWkPeGhHPksd48GBcIBdQYxRgqycCDwai25y4YJBO JuhgzkqDsbYEsr6mbawQQyUyUNd7YhfIrM3T5W01PT43gGiue7Sd8LCUah093w+hfJ5CWw== X-Google-Smtp-Source: AGHT+IGTkHaS4WsaD1MB5yDYw0Ec61JJnhUDeKTqMYnnbssbsNlTFClrJPdlXWxU8MqAbVgIpap4v6N7 X-Received: from plec12.prod.google.com ([2002:a17:902:f30c:b0:216:5566:3c11]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c8:b0:215:b74c:d7ad with SMTP id d9443c01a7336-21f4e73b574mr2943595ad.36.1738867912218; Thu, 06 Feb 2025 10:51:52 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:02 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-23-fvdl@google.com> Subject: [PATCH v3 22/28] x86/mm: set ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that hugetlb bootmem pages are allocated earlier, and available for section preinit (HVO-style), set ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT for x86_64, so that is can be done. This enables pre-HVO on x86_64. Signed-off-by: Frank van der Linden --- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 87198d957e2f..ccef99c0a2ba 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -146,6 +146,7 @@ config X86 select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP if X86_64 select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64 + select ARCH_WANT_SPARSEMEM_VMEMMAP_PREINIT if X86_64 select ARCH_WANTS_THP_SWAP if X86_64 select ARCH_HAS_PARANOID_L1D_FLUSH select BUILDTIME_TABLE_SORT --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A02AE1DE3BD for ; Thu, 6 Feb 2025 18:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867916; cv=none; b=rKvY45twInLJ/Vr6MNm0XomwGNWaa/ewLlRDT50wyJJgNoW7swE5nIJMPrecjhSvbtXBKaPa5PKTaIXAWGC44cMs3Z4b92iAEpbkWnukn0lvz3ZaAw+PCGCxq17j6Yv98OX0qTmAdF07PMZbHa1u7r+KApzZYr3EwluAfm4zRFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867916; c=relaxed/simple; bh=/gVxm+CO3j4k2UrkTYq4821BXZp+btUj4aX7Ztv8FA4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eyuuzkpfu6tnS6QNColswu4oDKloeUmauY1JBjxAs573vz9Sih0mlXPwLNk7FRAgKh+7yISTO+2/mjDhXwKJ03QEEu2xvmy+aifRjvW44Dq41nJrWi1RuM7AAV4JnoRFerYvy7Sjo0bP3pIQrsJ1RJPIvePWWqAwAAgZBgEo0HQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QS4b/0r2; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QS4b/0r2" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9d1ca50c9so3896771a91.0 for ; Thu, 06 Feb 2025 10:51:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867914; x=1739472714; darn=vger.kernel.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=LNVYMIarRsr0t8rcUQ946hUXqx5cMeLkMOmL07Z29aU=; b=QS4b/0r2A7wyd9WVSZ3ngjv0jYZUhbgBCm5QTJibvmhgQUmmh3qhfYPrek9kVRlM0Z CiPVqZapVzFlvpnOjGWqjvwWnuJTcWT/mDJRMYkrKmKhiWiKzKUtYMdIzvjKMKazGtur 0OY4HtrKvNllCZKziNDjk5VqnxvedEtmgvzvGWrCfDv/biXcK6pDQDRkjdgUm62DxrfP Sega3krKLjjhdqSG2sDTu9c32Vo2CvNQhfR8AHl4Srzx5855l0cdoWaHCoWeD7l8FYch S/BntJIX/q6AP30SjPXMtUQpZSaRulFk0DrYUb1ZpzXPF8HvrPcwX2kh6Vn3FVARo4kt 9u9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867914; x=1739472714; 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=LNVYMIarRsr0t8rcUQ946hUXqx5cMeLkMOmL07Z29aU=; b=CdqejdpKgMHuVNVJdVQ20OCoenmm/r0ZUBiDAH/7J8qDpKicGH1RUeqrTXiNNl5Jmw NZ65eAEBi0cM2XbS31hsrw9EMKu+U4Se1yBNJscS0jsPxUZp3Wevg3QeDi1jwLXFvqJ+ +AoApvs9h0iRrRwmfSMcefZ8+lQEiwMj8MMizi0KI2umqcm8GiUg9Zow5NCVOK+W2iXq Qyij7ujoa+XMGC6BIa7DqRBmAoHB5uG30/r7ErbbQFOqMld14FNTz5CwGeGUojCQ5nIk OFU8i/qzos4pOL4R2nTtmNpLuh26AFBAYV9P3cdcd16Ddo8J1W/7GMk7mWOVt6A8/g7L cttQ== X-Forwarded-Encrypted: i=1; AJvYcCVL7lDs0wC4B/m09CPuda3yx9kBPy4LKMWTFFHrHzDpOvK+xgo3SKOoOw2CJQNNcrvssF97jo71Ho5HLbM=@vger.kernel.org X-Gm-Message-State: AOJu0YxJCdjtzXZI+dCiRw+A35sBIdVItmGoWNrsneOywXJlaHI0Zaqj An27t3J4R240IucX/B0YJrqVqkWdct/xwP3luIlMgA/26ZE+R6TkiTTp3qCnUs2xvwLiNw== X-Google-Smtp-Source: AGHT+IFAHneXOb/4XfmdG9sW63Is4KRKWVRpRsXVSKXLK7gMZJrhr70i+wLMYHxanmPA9luzKj98Soc+ X-Received: from pfbko5.prod.google.com ([2002:a05:6a00:4605:b0:730:5607:bee4]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:139c:b0:72a:a7a4:b4cd with SMTP id d2e1a72fcca58-7305d54712emr393600b3a.21.1738867913918; Thu, 06 Feb 2025 10:51:53 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:03 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-24-fvdl@google.com> Subject: [PATCH v3 23/28] mm/cma: simplify zone intersection check 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" cma_activate_area walks all pages in the area, checking their zone individually to see if the area resides in more than one zone. Make this a little more efficient by using the recently introduced pfn_range_intersects_zones() function. Store the NUMA node id (if any) in the cma structure to facilitate this. Signed-off-by: Frank van der Linden --- mm/cma.c | 13 ++++++------- mm/cma.h | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index c7116a5819c5..d0227af4bb71 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -103,7 +103,6 @@ static void __init cma_activate_area(struct cma *cma) { unsigned long pfn, base_pfn; int allocrange, r; - struct zone *zone; struct cma_memrange *cmr; =20 for (allocrange =3D 0; allocrange < cma->nranges; allocrange++) { @@ -124,12 +123,8 @@ static void __init cma_activate_area(struct cma *cma) * CMA resv range to be in the same zone. */ WARN_ON_ONCE(!pfn_valid(base_pfn)); - zone =3D page_zone(pfn_to_page(base_pfn)); - for (pfn =3D base_pfn + 1; pfn < base_pfn + cmr->count; pfn++) { - WARN_ON_ONCE(!pfn_valid(pfn)); - if (page_zone(pfn_to_page(pfn)) !=3D zone) - goto cleanup; - } + if (pfn_range_intersects_zones(cma->nid, base_pfn, cmr->count)) + goto cleanup; =20 for (pfn =3D base_pfn; pfn < base_pfn + cmr->count; pfn +=3D pageblock_nr_pages) @@ -261,6 +256,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys= _addr_t size, cma->ranges[0].base_pfn =3D PFN_DOWN(base); cma->ranges[0].count =3D cma->count; cma->nranges =3D 1; + cma->nid =3D NUMA_NO_NODE; =20 *res_cma =3D cma; =20 @@ -497,6 +493,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t tot= al_size, } =20 cma->nranges =3D nr; + cma->nid =3D nid; *res_cma =3D cma; =20 out: @@ -684,6 +681,8 @@ static int __init __cma_declare_contiguous_nid(phys_add= r_t base, if (ret) memblock_phys_free(base, size); =20 + (*res_cma)->nid =3D nid; + return ret; } =20 diff --git a/mm/cma.h b/mm/cma.h index 5f39dd1aac91..ff79dba5508c 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -50,6 +50,8 @@ struct cma { struct cma_kobject *cma_kobj; #endif bool reserve_pages_on_error; + /* NUMA node (NUMA_NO_NODE if unspecified) */ + int nid; }; =20 extern struct cma cma_areas[MAX_CMA_AREAS]; --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35EBD1DE4D0 for ; Thu, 6 Feb 2025 18:51:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867917; cv=none; b=Nn3HZsRAQbhfcq67/cBOk03Gd7eHqLj7FtL4EPm6JSAWKPBNlZobbD8h9yVphwBRzBfi7vJPSF//qK/y1yelLmJFjLIFzMG0BSBFAN6U6vEJynGH/S0GuGGnskzOw3FwyYGC88tiZefG3kQvZJhuuSpf+nbm+m6YhWwyQ4z39rA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867917; c=relaxed/simple; bh=ulolHMBM9mu2AADD0bujgm62JTsEMiIusrvyXP1wP4w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Yynx71cPC0B47rRHDT/R8lK1j3jriv6SwfIklE/QUgv978Y5L+60ToYO6F3RHaQEpuH7KdffQ3l4UTIRI5eRHD+ZGA8cxYlYAxCS/DTKjED029TLFh5DZvAlBmySLJGoCTpjN/G93NGDsqLIPB1SG4qXWiIyySyEQ4JqsSij7eE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PrSyFThs; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PrSyFThs" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216750b679eso20321185ad.1 for ; Thu, 06 Feb 2025 10:51:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867915; x=1739472715; darn=vger.kernel.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=yt0ZJmO4eMnkUw1+mDBNXTwvRT3PLwjDe9mC0zKYM+w=; b=PrSyFThsyPor2Ba6xE9XCi6OhR3pOjqPsjWQUWpxl6SlxUZdFTvD/l8EVJBzi164yX hF7Uc8LYc8Fa307mGZYw9zOx1FjT96xBLHLa1FMkrtcPionnT4Aa7stC56sPmJjY92kt z+E4ZcPAG028IK+R4r46OXzGjKUtBwcw/FavlaXFZ+RxLQlO5J1AMRAzg0qg85ipXu+T VjI3ouiRY5UvPXbod/aY+AFKQcm4N484ZUmxrkzUctoxBguhxm3EdGivovLOJMQmz/cz hLx0qg+U4cCR5zipzfslAvhugJEtQTZ7AKsXCFIKcOBGFRF7XueCJXSaBnCJJgs0pKbN 7Y/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867915; x=1739472715; 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=yt0ZJmO4eMnkUw1+mDBNXTwvRT3PLwjDe9mC0zKYM+w=; b=lZQ3cP4jhHQ47avmXU3uEtRhw/qIaP8Zlb67WuurSIfHzL82ZKmr485qff/kx3KUJA tYJeV+havj7kvk4MUwNQVABzdVfT+NUXFbQclTpz4nFkDrrdnVgjZMsDA7HjtgVgE2YC zkqltt0I2fZFfxUAqViYzkZ0mm2rQCzrGdSm69Ehd9adEC0DvxfSE9LgPOEMgUWjNSmz MTNBycdWisQJM8VuWBPP4wqC86kDPA9LACSc1CcB1qHLsowG3QdmwZLcArWFijrGEzSb Jjr/60n5zysSrVnQrfrfFyU66+vYLkG1BXh0RGGkFmnCs7/vcFwOzgrXY/1qv7peu3Wa vmZg== X-Forwarded-Encrypted: i=1; AJvYcCXqEFLuBtd79Gp7I9fJCYHC6Vk7ccJQo1mQQfKvtODUVhEdW0RDijO8tf8Bp2ItNjOt4M0mbMuIPpfC8wg=@vger.kernel.org X-Gm-Message-State: AOJu0YwFl1FFOO9OPeANMytVUQEej4XWwjs5f6KkiCykEO4XiEO7Rg8J cUIaNFGyPC9sfy2HF824UxZl8qjE6RRSmQSzTiBC84FbSTPCWNyC6sL8gAuJQBrCFigrRw== X-Google-Smtp-Source: AGHT+IFZhH2u1+M+xFH2j0CpMHxi5A7C45tXR32gxTZbZ8iZD/tY8JR/54qpQpgRoE2PyZBUqmPt1YR+ X-Received: from pgcv13.prod.google.com ([2002:a05:6a02:530d:b0:801:d724:5abd]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9f8e:b0:1e1:94a2:275c with SMTP id adf61e73a8af0-1ee03a3f4camr941358637.18.1738867915504; Thu, 06 Feb 2025 10:51:55 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:04 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-25-fvdl@google.com> Subject: [PATCH v3 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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, gf= p_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 =20 #endif diff --git a/mm/cma.c b/mm/cma.c index d0227af4bb71..4388d941d381 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -99,6 +99,49 @@ static void cma_clear_bitmap(struct cma *cma, const stru= ct cma_memrange *cmr, spin_unlock_irqrestore(&cma->lock, flags); } =20 +/* + * 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 =3D test_bit(CMA_ZONES_VALID, &cma->flags); + if (valid_bit_set || test_bit(CMA_ZONES_INVALID, &cma->flags)) + return valid_bit_set; + + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + base_pfn =3D 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; } =20 + if (!cma_validate_zones(cma)) + goto cleanup; + for (r =3D 0; r < cma->nranges; r++) { cmr =3D &cma->ranges[r]; base_pfn =3D 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 =3D base_pfn; pfn < base_pfn + cmr->count; pfn +=3D 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); =20 /* 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 =3D 0; r < allocrange; r++) { cmr =3D &cma->ranges[r]; for (pfn =3D cmr->base_pfn; @@ -172,7 +208,7 @@ core_initcall(cma_init_reserved_areas); =20 void __init cma_reserve_pages_on_error(struct cma *cma) { - cma->reserve_pages_on_error =3D true; + set_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags); } =20 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; }; =20 +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; =20 --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E77A11DE891 for ; Thu, 6 Feb 2025 18:51:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867919; cv=none; b=K58SLZSfvkfnBiabnvGp8Td0ScLqC/x3FmJBeKrZw9k2NNykYWbjpnZSv3NMeF9mtG5dkDIQ+9cV5wZw8sVRikgF/Vr/aYY5AsuBdVv5ZYjqcK4DFYZHlCTpm2NH/kSRZDiX0RWtXHnaMp0X6MfEHv2HEOL63cGEB99LqqHxQ5A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867919; c=relaxed/simple; bh=D84483NlqV10bmFK5GBS+2bbvCjzLXYijg3lhqp9c80=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JIvrbYSx9G0T1sVmILTGGrqR0ELvDEzmtuoDPbKU8JjOVngK9shMIh+RlBeJpWvSasf5SjsvZS1kNwabk+td7LXcHzTAz6tie4zJjOQ/Tge5Tszef/djWo3Az7R2NN2lcls8OxUPAWE6TnE1cls85k80SvhH1x+WX95ZiwZxK8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VVp1kCih; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VVp1kCih" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9c02f54f2so2487215a91.3 for ; Thu, 06 Feb 2025 10:51:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867917; x=1739472717; darn=vger.kernel.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=Os2cqKctxCUcRS8FYnguBqsdmbx6qbrypyMDT5BCAmU=; b=VVp1kCih/33NfGc9NvoHBgP34y9/V09lG9sQWBdJDoBhqBh5m6l4WB/ZtarFkTfp+2 bggaHXXgeWA+s57SWoJtlkrmFaSlBKoyBWuacFZWdV2Qb43XotqT8tgItEHWDyAr9XMj Iks98uGz11oPlHoIlR+1VVCPlfOUQRek31xHd0V3N2FMf97SXLvjTUGksZHSnOkedliC 6bKV9j8AGdBEz+7069V/CeDG9TjcEGHKanOWKv0g3PQpcRfMRtCsyZS1YxMfRxXpByqd 6ym+VjtUrndeiTQXXPTZgdx99Q0VNwNYvDRC+ghcAPkz7r4LAlgk51LYax7th8OnCaQP QoJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867917; x=1739472717; 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=Os2cqKctxCUcRS8FYnguBqsdmbx6qbrypyMDT5BCAmU=; b=niKpG9nI7qxVR2F2GesMt1gC0jvP+XKPfzUsJmcCyzgHErxpPfwBoAkRsjbb4rbgEn UwVCiY3a9qv44M2oFuJGVVNZWhe2Iz8w4DyL0CWqNg6IteRYG0Gke5krhOf/dvUHMjjH nTiWj48LJe2pssRI3RQ64lMjFMS3+bXzQeYU8xRFPDFpI8I3FnK4xNVpGCwN8F7M/GTW S0sn/bKIxQjusPRpjLG+4AeBwwZyGuKkQWgKxPREh8ngcMrB129ONrJNjWqOQDd9X5ov Wc5fUI5ECjNVCJhmNrRlrIX8kGKeLZAfcm7QcQhcS1aUbGSElao2uUpj5uczkqahogOr RORQ== X-Forwarded-Encrypted: i=1; AJvYcCX5wBSxgeELWCw7ZpLAPicyHjMwXl40fJbLzCkWmMuTI5qeto9RD6HUNsNGWIbNpoKZ4U3AtE5mx1QB8fc=@vger.kernel.org X-Gm-Message-State: AOJu0YzTzq2H0BSG+CM5YSOL9dLr2yLiVvyGuo8WnGPCOJczGyDGrqCE G25xwhY6Tza91IQWmO8oppZcqlxoqDXvP/FKSYZIUhGF+I0tKv3zCnkxsYxg+2t6PQ8GuQ== X-Google-Smtp-Source: AGHT+IHVX/1oNg+jUxgfnaihB0fSHJ3u8Hg2NHEhvkhZ5xKtCbM1RyTOrbuLCZ62fn2ArYVFlFmi+NRu X-Received: from pfmb20.prod.google.com ([2002:a05:6a00:1154:b0:730:4a3b:49ca]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:420b:b0:725:e499:5b8a with SMTP id d2e1a72fcca58-7305d505b69mr532061b3a.15.1738867917188; Thu, 06 Feb 2025 10:51:57 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:05 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-26-fvdl@google.com> Subject: [PATCH v3 25/28] mm/cma: introduce interface for early reservations 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It can be desirable to reserve memory in a CMA area before it is activated, early in boot. Such reservations would effectively be memblock allocations, but they can be returned to the CMA area later. This functionality can be used to allow hugetlb bootmem allocations from a hugetlb CMA area. A new interface, cma_reserve_early is introduced. This allows for pageblock-aligned reservations. These reservations are skipped during the initial handoff of pages in a CMA area to the buddy allocator. The caller is responsible for making sure that the page structures are set up, and that the migrate type is set correctly, as with other memblock allocations that stick around. If the CMA area fails to activate (because it intersects with multiple zones), the reserved memory is not given to the buddy allocator, the caller needs to take care of that. Signed-off-by: Frank van der Linden --- mm/cma.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- mm/cma.h | 8 +++++ mm/internal.h | 16 ++++++++++ mm/mm_init.c | 9 ++++++ 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 4388d941d381..34a4df29af72 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -144,9 +144,10 @@ bool cma_validate_zones(struct cma *cma) =20 static void __init cma_activate_area(struct cma *cma) { - unsigned long pfn, base_pfn; + unsigned long pfn, end_pfn; int allocrange, r; struct cma_memrange *cmr; + unsigned long bitmap_count, count; =20 for (allocrange =3D 0; allocrange < cma->nranges; allocrange++) { cmr =3D &cma->ranges[allocrange]; @@ -161,8 +162,13 @@ static void __init cma_activate_area(struct cma *cma) =20 for (r =3D 0; r < cma->nranges; r++) { cmr =3D &cma->ranges[r]; - base_pfn =3D cmr->base_pfn; - for (pfn =3D base_pfn; pfn < base_pfn + cmr->count; + if (cmr->early_pfn !=3D cmr->base_pfn) { + count =3D cmr->early_pfn - cmr->base_pfn; + bitmap_count =3D cma_bitmap_pages_to_bits(cma, count); + bitmap_set(cmr->bitmap, 0, bitmap_count); + } + + for (pfn =3D cmr->early_pfn; pfn < cmr->base_pfn + cmr->count; pfn +=3D pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn)); } @@ -173,6 +179,7 @@ static void __init cma_activate_area(struct cma *cma) INIT_HLIST_HEAD(&cma->mem_head); spin_lock_init(&cma->mem_head_lock); #endif + set_bit(CMA_ACTIVATED, &cma->flags); =20 return; =20 @@ -184,9 +191,8 @@ static void __init cma_activate_area(struct cma *cma) if (!test_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags)) { for (r =3D 0; r < allocrange; r++) { cmr =3D &cma->ranges[r]; - for (pfn =3D cmr->base_pfn; - pfn < cmr->base_pfn + cmr->count; - pfn++) + end_pfn =3D cmr->base_pfn + cmr->count; + for (pfn =3D cmr->early_pfn; pfn < end_pfn; pfn++) free_reserved_page(pfn_to_page(pfn)); } } @@ -290,6 +296,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys= _addr_t size, return ret; =20 cma->ranges[0].base_pfn =3D PFN_DOWN(base); + cma->ranges[0].early_pfn =3D PFN_DOWN(base); cma->ranges[0].count =3D cma->count; cma->nranges =3D 1; cma->nid =3D NUMA_NO_NODE; @@ -509,6 +516,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t tot= al_size, nr, (u64)mlp->base, (u64)mlp->base + size); cmrp =3D &cma->ranges[nr++]; cmrp->base_pfn =3D PHYS_PFN(mlp->base); + cmrp->early_pfn =3D cmrp->base_pfn; cmrp->count =3D size >> PAGE_SHIFT; =20 sizeleft -=3D size; @@ -540,7 +548,6 @@ int __init cma_declare_contiguous_multi(phys_addr_t tot= al_size, pr_info("Reserved %lu MiB in %d range%s\n", (unsigned long)total_size / SZ_1M, nr, nr > 1 ? "s" : ""); - return ret; } =20 @@ -1034,3 +1041,65 @@ bool cma_intersects(struct cma *cma, unsigned long s= tart, unsigned long end) =20 return false; } + +/* + * Very basic function to reserve memory from a CMA area that has not + * yet been activated. This is expected to be called early, when the + * system is single-threaded, so there is no locking. The alignment + * checking is restrictive - only pageblock-aligned areas + * (CMA_MIN_ALIGNMENT_BYTES) may be reserved through this function. + * This keeps things simple, and is enough for the current use case. + * + * The CMA bitmaps have not yet been allocated, so just start + * reserving from the bottom up, using a PFN to keep track + * of what has been reserved. Unreserving is not possible. + * + * The caller is responsible for initializing the page structures + * in the area properly, since this just points to memblock-allocated + * memory. The caller should subsequently use init_cma_pageblock to + * set the migrate type and CMA stats the pageblocks that were reserved. + * + * If the CMA area fails to activate later, memory obtained through + * this interface is not handed to the page allocator, this is + * the responsibility of the caller (e.g. like normal memblock-allocated + * memory). + */ +void __init *cma_reserve_early(struct cma *cma, unsigned long size) +{ + int r; + struct cma_memrange *cmr; + unsigned long available; + void *ret =3D NULL; + + if (!cma || !cma->count) + return NULL; + /* + * Can only be called early in init. + */ + if (test_bit(CMA_ACTIVATED, &cma->flags)) + return NULL; + + if (!IS_ALIGNED(size, CMA_MIN_ALIGNMENT_BYTES)) + return NULL; + + if (!IS_ALIGNED(size, (PAGE_SIZE << cma->order_per_bit))) + return NULL; + + size >>=3D PAGE_SHIFT; + + if (size > cma->available_count) + return NULL; + + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + available =3D cmr->count - (cmr->early_pfn - cmr->base_pfn); + if (size <=3D available) { + ret =3D phys_to_virt(PFN_PHYS(cmr->early_pfn)); + cmr->early_pfn +=3D size; + cma->available_count -=3D size; + return ret; + } + } + + return ret; +} diff --git a/mm/cma.h b/mm/cma.h index bddc84b3cd96..df7fc623b7a6 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -16,9 +16,16 @@ struct cma_kobject { * and the total amount of memory requested, while smaller than the total * amount of memory available, is large enough that it doesn't fit in a * single physical memory range because of memory holes. + * + * Fields: + * @base_pfn: physical address of range + * @early_pfn: first PFN not reserved through cma_reserve_early + * @count: size of range + * @bitmap: bitmap of allocated (1 << order_per_bit)-sized chunks. */ struct cma_memrange { unsigned long base_pfn; + unsigned long early_pfn; unsigned long count; unsigned long *bitmap; #ifdef CONFIG_CMA_DEBUGFS @@ -58,6 +65,7 @@ enum cma_flags { CMA_RESERVE_PAGES_ON_ERROR, CMA_ZONES_VALID, CMA_ZONES_INVALID, + CMA_ACTIVATED, }; =20 extern struct cma cma_areas[MAX_CMA_AREAS]; diff --git a/mm/internal.h b/mm/internal.h index 63fda9bb9426..8318c8e6e589 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -848,6 +848,22 @@ void init_cma_reserved_pageblock(struct page *page); =20 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ =20 +struct cma; + +#ifdef CONFIG_CMA +void *cma_reserve_early(struct cma *cma, unsigned long size); +void init_cma_pageblock(struct page *page); +#else +static inline void *cma_reserve_early(struct cma *cma, unsigned long size) +{ + return NULL; +} +static inline void init_cma_pageblock(struct page *page) +{ +} +#endif + + int find_suitable_fallback(struct free_area *area, unsigned int order, int migratetype, bool only_stealable, bool *can_steal); =20 diff --git a/mm/mm_init.c b/mm/mm_init.c index f7d5b4fe1ae9..f31260fd393e 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2263,6 +2263,15 @@ void __init init_cma_reserved_pageblock(struct page = *page) adjust_managed_page_count(page, pageblock_nr_pages); page_zone(page)->cma_pages +=3D pageblock_nr_pages; } +/* + * Similar to above, but only set the migrate type and stats. + */ +void __init init_cma_pageblock(struct page *page) +{ + set_pageblock_migratetype(page, MIGRATE_CMA); + adjust_managed_page_count(page, pageblock_nr_pages); + page_zone(page)->cma_pages +=3D pageblock_nr_pages; +} #endif =20 void set_zone_contiguous(struct zone *zone) --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CF5E1DE3BD for ; Thu, 6 Feb 2025 18:51:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867921; cv=none; b=gaMTqp4WDuU+VginYCbihLb3ZRBJI0A8L5HXhffTWKB7UjALUzGJnr/TAQxZeYvR9w9hlajArItCvJuld6/VjTuJHOkGhkGMJU5h7Z3Y4EWDq4rhSV6IRvAnCp5+theUS0XL08BFqHEfb79sUbRspg/Xhtt7RDykQYcnnRtMhaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867921; c=relaxed/simple; bh=cTHHYaOJBnAqdjbNsjGEmmVo11mX4fA0kBs0AHyhlp4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DzFypXkkyWe3buBsf6dttrqJNwPp3CxT4fay+Q7h3lLEyNb2tPTy1ngdubgSIisc41+OhDGQcsq+a6pC9+C1My1kbGVs7maS/Jf5P2S7bx90YZASvpqR0URd4SBcKZ3nZRH/p2coFpWdprmSFCbzrrVoOj5WfCymM/2Ze2rwnVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SLPLzzeH; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SLPLzzeH" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216750b679eso20321845ad.1 for ; Thu, 06 Feb 2025 10:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867919; x=1739472719; darn=vger.kernel.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=HzKNFokGTQRMQwkaxAdlaexKlVOh5cMMgkZKyufFdYg=; b=SLPLzzeH9wSC3X4NRaLVK4GTKFCaE8mRKOAVVkCchBHq1K/Rj8dKv7S4fxWh6Pf1uK ZUB8RMDTAINb1mo0ihDENr8GAi6iCkXdiMSfcC7wRxtegCB3uEcSALm52XbHt25YS660 Q78sgAwUu+tbXXvM4CqOElXZdJpFegB2KbjEbPn/aOwvbvUjo/LcAcMq3ZPgZ8hJnlUT 4rpiJwlAQlAaZZmYvX+7GRL622DShFkr+Hkup4Is3wfj+0pdH6PO6UKAqAjRZxo+DL02 o4RJQHfOGM4wFdMKCTcvNLc5+XUT9fhJ/eH6A5JkM0HXGC4i+4Hc1ALH2nOJQ1QEHZCx oYUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867919; x=1739472719; 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=HzKNFokGTQRMQwkaxAdlaexKlVOh5cMMgkZKyufFdYg=; b=TZ1dfC7j06V+57slpPn30nLBm7rVU0llBlub47pOBQWcOF7Ejzbz456JG4FnjtjlkR KJ8/JOcBxTUzc9Pk4gHXdfxSKP6MS4eomg6yiwYy9L6rwqtS8FyyJQebEr+BN2Z70fXU CwruEF2/5dMvLgfhgq4dwwDXsFISgXcRRjNelHnIrVPBKJZ9z4vKs54jW3MYcmvzya7c 30o7aKi+/TVKMA2OotrqsR16YYznGK9aEVD0EUFDPWFkhQMBhhz0jzTc5aL/GQEwAm0k e4eakpNrjDRqMwhYqFMQJXiNvQDpWpy2nMikReHHOadrXAIJ2lB5IcaT/oMygerSkWwe 9Cyg== X-Forwarded-Encrypted: i=1; AJvYcCUKZFA0SiyY/GjGHbMS9+OTw4XSPIbEiiK+GLeFPKsGwbdJa8bBq0LNNxnN5/ZnC40ULOR+MQv4dChu4EE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxz3zPqAD6HMyqyVPmOwbFbedIv28hoIksuZkZSYSFSOihHutfF zri9vOnAuNdDyHeqKZ14zL6hQnYxbWXGcvkalmmRL9cUFEnC/xxC4lzi/JBtGEoO8/MykQ== X-Google-Smtp-Source: AGHT+IE7WeCaaWwdtjLGXnFjJ3HV2HveYH8iYSHnW1lYFmfFYzgFw1cgzb7HpN3mgbnk50S1qEYYDK+c X-Received: from pfblg21.prod.google.com ([2002:a05:6a00:7095:b0:730:46e0:7ad0]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9f8b:b0:1db:c20f:2c4d with SMTP id adf61e73a8af0-1ee03a22c01mr874861637.2.1738867918849; Thu, 06 Feb 2025 10:51:58 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:06 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-27-fvdl@google.com> Subject: [PATCH v3 26/28] mm/hugetlb: add hugetlb_cma_only cmdline option 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an option to force hugetlb gigantic pages to be allocated using CMA only (if hugetlb_cma is enabled). This avoids a fallback to allocation from the rest of system memory if the CMA allocation fails. This makes the size of hugetlb_cma a hard upper boundary for gigantic hugetlb page allocations. This is useful because, with a large CMA area, the kernel's unmovable allocations will have less room to work with and it is undesirable for new hugetlb gigantic page allocations to be done from that remaining area. It will eat in to the space available for unmovable allocations, leading to unwanted system behavior (OOMs because the kernel fails to do unmovable allocations). So, with this enabled, an administrator can force a hard upper bound for runtime gigantic page allocations, and have more predictable system behavior. Signed-off-by: Frank van der Linden --- Documentation/admin-guide/kernel-parameters.txt | 7 +++++++ mm/hugetlb.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index fb8752b42ec8..eb56b251ce10 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1892,6 +1892,13 @@ hugepages using the CMA allocator. If enabled, the boot-time allocation of gigantic hugepages is skipped. =20 + hugetlb_cma_only=3D + [HW,CMA,EARLY] When allocating new HugeTLB pages, only + try to allocate from the CMA areas. + + This option does nothing if hugetlb_cma=3D is not also + specified. + hugetlb_free_vmemmap=3D [KNL] Requires CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP enabled. diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 29b3a6e70a53..24a06d6d3617 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -59,6 +59,7 @@ struct hstate hstates[HUGE_MAX_HSTATE]; static struct cma *hugetlb_cma[MAX_NUMNODES]; static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; #endif +static bool hugetlb_cma_only; static unsigned long hugetlb_cma_size __initdata; =20 __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; @@ -1510,6 +1511,9 @@ static struct folio *alloc_gigantic_folio(struct hsta= te *h, gfp_t gfp_mask, } #endif if (!folio) { + if (hugetlb_cma_only) + return NULL; + folio =3D folio_alloc_gigantic(order, gfp_mask, nid, nodemask); if (!folio) return NULL; @@ -4731,6 +4735,9 @@ static __init void hugetlb_parse_params(void) =20 hcp->setup(hcp->val); } + + if (!hugetlb_cma_size) + hugetlb_cma_only =3D false; } =20 /* @@ -7843,6 +7850,13 @@ static int __init cmdline_parse_hugetlb_cma(char *p) =20 early_param("hugetlb_cma", cmdline_parse_hugetlb_cma); =20 +static int __init cmdline_parse_hugetlb_cma_only(char *p) +{ + return kstrtobool(p, &hugetlb_cma_only); +} + +early_param("hugetlb_cma_only", cmdline_parse_hugetlb_cma_only); + void __init hugetlb_cma_reserve(int order) { unsigned long size, reserved, per_node; --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 446FC1DEFDC for ; Thu, 6 Feb 2025 18:52:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867923; cv=none; b=T8JfXvAWT2qsVdceRUQqkywlo61CmULC3Wo1qgR0Qg5zclW4qzGe9zQeV/cP53sWCESsx3Ggt4i7HzWDfMxiCkSMOsn9LQSEc3zVjU/7XSw1wi7LevySU2qA0KOsNNNSGvlrZGBJRzIuUQ03tsCpm4UmHMBqfxhMjK4sI4yHYLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867923; c=relaxed/simple; bh=cyrSeMc5hJcu2E8e01CVDvVUc6jKADKmcci8d0KJDzw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fj1XrivqsmbwHs1WYoulmwNU+Ei/YVDJ2aawoDyJv6vn+NKE6NFw337Atu61CydnIZJVwDxQSJ7lhcfITUCbWRzqH2qfcotXgIPmHvVT/0+fVLt5rl3OHZfrV/fMkT0EGHhuqUx98r5V2H6Ml3hNvNSLAMY6TR7Y4qfojxoPr+Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3MGSDr5h; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3MGSDr5h" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f5538a2356so2572305a91.2 for ; Thu, 06 Feb 2025 10:52:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867920; x=1739472720; darn=vger.kernel.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=9FHf7X3ntolDh58znF+Bk2cZ6pAkHciPEwA7XP/h9Ew=; b=3MGSDr5hya/mRS6FhhMVfAodpItLPD3wiuWPlCZ5INw9oCnuOeVAPDn4PyIGzPmAk/ S0YyovouGvsceJL+8fk6O8t6+lqjvFrY3Dzv/2ufuSA5megcy5d3677JMIhEiC4s2AbB 5rcFPko3FfcnoMAt5rD2H2Y6H/CXdJhbInj0jDbpBHybqW0ajQIMA9grqzU3MgDR+OmX ZNtf9ObJI0tj4RmFwwJaK+XDati4y6fjNSnfuy8TRbLN1v8cFw+PZdw9X4XVmQkP5csp F0IH/dsJKtPPJTHUJEO2er8BEsYFSkhPd5FSDGTqAzvXnVgP32MGoYkftylHNmq8BqHk zCWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867920; x=1739472720; 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=9FHf7X3ntolDh58znF+Bk2cZ6pAkHciPEwA7XP/h9Ew=; b=T7VegQ0Qe1BUBWSK+hDBVwcPsqhQI4yh/doHntpAXztcQXkX3Ek9YIlN/7zo4VeIkv 3hjD73AAzC11KrggLs0QsLAV3V5FqQscnMlUtqJSrlTtswnuQCaUO9CJdRLLdj4wNJKS sGDdq0CacRjWbedIlDj/VMSMqJcJESbxtdu1r7f1Sh9aaF0URj56SzWKN+VcD7kKyfjA 1FmeakbX/s15ENNAxEp3G/I4DQ8FW+DSNUrX+zDaApWDGpHYY72YgEoSsMQ1Ub1NxeuC tTcKj+jIBgRlcyL+ZX1wBhXzB5LRq7D3PSHgHnOjcr5Zn/yc2+d5wn8nlb/Nu0RfAWB3 vaKw== X-Forwarded-Encrypted: i=1; AJvYcCUNoF1fiNO2854qxzatK3PDDPbICPYVa9kakXpqqIS8240Ig1E+WDp6447liXqKIdikU7SK4tAgzrE0Y3s=@vger.kernel.org X-Gm-Message-State: AOJu0YzfJCHAf+J5nx+jMUPIdmz01tJz3MIPE1Yohj4P9oJzZZUJHpWR /KVIQhmw+vvYmJCb9M8OVdIgycI8pcPQ6JdgHi6noVG0ULIh34xwshKdWL2LiWViExGmWA== X-Google-Smtp-Source: AGHT+IF0hEN8V7uU06C9tElOiOiDXvl7WJ62royjKbG+oprlqtroJ24zLedDhZAejMw2Fg4HCqLgipPv X-Received: from pfej4.prod.google.com ([2002:aa7:8d04:0:b0:725:c7de:e052]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4615:b0:72d:35ed:214b with SMTP id d2e1a72fcca58-7305d515e41mr566746b3a.24.1738867920500; Thu, 06 Feb 2025 10:52:00 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:07 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-28-fvdl@google.com> Subject: [PATCH v3 27/28] mm/hugetlb: enable bootmem allocation from CMA areas 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 , Madhavan Srinivasan , Michael Ellerman , linuxppc-dev@lists.ozlabs.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If hugetlb_cma_only is enabled, we know that hugetlb pages can only be allocated from CMA. Now that there is an interface to do early reservations from a CMA area (returning memblock memory), it can be used to allocate hugetlb pages from CMA. This also allows for doing pre-HVO on these pages (if enabled). Make sure to initialize the page structures and associated data correctly. Create a flag to signal that a hugetlb page has been allocated from CMA to make things a little easier. Some configurations of powerpc have a special hugetlb bootmem allocator, so introduce a boolean arch_specific_huge_bootmem_alloc that returns true if such an allocator is present. In that case, CMA bootmem allocations can't be used, so check that function before trying. Cc: Madhavan Srinivasan Cc: Michael Ellerman Cc: linuxppc-dev@lists.ozlabs.org Signed-off-by: Frank van der Linden --- arch/powerpc/include/asm/book3s/64/hugetlb.h | 6 + include/linux/hugetlb.h | 17 +++ mm/hugetlb.c | 121 ++++++++++++++----- 3 files changed, 113 insertions(+), 31 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/hugetlb.h b/arch/powerpc/in= clude/asm/book3s/64/hugetlb.h index f0bba9c5f9c3..bb786694dd26 100644 --- a/arch/powerpc/include/asm/book3s/64/hugetlb.h +++ b/arch/powerpc/include/asm/book3s/64/hugetlb.h @@ -94,4 +94,10 @@ static inline int check_and_get_huge_psize(int shift) return mmu_psize; } =20 +#define arch_has_huge_bootmem_alloc arch_has_huge_bootmem_alloc + +static inline bool arch_has_huge_bootmem_alloc(void) +{ + return (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled()); +} #endif diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 2512463bca49..6c6546b54934 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -591,6 +591,7 @@ enum hugetlb_page_flags { HPG_freed, HPG_vmemmap_optimized, HPG_raw_hwp_unreliable, + HPG_cma, __NR_HPAGEFLAGS, }; =20 @@ -650,6 +651,7 @@ HPAGEFLAG(Temporary, temporary) HPAGEFLAG(Freed, freed) HPAGEFLAG(VmemmapOptimized, vmemmap_optimized) HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable) +HPAGEFLAG(Cma, cma) =20 #ifdef CONFIG_HUGETLB_PAGE =20 @@ -678,14 +680,18 @@ struct hstate { char name[HSTATE_NAME_LEN]; }; =20 +struct cma; + struct huge_bootmem_page { struct list_head list; struct hstate *hstate; unsigned long flags; + struct cma *cma; }; =20 #define HUGE_BOOTMEM_HVO 0x0001 #define HUGE_BOOTMEM_ZONES_VALID 0x0002 +#define HUGE_BOOTMEM_CMA 0x0004 =20 bool hugetlb_bootmem_page_zones_valid(int nid, struct huge_bootmem_page *m= ); =20 @@ -823,6 +829,17 @@ static inline pte_t arch_make_huge_pte(pte_t entry, un= signed int shift, } #endif =20 +#ifndef arch_has_huge_bootmem_alloc +/* + * Some architectures do their own bootmem allocation, so they can't use + * early CMA allocation. + */ +static inline bool arch_has_huge_bootmem_alloc(void) +{ + return false; +} +#endif + static inline struct hstate *folio_hstate(struct folio *folio) { VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 24a06d6d3617..737ffd223d45 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -131,8 +131,10 @@ static void hugetlb_free_folio(struct folio *folio) #ifdef CONFIG_CMA int nid =3D folio_nid(folio); =20 - if (cma_free_folio(hugetlb_cma[nid], folio)) + if (folio_test_hugetlb_cma(folio)) { + WARN_ON_ONCE(!cma_free_folio(hugetlb_cma[nid], folio)); return; + } #endif folio_put(folio); } @@ -1508,6 +1510,9 @@ static struct folio *alloc_gigantic_folio(struct hsta= te *h, gfp_t gfp_mask, break; } } + + if (folio) + folio_set_hugetlb_cma(folio); } #endif if (!folio) { @@ -3174,6 +3179,53 @@ struct folio *alloc_hugetlb_folio(struct vm_area_str= uct *vma, return ERR_PTR(-ENOSPC); } =20 +static bool __init hugetlb_early_cma(struct hstate *h) +{ + if (arch_has_huge_bootmem_alloc()) + return false; + + return (hstate_is_gigantic(h) && hugetlb_cma_only); +} + +static __init void *alloc_bootmem(struct hstate *h, int nid) +{ + struct huge_bootmem_page *m; + unsigned long flags; + struct cma *cma; + +#ifdef CONFIG_CMA + if (hugetlb_early_cma(h)) { + flags =3D HUGE_BOOTMEM_CMA; + cma =3D hugetlb_cma[nid]; + m =3D cma_reserve_early(cma, huge_page_size(h)); + } else +#endif + { + flags =3D 0; + cma =3D NULL; + m =3D memblock_alloc_try_nid_raw(huge_page_size(h), + huge_page_size(h), 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); + } + + if (m) { + /* + * Use the beginning of the huge page to store the + * huge_bootmem_page struct (until gather_bootmem + * puts them into the mem_map). + * + * Put them into a private list first because mem_map + * is not up yet. + */ + INIT_LIST_HEAD(&m->list); + list_add(&m->list, &huge_boot_pages[nid]); + m->hstate =3D h; + m->flags =3D flags; + m->cma =3D cma; + } + + return m; +} + int alloc_bootmem_huge_page(struct hstate *h, int nid) __attribute__ ((weak, alias("__alloc_bootmem_huge_page"))); int __alloc_bootmem_huge_page(struct hstate *h, int nid) @@ -3183,17 +3235,14 @@ int __alloc_bootmem_huge_page(struct hstate *h, int= nid) =20 /* do node specific alloc */ if (nid !=3D NUMA_NO_NODE) { - m =3D memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h), - 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); + m =3D alloc_bootmem(h, node); if (!m) return 0; goto found; } /* allocate from next node when distributing huge pages */ for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, &node_= states[N_ONLINE]) { - m =3D memblock_alloc_try_nid_raw( - huge_page_size(h), huge_page_size(h), - 0, MEMBLOCK_ALLOC_ACCESSIBLE, node); + m =3D alloc_bootmem(h, node); if (m) break; } @@ -3202,7 +3251,6 @@ int __alloc_bootmem_huge_page(struct hstate *h, int n= id) return 0; =20 found: - /* * Only initialize the head struct page in memmap_init_reserved_pages, * rest of the struct pages will be initialized by the HugeTLB @@ -3212,18 +3260,6 @@ int __alloc_bootmem_huge_page(struct hstate *h, int = nid) */ memblock_reserved_mark_noinit(virt_to_phys((void *)m + PAGE_SIZE), huge_page_size(h) - PAGE_SIZE); - /* - * Use the beginning of the huge page to store the - * huge_bootmem_page struct (until gather_bootmem - * puts them into the mem_map). - * - * Put them into a private list first because mem_map - * is not up yet. - */ - INIT_LIST_HEAD(&m->list); - list_add(&m->list, &huge_boot_pages[node]); - m->hstate =3D h; - m->flags =3D 0; return 1; } =20 @@ -3264,13 +3300,25 @@ static void __init hugetlb_folio_init_vmemmap(struc= t folio *folio, prep_compound_head((struct page *)folio, huge_page_order(h)); } =20 +static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m) +{ + return m->flags & HUGE_BOOTMEM_HVO; +} + +static bool __init hugetlb_bootmem_page_earlycma(struct huge_bootmem_page = *m) +{ + return m->flags & HUGE_BOOTMEM_CMA; +} + /* * memblock-allocated pageblocks might not have the migrate type set * if marked with the 'noinit' flag. Set it to the default (MIGRATE_MOVABL= E) - * here. + * here, or MIGRATE_CMA if this was a page allocated through an early CMA + * reservation. * - * Note that this will not write the page struct, it is ok (and necessary) - * to do this on vmemmap optimized folios. + * In case of vmemmap optimized folios, the tail vmemmap pages are mapped + * read-only, but that's ok - for sparse vmemmap this does not write to + * the page structure. */ static void __init hugetlb_bootmem_init_migratetype(struct folio *folio, struct hstate *h) @@ -3279,9 +3327,13 @@ static void __init hugetlb_bootmem_init_migratetype(= struct folio *folio, =20 WARN_ON_ONCE(!pageblock_aligned(folio_pfn(folio))); =20 - for (i =3D 0; i < nr_pages; i +=3D pageblock_nr_pages) - set_pageblock_migratetype(folio_page(folio, i), + for (i =3D 0; i < nr_pages; i +=3D pageblock_nr_pages) { + if (folio_test_hugetlb_cma(folio)) + init_cma_pageblock(folio_page(folio, i)); + else + set_pageblock_migratetype(folio_page(folio, i), MIGRATE_MOVABLE); + } } =20 static void __init prep_and_add_bootmem_folios(struct hstate *h, @@ -3327,10 +3379,16 @@ bool __init hugetlb_bootmem_page_zones_valid(int ni= d, return true; } =20 + if (hugetlb_bootmem_page_earlycma(m)) { + valid =3D cma_validate_zones(m->cma); + goto out; + } + start_pfn =3D virt_to_phys(m) >> PAGE_SHIFT; =20 valid =3D !pfn_range_intersects_zones(nid, start_pfn, pages_per_huge_page(m->hstate)); +out: if (!valid) hstate_boot_nrinvalid[hstate_index(m->hstate)]++; =20 @@ -3359,11 +3417,6 @@ static void __init hugetlb_bootmem_free_invalid_page= (int nid, struct page *page, } } =20 -static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m) -{ - return (m->flags & HUGE_BOOTMEM_HVO); -} - /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. @@ -3413,6 +3466,9 @@ static void __init gather_bootmem_prealloc_node(unsig= ned long nid) */ folio_set_hugetlb_vmemmap_optimized(folio); =20 + if (hugetlb_bootmem_page_earlycma(m)) + folio_set_hugetlb_cma(folio); + list_add(&folio->lru, &folio_list); =20 /* @@ -3605,8 +3661,11 @@ static void __init hugetlb_hstate_alloc_pages(struct= hstate *h) { unsigned long allocated; =20 - /* skip gigantic hugepages allocation if hugetlb_cma enabled */ - if (hstate_is_gigantic(h) && hugetlb_cma_size) { + /* + * Skip gigantic hugepages allocation if early CMA + * reservations are not available. + */ + if (hstate_is_gigantic(h) && hugetlb_cma_size && !hugetlb_early_cma(h)) { pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation= \n"); return; } --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 00:47:09 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF97D1DEFF5 for ; Thu, 6 Feb 2025 18:52:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867924; cv=none; b=dahubKi3i+MLHQ6N93CEx2btcrPriBs3I25GRgKmOQg0DD7P1tCvIPZoD7WsZWviR/9vZBhAGcKFlYT9c/RhygUaAIlZSR4BQImyd0redjYQpKgwtZeUuCkEIAAXRKWpXP7tQkvw1yeuzRDV3iwnERXLRkyWOF0tLuuG1WvcYZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738867924; c=relaxed/simple; bh=lUVJq72Dt1yIUoe+9rZ3MtKQBKd29l39pHoyXgvDVI8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PKAJD/noOcB/2S96jyjAV0VxFTNp7ktDuT2tXB9SANl/wvyIMslXYhXXZkZwuFarEJxeTYgOzDzAOzECjMzifGHiWlaNmNhDkXdYBmUgQcDzMaJbV9/vHyYWXwjpLfvBuocUsX9s+H0wuQrgMFL3118HapTj5S79ksBUl7gfzeA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0Z6fGNgA; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0Z6fGNgA" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9fdc4dc84so2493965a91.2 for ; Thu, 06 Feb 2025 10:52:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867922; x=1739472722; darn=vger.kernel.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=RA4qMEgFAFc3FubUWoNTLw2qbOjPU8qUi4LYfmqlluQ=; b=0Z6fGNgAPxODy8VQQrwqg0UgDBZh9llfCNc5QhtnvihouRz+GwmA5YIgc3o5g1CfD9 69bRHV/bGNqswQzQXXZFiI8X0aCzhXoVmCdFb3P6FI45g9aBkZ3862vRN1Ny5MatVU2b 2PeEbE2RH+T15roPpe3nU+h/hsGDbNKc/hQ5G4hGN4nVf9yQ4QgMjdcMBb6edz5VNWxq wsg9YueXEP3lT1CIq6SX7FEBWc0ZSxIJ1p84riC5+lfq4kdOHdcvUZqF35fcHI/l5pGF 2YWbPMhf1KfkL/6mAFeYl155a2kx8RM+rd5E8uDfjHy1YESk+WcVGcODKXTHmoWHSRPf TSnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867922; x=1739472722; 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=RA4qMEgFAFc3FubUWoNTLw2qbOjPU8qUi4LYfmqlluQ=; b=kFSWp1EyyjsJ6wYokPtNa3RY4fR+kbStTUFmUB+wq5CMRv+BXTzN8roq7HZn/BOZz1 McgaQreVsGpEKvUYQf/bKNrr5oIYHAPzQvVgG1tt0c5gIvIw6/PFt8Kzl5izgnrrmXTt N6HS4P+E7AgZJe7/nLyHM6vyRqWTM1fegGLdKZPlluTCsZJmsQ9BuddfTvkJWdhc2ZtG vKSoJG0YJwfn5ZQ1BjRfRSTFIKEkfBtHTioZ8pzgQAP0HCMPSlC3tgTClvV4ylT99OAi gFXz3aASPIP8TfMQSTtJeDYDceBV2F+pSSfeYQcoS7UuAAmeVadWvBjoL/83JgM7R5uy 7DbQ== X-Forwarded-Encrypted: i=1; AJvYcCWxSCJ0o9VsAjsKVrH4AIeNW2pj5TmyDGEvVO/h72y13dBg2e6XNOpVSIYnkQmuYtcMNUpXSydMeSq7DtU=@vger.kernel.org X-Gm-Message-State: AOJu0YyiLZ5ecLjIpcSwi2tNJmj7hNH8M5GrfwWwwsPR8OytQ9+6bF+H aojuBtd9Y6JlyXErMf+y91zs+WQAoWrL8Gkpg+LNsXXOOmA7TtrCNvXCzHyCKScaDcm5SQ== X-Google-Smtp-Source: AGHT+IFe94pSmQByeR+90nxbijLeSbDXRsGX0IDwX7sg28d/h13OpEfzDDRzoANf9kA7MGn2kI9lVpbH X-Received: from pfiy10.prod.google.com ([2002:a05:6a00:190a:b0:725:df7a:f4e3]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2289:b0:727:3fd5:b530 with SMTP id d2e1a72fcca58-7305d4f01f2mr468389b3a.15.1738867922247; Thu, 06 Feb 2025 10:52:02 -0800 (PST) Date: Thu, 6 Feb 2025 18:51:08 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-29-fvdl@google.com> Subject: [PATCH v3 28/28] mm/hugetlb: move hugetlb CMA code in to its own file 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hugetlb.c contained a number of CONFIG_CMA ifdefs, and the code inside them was large enough to merit being in its own file, so move it, cleaning up things a bit. Hide some direct variable access behind functions to accommodate the move. No functional change intended. Signed-off-by: Frank van der Linden --- MAINTAINERS | 2 + mm/Makefile | 3 + mm/hugetlb.c | 252 +++------------------------------------------ mm/hugetlb_cma.c | 258 +++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_cma.h | 55 ++++++++++ 5 files changed, 334 insertions(+), 236 deletions(-) create mode 100644 mm/hugetlb_cma.c create mode 100644 mm/hugetlb_cma.h diff --git a/MAINTAINERS b/MAINTAINERS index 896a307fa065..e72a1f247b80 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10708,6 +10708,8 @@ F: fs/hugetlbfs/ F: include/linux/hugetlb.h F: include/trace/events/hugetlbfs.h F: mm/hugetlb.c +F: mm/hugetlb_cma.c +F: mm/hugetlb_cma.h F: mm/hugetlb_vmemmap.c F: mm/hugetlb_vmemmap.h F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c diff --git a/mm/Makefile b/mm/Makefile index 850386a67b3e..810ccd45d270 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -79,6 +79,9 @@ obj-$(CONFIG_SWAP) +=3D page_io.o swap_state.o swapfile.o= swap_slots.o obj-$(CONFIG_ZSWAP) +=3D zswap.o obj-$(CONFIG_HAS_DMA) +=3D dmapool.o obj-$(CONFIG_HUGETLBFS) +=3D hugetlb.o +ifdef CONFIG_CMA +obj-$(CONFIG_HUGETLBFS) +=3D hugetlb_cma.o +endif obj-$(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) +=3D hugetlb_vmemmap.o obj-$(CONFIG_NUMA) +=3D mempolicy.o obj-$(CONFIG_SPARSEMEM) +=3D sparse.o diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 737ffd223d45..30bc34d43fbf 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -49,19 +49,13 @@ #include #include "internal.h" #include "hugetlb_vmemmap.h" +#include "hugetlb_cma.h" #include =20 int hugetlb_max_hstate __read_mostly; unsigned int default_hstate_idx; struct hstate hstates[HUGE_MAX_HSTATE]; =20 -#ifdef CONFIG_CMA -static struct cma *hugetlb_cma[MAX_NUMNODES]; -static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; -#endif -static bool hugetlb_cma_only; -static unsigned long hugetlb_cma_size __initdata; - __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; static unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE] __initdata; =20 @@ -128,14 +122,11 @@ static struct resv_map *vma_resv_map(struct vm_area_s= truct *vma); =20 static void hugetlb_free_folio(struct folio *folio) { -#ifdef CONFIG_CMA - int nid =3D folio_nid(folio); - if (folio_test_hugetlb_cma(folio)) { - WARN_ON_ONCE(!cma_free_folio(hugetlb_cma[nid], folio)); + hugetlb_cma_free_folio(folio); return; } -#endif + folio_put(folio); } =20 @@ -1492,31 +1483,9 @@ static struct folio *alloc_gigantic_folio(struct hst= ate *h, gfp_t gfp_mask, if (nid =3D=3D NUMA_NO_NODE) nid =3D numa_mem_id(); retry: - folio =3D NULL; -#ifdef CONFIG_CMA - { - int node; - - if (hugetlb_cma[nid]) - folio =3D cma_alloc_folio(hugetlb_cma[nid], order, gfp_mask); - - if (!folio && !(gfp_mask & __GFP_THISNODE)) { - for_each_node_mask(node, *nodemask) { - if (node =3D=3D nid || !hugetlb_cma[node]) - continue; - - folio =3D cma_alloc_folio(hugetlb_cma[node], order, gfp_mask); - if (folio) - break; - } - } - - if (folio) - folio_set_hugetlb_cma(folio); - } -#endif + folio =3D hugetlb_cma_alloc_folio(h, gfp_mask, nid, nodemask); if (!folio) { - if (hugetlb_cma_only) + if (hugetlb_cma_exclusive_alloc()) return NULL; =20 folio =3D folio_alloc_gigantic(order, gfp_mask, nid, nodemask); @@ -3179,32 +3148,19 @@ struct folio *alloc_hugetlb_folio(struct vm_area_st= ruct *vma, return ERR_PTR(-ENOSPC); } =20 -static bool __init hugetlb_early_cma(struct hstate *h) -{ - if (arch_has_huge_bootmem_alloc()) - return false; - - return (hstate_is_gigantic(h) && hugetlb_cma_only); -} - static __init void *alloc_bootmem(struct hstate *h, int nid) { struct huge_bootmem_page *m; - unsigned long flags; - struct cma *cma; =20 -#ifdef CONFIG_CMA - if (hugetlb_early_cma(h)) { - flags =3D HUGE_BOOTMEM_CMA; - cma =3D hugetlb_cma[nid]; - m =3D cma_reserve_early(cma, huge_page_size(h)); - } else -#endif - { - flags =3D 0; - cma =3D NULL; + if (hugetlb_early_cma(h)) + m =3D hugetlb_cma_alloc_bootmem(h, nid); + else { m =3D memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h), 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); + if (m) { + m->flags =3D 0; + m->cma =3D NULL; + } } =20 if (m) { @@ -3219,8 +3175,6 @@ static __init void *alloc_bootmem(struct hstate *h, i= nt nid) INIT_LIST_HEAD(&m->list); list_add(&m->list, &huge_boot_pages[nid]); m->hstate =3D h; - m->flags =3D flags; - m->cma =3D cma; } =20 return m; @@ -3665,7 +3619,8 @@ static void __init hugetlb_hstate_alloc_pages(struct = hstate *h) * Skip gigantic hugepages allocation if early CMA * reservations are not available. */ - if (hstate_is_gigantic(h) && hugetlb_cma_size && !hugetlb_early_cma(h)) { + if (hstate_is_gigantic(h) && hugetlb_cma_total_size() && + !hugetlb_early_cma(h)) { pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation= \n"); return; } @@ -3702,7 +3657,7 @@ static void __init hugetlb_init_hstates(void) */ if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) continue; - if (hugetlb_cma_size && h->order <=3D HUGETLB_PAGE_ORDER) + if (hugetlb_cma_total_size() && h->order <=3D HUGETLB_PAGE_ORDER) continue; for_each_hstate(h2) { if (h2 =3D=3D h) @@ -4604,14 +4559,6 @@ static void hugetlb_register_all_nodes(void) { } =20 #endif =20 -#ifdef CONFIG_CMA -static void __init hugetlb_cma_check(void); -#else -static inline __init void hugetlb_cma_check(void) -{ -} -#endif - static void __init hugetlb_sysfs_init(void) { struct hstate *h; @@ -4795,8 +4742,7 @@ static __init void hugetlb_parse_params(void) hcp->setup(hcp->val); } =20 - if (!hugetlb_cma_size) - hugetlb_cma_only =3D false; + hugetlb_cma_validate_params(); } =20 /* @@ -7866,169 +7812,3 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct= *vma) hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), ALIGN_DOWN(vma->vm_end, PUD_SIZE)); } - -#ifdef CONFIG_CMA -static bool cma_reserve_called __initdata; - -static int __init cmdline_parse_hugetlb_cma(char *p) -{ - int nid, count =3D 0; - unsigned long tmp; - char *s =3D p; - - while (*s) { - if (sscanf(s, "%lu%n", &tmp, &count) !=3D 1) - break; - - if (s[count] =3D=3D ':') { - if (tmp >=3D MAX_NUMNODES) - break; - nid =3D array_index_nospec(tmp, MAX_NUMNODES); - - s +=3D count + 1; - tmp =3D memparse(s, &s); - hugetlb_cma_size_in_node[nid] =3D tmp; - hugetlb_cma_size +=3D tmp; - - /* - * Skip the separator if have one, otherwise - * break the parsing. - */ - if (*s =3D=3D ',') - s++; - else - break; - } else { - hugetlb_cma_size =3D memparse(p, &p); - break; - } - } - - return 0; -} - -early_param("hugetlb_cma", cmdline_parse_hugetlb_cma); - -static int __init cmdline_parse_hugetlb_cma_only(char *p) -{ - return kstrtobool(p, &hugetlb_cma_only); -} - -early_param("hugetlb_cma_only", cmdline_parse_hugetlb_cma_only); - -void __init hugetlb_cma_reserve(int order) -{ - unsigned long size, reserved, per_node; - bool node_specific_cma_alloc =3D false; - int nid; - - /* - * HugeTLB CMA reservation is required for gigantic - * huge pages which could not be allocated via the - * page allocator. Just warn if there is any change - * breaking this assumption. - */ - VM_WARN_ON(order <=3D MAX_PAGE_ORDER); - cma_reserve_called =3D true; - - if (!hugetlb_cma_size) - return; - - for (nid =3D 0; nid < MAX_NUMNODES; nid++) { - if (hugetlb_cma_size_in_node[nid] =3D=3D 0) - continue; - - if (!node_online(nid)) { - pr_warn("hugetlb_cma: invalid node %d specified\n", nid); - hugetlb_cma_size -=3D hugetlb_cma_size_in_node[nid]; - hugetlb_cma_size_in_node[nid] =3D 0; - continue; - } - - if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) { - pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n", - nid, (PAGE_SIZE << order) / SZ_1M); - hugetlb_cma_size -=3D hugetlb_cma_size_in_node[nid]; - hugetlb_cma_size_in_node[nid] =3D 0; - } else { - node_specific_cma_alloc =3D true; - } - } - - /* Validate the CMA size again in case some invalid nodes specified. */ - if (!hugetlb_cma_size) - return; - - if (hugetlb_cma_size < (PAGE_SIZE << order)) { - pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n", - (PAGE_SIZE << order) / SZ_1M); - hugetlb_cma_size =3D 0; - return; - } - - if (!node_specific_cma_alloc) { - /* - * If 3 GB area is requested on a machine with 4 numa nodes, - * let's allocate 1 GB on first three nodes and ignore the last one. - */ - per_node =3D DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes); - pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", - hugetlb_cma_size / SZ_1M, per_node / SZ_1M); - } - - reserved =3D 0; - for_each_online_node(nid) { - int res; - char name[CMA_MAX_NAME]; - - if (node_specific_cma_alloc) { - if (hugetlb_cma_size_in_node[nid] =3D=3D 0) - continue; - - size =3D hugetlb_cma_size_in_node[nid]; - } else { - size =3D min(per_node, hugetlb_cma_size - reserved); - } - - size =3D round_up(size, PAGE_SIZE << order); - - snprintf(name, sizeof(name), "hugetlb%d", nid); - /* - * Note that 'order per bit' is based on smallest size that - * may be returned to CMA allocator in the case of - * huge page demotion. - */ - res =3D cma_declare_contiguous_multi(size, PAGE_SIZE << order, - HUGETLB_PAGE_ORDER, name, - &hugetlb_cma[nid], nid); - if (res) { - pr_warn("hugetlb_cma: reservation failed: err %d, node %d", - res, nid); - continue; - } - - reserved +=3D size; - pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", - size / SZ_1M, nid); - - if (reserved >=3D hugetlb_cma_size) - break; - } - - if (!reserved) - /* - * hugetlb_cma_size is used to determine if allocations from - * cma are possible. Set to zero if no cma regions are set up. - */ - hugetlb_cma_size =3D 0; -} - -static void __init hugetlb_cma_check(void) -{ - if (!hugetlb_cma_size || cma_reserve_called) - return; - - pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); -} - -#endif /* CONFIG_CMA */ diff --git a/mm/hugetlb_cma.c b/mm/hugetlb_cma.c new file mode 100644 index 000000000000..3ea9cd0f6b9f --- /dev/null +++ b/mm/hugetlb_cma.c @@ -0,0 +1,258 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include + +#include +#include + +#include +#include "internal.h" +#include "hugetlb_cma.h" + + +static struct cma *hugetlb_cma[MAX_NUMNODES]; +static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; +static bool hugetlb_cma_only; +static unsigned long hugetlb_cma_size __initdata; + +void hugetlb_cma_free_folio(struct folio *folio) +{ + int nid =3D folio_nid(folio); + + WARN_ON_ONCE(!cma_free_folio(hugetlb_cma[nid], folio)); +} + + +struct folio *hugetlb_cma_alloc_folio(struct hstate *h, gfp_t gfp_mask, + int nid, nodemask_t *nodemask) +{ + int node; + int order =3D huge_page_order(h); + struct folio *folio =3D NULL; + + if (hugetlb_cma[nid]) + folio =3D cma_alloc_folio(hugetlb_cma[nid], order, gfp_mask); + + if (!folio && !(gfp_mask & __GFP_THISNODE)) { + for_each_node_mask(node, *nodemask) { + if (node =3D=3D nid || !hugetlb_cma[node]) + continue; + + folio =3D cma_alloc_folio(hugetlb_cma[node], order, gfp_mask); + if (folio) + break; + } + } + + if (folio) + folio_set_hugetlb_cma(folio); + + return folio; +} + +struct huge_bootmem_page * __init +hugetlb_cma_alloc_bootmem(struct hstate *h, int nid) +{ + struct cma *cma; + struct huge_bootmem_page *m; + + cma =3D hugetlb_cma[nid]; + m =3D cma_reserve_early(cma, huge_page_size(h)); + if (m) { + m->flags =3D HUGE_BOOTMEM_CMA; + m->cma =3D cma; + } + + return m; +} + + +static bool cma_reserve_called __initdata; + +static int __init cmdline_parse_hugetlb_cma(char *p) +{ + int nid, count =3D 0; + unsigned long tmp; + char *s =3D p; + + while (*s) { + if (sscanf(s, "%lu%n", &tmp, &count) !=3D 1) + break; + + if (s[count] =3D=3D ':') { + if (tmp >=3D MAX_NUMNODES) + break; + nid =3D array_index_nospec(tmp, MAX_NUMNODES); + + s +=3D count + 1; + tmp =3D memparse(s, &s); + hugetlb_cma_size_in_node[nid] =3D tmp; + hugetlb_cma_size +=3D tmp; + + /* + * Skip the separator if have one, otherwise + * break the parsing. + */ + if (*s =3D=3D ',') + s++; + else + break; + } else { + hugetlb_cma_size =3D memparse(p, &p); + break; + } + } + + return 0; +} + +early_param("hugetlb_cma", cmdline_parse_hugetlb_cma); + +static int __init cmdline_parse_hugetlb_cma_only(char *p) +{ + return kstrtobool(p, &hugetlb_cma_only); +} + +early_param("hugetlb_cma_only", cmdline_parse_hugetlb_cma_only); + +void __init hugetlb_cma_reserve(int order) +{ + unsigned long size, reserved, per_node; + bool node_specific_cma_alloc =3D false; + int nid; + + /* + * HugeTLB CMA reservation is required for gigantic + * huge pages which could not be allocated via the + * page allocator. Just warn if there is any change + * breaking this assumption. + */ + VM_WARN_ON(order <=3D MAX_PAGE_ORDER); + cma_reserve_called =3D true; + + if (!hugetlb_cma_size) + return; + + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + if (hugetlb_cma_size_in_node[nid] =3D=3D 0) + continue; + + if (!node_online(nid)) { + pr_warn("hugetlb_cma: invalid node %d specified\n", nid); + hugetlb_cma_size -=3D hugetlb_cma_size_in_node[nid]; + hugetlb_cma_size_in_node[nid] =3D 0; + continue; + } + + if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) { + pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n", + nid, (PAGE_SIZE << order) / SZ_1M); + hugetlb_cma_size -=3D hugetlb_cma_size_in_node[nid]; + hugetlb_cma_size_in_node[nid] =3D 0; + } else { + node_specific_cma_alloc =3D true; + } + } + + /* Validate the CMA size again in case some invalid nodes specified. */ + if (!hugetlb_cma_size) + return; + + if (hugetlb_cma_size < (PAGE_SIZE << order)) { + pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n", + (PAGE_SIZE << order) / SZ_1M); + hugetlb_cma_size =3D 0; + return; + } + + if (!node_specific_cma_alloc) { + /* + * If 3 GB area is requested on a machine with 4 numa nodes, + * let's allocate 1 GB on first three nodes and ignore the last one. + */ + per_node =3D DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes); + pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", + hugetlb_cma_size / SZ_1M, per_node / SZ_1M); + } + + reserved =3D 0; + for_each_online_node(nid) { + int res; + char name[CMA_MAX_NAME]; + + if (node_specific_cma_alloc) { + if (hugetlb_cma_size_in_node[nid] =3D=3D 0) + continue; + + size =3D hugetlb_cma_size_in_node[nid]; + } else { + size =3D min(per_node, hugetlb_cma_size - reserved); + } + + size =3D round_up(size, PAGE_SIZE << order); + + snprintf(name, sizeof(name), "hugetlb%d", nid); + /* + * Note that 'order per bit' is based on smallest size that + * may be returned to CMA allocator in the case of + * huge page demotion. + */ + res =3D cma_declare_contiguous_multi(size, PAGE_SIZE << order, + HUGETLB_PAGE_ORDER, name, + &hugetlb_cma[nid], nid); + if (res) { + pr_warn("hugetlb_cma: reservation failed: err %d, node %d", + res, nid); + continue; + } + + reserved +=3D size; + pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", + size / SZ_1M, nid); + + if (reserved >=3D hugetlb_cma_size) + break; + } + + if (!reserved) + /* + * hugetlb_cma_size is used to determine if allocations from + * cma are possible. Set to zero if no cma regions are set up. + */ + hugetlb_cma_size =3D 0; +} + +void __init hugetlb_cma_check(void) +{ + if (!hugetlb_cma_size || cma_reserve_called) + return; + + pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); +} + +bool hugetlb_cma_exclusive_alloc(void) +{ + return hugetlb_cma_only; +} + +unsigned long __init hugetlb_cma_total_size(void) +{ + return hugetlb_cma_size; +} + +void __init hugetlb_cma_validate_params(void) +{ + if (!hugetlb_cma_size) + hugetlb_cma_only =3D false; +} + +bool __init hugetlb_early_cma(struct hstate *h) +{ + if (arch_has_huge_bootmem_alloc()) + return false; + + return hstate_is_gigantic(h) && hugetlb_cma_only; +} diff --git a/mm/hugetlb_cma.h b/mm/hugetlb_cma.h new file mode 100644 index 000000000000..ea400294bddd --- /dev/null +++ b/mm/hugetlb_cma.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_HUGETLB_CMA_H +#define _LINUX_HUGETLB_CMA_H + +#ifdef CONFIG_CMA +void hugetlb_cma_free_folio(struct folio *folio); +struct folio *hugetlb_cma_alloc_folio(struct hstate *h, gfp_t gfp_mask, + int nid, nodemask_t *nodemask); +struct huge_bootmem_page *hugetlb_cma_alloc_bootmem(struct hstate *h, int = nid); +void hugetlb_cma_check(void); +bool hugetlb_cma_exclusive_alloc(void); +unsigned long hugetlb_cma_total_size(void); +void hugetlb_cma_validate_params(void); +bool hugetlb_early_cma(struct hstate *h); +#else +static inline void hugetlb_cma_free_folio(struct folio *folio) +{ +} + +static inline struct folio *hugetlb_cma_alloc_folio(struct hstate *h, + gfp_t gfp_mask, int nid, nodemask_t *nodemask) +{ + return NULL; +} + +static inline +struct huge_bootmem_page *hugetlb_cma_alloc_bootmem(struct hstate *h, int = nid) +{ + return NULL; +} + +static inline void hugetlb_cma_check(void) +{ +} + +static inline bool hugetlb_cma_exclusive_alloc(void) +{ + return false; +} + +static inline unsigned long hugetlb_cma_total_size(void) +{ + return 0; +} + +static inline void hugetlb_cma_validate_params(void) +{ +} + +static inline bool hugetlb_early_cma(struct hstate *h) +{ + return false; +} +#endif +#endif --=20 2.48.1.502.g6dc24dfdaf-goog