From nobody Mon Feb 9 19:26:45 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 243AF1D6DB8 for ; Wed, 29 Jan 2025 22:42:18 +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=1738190540; cv=none; b=oZ/yxdEke+v7c+S5Ow8bkYyQlvgq+kBxaoiVbMye1/WAeINXXN5J9MdkYeEnMG0dl5UuD2gclo+CEvNkGLldP3Vg6UGVZhj4DADxraEHE2IUL4MgxURLRWIQidudKRerjMOmPrnHC9QRP+sIENqbVqfUXu32oAsF1IC0T9QjHt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190540; c=relaxed/simple; bh=GHBWu9lNt2ri18ecsXuz9716IJyH4boBN0tW9QvMgJU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WgTRjQBgkwEMYLlgeRMlBqZXDqhw9QqX3zHMTud8XQTpSMMQc03XLdYMTpAISm7GROkEA9kpV3sHKcJ4KchMorzgnFsMBhFlJ0tiRzTCCN6G6tuSeRnv8rV2wRv1noVkC98V8LNum2WTGCXD6lTmpTEXW6sts2+o38wUxcW6pyM= 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=TdqqxF4/; 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="TdqqxF4/" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef79403c5eso377575a91.0 for ; Wed, 29 Jan 2025 14:42:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190538; x=1738795338; 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=juYA+sItEByN7zxk1dgdSJwdSPC9Jk78U+kCT9wOvNQ=; b=TdqqxF4/UfxRFdR3RbnszZmSVfmb3sTHGqSffQgMDxyQrbbsuITuTRnm3VQ/iswe0x Gtzvv3Emu7+aSygfy9hc4tjxzEe/BIMEPUn2HFb6eTsbzqvU3kw1ME6UDtdFheUmR9c5 zPKsCQCBdyVkFbJrzUJWNKEm9Ct3wV69Lt0xgiQY09D2jxkllLrWy274sjF8QvF97Ffx D+ohrq561zLgCUBIOZJppuSwtPHnjWJJyU0tY8gLo99+V9zzu279tJjce998dFHjaqZO CMX54L3W2sTgww5yH3sxAg5v5Kz9Gt4lPW0Ud/4pBhOV+l15MZdWebYkqanGEGpL7eBS UtqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190538; x=1738795338; 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=juYA+sItEByN7zxk1dgdSJwdSPC9Jk78U+kCT9wOvNQ=; b=NAaxLiQR10hk7RoxWfvAVZSIs051e1Xd2jET1ncH2pbBpJMi9BRcAb8bBG081mtRVJ XAcuGOjLd5BwgQcTLGO8udOHk/ifaLx4XzAxOozq4guRHZKnMzmDs3k4PmUViiGMD/JO h7XdX9aL27vYUMYbiXbguNqI9h4NMaOBjrF0Qh2HuGZRmE+wlcAQdvKsLmRdsZPa56L3 mhPROC1+OUl9U6cNww89Mev89ImDPxJ8HdUcimau4C1Oun85AEKg+VXHuGsWWyDH+WYv 7kPFFNGFqY/Ugy6tomSmI6eVMaiZdfv/3Gqq5nLwg1j9UDCZrPZR745iiCaUE61DZexc plMw== X-Forwarded-Encrypted: i=1; AJvYcCVuYyIJEh8u3WQaWYJA8hpqU7h/yjfYt5UALacLJJVkBmtreGxjcdNHdjUdfvXiCKlGdLyeKcSOEp972ic=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2uafIkSmcKORTwi7wF8PPv7ROVBjbGtz8tbCTp+HsYx+MGqiO 5Oza5/v/TX31A9/J2RBluOzue2xP/03o9CRUDlY9acrw6Mp9tAv/EckTJf0Ty4E36YpXng== X-Google-Smtp-Source: AGHT+IEg9BYYPtNubsZcQ0Mno7chp+fxxke+KJ5TxIoSy9tvDjU8NGhzVm6RSWY3Cmxnn4PyjSXyESpN X-Received: from pfwz41.prod.google.com ([2002:a05:6a00:1da9:b0:72d:5313:d4ab]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e14:b0:71e:4cff:2654 with SMTP id d2e1a72fcca58-72fd0bf7126mr6635395b3a.6.1738190538358; Wed, 29 Jan 2025 14:42:18 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:30 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-2-fvdl@google.com> Subject: [PATCH v2 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 --- 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 B389E1E25FA for ; Wed, 29 Jan 2025 22:42:20 +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=1738190544; cv=none; b=ZU9+3vSICWcsDyGTFkHpWKC4nyR+Oy5tZ+M7t5QtjPHVJc+pyn1VDcbAT17/kCxw8aMoNOWjclEnKNy6oOK8giOShyhXdkaTYKgCrUqeKF+OLOPB2MWbGL1zlay67p7WDoRENAgBMmLhqoIn19nYVz7l5ekbnnj4EqXu9Xg/SP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190544; c=relaxed/simple; bh=d1uKuaXZ66ejaLNE77zVp9yjLu7e7zOz6eWikP0cx8Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=l8M75XMKGImJgGN7ABmWHljBkk6IdLxYzZQngj11b4FfvMArhsyrln5g/TjRwpkalTt7ou0/xKPBgG/rWMexXyqMSDZArB7v0hjRIHG2VELtf3/Pi/DZc7IcTUx4jZ0ovqiCHm6iGM66fkF1dZ71twbssQ93Fpca4HxUMM46vx4= 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=S2dsJ4Ve; 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="S2dsJ4Ve" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2163a2a1ec2so4710375ad.1 for ; Wed, 29 Jan 2025 14:42:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190540; x=1738795340; 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=FrI7QbENgy0P+q5nyeLpRs8mThcnQPC6yT2Hdo5n00w=; b=S2dsJ4Veg27v1nfceTsUmd0goZGknQ88KC3nxZ+YNh4BXXtvjEX7MVX1EJdI76GM0V z1h6n1cNxTBIPpxs/pkZmCS/kACYru/K7rQ1mY1Vn4UqdB2NR/0Qplrbve7aE774J03s 4nzbpiI6jkj73Q2a5i5m1BlLURZVrELyjyqtpKjsgxOpuJDAQXtp9DCAj7T9bp/wfb01 Y4/DtBN1rZzV7rOefWgtEeLMP/5089zYn+OUxkZvtGmrsyWyWIspvziPncZxaktyb8kZ Ra8OglNfk974Fvo/a3AjDl4KG7SJhyJ5QdkFIvqlOOi8h7kBrDvJGUNDxhQMUfYN9fpC iHsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190540; x=1738795340; 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=FrI7QbENgy0P+q5nyeLpRs8mThcnQPC6yT2Hdo5n00w=; b=QfnkeQGaXtjzTLPtTzKNoGh/bWnBZ0BoaSV/dZTkhx5o+KTXFJM4TbTydAYmlRQzoi QAyAPPq6peI3CtgMzXwIY6F6O47RcnFvnzDoWQB1RJPUEV9ekbVlFum9uUal7vli/5CO je12fkevc6JLMq2Ux6ueFZr7bXrJm+Pt9UsmY9j5tL7U+mVSZWXS9fHvEl+IpAKMEQgg jVKSZtdQiCN4bcGzWVszU+fImCvDGqRbhE+pcvTQANp7XztSDrn47q3/YOkxojgaRVE8 HyIlOTyW4X9us+xHep+XB06rQMUj9e0ZRJm1+IA+JQ83+5353ptC0dqp703GXb90u7XB eaiA== X-Forwarded-Encrypted: i=1; AJvYcCX9pUJYa+znDGmSPJ9lwd5SDPiYPIthtbzXn++HFtCUzJaekizE+CyhcZemxGNcuvXo2/LQ8pacnoXIrc0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/o22nDUBm6L5CgDdHQa4Y/1aSTOPb2AiFFnFRK8S/W4Bc6jcY PLiDqmsqf8XWycX2m/Z3NdK3zipYdlLNtQ5BctcxLk7+OPZ6E0C3SySOXN0H7HigVq7g3w== X-Google-Smtp-Source: AGHT+IE6OcN4Fv9Lq981yNfAOyQkQ7+cKy+E72YrclXqMoOWbYfq/LpCc0bLhE6kWz72gU1kbvdi7LZm X-Received: from pfbds9.prod.google.com ([2002:a05:6a00:4ac9:b0:728:2357:646a]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a1d:b0:725:f1ca:fd75 with SMTP id d2e1a72fcca58-72fd0bbe3d0mr7123251b3a.2.1738190540016; Wed, 29 Jan 2025 14:42:20 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:31 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-3-fvdl@google.com> Subject: [PATCH v2 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 --- include/linux/cma.h | 3 + mm/cma.c | 604 +++++++++++++++++++++++++++++++++++--------- mm/cma.h | 27 +- mm/cma_debug.c | 56 ++-- 4 files changed, 552 insertions(+), 138 deletions(-) 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..c20255161642 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); + { + 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,48 @@ 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; +} + +/** + * cma_alloc() - allocate pages from contiguous area + * @cma: Contiguous memory region for which the allocation is performed. + * @count: Requested number of pages. + * @align: Requested alignment of pages (in PAGE_SIZE order). + * @no_warn: Avoid printing message about failed allocation + * + * This function allocates part of contiguous memory on specific + * contiguous memory area. + */ +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 +851,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 +895,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 +935,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..400f589756ba 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[3]; + 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 40B001E25F7 for ; Wed, 29 Jan 2025 22:42:22 +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=1738190543; cv=none; b=goRW6U/8QP9/wQQt+GqplCdzEiFED1zXFRcm8DAZ5810JvSGAyJwrX7n4/knAtt9gUyh+5sbTDVJE6bzDjVqK9arqDZQG4UOSPz8Cnwuc6E3HmyLYJvWR9IHqV+z0VIwD5j1yoCPqvLMtW4TEal/4KtK+od2GifM8j1WFGNbFXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190543; c=relaxed/simple; bh=iIVz0iaFnVdC85TvJU7bWEbfgairNIkAhFvDjYIxMbk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ghq+fQq3/z5KKck7y6GUJ2gYw59GZuVWFSXEJ6FSrUUl3+PFdMEL3Aabg5r7uwEDEssGmEcStTe+pTERR5eFnUQT5UOytw/wXG9wfO0MAgmwETCdHX+rOqntcV+jJQzMvPnFc6/JIrSAIPh+zIBjSfd2t6fb/N8iKPG1t63ud24= 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=YCSzIOXF; 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="YCSzIOXF" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21640607349so3640055ad.0 for ; Wed, 29 Jan 2025 14:42:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190541; x=1738795341; 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=dXf4/6vVFsOQWOL4yP6AMC2Up5+2J9mezQuBbeYawcA=; b=YCSzIOXFRoaBiOzY+2vIaH5dWYPiSjSR3lSnv3lA3Ta7S0HWdS/L7YOAIePQWW9vC9 1NDZBJmTUoF/dvWhELPNc7YQvA6koCxHb55SvtR64dGDOFqBRbKjw+1v8qqq9VnhL3QB dGmHaaGdyWNjzuozM2ocawrP+tz2luG4fGwmSBXPYYi4xRTC0I+Y8CmMhHo5/g9/oBKL sG+PpJSWUqqCTu8q5J8RBs70ewDdVoMFxudqYRu4HENNyYOwD2ZabOZfh+8NjKhexALS BvMuYsXMFW6k+e6cmD1kZkImjE4jDGZNtIhH5P2/ZiPIpN0s+yKNqAkFOt0inX2SECYF e+Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190541; x=1738795341; 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=dXf4/6vVFsOQWOL4yP6AMC2Up5+2J9mezQuBbeYawcA=; b=wCUyWNUmAqwVWSSzGTeUl7+KUUOQQWMGpZfiFkQtQ0hJwidrgOuRj2mcsKh1Wbarsx hGaFKI4ITJd+vfa5l5PWuxxcpbMygxiva6B2rhj799tnTlkmk9jQtCMw7fIDy6FSk6dw xVXWAWCTylp3uD8l+HEoUheKZLencs6/leK7jChrqoxyN6g4zjqSai298ao00QZI6eQ5 Bg8P+78XVC4H9ZGGn84HdelXq+BKhap1kWaBT+LBx2x5xftLOX2HOXGB9MRzhN283azw P370uNEuUXsh8Di/ELHX6/Ssa6HcFOYkL9qf1gcvJU//X21VCtkJ0y9EhUuvtK0F2NRk Wp8g== X-Forwarded-Encrypted: i=1; AJvYcCXKhfSCYibW0XoVPLOWaO0MfGT4kMO9xIiezNHVL0XH3Ss0+nAMN45pxnivDDGfIUXfBdDzPIpkFOnIIMo=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1uV7WGZStlcfP5dkEpKIVlud/spBwBxhbk51q3q6b4xa+agLP UVU2TgzynS7o/YYNVD4NaCeAjabUH4XHDyWqWEO8yYAOPSiZN6UnPEvJqipGYoGTEvba0g== X-Google-Smtp-Source: AGHT+IFtjyy0fiL5jPCk8tJl4v0XKuHY7KQNCKtXfPi9xeiKgYj5YSOrbzBr7gtJ/uGT4M5R65Qgn0uu X-Received: from pfwy16.prod.google.com ([2002:a05:6a00:1c90:b0:725:e46a:4fdd]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:458a:b0:1e4:8fdd:8c77 with SMTP id adf61e73a8af0-1ed7a479222mr8328798637.8.1738190541605; Wed, 29 Jan 2025 14:42:21 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:32 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-4-fvdl@google.com> Subject: [PATCH v2 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 c20255161642..1704d5be6a07 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -988,3 +988,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 157781AF0D0 for ; Wed, 29 Jan 2025 22:42:23 +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=1738190545; cv=none; b=C7GUXYXE6XgbxFuf1r6v4iOS1NYMcMEZuMmqsXjmnlMkQWpT7rZHcfZVMlu3ZUaRyfs6Q17kEmKJ8g0s+/MFVM1+kTjjxTel4dyF/mMDKTPQnqZXYUJtlNVxYX0R0Zx7M4axjBeYrPICSD/uwABEt4Lsm9+hWgo9Geut3YUodFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190545; c=relaxed/simple; bh=Bx7rl32jj+RwkpgVjGbMqaXqMbbBmCxfh9mV2eRZvjQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UAQoJGJqkg1rfEs/JK4TJaW91sYelUsIPidou3tMBdCri0x59h9lCoic6vnZwEEqKNRDO636QaOKAs0TAZ3ATyPtJEx3VbyFVpGNOkYCa/e1uh6PpKODkrUfOGojF6SfCaPOSR6GnYWbcxUKXJnTpKR+RN3n/q49qXO3MQDA108= 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=N2QhgiYG; 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="N2QhgiYG" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2efa74481fdso243743a91.1 for ; Wed, 29 Jan 2025 14:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190543; x=1738795343; 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=U/uGGJrSbaHhwDwSIFP9+o74IAHqfAjITEitLcS5SV0=; b=N2QhgiYGGQndriv3c62yi6wXoJ36KnB9MzFQGfwJAcl17Eq2Wj+9WW5juYZxWIh05J NlbFVEm3H42Mc0C0sMMJMMo6Q2wDNVqB3W8wqsU/+DwlZkmYPtgloiuny2dgOREXEg3v gv14pM39fqdko4RmeRiuE0On8Z3pTVvfOlZ/Ed2aHrJM++fU9EeO8oubrhEuUzrRxDca qHsVU49NLyv2buuliTxCJEqL9aNwY7ZjVHyJ+YoCbk9kzzri6Lu/jlEY17ccaQg0bAKp 2BZ+KUp/dHag5SIcG21A3/S+drvJvLQlaJMwQ91tAaVhwp1FnGed1N2yYO7v1VR43I7W NQAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190543; x=1738795343; 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=U/uGGJrSbaHhwDwSIFP9+o74IAHqfAjITEitLcS5SV0=; b=Lo+QhqqsanuTl2DMz+Fg6Nl7Mr5TrbFzaw15r5XSgLkl+Wo71BhGlpqhkVtqUq2HnA rdIvRtVZ+VYl5dxFwggqq4aVbzwwi1IBnYbnXr3wME8yAhhZ7tM1KNuMkIlpoEQRd5OV 3iAq5ZUTJJsjkNJ/uhpbi1qZzWz9u40fJfuJle+7qV/uez15KjrKZXkWZAfwzABxfC+C CB+mYMDk8zedMLVSowg761Nd5SeD+y4JuO9nqlMRXsckamRe4ycs4DE9HXPxKJe+WkHY XQuEf+7b/08vH9vmp4n6TCC9/CRqfvm5r2sChzLAeyujPNKqojaT3YLB/uLORtjVxnfV 13Nw== X-Forwarded-Encrypted: i=1; AJvYcCUPFNgw1slz6DY18qLKkvzOWOk9eD87evzk9n2cl4OENK5omH6bGglaDiJRxg3KgEoqEBhmBYT62eBNvvI=@vger.kernel.org X-Gm-Message-State: AOJu0YzAG9xVg4oDLIBml66WIla4O8alaYsQd+ewjMpF0mlwq4fWWbcO 1GNOo+NcrFtlqhrXt3UyD2RP4xepVB9NLlEF2s9hOhMf15qkezb7pWsLyjfv6ekcBmlLBQ== X-Google-Smtp-Source: AGHT+IH06TMw+Px25LxLv4kXiMFgHunln09KW7I6gk88clhDm/mxCvzjecCS4D0Ez1GRYeHXSw6XGkek X-Received: from pfbbt6.prod.google.com ([2002:a05:6a00:4386:b0:72f:59c7:f942]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4f8c:b0:728:927b:7de2 with SMTP id d2e1a72fcca58-72fd0be829amr8421810b3a.8.1738190543291; Wed, 29 Jan 2025 14:42:23 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:33 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-5-fvdl@google.com> Subject: [PATCH v2 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 3b25b69aa94f..bc8af09a3105 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 CAE0F1E5710 for ; Wed, 29 Jan 2025 22:42:25 +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=1738190547; cv=none; b=rByZcdZ06Tse4+0MUv7UrEToZigNu15D0Tog9/VJuFQrXwnr7HRu8AJYRLn2wqVBwjFx1f1MDHRqw5N+MulCh0yt2hKubcBIXHp9w7/pVGtQ4HHUbhspfjEkD76qusIZ/Zsdb+7HlzA9uzA13DBinpWF8NED2MGOcat7/VwvaHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190547; c=relaxed/simple; bh=KdcYTCg3QXz7nbo5Ffd3SUvOx6WNAhWFlTqAKJmvDvk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Sa8wSAIvFoyQQdG02HW/cNpaAdWpCzAVJp9o70dsszDjE9T10TfWjT7cvU1coAzbTiQxsgwpeBDYNRAuC4bEZBIzIqhCAXDLwLYTMIqUxAX+Af9xMEEzei8XdCVxLcMjCozH4FSwdhM1NSy3YhSPkraLAc12QlcJxrvp0/yN814= 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=S3MB79Ne; 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="S3MB79Ne" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21638389f63so2196645ad.1 for ; Wed, 29 Jan 2025 14:42:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190545; x=1738795345; 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=pO0e6xegNdeyhYt32o5iXhR2JH8dHRA9GoUIxGwwPLE=; b=S3MB79NecKq/icNr8RksZrYZTLdc/pExFSpMaVHsLT7PnuaHmqeXULlWcpUSKFWQZc 0SFbG9W7D4KQYOtgJubb2Hj2ahrLMn1HRYVDFzJncyMj+xUduH1hfhUTwk/aP/2w94TX 2llgRA0njdEo1cXUcqY6vYizOEJpVoSxnk3F4ebKq++tXBFDrRKbFQ6fuOxZyaETEwfo iycet+Tuk7xz3yuwtB0YLQIoSvmvtKz93I7qU4jbmnLWSGxsOdnHtAJt/COyg3Sn6Lsm axl7V0Sw6GJDTY6FYaqPH0kbISd6cF04VviBPbKBx6k33wq4K2GBlUWaB1IujqS9+XN8 62GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190545; x=1738795345; 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=pO0e6xegNdeyhYt32o5iXhR2JH8dHRA9GoUIxGwwPLE=; b=RW5ueE31xyMST0dX2viK5FChPEFSA68iWThwToeXku2eCRZHCNlGlyGM7feQ8NacaN 4dTotFebUQgLHshlmPdFOzb9hGkj1iEJvFD1/iSLDuljQeZMCf+5zsNokpFWPAdTMFkx 60OOiRqT5rt7A2qmoL8gt/30yg6zc+2VLZgOs5iUvrkHpxef/2qKS6JH3wssNVrtcHNt dvjIPw7+3aa3wgvs5CYuGgQRcY9W0IB7A1/0g3Oes+2dCnebYDHyGq6XF7Kpzskzur/2 4rQiDCy+zuAjtWqu/Ege4dFMwbjbv7tJwaMLP2gMZIWS3jnDVW5PACosgR2EFk4ABs/d Jvtw== X-Forwarded-Encrypted: i=1; AJvYcCV24S/h+wSDylhc88XCiBQBi7WRzQGBFWr5Om1BIEX4MmcZOi4uabX/J++fM7WCn0032dIQqmUK0QTTU+A=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5Th2xV6/Amm+xPA9tyS2G0c+CgKar5MnqdDt/rpiT8c8mQW6d rW4Ry+EqzMtSzn1IKVY54xtayv25NoI8kVhuJS7t1U3tl6mVgzrAOP3G4ALk8xAoXadG1w== X-Google-Smtp-Source: AGHT+IFwoboaFCPCNze444ZM4fyXAd+gC8WqzGJgwGWoda0w7P4FTxdLEGdWCLzLUoUhT0C9JBl48K6X X-Received: from pfwz40.prod.google.com ([2002:a05:6a00:1da8:b0:725:df7a:f4e3]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6d99:b0:1e1:b8bf:8e80 with SMTP id adf61e73a8af0-1ed7a6e16f2mr7506593637.41.1738190544972; Wed, 29 Jan 2025 14:42:24 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:34 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-6-fvdl@google.com> Subject: [PATCH v2 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 bc8af09a3105..18d308d5df6d 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 5530B1E7C18 for ; Wed, 29 Jan 2025 22:42:27 +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=1738190548; cv=none; b=swYTI6yO03umo45k3D7F+ykmgB7Hzhau7/ypp8SSYEDZfYdZuvVIZF0sPZXv1jFNC4TJsl1CQQSBGGCh7d4qM+t/LQsRX5N7VUIR1KBbBrdG/OiCvQE323ojQsoxSWf5Uy1B0OnZ+SbK6BB9Wc9CTEXOmkWn5FVoCda51uSlacw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190548; c=relaxed/simple; bh=QTHPlD8DBqsUzmejBD606RhshqtkZC/oNgf+Sxeez8k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DytXg46C/9MupA+us754lVafqIBQdljJuMQQ+lU6el+Ki0Yk/StoGZw+tz8kllOjAL1rs+rBo3MBL0hqdhIPDnZXvBzU9scRwUj9QDC+SbUfrBOxw8b2s74y7RarYQlZeHmhaAvX79YZQ3oZM1NXteFeAxnxe5UwDkOqpD+iETc= 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=kBujwiMl; 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="kBujwiMl" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21648c8601cso2323415ad.2 for ; Wed, 29 Jan 2025 14:42:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190546; x=1738795346; 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=HdRvrkoUQwVvcsaszF053Gf7pOvYwH0DkTGUOoJ3gDo=; b=kBujwiMlk4DLw0fuvu43TignSypcqn+lvgialRfD2v9stTQ4M/kTMEszQVsRLmQGh/ Ox7iNJev+6Bnl9/Y/HnyzFqwqdTJP9X7YvLZpz+9JtPP2bXFUwKZNBXOx2awav6NB+o1 XxTobjDR09LIxjG9+LfXz+W25/GgRfmHODNe9ZYBN56MuH5Vn4husEFP8nJ7hc8SnohG BKAiCe/2d1UJRAbflEt+59Pn7VCbFxcbW5DB7kDNY18wv1iXzm8fELwDsCtl/rj1IIiO 9NzlKvEhlF/wII1Tdq9T7LaNrlDeZY9UnNZOmK4rpuwwM9Mr9GkaIck84ueAfo2qvSOy 7dAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190546; x=1738795346; 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=HdRvrkoUQwVvcsaszF053Gf7pOvYwH0DkTGUOoJ3gDo=; b=NhZyUU+/SZRWoUQia4vUShR3dUF2Zo4syBelnX7hSYOyudPA50bcU4ALlc6/ndyHiZ VqwEJwuZKahqzipQuiVqLLGVEsrCbOehz18QsUQVQx6oytQSB8oqCpQPFi1wKGUKu9bF Wmj9xM5bEB+Sby8Xmo+rqgEayiLDeT85ZRiiYAa78SRuJi7hzeD1S8os7jcJ2TxlMsiL QER0gm/ledifcKF4uq6TebyI7z2JKzqjdaJtUH58PpCysqWxUtMDkEduGMAYFCTxYUZw nqccc7H9Cdp4BS+zUwzd5iMMP/CdOEbTmS7Twab6ngyLwC8YaOK0AFWi1m+Nmv46cj5q DRIg== X-Forwarded-Encrypted: i=1; AJvYcCVQUpmyBUyZERdbdCvE407lvKypDpqcYl8yIF5Qw98+DwjEiQSeqPKUIkxmLGcgfxv19jm531FeikQ//J4=@vger.kernel.org X-Gm-Message-State: AOJu0YyNX9/MesPk4sriq97owNJ29qFQwKJCUsf5M5rQgdscInBmeIZc cxPYUgHgvmvouzlojnhwNoUZxiALZk28CclTygWPWN5auH+MjM5zhAboFCqJo783YSH+Ug== X-Google-Smtp-Source: AGHT+IFRGE8bafbhkN5A9bi7pt5tKOVuNMtgjq5HbfI2cYsNenspZDtaK1fZTtPkQMrimOxYLTsrX/5U X-Received: from pfbcq13.prod.google.com ([2002:a05:6a00:330d:b0:727:2d74:d385]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4d05:b0:1eb:22e5:bb76 with SMTP id adf61e73a8af0-1ed7a648dfamr7753541637.42.1738190546527; Wed, 29 Jan 2025 14:42:26 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:35 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-7-fvdl@google.com> Subject: [PATCH v2 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 --- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 18d308d5df6d..196359254cfb 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 E63EF1AF0D0 for ; Wed, 29 Jan 2025 22:42: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=1738190550; cv=none; b=rS5nZq1e2JijKuA4t+loHJ9vTnEhJaWVr9DZiyHgCOwn96nQvPole4Q3qc+uwgUKWRywhg8U7n7jgHl8garzS2MqiEjxccJ+GL0BUk6aJ7YkX98IhvpO5HOLjEQadv5mNm2LrWpfEYNP7TXHhTdk5Gn3x5BV0o5DaGQqHPmAu04= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190550; c=relaxed/simple; bh=WiR1eqhOiClwJ0UZlwjHo0TqFZ4FOdPtQ5vRV8aDy9k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mfftfE6PPY78GIkopC6XcKy6KSAjRaUHi4iLi4cey3brXgPedeoQo7dzVhDHeifeR16k0g2N4AJ5TW86XfgnF13W1/O4LNkG4ETzx2yO1wDRXRjzeHoZZ3A09SpPUFMFsO88w2Fii6VUdLNKxY5E1mvHfg7em9I8FuzGyF0Dkkk= 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=qv7nCl9+; 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="qv7nCl9+" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef9204f898so229533a91.2 for ; Wed, 29 Jan 2025 14:42:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190548; x=1738795348; 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=Wwa6fsXZRK5gAo5OnSYVBrwDMxNqaUw9sa9Xi3hrPeg=; b=qv7nCl9+Ncg7wMfYc8mDXMTPy8oTJ3Yh1mMVjNK22dCHTBK7N5mrq9nvavJ9IKlnje nVP3BmlvtBzDol2SD93j6QQURgsMR4SimcpbRvuB/Gh7iwHKw7JoSIyh6JL3kseKuX4q TyKRwvWLXojU5R8kMR9yWoPvx8UykdSyGYRbXX0TFIIdW2PT/nDVgDEKxmVzUnMYDa0Y hlKwwUEIHVA2GRJ5jz8VS5n2JfrZj+pf4aTYOIgc1vuv7lZF3fq5FDbS2arwufkSENLT I+l3zfvJeL0K4Vcy6cGNpthXpxREjB/hf9smpcY1f/vAlRUdHtthVzDbAZCqcuQa1f7G fpFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190548; x=1738795348; 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=Wwa6fsXZRK5gAo5OnSYVBrwDMxNqaUw9sa9Xi3hrPeg=; b=wxbTlrfa4qQKNQ9gOzd696IJdP6bTb9FNQ5W5ChmUAzABbkPmmGgS/KnW68iaYYrzm 46L7dqgF4KWjzPTbwmyb7HGlEGFiTQReNnJ4N8s1E6s/0EH2D9F3md9aSllHYxfg8Ysm mUR3Mv7cf+NRFnkmKfVc+JIBy9bng8UGEtQRMunxPfTNHwI8bQ1LjJlXS7bByQtxv24d k68YUVPGQyyEd7K5pi2ibvKM1rwMsYYVMXB8n1jjI+Nb0N1vfqe7ueVeddRyQljQn6ny GBXjr7+afa+qkXZ7gseoyx9yoqKEjR43cwYtTAzaDUCeRkyrnv1NZBg5M8vxan/OD9N/ mVjg== X-Forwarded-Encrypted: i=1; AJvYcCWZ4dmImTfMvZgZt2SvcP1WnktwbFX23P17lktCynyD4HT1k6xlk7Wb409h/dsSnslCRgyL+VTaLK8oxEg=@vger.kernel.org X-Gm-Message-State: AOJu0YwWd2JzxO8XfRtsY21OqnvhsHSYXTqfPMc8lWfQ9uRTKjtdVr7b tvhw8qqY3bpmR+iyj6lZaSSuhYlRs0/zrNjauASlWnTu2YLtqD8s00UJrAlmYqkOAFLnUw== X-Google-Smtp-Source: AGHT+IF59vnW7g2LrX0k3OPFChcYieeAbAKwK+MeTYrxX422BkPNegPy+HquNkgn402HeoagQo0/5oe0 X-Received: from pfbcn10.prod.google.com ([2002:a05:6a00:340a:b0:728:e76c:253f]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e88:b0:725:df1a:288 with SMTP id d2e1a72fcca58-72fd0c74f49mr8427084b3a.24.1738190548297; Wed, 29 Jan 2025 14:42:28 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:36 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-8-fvdl@google.com> Subject: [PATCH v2 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 196359254cfb..20d54eaf2bad 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 932081E98E8 for ; Wed, 29 Jan 2025 22:42:30 +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=1738190552; cv=none; b=kYY+F8eIe+XKXS1sS/OuTklzLeJ/giQP36ivNoDNpuyKXDb8h2xUt3YdQP5Q57y8xKBbLjBviFBHbDUOjeIPhcC6lIqW1ec09fCVPgvK/Ti/jPVuzUufwsNIrV8efdZPD5JwASedUaS5XPKLVyo9NoscRWP96c+ohWC/vt5tnsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190552; c=relaxed/simple; bh=IHhNerkpqWadEx3X6ldRgt0DsSW4NT3Yfiftd6xvrZ0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KdMjIMU/n9r86jM61L0qk8W1Ks4nxxKIVbvv1kkwzh8EJtoXgLTCTtNEOzFvcpwxSB7ii5ZCGZwc1KoXzYR8JSOB8sLqe/+BUal7L4Iuw0Ur6x28GFkt5Y+KG9LqBh8v1Yu0s8VDqdJk/C+dcJHa56TIFVOtcHUHY680MT1odp4= 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=aPiiCI5O; 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="aPiiCI5O" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2167141e00eso3106835ad.2 for ; Wed, 29 Jan 2025 14:42:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190550; x=1738795350; 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=pAg9E+yVjs4X3E0Z5lKinfq3rHknPoyNwrdyz8zbKYE=; b=aPiiCI5O4iMsbarFpJtH0ysowq6z0YyKLE5/wOfC8BTVcf/viXebyDSogXNeBnRqjg FiFtwQFlrLVCtcDpN2DepbW69Cyt8sKXxWeUvfSklftVgLUBV5PUWD+VDxGt62ngcWks f+Vv5bqReYeC2KgVeAa7+4Bv9Jzw/A/QBRgNNJ7Iw1Tl7hWyFy4AKERTgq83WRAkWJLI 1zTa3KMSnSlTL5qUoozYgSlS5CNabVmaND2fBqDX04Zee1On+U6BUL3zNaD8FyK490l/ TkdeGRN4qClDZpY/s4WD8annPG1bsjllmtjvP5vH0CdphxHZriJmfxSYlaZIcmcyct86 eb0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190550; x=1738795350; 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=pAg9E+yVjs4X3E0Z5lKinfq3rHknPoyNwrdyz8zbKYE=; b=tuOaFJ7SslPVI9gkQSc17dl0yVch3v2IuNRJ8AzpLGtn7veK+M/4/WjaoIjBuCfTd5 di0+Ps63hIf0o7khSdr7a57lZMISzCXK4Ef/ygM5I//cHSrXbsqQFN89l8IViosYH1vG uQzWKnUgpv9czUQFfrl/tDl7nrMXhLZiWk9yyxMLjqQnRvM2eg3kFxKdBooEprrMnxb/ Ufv4H00B3J/h6CWy1pWLU3ndb7i89CUakEBbTCIA2YlqdatmaIlIUkqoe+66HtMRLrfV SopV1iQzEQyFxijSXeXiyxwNBTHl/9mOs1aDeq1TFpsdUBIfTJo1YlVTyp1z0gM2DKBD ubRQ== X-Forwarded-Encrypted: i=1; AJvYcCU1j8MCeWMbBV/RkNvpMtCgqNteizmRhKSYBNsp7l6ElWJ3S2YgTFgve6qnQItxA3AAfQDGr6/hYxXS+TA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyg0BA6jjPIU+89pTHUbPRE/eY/FkznB1LQ10uTdDXH1VCkcPMy vsqO3O6aEdYsE6gdGg5JsLk4K3HJfn3UaraUiATTbjg7d4iw5MrwreqivN/jgBw6xImu2w== X-Google-Smtp-Source: AGHT+IEltS0buyyeuzvXAhB1x/AMS4GCVgwLNavg/F7vz30dsXHueQ5FkQjVnKOZ3LafqctlXAj1MQpa X-Received: from pfbbw12.prod.google.com ([2002:a05:6a00:408c:b0:72d:7bb4:ffc]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e15:b0:728:e40d:c5fc with SMTP id d2e1a72fcca58-72fd0c7c61fmr7331538b3a.22.1738190549941; Wed, 29 Jan 2025 14:42:29 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:37 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-9-fvdl@google.com> Subject: [PATCH v2 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 20d54eaf2bad..c16ed9790022 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 419EE1E25EC for ; Wed, 29 Jan 2025 22:42: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=1738190553; cv=none; b=G3EVg9FMYihfqyr3PSTg5tImMYsz1JbtKPkI405oLIWFdygM3d7C8hmLZWnEn2YbZI+wmo+T9/hh7czMTJIDHbtuDoJOCvt7a7qnQ1jloJ2hoRbIalxjPwaOdlHILqsj/sz5De0BybIBeXd1gz0HLzJKRzuHo0wIi2ASZbglk+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190553; c=relaxed/simple; bh=w2N6ciGEMzDasE8zwLLcmma2BXE5QxdMUWrsHXmFKeU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KBbREE5c4uhaTAAHtIJVsnGscKyblhH+tWYYfc0hOkXZKgbvDfg3QlhSyKXNdnnD2G8C3t70ZLsQGhl5aRetK9/F2PvQFOTWPPo3MPmEZ+5GOU/5sDEd58cK7Hh//49Cz2rWoGgx/Ww1KhzWel8a6NyMyD2brnoxR70+CLsQzOU= 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=QFCygw2H; 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="QFCygw2H" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f46b7851fcso350034a91.1 for ; Wed, 29 Jan 2025 14:42:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190551; x=1738795351; 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=NdaXvVKgkNqaabYpHFU5W/ymB41iIHIErbbYhcizrz0=; b=QFCygw2HeuLI3l45oe21/UVh/6n6x8uODeKcuTLPBwE27EQ+Q6lum/GHoLSDFCDYUB b45MQvzU9ge8RSVlVGkMp3+QliQCJ+fBarc5JgB3wg5cgPiz9OGj6v67CYlVBb0YZU3t X5gzF8OLoOKmEuORdJKhUsjVRw4aV8YLutV5EOBQSWpDYK115RRxWIhmS+HJSUEqNPVb 07dL3uja+pkOeoC20LIxDakkJYhU1LyYwLQbU1Vtsv4orsZImSiP8UhQdi5h5movbesQ Bj6OC9ncqsuhMCO9xwZlp92eTX7hEBDsXZ+xjuiLdexAR5B6sUy07VQ6FVL4g5asLox9 6sbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190551; x=1738795351; 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=NdaXvVKgkNqaabYpHFU5W/ymB41iIHIErbbYhcizrz0=; b=wwWxH3P99To0h7kO/uyoM0//YwactEwdni8PFacRThtz2G3GW/9BaTsXi0MsoCpTbi 43LQfEi5u8Zn2m3hd3Xn0gaHdPD04jfnGHCiLlAdlv0p6nqlLU5Ng7mPe+qbC90mTRIE 0E4hXHndPCbQnL5y4J0OGcPhBlwkp096ArJ4d+nN9gr+6RsyPrCzF3pZTp4rNJoYHGP/ X5wyC+lM08c4Z3WY2y8+yHHl7lOHrwSgrI6kpUOrrv8MyKgchbtXBHW8lunVa1HYrreR qmv06LXdNl0SYK5gXvQ4y2oR0y4Zx9Ine+gpn7w3Xn+C5NEh7a0HUoHyHB9ZTCNSxB5v I2sw== X-Forwarded-Encrypted: i=1; AJvYcCXwVzdUE3LZ2RgbdSUSgw08Ir2QOYkjbul9eEEbqntXL8Ot6GBoaIUn1r5qnwYGn0ASg+tjWz5PQ1wYilY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyb4fJc53ryyiHN2VGzRepB/af/nhw8CzV42rYGN/46y7/ZUaAm 5kslQd5Dt3UUfW9AFz4e4+wVRzWbVrM81allnGovDCfoNVqkwc9aRIqO8icVfAujPQDlbg== X-Google-Smtp-Source: AGHT+IHw/wrvs6eYtSXxybbi6ZpEmFFHuoZkE0uX4Iwdoy1hJPle0E2kJ2J6Mh6qYpoJPyZfUbzKoCH1 X-Received: from pfxa13.prod.google.com ([2002:a05:6a00:1d0d:b0:725:d9ab:3f2e]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10d1:b0:72a:8b8f:a0f1 with SMTP id d2e1a72fcca58-72fd0c9460cmr6874234b3a.20.1738190551535; Wed, 29 Jan 2025 14:42:31 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:38 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-10-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 DFC5F1EBA05 for ; Wed, 29 Jan 2025 22:42:33 +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=1738190555; cv=none; b=n3th0R1gNwVydJrE1q0II51gA2UMwEHXvk/o1VgJ0JBZuzx0QN77IIm4fvvQKxb7kxZi3dSb72YXsxM3h6KOBrdpzfi8NyVapeFpn+7na78Cv/u+Xb7b6/ub8g6kmIgqPGiELqa+fiZrlIbyO09TcujDizOjjbHCsLsIbdz7ODc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190555; c=relaxed/simple; bh=uw8dUV+Eq/IhNk6YZYVld+WFWUUDZ8dQNMpz0cLYdOo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AGV1+L/mg2+rQBJuVSAMekzHE6qVTb0JwpbwkDfOTsz7N5w/uFoKWPsZ3F4jLqzRGEWNLdSKzjMHpPPQzhxBnDkGpyCOmXObgR7BEojz9L7j+RVIKuaTguXNNu/tCN22mwxX2IoJ2DuKMQ3yH3datCqGQsFzYwlpMMcm8PiXSRA= 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=c73eDBnu; 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="c73eDBnu" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f2a9f056a8so220888a91.2 for ; Wed, 29 Jan 2025 14:42:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190553; x=1738795353; 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=GH4/nN/ipl8VhnVSg142rgT3YjHhqiHRlTH7bGddYdM=; b=c73eDBnuLcjbB6IT/ATi39rM4YDboYgNEBTQLhxLZvT8zyoqe8kgZx9Cfx8gW7bXFd UL+LBcPbFDWaZqkshKUVLa9cZ3GV6aHj8idNtuuoMtWmuVstlc0IfJF8vKffpJRiwmnb 0brwguhwVGV3F8AVO+SRsub54gOjERI8DPKeTJmk27D9HjUhfmsUNIYGMZ8Ok29GvWxL FLTkCOm+Tzc0EmpdiU4/edPjkgF8cehmZLPLVX9b+4vpEwKBW62PbRIVPUH3QF/698jc DXlgtPuPEv3VyNRZGZ9GQZxKXzxBgX6ovIH8PVzVvA0XrS0M1COiw6vdNCe9ipVvMHZr eIkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190553; x=1738795353; 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=GH4/nN/ipl8VhnVSg142rgT3YjHhqiHRlTH7bGddYdM=; b=BqO9eyFEkWHshWndDj5TOxxOUQTfZ/CPhKFo1nmwWLoVdtzLBJ7Vu9pDtANtEI3fMO iO2h4FvtV0KQQVH9RdVX3y8iUNFH8toOtB3/I2xSAhSjsy6X5r0GCSDHZG9QFopp+ZhY bEZ2BY9ZvIC0Jq3T5ouD2kfIaPFmw9OtCwuATUXAefChIIPiKA58xHTNnG0bNkS5nrcK OP/cJ4dTQ6r0gqg2S/CLBV/GWm2r/SxNVHDMM5b02lKjffBpOT/su1y36oG2rbEZYRL6 DMB80eEBaElqDsjFkJwarqBgN3WI97f+Z8f+w3JDIHQ784j5vWGYI64BClmcBqlo8wau 0qNQ== X-Forwarded-Encrypted: i=1; AJvYcCXR3yH/tc7KyQ3CaKrwF28EIm1y7kMGLRabFhCaCNQWtgHOE/BBQY1VurD5K9st4nVUwmXI/pftcE/NZUo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6pxtvltl1DanBKHZt7magew6Z9509fBKZx4XnNoNkNifj3Xkx P/EGcoeCtc0MFccrvIqEba8zUZQpthy34lXmJcW48NFLYAvtbAjq/8UjX7rArkyaKltagg== X-Google-Smtp-Source: AGHT+IG5Mrax3LyQOG1HOui64BSR1jWBAiLbkCcjUDrw6952WvIrjLf65/po8XH13fz8CLKODbLidR2X X-Received: from pfbds9.prod.google.com ([2002:a05:6a00:4ac9:b0:728:2357:646a]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4c09:b0:72f:c510:dcb5 with SMTP id d2e1a72fcca58-72fd0c6227dmr7072755b3a.17.1738190553153; Wed, 29 Jan 2025 14:42:33 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:39 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-11-fvdl@google.com> Subject: [PATCH v2 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 | 1 + include/linux/bootmem_info.h | 7 +++++++ include/linux/mm.h | 3 --- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/mm/init_64.c b/arch/powerpc/mm/init_64.c index d96bbc001e73..c2d99d68d40e 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 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 557CA1E3793 for ; Wed, 29 Jan 2025 22:42:35 +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=1738190557; cv=none; b=Xeb2oxkCJb6Y3suTinTnrdfhFFA0u4aIVKX6xeT5Kl/s5SFb+ppWf7MjMwusePouUSRjQYA81wSqh1p0FzTYcupRlYojOBK8FLftiDhl9ZDglA9Qqocxrt3KIUredAsZ7emuUxK/qEC1dE3biNuPokRTxPU5BK0AuI4SzbJ7bwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190557; c=relaxed/simple; bh=zDJ27mnhaWsWaFzKyvseHOhCRyk9rV8kONpJ8ZDyEwA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D40iPmoR1QUm18VGYyqNA0Z1ZrFt8XapX+yaSC1qA4xRN14CqZdas/eN/Sph0KjzJZ+2DV+4apYfeE5jh1/Cu02Fu/nI4auTt2gPfHyY36VX3P5wLqqdC//ed5AzESkgkM0u0SkfthvacD6w/QhgGh7fWEKsQAIwyPIy9LDVvy8= 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=auyMcOiS; 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="auyMcOiS" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f5538a2356so221171a91.2 for ; Wed, 29 Jan 2025 14:42:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190554; x=1738795354; 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=3UXEGVf17TVzbPjgEhSFIqZ1yVI6Ua/dfF01nsCmm7A=; b=auyMcOiSYpRG/j1x+hJ5Ccx8h6eiGMY/3+FzocnDekgXhTySsEMjIH/cWDKwxanmFe qF4LgJBJtIY9CxahuV6yUhQqdTTvY78foJg21U+oryUGvYDNPi1U6hr03NrbRRCmzgvu fcIHOcGOiojax0pe6u6TRlODvNB/fDt/rm5yo96bFChVMoMzFS9PtrwowyDpmgMWYJn1 1tzSjyqyGOCCiYBrlYga9C0tsnNXzU3hw0hqst6aisbd2+4nWCCd7Ew5bWsM/9X5msGu j1Wv4+fhCmukKGvbYK3BewdYF9iDtvnmm54gGQzvy6kIZVkg8ZJyUbkt/Gl32rmxC3Vk ywqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190554; x=1738795354; 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=3UXEGVf17TVzbPjgEhSFIqZ1yVI6Ua/dfF01nsCmm7A=; b=GERAY6O6lfjAnS9iO3/GFJD1bSPWTnKeOkekwkVVtTRuypeDVm4UsXHIQXvbGEG/2N 60LLAdAXdZT6O84oUblsruMI32JboH2XH1V2DJEBEPXP7DjoUBN3/FWjmhfI31rw2UY2 PFK46UzziAGqQjUSBivkQBhZa5OZnbPzNZ+eXvV3FKG3j6fGJPjECb0wIj/4psuuyTZ1 DtLpoc2OqmkFs7RcMicztvNjJaZcPF2oynowp22ThYgb2F85rjPT7hwu6gAvwxRL5dO5 8/FtBWgRknw628F0F9z2Yqarx1c+J5UrhBuftAuBx5YwQ9TkJuEg7EOitjBYmsLCr2yx GcKQ== X-Forwarded-Encrypted: i=1; AJvYcCXKt67/2vu+Em3UDpLQDtt+53IibVsLM51gbLsrYj/x5JXeB5/EzetQlqXrD0BZE3cx2wJRa2so4aq7vF0=@vger.kernel.org X-Gm-Message-State: AOJu0YzANEKidsrVje8sRKnxXYA0n6qpKTR97Dp7S3OJS7creMdgWMUH q1Lb2vG3vfC55bclQ2D7un5hSvneOKYpp2mRfuVCZ6HhsnHeh7Vn8ol2Uc/a4So8LBx8ZQ== X-Google-Smtp-Source: AGHT+IHj6bsBkN24xzMdTMA+I9+VEM1UbHXLrVTD4bOpHV3AO+9zEskdr0kUa6IbeUS8dcGGf/WyQAID X-Received: from pfbcd15.prod.google.com ([2002:a05:6a00:420f:b0:729:14f9:2f50]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:140f:b0:725:e015:9082 with SMTP id d2e1a72fcca58-72fd0bcd790mr6189047b3a.5.1738190554657; Wed, 29 Jan 2025 14:42:34 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:40 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-12-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 EBD601EC01F for ; Wed, 29 Jan 2025 22:42:36 +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=1738190558; cv=none; b=YOg/s6khU/2ewvUmjXrtqOzObFrc0TBua+LZH7ERvCwPUBg5Z4oSw+tIXiK3tjdWdqQbPBOlP/55KdBPy+8G2h2Fn0kEIr2M9fzlZdfIPreF5hvXsCwqHR30gZzZn5lXTi+eslbwX6MSeRP/CKZb+/xqHOxbVHxJFyMe93EcF3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190558; c=relaxed/simple; bh=Gu1u2DXo15ac0AxVzDZnMFcfHwPhqJHHi/OiRcW6shA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ISQ/LUcYpmxRI87xsVBf7RJjq7naRcaRrnHsbE2Wx18NB9P++IlAXyMDYM5wZddjoBBRMfVghpCqqki5tcm8z4ay3mdhXHjcF1e5LAwN/jJzgZrOuZcNGjI1XiJol53tyCI+R9SMDNm+4zucyfl/UPqpVLv5/HcOMnM/iwOyqdQ= 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=JjXgkvh/; 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="JjXgkvh/" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21661949f23so4144655ad.3 for ; Wed, 29 Jan 2025 14:42:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190556; x=1738795356; 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=tk6hF6rqBW9AusrfDePvQanEIhsmPpyv3aYoz52NJE8=; b=JjXgkvh/k+gZ8iePhzpZ8zJ7fIe6NVU9x5T5Fm5vwcxc2dFVbReUhQzerQEkQ8MP5n XeAzzhshbafqrU7NM49eJdpU0ZtCueJ7kXaYTIjEJFDERKxN9lHL7CpLHAYOQJ2r8PpU 3eQsh9pLZt+vyOviOPxmsBjFW/y16/2B5F+8LICwFKvg7spzPsOTAe1YJ/ECF1QohPKr Ll6bAORgqGUXqOfleTp11hcExnGqcpGWpfalizrYln417mCywTVZWRny+b1NcLtvNE9Z Ci+QxnCcgVJnX6k9TGvHcNQ6IYumV3hM2IJU/HI3H4XS/8vavqTSj+rHoMJjbJzPm2fr kPZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190556; x=1738795356; 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=tk6hF6rqBW9AusrfDePvQanEIhsmPpyv3aYoz52NJE8=; b=dmQR106NxRQInEATLbgT/tgB6YLCO4FbGuWHApw31HaGxmHUdEt2bOKSz7zGvHXhDV lytfoJvi57VZrRg0mnjxt6Hyl6b+YtJZoexTpyRfEnJV0dtN4n3y5DHEmKZuROWHRcSr 00i4N8itDGfLSYXaBKc38t9cN9Dyb32qpaMCulV/dSq2sGNrbTJ65Mog4ZIp5eIpT3NJ Pk9zTXt8CH8WKxRZRtAWlM8gGaXOu1+gg2685y1GlkolNWd/A4VlHnkaQzuhikw9QqC9 8L6Tl4KU2CZeHH8TorgVDc7nmTbLWF5Cr56RZd+KQ5q6vXMoh8EjGkZXj5hHH1iZ66v0 CMNw== X-Forwarded-Encrypted: i=1; AJvYcCU87ZVaK9dZe+wKrRRY8HtuyafYuuRNEgsQLGSQVfe4UVjVPRKHWspGrEulfSCqwXk9+J/POhhw0pUgWPE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxw+1gRQ+yG0ZvBBQqzpiewf0cNyy9LpUj84l0Qrt+8jS4jb3Zs Y1ozo/qee6nNEMJPmfXgJxklEzHRHBqihNu2K1st+n9MMCx6gpIx2U+PmSB2YjWwDhPk2w== X-Google-Smtp-Source: AGHT+IEqjJb+x+dhP5rhHYNRAeOuV6o2kqh55KQZeq8wjY02yXG128DZoAUhFzB6wXoeeE68EWhcrrD9 X-Received: from pfbay42.prod.google.com ([2002:a05:6a00:302a:b0:729:9f1:663e]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c91b:b0:1eb:3623:59fd with SMTP id adf61e73a8af0-1ed7a48c425mr8653488637.4.1738190556311; Wed, 29 Jan 2025 14:42:36 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:41 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-13-fvdl@google.com> Subject: [PATCH v2 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 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index c16ed9790022..e5ca5cf2c6fd 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -36,6 +36,7 @@ #include #include #include +#include =20 #include #include @@ -3258,6 +3259,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 +3300,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 712941E3DFD for ; Wed, 29 Jan 2025 22:42:38 +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=1738190560; cv=none; b=YVvAm3wpyOoLQz+1J5JWja/t6L6nqoe7aMT4jYhDMu8HFmTkh+lqQkTm8b3E4N3RVcauPC3mMPHFQC/2gg4vN2aE+yyMvkAIIA+lbfaQA2L7zIsLCIwtfkEHNT2jRLWv5GznDdHVmLodWkVUJeJBB4s3yxHKWomnooaIzg1WVgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190560; c=relaxed/simple; bh=F1U1d0suBs4upbIvyU8zs6jNKjEaoO4dYrbC2u9MVqE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nkCT4SVdbT1KYLmr8ze8qZu46UMkFFRpEdzOSLJ44xTTqsuVFh8n5ZcifzTg8vSazMht4tgCI72qtpF+bVL77Scd2rzSrEs777c7d0TMIbylDdAUZMMakB/llA3PKLviAQ6dv2DPVJpPpwxb/TP1WQ9yiR/yYZ3ZYAgzdBZ6lQE= 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=B0dnyZ8a; 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="B0dnyZ8a" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21655569152so3146515ad.2 for ; Wed, 29 Jan 2025 14:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190557; x=1738795357; 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=xLt9HBc0qt/kG8opRR3xwjjIGa13UKxYV2/Ut2PpccA=; b=B0dnyZ8aheOvKe+LMtQwocv/BC9+bVQA3MO0KjQECzAvpm3qTCCmOnttn5TufMh6aD MBMzCvDYZfH/Dp4wSuGJL7xIfasZZZdaIMBXepHdIj080fkWWFKT4uJg0C0/QMZ9gVad yl+GcfyKSr7n3LFJKTaOah6UkroNG1sgvrDpDOq6aPCheGVS4j9dYb2pUixmWdplylAv PXQCyKYl/nisE9wt5jmaJXIclN8tRkjwYOolPLsDp98JwfETV+YkRLmQDHVWba5LHJUo ipK6DlAMC0SGqp6OEjdgrzcY1DBBsFRlLdT1Z3EUafYZg3R9sIygYz+BOP5kyWoF0+CH vkyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190557; x=1738795357; 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=xLt9HBc0qt/kG8opRR3xwjjIGa13UKxYV2/Ut2PpccA=; b=lVBuyBmwlltu3VOZpVbQhiKRLigxdeFtHnvG8MgV6l5BxULeTj9/HAwpr8nA7hE1ip CF1hPqyF5i/Zy9kQY3ChQ9lsKSIPBV3xuvtEoZPnUwOyFbOBUSTvITWddXMU5tcGaFtG FOMmOUTlVIpuNLtCGVSyB8WDl9h+7wxGGkF6yeKfkCjvCzlpVIfqW6c71U4KVgparn1l Ubw3Vnx7CwPJ+WAxClUiB+4hD/K8jpXjcPndsRk8jy0AtH/+4c0xIHFJe56GiCu4KLP7 wE1xujpSGHdSkOMQmwFifbBqAnXoaBKReiRH4VvkU1UL/7Wc3uTpyWXRSZKrMYhEJ8zb n5Qw== X-Forwarded-Encrypted: i=1; AJvYcCXzognYu6sEs1OJ3e/dn8hl+5uEXWKaKUuM8WFYdk3IIZgZbTeMEijiLM0q8dTj6puSSgeE7OTNPT9UADQ=@vger.kernel.org X-Gm-Message-State: AOJu0YygcK7w9Wgl0RUF4OvMa3B0xjdPlApKtZU3J+juGexzYJHQ1iYc WN5SFtqm5uc7W7k3oyZN2JjViAYcVdLuFvG5nIdLznOpDUn2QVG7jcwRSrY4xt9qnpJ6Ig== X-Google-Smtp-Source: AGHT+IG77o+oLUYvd4dsteZDfBQBaEerJ11jO3U7DjFTw9ASxdHAtRSoV+qn91gtydr1b6v63zVD5abu X-Received: from plbmf13.prod.google.com ([2002:a17:902:fc8d:b0:215:5a53:ee06]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d551:b0:216:5556:8b46 with SMTP id d9443c01a7336-21dd7e0728fmr78854645ad.49.1738190557688; Wed, 29 Jan 2025 14:42:37 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:42 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-14-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 B857D1EE7AA for ; Wed, 29 Jan 2025 22:42: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=1738190561; cv=none; b=kYfuVrQfv6so6Rup3MJ3sIwysK3KnTji6nBQLTXlZTQ8zYus8NYeQ+QZHSAMEgaWM5zb+vIoFrrwQmECnWB1jM5T0MMBM4dnIWATeprRgFrq093EepchVOJnR7eIxgB9R1kMZnClJE8fqdlGLXi1rh/aTAyn3oLJ5MT6iF1JHmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190561; c=relaxed/simple; bh=uliIavhn3jJdykzLNJFxBtoJrXgefAaukHFwz7YW/Ek=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HRdl0RvrTprzh0xJIGDytyg7+GO0jzA3J4kSJLLIXlqCxFvwR1DWJ74q7smPNydgUbLxFizQRpPWrWFGsi2DcuI2uMeeUBuWq8b24dvgCp2j9ahSuq70HX2cjQouxR+dOqYZyS3TLyOgR5QmjpQVUHizYLLQ4vGmqL074uNT9xQ= 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=QwkQcFHf; 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="QwkQcFHf" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21650d4612eso3939735ad.2 for ; Wed, 29 Jan 2025 14:42:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190559; x=1738795359; 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=Bphjz2B5H2n+p6toKCuCjUfnxwYaF6wPJ0biY9P/V2E=; b=QwkQcFHfZ9PxWB0WiCnxscT3dxZOgsF8T3lv09GZW080ErDzANG2B+nTpuVgLzjjip yV8nr1p8cIPuLrxrYBKRT77U674tf/Su/QXDZnMu5n6CiNt7CplrU2OxTMcoa0PeAxbp 78lewGxRP+bkXdRceQuuxshT+7hCrHdhFaLKbfRUP3nXuD66TstO8IGmuIAPbk+548g3 tl5sSL+nU8Pzu5auY8Zw/rqCTMLpsPiw85bvRaiQ0Fvx0B+dhzlkCiO2XfvJ/ocRrn0+ qDxR1or2kcYyKSm2DId5xoqm3MusFyv/3n4raWzdzOp6FZjz+A0dOt3ubAF8GZt7FbCW HA4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190559; x=1738795359; 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=Bphjz2B5H2n+p6toKCuCjUfnxwYaF6wPJ0biY9P/V2E=; b=eT6g7N8iNfuH1aje203aB9UGR2dWsMPF8QsBBLYdPkM525TM+90s2sOZgtuSPl2YMy H2MEIW3qAsIIiSAIAC3zZMNnjymD43e02ypbD1UALPNugdwSchxGD29WWuNvU41BifKn +ssleGbvtJ8NlwRwxi9gW8ZOR9tizbLZAV27ejw9RYH1HSpC8qUd9yHVam8AkrMD5XZv tB+2NVkNjwd2EWVYwoBwFt6ShQCsT34snQA8Iy2UJY0AKi/FH6XyBLAW2ZqE/qc+pQbc BgKuFe87m6qabXef8WHPLsA4NDdRXHZPL6pjCk6GI6Nvbhds5AxtiV8rjBoZZ++GVJGa lgeQ== X-Forwarded-Encrypted: i=1; AJvYcCWCl6RT+ec8Jf5qMmZnJ1fbjmbUpFbfS/9wuNq7bmGkc5XsRel/MvMnKaUiFw0WxWbO+L0CKZ8cc9HfM7g=@vger.kernel.org X-Gm-Message-State: AOJu0YxurCtvbPEN5g8uYGkIAO9m9mlSO0yBDEBuyTls6FUTP1ywHqsT s+2XdA0i2g4L6KagLNVS3hc8nd/i4mu5M5Kg0hz7wIeUn5sCXWrWlVQp4BdFXMenxU+zBA== X-Google-Smtp-Source: AGHT+IEupuZBL681CzwqacE6l9j1whrVwEezyuvYbWICKt+dR8gVuQALet2uv2LXpK1TnJ3eAsBEWAai X-Received: from plxd18.prod.google.com ([2002:a17:902:ef12:b0:21d:dae1:77e8]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ea02:b0:21c:fb6:7c50 with SMTP id d9443c01a7336-21dd7d8aa02mr76684975ad.31.1738190559165; Wed, 29 Jan 2025 14:42:39 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:43 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-15-fvdl@google.com> Subject: [PATCH v2 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 e5ca5cf2c6fd..a0a87d1a8569 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -63,6 +63,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]; +__initdata unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE]; =20 /* * Due to ordering constraints across the init code for various @@ -3309,6 +3310,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. @@ -3316,14 +3355,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. @@ -3595,13 +3645,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 8A2AB1EEA4B for ; Wed, 29 Jan 2025 22:42:41 +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=1738190563; cv=none; b=J3UhkfPttva0+0clfKQa9ipcxrp/njKyUsveaQP1VWKXQWe5jxzMvBoWMEqnRVa1oPL+tLUU5QqooyiV5bT0WB3mV7nhWKrXPUsiAOOs8iDLj0xglqds3g1bJt7B5PVOA5vQ0arqHZ3sib8o8xejyNGA85Bswla4inQ4AeGVXMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190563; c=relaxed/simple; bh=afzoeEgScdzoLRJeIhQVvNfYS6XRmn7ijiQwW15m+Kw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UM1HdAINFG+DSaQZcM7QRLSXHwWLomhDYKaPsS0uy0hHjnUrKCvqETUgamvl83B/SGNICpmIVKKVnwkJ1iSMNHDiLcSl16DQK0lhGdquEzmYYCfbxj+WgqG3RwRese0ZInqVdSVV8N+5CBOtCd5WJxfkooCF822CZqnSNEd59ek= 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=p9QBqXLv; 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="p9QBqXLv" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166855029eso3072785ad.0 for ; Wed, 29 Jan 2025 14:42:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190561; x=1738795361; 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=csxUnMb8aBY+LA7oJhb6s5xyn3ur++3PPVPSPZ4uvnE=; b=p9QBqXLv3aQRv7qy+Za+z2eaSzOnFno6cJJKZ8CctimxTXHXTXweM85GThlYN/nmmV /kiflRtmxGrZ7f/ZZ7VZL6xbWoRfkdN/9aU7WFdNhqWLBgc8wQxg3UXRP2ZJJpzBFYMz HR2QnUSg9UnhxFuKUAJLiOxTCMChTv40UBAIteVWeGc+mQ4wK+JQeNSyW/VrTUn1logQ LUwZfXp7KRaLFqbySwH618Ic3CaUZ1Tp/RhscoZ+eiPNC4ta7lvFVGZX8nV1tqxDxCzC V0NR6sT1fr+wWyDNaNmYbbaGpRFXP2EzVxBUsUSZi4EXfVabGHmj4uGUcN8QI/nYDBi3 kzuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190561; x=1738795361; 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=csxUnMb8aBY+LA7oJhb6s5xyn3ur++3PPVPSPZ4uvnE=; b=ErZQEB9eGcdo0QsLhBM/VbwqJb1n85bYU2JdPR2By4R84BVwhvlZVQ8hMs0XTNGfvH uuMr3oFRCGlqX9vtd90rzaVbnFZtzyu51NKwTpZCSL8GWPWgiyORYhQfmp2nSUsJIYMY jcuCIT7Lc8nlsXWfRICNFFi+PvhpOudgYmCjGTJVGpgwJ0D5HKvg6Q4QqETNl/uAlQf8 PeY4NHJc/icT+6q+4lPG+8YSXaXbbsbbfw/TgbBO3v8b6OnfnUUs6+R+EuFSal/XLTo5 Z4Qkebeh7OhJ31qoiLK72DJ5OK4WFEddXZ00jE5/T6wP9YNDsFr77UG2R8SJi6RYwVRt UTIg== X-Forwarded-Encrypted: i=1; AJvYcCX/holTjDRJJtp6g0DS4zdYkSOmCGzLj1DzUCdUYOrRhtBMrLbDxbQDhb80cqVxROhq8H/vQo/7BHpQlNU=@vger.kernel.org X-Gm-Message-State: AOJu0YyJuZoBHA6EeZc1akMMNOX7qvwzNX+cyDYdOXld2uQZZ+W2+kFW ogbMLvWwKP7OOkJ4Wy8K6iHZ9OIA6RcGOkEFHwQUEZbZ6no3HyV6INMstbTNPxorIw/0iA== X-Google-Smtp-Source: AGHT+IE8Pv/vPlQg3Zsgd/miPk81S+cEO0cUOfh7UtusaUeUsumjXw+Yjo59fdOEUHr7Es0q7li1hf72 X-Received: from pfbbe5.prod.google.com ([2002:a05:6a00:1f05:b0:728:e3af:6bb0]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a0f:b0:71e:e4f:3e58 with SMTP id d2e1a72fcca58-72fd0c623ccmr6357836b3a.17.1738190560818; Wed, 29 Jan 2025 14:42:40 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:44 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-16-fvdl@google.com> Subject: [PATCH v2 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..bee22ca93654 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) { 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, 0, 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, 0, 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, 0, 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, 0, 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 ECB451EF0A1 for ; Wed, 29 Jan 2025 22:42:42 +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=1738190564; cv=none; b=RuNhXVbrWVF6yqOziLTcNeT7mSnfdJVBRzM5rlw9Kjam9JnEaWXGKPj3wWdsFbkqFwhCbJWmCpe/TCN1d5Mq8+vyd4CBShMTXbJoZJXfnXaCRwvJADRkBj/V6S54Eu5xj9fT2X0mWCZ/aaOqhDsNNlBKBIhkZXMmNZPwmaF5SiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190564; c=relaxed/simple; bh=eHCG7WY67UGKYrY44Wy7HHVBJHdyer2l/4x7EPtb/Sc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qai+a989OmV4uz4VSCj2OtE/Fxqcl9ywZSFLUdAcWbhDCj04H4ZX8FZ0MAYyPygz4AU1wBr2h9KdHfJ7t1ioWGZ5+So9sz1YHC0Zd4q9B/UhS0NQyQ784JB9oT7GbZIfP3u+y3l38zTZ0GmMSg0xUyczd6VcliczNV4Z+aq/5Cc= 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=usReCv4Q; 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="usReCv4Q" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef35de8901so218745a91.3 for ; Wed, 29 Jan 2025 14:42:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190562; x=1738795362; 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=+CyhHbVHq5bo9c6pVj60jCqfagJdD0XsxqtGn3cmahY=; b=usReCv4Q3jIUBX7wsnCiROnSOR29dJ6XOFH7wLoXQMh1Wy7qmcl1+qROYfrcIK535c FOt+EElpX6N/vjMG5xGvrBqV4qy85XabgzhFzIBTBNAm8DF8duML9yMe4+tkZz3bgscH 6ySMn5acPCELQWQwD56EsFTXDkQxerBvbLPZ80lMuWmmev5r+jbfwJ/pY2I4a6+vgjXj b5skCTvk9WZ443nPha/Zg1s6kjqEEtabRubffEy+a4kRWV56D/RkYMVnFjugrrdDQ2F5 UzIX6AWHy1duw9vuJcqP3vR5OQ/QCYMB6QMSN5ZP+N51h9oqk80XOfPgacJ1EPsoQqeC qwzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190562; x=1738795362; 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=+CyhHbVHq5bo9c6pVj60jCqfagJdD0XsxqtGn3cmahY=; b=pIezIQxaNSpJrlnXCds7lzRjrE3c1XavROBJUxeuCbB7J0t37PAHu3L+dUEpPa5/oT fUSDNBeMMqgXQgnEB2aVzldGw4VqpL4yEe2AcyzVpqdlmp6K50ItrL7hfuY32RueDZQL ZFsXHBiOlKcnG/9aOFyfTfNh5waz0ce14v8wF4isU9t8DL53APCNVuUOqdmJuDjUk4Uw 5F3I5oF+Nitnf3XTj9sIPyV3ptK9uEZx4J1IJ8CoaAQ4NwBJH0FuN0WTSAd+yMufqPMZ b+KyFo+VcCuwhVtn0spYcgaUXFOhp8CGXYqEEStSuFYFnGw0B85VsfEggMIb5AR5N2ZJ OHDw== X-Forwarded-Encrypted: i=1; AJvYcCU71TUBrc8jhW48OlxJGl2R/2HxQUQXRt3/EYobORv0pk/EYujbo85acKmFu0Z4N2aIr13PD8QbkDyi0XE=@vger.kernel.org X-Gm-Message-State: AOJu0Ywxyy979mSVMykHBkf0o+fOPmqrzAd0PcBktazwd/UhJ/nSi8Or 3mLoX6N4b7932AUVKrl//Gu9xAtiJN9BLvgiqIPMT5QgSl963xGISqcqA9tGWhPN+ELgLg== X-Google-Smtp-Source: AGHT+IE89jnX+A7X0+O3NBJ62H6UTgZMQLLOdEOYxcL1kGIfjMQLQgaW2k8akmMH2SlD3zsDkP21quJB X-Received: from pjbpa2.prod.google.com ([2002:a17:90b:2642:b0:2f4:3ea1:9033]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f8c:b0:2ee:94d1:7a89 with SMTP id 98e67ed59e1d1-2f83ab8c371mr6910663a91.1.1738190562213; Wed, 29 Jan 2025 14:42:42 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:45 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-17-fvdl@google.com> Subject: [PATCH v2 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 a0a87d1a8569..0a27659d9290 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4911,16 +4911,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 68F7D1F03C2 for ; Wed, 29 Jan 2025 22:42:44 +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=1738190565; cv=none; b=CmtKPsA1eV88xe/R7mgOF+7WvsquQdTs5mSFHOOuccI7WoSsHS2gxLW8mFSZmZmZ1g6w5CXY2BsESG7/dfj7QbItEKn3XfezJWZ8BHGcwZuOEGndAtACTDsqAzMWYE3pdt0FxGsg+ejeszBrTe3B3KfPQS/j6+QYYCbZPCDmiq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190565; c=relaxed/simple; bh=6bj6ZRW/p7dPQYiEdbPQw97OkjElbWxQy/n31wZ6H6A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tUoe06kXDSfJ4rd5fy8xnCSkZvJ3helll8xHRbCWQgUunNtUMQtqI3xcGOtzPQDFFZEtTmdhvjxPyiAC0UyCUGliZkEsDdJ3KnnGDDCHCbwsfCIhq5fY/r1QSQ+1WjI+lKw4D/zsjxLQBXhADVMKvhb2t92gkYMYJTXmYQiAXVc= 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=Zfe5agDH; 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="Zfe5agDH" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216266cc0acso22035475ad.0 for ; Wed, 29 Jan 2025 14:42:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190564; x=1738795364; 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=owdlgfr/VPS42Eha44p1LufvQJZlV8Uk+cba5IVjd9o=; b=Zfe5agDHDsjzQnLAKtb6INd2NRQT+/SgZW8LFdqB1u5P+S7uo6oSDz/arblpAxO+Do RWAhh168O0P/OCvBp9uuuEpgoaRe6L7gG2uqJVmWuK+gRradS/k0lzj9aaD6O/YhB17/ APPH9NEFisDmnxwoq7W7eLXt/vnz8QGpUbCe+VxwEqIiZEgSTAufEvmqY3DdTktoI3Aa t88bFoGOYTehNc1djbkDAHZtyGxmsVxMLp/Yt6MgJhmht5e64An886LujLaLJKnonMVz Tsm9/QBM/hkIovjwo9Zx7wOkJ0TT+MqvUeGqAF44Yr5oXzNapASqH+jbGPO7Q495gRlk 3u6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190564; x=1738795364; 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=owdlgfr/VPS42Eha44p1LufvQJZlV8Uk+cba5IVjd9o=; b=ZY/kJu8GAcB2O7irLps0mqQk8zrk7Y0uIgaZzmxDLmuL+RYIcEZSct3DAyARi3W/F6 Rs4Nt5ehNjrB5fgIH69wq8EYIJfdZwQVZmlaUod4ZDXBkQBWf1OPMeF8toMqM06WDCR+ 6Mz4FqKHPPAfG/t4rdGGSlOWWPfWK5a24DpStnwqXnLjIwrIRHiqmfrFYYdjAwzLCC7C /aLGj/1OFPIZ0HrNmuHrXX9bSEoiXekhC5DbOozjwsdBT7+CKETso1ei+d1PJNICMjPv xu4DRowL1lmYw3644/L6agNwSfdKkU9kNHrga4YpSVyK5SAVdGLkiOUCg2TFKRUX1biV yjTQ== X-Forwarded-Encrypted: i=1; AJvYcCV7Nql0r+8GXKACfFq6/N3VGeZFKzwwnRpn+3RgIaBJN4YnL7byGFAY1RL/wYixqLzaFJXA04ZCHkLv25c=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8z27EdKwXFEGNnzX9Dy9O4s8yqDuNjdZ/JGEJ5mTwMWnfD1gZ 1M3TpYxjBriK51BNzK7E96ByEXIlLCz2GmiYPVf/EkVWcXhCY0JNWlC03fzp4EK5kQhICg== X-Google-Smtp-Source: AGHT+IHEVdOPU8hWxfFhDQfhZQ8pAOtZCFOUBUvtqAFn5Pxdp3YDWz7gu5clrbq1G86WIUxTF3GKcBps X-Received: from pfbeg27.prod.google.com ([2002:a05:6a00:801b:b0:72a:89d4:9641]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:388:b0:725:d64c:f122 with SMTP id d2e1a72fcca58-72fe2ccbc83mr1635907b3a.2.1738190563842; Wed, 29 Jan 2025 14:42:43 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:46 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-18-fvdl@google.com> Subject: [PATCH v2 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 0a27659d9290..7879e772c0d9 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3579,7 +3579,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) { @@ -3587,17 +3586,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; @@ -4921,13 +4909,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 0B6BB1F03F2 for ; Wed, 29 Jan 2025 22:42:45 +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=1738190567; cv=none; b=fZRA3QblDoAle3KgqJOc2HZ9M5sHK0yiETXKYLacPif2EQHFyLLwW1JNS2Y/1qPLCKFoR/vQ8GH1kqOzUHzlz1jM0q3xYwBqwR1E5g92vbdkyYg/87hdW1ihERg8dn9oQrotajGXEsTCwyyEGgYvthj25JWn+/X2e9BMI6jOaro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190567; c=relaxed/simple; bh=HfbyCR/UdkrFbf8gwpxGZZf0WSxAsotcMLXVmrX2V4w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N8D6xmAAK9hMrtu7Js0Pakixv6cwwRpuoYMRW/81jd58Ypd/JmE1WFsQWwBr31TRjpAjnED+owVxC1yLleISvCeRBbcNrAF/SByxYBQnLvuxaITOfEqC1BDxVU9kuimImXMiOkuqKAEGBVcxOQO3On6HQkQBxiRlTss0XY8E3Dk= 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=regx/dWn; 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="regx/dWn" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9e38b0cfso253172a91.0 for ; Wed, 29 Jan 2025 14:42:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190565; x=1738795365; 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=s6D2Er1AWUGlWuHRy7b4wDChkIROtbX0tri1lKXEI3Q=; b=regx/dWnzIFqFaLAKauQ2ywwVoet+iNCHsu1L2Q4G+33WTqty7sirgTUVb5W1189mj esjHsIzfi6cUl2pkog3e0PswxtgYPWBNvXab7EJU5DCje1TLCcak+2vn4RbFgoq5hCh4 cVIY7MUDXyhjmm7GKWFdnRvOM31kLx5wqOHoBfvFy9jNOI8r8gPx/GkMsBG3ipi0o3hj ycC3GXgJHSXfQatH4qgNpKsYXZHFoPGTIhIycpALn/rLU22ugCo+nka5LSXm0TUhE3/a NqZjBqtUf7nuUOzgDHIiN4t9iJSPvRgxmR6OXS+dAMgZTJtZg8jnNvB2/eVhnDvsG/ux NdDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190565; x=1738795365; 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=s6D2Er1AWUGlWuHRy7b4wDChkIROtbX0tri1lKXEI3Q=; b=DKQr8dhhDR65qm8SoYy4FMhX2a8eh4fmQiYCESKQ7WqgZOaB/mCWFi6B5sPXkVpJ16 T+dSWEnPShYMc+Rw51aPF37+Qq1zZk1Kr1pxxrNDSuNNVD59g2K7AZ4YZa6e58GzVivO kED14co/qrJTVEBw0gnONiGIpH1RWtcjcpK0//0uT9IxfFoVCmWuOHrXvylhtvqCBt/0 2qxEzD3fAuNi4kj8t7aIuioCJYV5HQGmU9mYQsUUlg4yQ1GSr+KB+nsd+8YFh30jDIC/ WFuMXqc/XikcjjsrA5U+vg/TkAzSh0GhSxhD4ChEhhezwQEwo8mB9CMNeyNyF2rGdjMK yqTQ== X-Forwarded-Encrypted: i=1; AJvYcCXmwChjVTNztvTD4HwN6ef4T/OoztpGOe1QZ2PMqZskvnmrtnOKsgmnUytxT8PGOPdkmMQ/mTmmSgmo61o=@vger.kernel.org X-Gm-Message-State: AOJu0Yzg0AIjrjxRzs3Nr+q4FtXLPG/ztPsk5jWEGP1hw+mR+yCBtQ+a 5iKkhdkz5ZuyNjFiVI5AV4UrlZIjipeygJu9LWA4e1CGAnST2IcBYn+ccL7R4dQgaajLvg== X-Google-Smtp-Source: AGHT+IFTdSQExYmjG/I0+G58ZhbdOePk8o9BWwD8WMnaUq4tuY2bhx7wjPi5ZMIwpT+tjSLMeHFg+Bgp X-Received: from pfav7.prod.google.com ([2002:a05:6a00:ab07:b0:728:b8e3:993f]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:acc:b0:725:aa5d:f217 with SMTP id d2e1a72fcca58-72fd0be4e2fmr6604034b3a.7.1738190565418; Wed, 29 Jan 2025 14:42:45 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:47 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-19-fvdl@google.com> Subject: [PATCH v2 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 7879e772c0d9..b48f8638c9af 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3220,6 +3220,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 @@ -3287,7 +3288,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)) { @@ -3316,6 +3317,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, @@ -3348,6 +3356,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. @@ -3388,6 +3401,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 9C1031F0E5B for ; Wed, 29 Jan 2025 22:42:47 +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=1738190569; cv=none; b=tJo0wJ9SylK77AN6QGD9P/TFowDowZsq3jhZK3AVX4sFfDhx/4PK00wDozuLTmti9eII35fZ9g0LEkAOKPlFSbuw3VWovfGDr2QFME4AxRQwtS0c0tndOH8wU0G3b2Qwx54eagZthwjNGjnOdfdRVjTiN9P2trGh1weq6Ly4IzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190569; c=relaxed/simple; bh=LJFPDBtGOdNbq7T310/ViXNMs77gIYUubuHhRJOJFWo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rBTeq/MDQuron1qibLiepRwzqsRfNMZUvrVlUXtIwbsqNGh39wqW31PsU7pxp1RhonzDvhMrEAl5InDpvCjIW2bJcsyo4Lbqo3izQvyKe7St3Uvf5C2UUGHs6k2yQTRWtQ598xyCGtWjeUbjlZFt5wp51fXZ1hUUe4ShC4pLDQY= 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=DRkL03lW; 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="DRkL03lW" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f5538a2356so221322a91.2 for ; Wed, 29 Jan 2025 14:42:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190567; x=1738795367; 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=X7mcGCY9R/2wAfvNybb5kTke1CSbZ/pNKwqh3Rdbh4M=; b=DRkL03lWMx/G7V3yrmVHa6BEu4lf7PsOkSMBlCV5Mlf+OpKo992j7U3xVWkAnTOo1c q7sQhRbcAZROlhE5jhGb+52Wt2ekeLZYLTiEnOfGpgTTMO9uPmXepNOd8VSyTX5oXbku 84wcAooMMBq6+asLAfKAB4xjbB97CyfzZUb906FdhOfh/RJsDQfwUMetnz0aImaQ4+mE +z6p4dhqjbUjJZVbIkHj2oGsDQz706kkPe9F6iWLpwvagyzq298T6wYJQcxHOq4iw70N QlwuF54u5lSu6sq4BkN2kuexO8LijLvj/LhKdWhQ1lYNBBdYPE72Gb0/Dv+xnC/+5Oqi VYUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190567; x=1738795367; 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=X7mcGCY9R/2wAfvNybb5kTke1CSbZ/pNKwqh3Rdbh4M=; b=qZiBDceTeF1m42ZdFOM+GyfZpHL23M4sXXcRjVZLt9f3SfWn6MyxvhyiEo/UQGoOay v+3KpfrALhSGMQ3QPDDtWCBRwPRI+uQ5W6iaJPZvSRvrCP3tqI/omoTzd5TdzEdqFqmx uFnqqsypxeinug5HEOsq2P/o0EuVvmU6fuVpLzwjoQAwkQ8+AuILgGZbJJRbRwTwZxPv 31OEqfXez9WdMSg6py0e08SskJkf+uSueheiY9S6EY3EAcB3VfX5WfylDlRVKAWrkMwK qAPrJhZu2dxTYgDR0sS0oc72u5p5SjJeRDqfMyfhJ+fnIKJsJztSPFdY+w4Q2mI9GOyg xJWw== X-Forwarded-Encrypted: i=1; AJvYcCVa6y9ZfkKZ4ZmTAYkdKQOtRKB0jum59p3/c6HGrwWhectR+HtR1rYhw05dydZ7zeiC6EhLJZPih5FBogs=@vger.kernel.org X-Gm-Message-State: AOJu0YzztmAK/GHHd7DKaa/RRJ6ic70kpngSk5EdeZscL0ua1DxW6LAv FY+3D70m975deVVAQ5/Y0nhY4HM/tS6wTX/PvVJKEe84cMTbg421HdcW4A0sS7kVDp5dBg== X-Google-Smtp-Source: AGHT+IGG+kPFiJE3B/odsMz1ETLGa3V2S5NMwNDqcw7c4uCRCsh1XEHgPCNnT2AiKU+A8bBdmV2pW4ZW X-Received: from pfbcp23.prod.google.com ([2002:a05:6a00:3497:b0:725:f14a:b57c]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3924:b0:728:e2cc:bfd6 with SMTP id d2e1a72fcca58-72fd0c679b4mr6370617b3a.18.1738190566976; Wed, 29 Jan 2025 14:42:46 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:48 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-20-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 131831F1526 for ; Wed, 29 Jan 2025 22:42:48 +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=1738190571; cv=none; b=qf9bUeYKx8EwZ/816x/aJ+VM8OQ9d9PekEnu112MOWX5eXfnUSCVsF/3eTLrSYTp8pjxclUyHbAX9l0m9x6FSmHIjQJidpqNzu6XF4gBSg4+J6GYkHr6JSTZiNEvTRgV6NHm3DqMjDQg+xRDlVuovBfAq9uAcmRVm8TGzylYknY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190571; c=relaxed/simple; bh=mGy4fjcsHG2azxtHY5NUUKIT+cauYXSJYxyCrT59kAM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G3LGwVgATJi/w2UCPg8+8h2Kkv6bh0d2xE2/oJoOCXKl3LFprp3tqUWIBQk5MSrd/oaOUBLpMfKhLTwgk0904C41pJeTU3smStugEyKr6Nd4icj/eAebyj/+02UHtqehd6yh2p6sxii0yCBpLKiMDiz2JUy3Ifz4fcZjJTCnDzI= 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=dM3/F/yJ; 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="dM3/F/yJ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2161d5b3eb5so2654225ad.3 for ; Wed, 29 Jan 2025 14:42:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190568; x=1738795368; 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=98CKfAXCouaMhImAnm0e8KjEMzprOEcP3h/4GQttOQs=; b=dM3/F/yJ0eAWRLWvFZqBbIhi/bgts6KCYL9uiJpUcbSllSx1y9mwsFVx25tNBq+B89 /ccT2E+pHydzSQx74+ghjJBXf05a35m7qBJuXz3ej5hLKoF2w7GhSlRRs8IL8o1NsVWF wH/rrxC6KVgKg8PpCFDSHs+uemHcK9idwyQZ37VBL4a44HpdtPqZ4KozU89IabsES3yv mxpCl7sHZyK9N38/bItIUKiyC1+df1+OXspFIO/tmLbWpwCdNDobzoPRPe67A8OapD9s 2zKcZyZfMOfKOb3WnMbmRLElWoIEkaj2gZOlud7SP5qBsi7RiQ83PA8nnwqs3CYvzcuj aDtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190568; x=1738795368; 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=98CKfAXCouaMhImAnm0e8KjEMzprOEcP3h/4GQttOQs=; b=tXZ/WEQz1RhakNyE4WexlNeWoyC7+0F7nqrtdFsA5qf2Xa/1TjXbSZbsFY8/Htj8dh gD4ODIOzdmA925izifoH4sAcMAGIZljRyEs3VoID5HVf9yfoyeCk6Wxt2CP9bwRw1g9m liFU51Lm4A0mvKNN/DFK/DB6Ud56r1bCY2siaIiYZp5Fgsg46aYLPvHdv0RWCqknS7XS 1791JmFef8XJ/xDweSBbHxbP4AzrodKCk5qcAd4Ndr1Y2xsw3SHSuPQNJtLkQQzlbbo5 H+TymNIDTwtsuPf67SeswvGI4ha7Cupg5mouSqYSNwvxMOMAAOJCPekbo/shFbmtX1WS ukuA== X-Forwarded-Encrypted: i=1; AJvYcCUErH7pwiuoTPUJ9q5EcxyTtHOaFlRQRmToc0rdv8f7Ny76Xwy6DAzMyTjoacCRf6blZVnibyQ1ig2ZBfQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyfQDQBo2VW0txWVFh5mdc80ArH5vXKYeghxLHN/ln40GlO5anW fLp1bO533ghOkVKzuMeDcSkcCllpcX18kvGOdnTR29+SFDSwwydmYx3o5q+c5Kq6jgn8CQ== X-Google-Smtp-Source: AGHT+IEY1cyX1Tlj45D+PYHBljqRW8zszqnM/nXeXUYLBp6biLr4qmhI4En+OAtFaTTk0P/jl6YDzVhE X-Received: from pfbb14.prod.google.com ([2002:a05:6a00:ac8e:b0:72a:a7a4:9a53]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a1e:b0:725:b347:c3cc with SMTP id d2e1a72fcca58-72fd0c7bfadmr7456497b3a.23.1738190568330; Wed, 29 Jan 2025 14:42:48 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:49 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-21-fvdl@google.com> Subject: [PATCH v2 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 b48f8638c9af..5af544960052 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3311,8 +3311,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 bee22ca93654..29647fd3d606 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 7AAB01F37B7 for ; Wed, 29 Jan 2025 22:42:50 +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=1738190572; cv=none; b=aJmmq9XdLywHtZxAWjz7IvqAG9QMAs3BhjIsNXiLlwWRpEVauw0Av6eNRRy9AOZrtvNSfsXDtWdPgM58cAddSzfzgGjtFqdWwlV93EgHkgFHmD92aomB5xK9AMFGBKMgqEwvAftsxLPzMI3caJXRHnZF6pFTuix163papvzr8RY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190572; c=relaxed/simple; bh=PhxBtYJUW/kKgXQ6h3YdeGhAEyBHcClaMjIq80aDBUo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EMDhrm6mLBX/MN3zDMT/LutpaTt5NrAAzL6LDpg39hKPzntgWE9PvqS39zFH9HhLUAZps513/1J3bQPAzUdMJK6hRm/Jg+cMTPqxsvI9IhN3s/YJiU6JZlPp/PHzMLVhdQT099IO+D8jAp9eeSh1fV6Rm9wp8mHuU6Y8ILbXn8I= 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=Me1zxBgo; 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="Me1zxBgo" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21650d4612eso3943495ad.2 for ; Wed, 29 Jan 2025 14:42:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190569; x=1738795369; 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=m0+bWUW0pfRrXDtFyRBgF5DnwWrUxEqL81u9NMeAeCs=; b=Me1zxBgoXq34sGQb77SNNdIob1wQAfZKJqPNRmOIussKiF5kaXYIth1944NH6pcQf2 xLdJFygE/CLx/HMZg3i87klYl08OLzIUkQ0U3WFwjiSBcVGK2m9xqYKH4c4jnCwG83Gy hMG0+Q+tsgN08NGaymKNxThG1RODu6ADFoo4S9y9ml5tRO4xXXeZC26tuW63OUP9ipdS 7dxUFEBScgXo5tlLRtTSQ07lx12f0SyUYbD6zle+X3GYlZtQvlZZafAqVK5ht0Dmmi0u G3ElVJoZFY6JMLGsG3bGlfdYXeYkOkBK5okvKOUJnK2x/w59T2k9mh422oik6BKmloCc m+6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190569; x=1738795369; 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=m0+bWUW0pfRrXDtFyRBgF5DnwWrUxEqL81u9NMeAeCs=; b=KnhII6T4YEeLgbOhs04r1sjoARfxkANak3KsHUlGUex/cSOTQNLihvvqf9YTrUsDpf eC8dkvByGsbXcZ/KI7ZjStIyM6Xgvc8mC+d1YxXJsS/NvK5vDn0vLym1j+rp9mDW7xHO 2hjPcuJvi/sx3ajW7Dhybd8ulg8pbwwnqzS72hNLEKWEcgXZuzUpp5lfsBLSz5BGHLAc 3NFEv27nK9CoVtbar3pMYV8FKzJRmyn5yl9iYeOizD37cyCyi+mARUabDBqPCq5sz0Nk SUIhtBFTw928l29YZI0j8Peo4Bmb3E5VdeKn08F37tbzTaFybGKiDBE0TmdqNILszYGN wsQQ== X-Forwarded-Encrypted: i=1; AJvYcCVEbHukoBD7qNiqmkQ80pRWVO7UNkouw1KgDfDdrl7JdjmtG9nNKQWtewHNVVTtBR3I6BfNwDE8zBb/cxo=@vger.kernel.org X-Gm-Message-State: AOJu0YwpkMwuTWFjXT8XG+gI0Ol5m98LRHmIdCYAELIMKan8ui/MlYmB p1UqkZpw/Zk4pEEnLykep8bOyGIGfqnfeTEURQPRxfQ+GQBMFgH8lChyl1hbMwGMAOuqXw== X-Google-Smtp-Source: AGHT+IGzEL5Rip0Mz0fmGy5xX7dEt6FjPy6j1I5Lgvi49arCFvPrf2CjjL7zxLhk7rZXQGyKts87I7hf X-Received: from pgwg3.prod.google.com ([2002:a65:6cc3:0:b0:7fd:50ab:dc45]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:498:b0:1e1:a647:8a54 with SMTP id adf61e73a8af0-1ed7a640d32mr8499455637.20.1738190569677; Wed, 29 Jan 2025 14:42:49 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:50 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-22-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 082211F471A for ; Wed, 29 Jan 2025 22:42:51 +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=1738190573; cv=none; b=g7EeNU66JieQ/zmHgCDFTLB/onmziAqOY6C6+Y0DOld3hfe4SYle5op7UUTnnBS846dhtTrkBGN23vpV6Q4b5gR8dWMxXCiwuzLkD5L4svL909FzTvsTNPq/vypx9HIUVqrjusTRMGZ+YkFFbpWsnTh76FqH7Wv/E3KMIOJcbO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190573; c=relaxed/simple; bh=wvVzIzAs5+WDE+GJcz/oHr4G45IfBDRvB4m4C4Fb9Js=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dbsIvwl3iz1B5m8KOSrI0XcRQFyyT74kNFJsrbp/vz6wWjJzOro/Lxi+ZMm18H0LrwQmrtalGwe3p4RxKjFTXTJhdnCA/0OYI15HoIicRiq/Hw1+aGLgV1hGwIuwi1QEh3OyMwPAmj7CUPfOKfGwPUmff4imZQf34z0zU6b7a6Y= 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=yTIcXJTw; 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="yTIcXJTw" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21661949f23so4155375ad.3 for ; Wed, 29 Jan 2025 14:42:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190571; x=1738795371; 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=Cb+6DeiIrDYs6+D7kWz+SSV0N/KlwTlgW0EvhXblryk=; b=yTIcXJTwMRFGihYxBP/DmpvRg1XjjngH6QZydQdMG9kTDLJUlryPumxPTGqZ67ZrUL 2YTUbMtn0UKtviW6hf0FmdZokP/mjJRSN/JXKd28Kb6EbwOtxrS4gQG7EE6mrIr7+39p 1UfqFhTgmYWelTRmiyOwTI33c51osIx/vSZlthFUKPM6sFQuqTd/M/KBk79C6Tt9OCT8 3Pr1dRuQ4pmQ1DJpWo5CPiNuWYhsIvYxT3Za9LvfqDWCX6APmovHOSeMrXeb3d4UR/3D 3Btt1de8HWGaAyvWbXXXKNT+jXTxPy91OXOOFQys+jzDKRS0DgtHOz7I0HFsVPaRWJlW vvkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190571; x=1738795371; 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=Cb+6DeiIrDYs6+D7kWz+SSV0N/KlwTlgW0EvhXblryk=; b=MMbT50yFg76NOut8qhWpIbYL3hZzfytOWGL2GEqxRBb1hmF3UI0pgPOCDzZ1EkLhjU SRJeY54aZpWMCMtSfDLH/Vc7d7DWbaE4f0Jg2XockkbblNFcHAjv/HTqHFJURVaxC7+8 kB2bnCedumj3Et5YKudPAWV43zy2oK/w9pOYCO2K/V1lYUFk/WB7UZyxQU8GXs3RbFd3 w4YK54WUSvYk/hn8Sau+M2idcUJk9xlOAfZaODeNsRAgh4tdHN1yVn611D6UCPu6jlIQ iQgdwj/sGo16xJWg71edtzS3OLkv/coP2kBj6ndn0ge4pTQQU39bS7RgMTAujoJZes1r P4EA== X-Forwarded-Encrypted: i=1; AJvYcCWW6Vmar6UY66Ysqi0OiJ25I2/VgVZbkvGeGz+guWr4plDossHrzH+ZZbDbBUXa8nPAdrZBvGNmiv1UY10=@vger.kernel.org X-Gm-Message-State: AOJu0Yxng2SICH+RQys/RWFmwamh2JqNSQxjj/GCnKY+aFVUZuf0Bqxh /ZM4RwF4Ak1q/JYLRBJLzxbmSlwBzG4aUjC3+CSSeyzpGpYmebyfU7YAoZUHrRa1q6X56Q== X-Google-Smtp-Source: AGHT+IGDZN67byriIZ5TIJ74e/Xl+F0BuBhYUaCM7mwYBiMBi2VTra4u5+oA1ptyGp1vtiEeW3/9K7g+ X-Received: from plgi2.prod.google.com ([2002:a17:902:cf02:b0:215:ac55:d3ec]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f687:b0:216:6901:6847 with SMTP id d9443c01a7336-21dd7de1cd0mr67719505ad.42.1738190570966; Wed, 29 Jan 2025 14:42:50 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:51 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-23-fvdl@google.com> Subject: [PATCH v2 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 40B851F55FD for ; Wed, 29 Jan 2025 22:42:53 +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=1738190574; cv=none; b=eXbcF6mRR7Ij+FkRx9+JuFFzXtYqdjY/40UnluM3MoTuVNcSi3RTB1zNWun2dzKSgeqUXjwSQqC22MdU4xFeaRtKXq/8TlCrOrKJVgThWBqNSvMUr1ZibhfzgzI5LhdyYoczto8dD1w98vYnHm8RWbsiOob+dT6K44cpGbYDJYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190574; c=relaxed/simple; bh=Vt7ujdZKCvDORcNh9GG00ZkzbBiErZxGi6KwynczEcs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qt29eCuAa/qxM4VFDPHIM8BNou/ODomdOTWuIgAK/zt+j/6JV93rtYtsTHznuK4fqbd8tFM+GzxbV+o5YZkaI442H+X+LaKPbJi6x+vGvCVxUafIuFbjYIVEoW3pgRhAQS6IaQdnDAuYE1P+QZ8XJcMBqtAanzoc26/SUOXkl5U= 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=1mYm0wXV; 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="1mYm0wXV" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21640607349so3649575ad.0 for ; Wed, 29 Jan 2025 14:42:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190572; x=1738795372; 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=B9+SFAfifGO+4WMyk38z+oj+T8+477UR0bm18OCN8QQ=; b=1mYm0wXV7aVohm8PlxtXjkoE/c3p/N6h34k8TPyLitrMQaasD/pG+mAj4klC8Y2Ifq 7becEPDMnL0w5uIsizLoVIX24gsL6SlwPh7D9Ywyyr0q0SYWJ9sWF5dCdyTA8YMjpt6Q nNNsIvUARcT6JVBq+YrmpGx+m2uj4tLP0HNy5GTt/D5KJn9NNU++t0VpI4Wy6os2g/LE uLK5E9ahJK5V2O6ME+6En2t/0qGUn2BfxEsnU+308o4QjudwFssilMV7FDWWKtH5saAF 8oxNkMEgVdfDgSfpDB0OAWnTodQ0D6FTel0GLpmCD5CIX2xwrIjcFN7BgJk8l4iV6Bw4 YYew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190572; x=1738795372; 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=B9+SFAfifGO+4WMyk38z+oj+T8+477UR0bm18OCN8QQ=; b=OmyUs0BnyvOF/6P9QRJVXk98OA3WSSDiUMbv0Q5E9uWvfOIC2JSh5ENCuptp/WvP5F zQkgc9m+dhaiRWiu6CaOzYX5ekKVya1nIQT/CPhFHgpmb5kYDSsDoBq/mN2zOnuw2WO6 x+JIAO//zdG2SRG5f/cufOAI8VLG0XI2Y5b6PqD4v6HVniYXASqQaPfxZNWYGBWzqy+l Gg2ik2ElubSI7pZ3nRU82tx3E3pKZkweJMWpc03qG/2tHGlkFyz0vL9u96TRfmZzT7Az rPIG6TkBg0r1JnEVA0Blzq+/s1i1hKyTZ/f2TZnwlgQ3zHVlLVSI3NQ36n1G/CelSKmk aF8A== X-Forwarded-Encrypted: i=1; AJvYcCXzmojStZ51Unstc4nFosc7BPMcLUybk012KjXS/OV7j/XXbqNG4TzlWjvkDlYzMC38uWXFRzOW3AzjhrM=@vger.kernel.org X-Gm-Message-State: AOJu0Yyjsm/e8pFaN0Rjd7lNGWRW0AADoiS8WtJlNrPJKb7YngDtO7Gp 8lklc0As338ptl/K8D5Re2dUorR9nJQ7RIx9bp1kNBRXN23NTBZ9isURId2kNzWA5pa72w== X-Google-Smtp-Source: AGHT+IEfESeGYmpm9QvSf55e7+bojIZbnayRY5HSnlK9/hO+fi+i5SAew2Blg7kqEkcf7da+HHBE7s7h X-Received: from pgbca33.prod.google.com ([2002:a05:6a02:6a1:b0:801:9268:c344]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3387:b0:1e0:c9a9:a950 with SMTP id adf61e73a8af0-1ed7a61d4admr7290847637.39.1738190572647; Wed, 29 Jan 2025 14:42:52 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:52 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-24-fvdl@google.com> Subject: [PATCH v2 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 1704d5be6a07..6ad631c9fdca 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 AEA181F63CF for ; Wed, 29 Jan 2025 22:42:54 +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=1738190576; cv=none; b=Kzq3dGk0UdtdJzq3f/btesOYjk+3p3PrNnnQ8lQdPPqhPGaKthnOPVnh1Ld0toMyVdVtdj9O0Q1zQkcwnDDRpvoN8j7Na7TkGWy52ZFrW9tZLOKpIXup0T+IpNnUSF72fbli1Sp4oU2kP8b+1u5rNUp7Z2dcEThDuJzswn9hXcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190576; c=relaxed/simple; bh=2sbmT7XtFZlzqPrXcTHMyauFid3nwdk6aZCeOqsQMuo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RvrxAz9X5G6/tb2PipBd6jx/TQYpDQnxe7YpvNCHt5+qCpp92qOryVmbUqyN9TaDtLS8K2jspk1Gq8MT/pzW0pE+rYi2lJpT6AsSXiK6r2ddPwE2hE5G7sAQ8Xnc90jEtWoF3N6yhEBe2/BGcCL8cKPgfxq2rh8njpB4h4p5Xx4= 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=et8bWR9F; 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="et8bWR9F" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21632eacb31so2133525ad.0 for ; Wed, 29 Jan 2025 14:42:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190574; x=1738795374; 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=1345eHEo/n5m5FCY0Prqgv+HivXDQh8NAfYom1ZqdDA=; b=et8bWR9F0VU6SIHetciEk6GwTVUPRocFaJVBb07VTZdjHeRT/8lIC1JD+Twz7BNX43 jL2flpwJWcfOM3BpVxcsCpFYxdmLBgafXodjYd6xtZGgA4U9qPL8/Lfeu30HTZGduAng 0OzakKhMPDy1Ukon5lNJiltOwYselS6W4MhApk14OaAijV9Rr3f7yXUWaKNqE6yV+J1l 9xmqv/W4oZQY/yrbcHGc8/A0LFsEbKTE4dfWOmbYSE2c59P19omoKciQ1JH948XHh8Sx TNCiQkgH6ejDba/hhA4uGVGZmURFgaKIo3qDQYGrPxlxXSk3fbIFFLHbJeDwfsxNI3+g uCRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190574; x=1738795374; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1345eHEo/n5m5FCY0Prqgv+HivXDQh8NAfYom1ZqdDA=; b=a6+02dgI5cQNdmCMENsKZQRj/AP2Jfrx6lLRkSGtiz//SQl23jAwl+v2a210zZlrZy sW30LWVIg3EaLqoWkUeNtIMZHom4ZcJGZVrGuNX+rbp2/TC4YMWuxD+u+jq1tNvf0d6A bY1QJPpD/a3OlRd7+UJd/OJ/Fuy5krc0bGJ0a/5C7izXw68bnhU0mRqlv7QXazAm+nIu zWW+nPdNrUFgOZwyvgp+AmpfyxFzJxhPdtyx+04XOiDUOCl1Hlv9E1IyMesboHMXco7I aCAIYkeXd1Pb79i8UhzcaONwHrBlgyqMOIzxBmoIsrl22VFg2SFzbGZauUQcKRTLkR+n pYaw== X-Forwarded-Encrypted: i=1; AJvYcCUt6Am5WuA5sQBVjbSL2J+fV5swmXONkNWmgaEvEPr+DZ/vTefQHB2+8BPfx3sttoDc7HpmRA4eIvM7a48=@vger.kernel.org X-Gm-Message-State: AOJu0YyGrg1oGrnp6e0Jv00qzjwJ0u2shk/68CdAIb/Z3TPl9pBn+P1x XMbiBE9JCwBdifZ069zJCAG3hvGZeoOkK4FMiM5sPjFHnjkrZvhRHwOFZ4sO2qFGntGtEA== X-Google-Smtp-Source: AGHT+IFgKpf48L7TtwmSEw8t26tIVwiTmSEC5MTe4TDZk31dErMHXlLcuwwt7qrIclDuDAgLXqefYm/Q X-Received: from pfu1.prod.google.com ([2002:a05:6a00:a381:b0:725:f324:ad1c]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:1508:b0:1e0:dc7b:4ee9 with SMTP id adf61e73a8af0-1ed7a5b66c4mr8200655637.8.1738190574200; Wed, 29 Jan 2025 14:42:54 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:53 +0000 In-Reply-To: <20250129224157.2046079-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-25-fvdl@google.com> Subject: [PATCH v2 24/28] mm/cma: introduce a cma validate function From: Frank van der Linden To: akpm@linux-foundation.org, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: yuzhao@google.com, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden 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 6ad631c9fdca..41248dee7197 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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 3C1571F666B for ; Wed, 29 Jan 2025 22:42:56 +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=1738190578; cv=none; b=DZkRjzQM0H7UdaweWwQ5+tyjZoAUWbafjrz9mKFKFhzwPcnmjw8yfot/WiPLm4VBMlh1BuFRQyc28qP/s82vF8hgWXOghHiHVd7V/1UC1o2G+dxyK5fRwl2oLgZ72YAZxwtODaXgpN8nKTI5sY5gkgBXZaNn6PTMPVy3LEcaOGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190578; c=relaxed/simple; bh=mj+2l4i1G3XBDhQnviHtShRy/tMInv4rlEMGBrafl7c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vCWNbVRi2da7MDbFjAwY/pcSK0jnPQMJqVJUuZ0NpHiZA6l2ByHv4rAO0oorBOn25oPHhwG8ervXR0Jq3h0VHpIYVtAET58XKbSjwW++LZ/jDriaHiC0N3YgITaKoxDcSxI4pfkcbjRg85MV8BTvkDGS5d6zZOnA25cPUif2UX0= 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=rlPJWWu3; 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="rlPJWWu3" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2161d5b3eb5so2659915ad.3 for ; Wed, 29 Jan 2025 14:42:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190576; x=1738795376; 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=St+q1DoY+42s4g4QcYXYYqTt/AeFuMkTKVQ73Ddb3Ys=; b=rlPJWWu3n6FCVAhoR++uebQmHGjc0z3ppJsVTsOB4qHmnhb7uln2GHADv+iEvNSJmc dfYzG66NnTpoJwqdj11A4fmw6gUGhQqqFOYaT0z3iEdhYYRFetjAv4CKQXlQDa+bJJxH igmGJTqvP6aJ3D4jePtgRwWPo89YlssEtC5lnagLoUKWO0CHgu09bFvTlYhjnWduxqE9 HVMg7rddn1FxOPcR/CFbUUBvswxllRExL77qiNPCIX60R9UbXFAmiUxzGL0TfCKZZzOC OBT9v2gzseN24EUBaIwyGydhR/e6Rcjfj9zGgaD4Nuh4mhnttqT1OSWPPTZOPVe0U2cl UGUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190576; x=1738795376; 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=St+q1DoY+42s4g4QcYXYYqTt/AeFuMkTKVQ73Ddb3Ys=; b=NkLNWjmmu4JV7WIt2CbvfABZxxB8hhHTWcTORJeyO2pHRUE/lunuTqSKwiGxkhxd6n Ksh0hIREd72mqb9rN3acbQXws61CetuwC8KAxegPnBtpMZjFLx9zS6aAyjfj1SNf0y3G ZpIRLXn0OEIw0dRPQULkRXonO70RsKdaFPqvJH4MQ6hskjxmdG50uJ9tz4i6gencqNJ3 cTF4M6gFp5pfnkM6AplFUtSaAu1eyGFIX4LNZQtut3TAC+Km4roBlU+39r+XiDfwJlVq r08SZqo6kv2IodomxJzcaZ/frkC2pbrfXkw1XoAYMh5HHGMsTXD3lkBLKYguRE+l02Rf 4vRA== X-Forwarded-Encrypted: i=1; AJvYcCWRjkGOR9jWthGHKyY9WEEnUvOddhSi4aC/b5H9y6OIOSmqmQfccL5ebP5woh2iYQ3uw801eeFw3axBsxs=@vger.kernel.org X-Gm-Message-State: AOJu0YwgDPjWPleTcQPgzHJX89ANc7LAOsIu2zC0Ymw5V2AbQI8SJheR p4He7/cofzStuxD8w16JkWAv/vEK2faQmPll0qJ7O7JW+OBVcBn703K2WPeTSiX5LhKIQA== X-Google-Smtp-Source: AGHT+IHKa9OKErOBIP4kt8mPwsrgnK1mz1JVZAjaYOuo9N5ZF7fBntLgqqGp6sxXhVo/+8TW/rd/4Vec X-Received: from plbq8.prod.google.com ([2002:a17:903:1788:b0:216:2234:bf3e]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:298d:b0:216:3436:b87e with SMTP id d9443c01a7336-21dd7dff854mr91313615ad.44.1738190575742; Wed, 29 Jan 2025 14:42:55 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:54 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-26-fvdl@google.com> Subject: [PATCH v2 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 41248dee7197..2b1e264e4e99 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 @@ -1044,3 +1051,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 07C111F755D for ; Wed, 29 Jan 2025 22:42: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=1738190579; cv=none; b=DE5KSJZ76fe18J2PwnaEBuEuCVnSiIyzwziNsAsj38swyei5M96N8kUeD0g+zbulEwzLPGFeTD0lIThvbLiJXsiC2BocWVSynGEd23TPR013bm6oSWMkzMZmpek/dn1RU6FK7gYInkvBYwYwumN2Sxu2cslQA9dSs1CLgDyfZyY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190579; c=relaxed/simple; bh=5XwIis+2tn4egFHSvORYEbOh+UDPN61bwvJm+W0mrv4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sicmWxUno5WDVAojAcA4m2lv62YODH2udOAoyy1Y5nck9j/w/kQL9V4JOFMMhnaeIk+Vtn/m6yo3SXtUIxUjLDZYG/ScyXsDkvFaLsMP98BnhIQrG8+iNnrU4OTUa2rAC/cGCEiVOl3wIgj/mI+qRYQchLOOmUwj08PTEiWzPLs= 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=r7I06XhE; 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="r7I06XhE" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f5538a2356so221651a91.2 for ; Wed, 29 Jan 2025 14:42:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190577; x=1738795377; 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=Nz/2tKPy0YCm5EodMQQ+b1Mhej+Q9ynW9kEvQcn7esU=; b=r7I06XhEvdP+qSdS4ySe64dFsEpHwvt9QxKWF6khB4i19HDHnEJB9vbifYp5u5UoM1 T+xwE+sAxKgUHGP0/nnRLqXudrkDRL18ycQ/74Gqwo89RI0l39pp/JGVrFHLKGJa5Sza wefVn+/2Dh0X7ut1oNSIzbbCsaxGourgTgZhvnFnGBhI2+DbKa10bR8yC3R35c3kkRqO 3gWslDL7pj9CWsvARrs5YsznraGMto0eEGhHsAZIy8O/8Tr95i9WACvGD/d56rU/KjMM sFnXYqoEdb34mEclOHA/gfUaDEtSR8V6q6rScjztIBngB57O+WB8XckoLjVs/KMDV6u4 At0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190577; x=1738795377; 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=Nz/2tKPy0YCm5EodMQQ+b1Mhej+Q9ynW9kEvQcn7esU=; b=gD0UlZZ8KyKTWpyME1BFXyznPVu/y2KjSlKJ3E4N4bRZkMM3Rp8kvW2Cb5EB4NRsmv msRqARZ8GbCooeaeayDNmyVjQJ37Lc7N1iWLN73Hjkmv0JGT1A2WHuTIrurKmBKZmsrb ZvhVhPygqHct5rZ4xTFEud9Q0zq+iyD3R78o3++HvHNrjcfSPll05h5pMtpGSsQGz5m+ yZEGD2u08IbrnqJY85tsCqO4yOfJrYW3g+HI6whrsDSo43YZAIjPK0OqOIcko9h2JJom CoXjn8q4wznV7wTvTs2MZgkE/YiLT3yJVOvhGKTECYu2zv4g4vnUWpDjdk8F0l/HecP5 aN7g== X-Forwarded-Encrypted: i=1; AJvYcCU3vL20cQcXeL8x3LGFW9qqKXYFs6AIaUnxRrY+omDMo14Jh8649R/3NIzNOZ5D9fS2lmajQSzf14wx758=@vger.kernel.org X-Gm-Message-State: AOJu0Yyfnn40l/I69rxeB1zA5L6vWm9ayTBQlVSp63q6IDRYX0KKveix PCcsrvlDax3oXEfJaX2YXhkPIEvyBV6uhzZhsDL8ktnTuQO4QbCtDvR/FlbndlhOGdGR2A== X-Google-Smtp-Source: AGHT+IGXUZD7q/hJ2fVVwgnyF88t+mgr40dGpUsg8S5E9Rm1s3dTi62LjPOI+SAicO9GvtyTUPW1WmMG X-Received: from pfbcw17.prod.google.com ([2002:a05:6a00:4511:b0:725:cd3b:3256]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b8b:b0:72d:35ed:214b with SMTP id d2e1a72fcca58-72fd0c8bfb4mr7028832b3a.24.1738190577253; Wed, 29 Jan 2025 14:42:57 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:55 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-27-fvdl@google.com> Subject: [PATCH v2 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 5af544960052..c227d0b9cf1e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -60,6 +60,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]; @@ -1511,6 +1512,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; @@ -4732,6 +4736,9 @@ static __init void hugetlb_parse_params(void) =20 hcp->setup(hcp->val); } + + if (!hugetlb_cma_size) + hugetlb_cma_only =3D false; } =20 /* @@ -7844,6 +7851,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 721C61F76A5 for ; Wed, 29 Jan 2025 22:42:59 +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=1738190581; cv=none; b=u8yiOPhNGLvQ1fuOyq5G5/G5GuPlXp2s0SPPgbcnkNIycfxLBwVKyHK5LoIA3NdGnlQkY3LvOQ+S/9n6Zpwp0rENla1xvK12rwiQHPVMxfB1ph/ulesKx6DAM0iQ5DmO7d8IcgC7u17MeLn9nO8XjCJ925aJwBAT/3toyQ8xkRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190581; c=relaxed/simple; bh=fAeV/E1q5qgp9++nghxIuTERofsxLaqa501aimCgAt0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TxeTxV+9Xh6Ro2T4gwU1LqWSSY3Gm4/wriRWMVqHzoX64zqzX5r5cf0qsU+s6ZbuwU02wSibDJph00qqos1v3oKjzqy5XjVQtmz5EUz2hSNSqGEUrkHjw1sWfLPlUzgSGVSVkVvcdTaingaIMJtjK7EKRwyL0T0hDWjidXqNsd8= 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=YVdd5y2T; 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="YVdd5y2T" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f83e54432dso300449a91.2 for ; Wed, 29 Jan 2025 14:42:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190579; x=1738795379; 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=L0ZG8uhT3OL4gz53zsZU4uOQev18QCwSpJ1z8Lyz6lA=; b=YVdd5y2TmtUS/rcA09wXsbtrlqVilt20LDNtT8kbdFXmuSXiK5Ixm0vp161ESJ77c1 hBuIaHgh4IU15dynbA3vIOuuFXJOadPF+0BzKXO0lcsaU3PwoaVF8KkhbSMK6GcM5tnp FwUB2lUmQMPLZbMg5JhoILerW6wY/KHPelH82+cAtnx/bjTC3SX9e+VGv0nzf4v3OmJ2 jLS35ctRPB+8qpwaxr2yRfxGW4/0LBoyobRc3Xq4yicZ4KPFK2XRkA+jF9VqGtO6nwj0 Dqzk5d+PEQ1mUfF4VdFHyNeHgL1pfq+UIh4EP7aW17nnr7IvTaOlAsBY1GVc1S/uYyA8 CzMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190579; x=1738795379; 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=L0ZG8uhT3OL4gz53zsZU4uOQev18QCwSpJ1z8Lyz6lA=; b=jc0KOn6baGBT6Lp+LNlaXpL4rmaJX1PZhGNvrZ725FAVDek9KGCaHgRSG2rlxOjtDy MyODFRd2kCNJEQj+6tdyY+xLFS58D6sGsTC4QKkw1TX2AsOb4cS71vx3p//7pWYpOHqm 5uegACzUxX9oxT6MGhcrd6X+4vabmi+KS39tE26x5/57WrK8xBxYQlSrxIqSJPr2DPPM yNLzHKK6xFEKfxw39zwQ5I3SGt7f+Gydqtk/2McJ9ebV4uP8s6lFM/3AyUKUCEeB1kgx v7fIGzQR7A412v8exhxo51/DTFpCT7m/ky0ze3EnQJQoNwgoTgM7KEEFaCvWBIgqsntR 0p4A== X-Forwarded-Encrypted: i=1; AJvYcCUI8sDDpEIQVCL5/mw0+NFn3DcyUkObz1XEeeftU3P4vFlZXlWz7QhOWWy+sJxTgJKqDV9hDTGqKa9N17U=@vger.kernel.org X-Gm-Message-State: AOJu0YyXv/tVqnTLqDb79J0XTixhuVSvF484z/gVkdPudH3PC6RqqEwK +pw9aMptFEEA/0hodQOe0jOl8V/lD0Nylih9lv/0brxyYWJbigKv3TV+mAmLnUIJ3ndMkg== X-Google-Smtp-Source: AGHT+IEuTmiv3H9rTX6kMAO3mmM4IDiqwCIIyWovC1QOoLLo+K5CwA7MUZZn4zxCiCikjDN8GtWu9oQz X-Received: from pfbcl4.prod.google.com ([2002:a05:6a00:32c4:b0:725:d033:af87]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3919:b0:725:df1a:282 with SMTP id d2e1a72fcca58-72fd0be3556mr6904654b3a.10.1738190578851; Wed, 29 Jan 2025 14:42:58 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:56 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-28-fvdl@google.com> Subject: [PATCH v2 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 c227d0b9cf1e..5a3e9f7deaba 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -132,8 +132,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); } @@ -1509,6 +1511,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) { @@ -3175,6 +3180,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) @@ -3184,17 +3236,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; } @@ -3203,7 +3252,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 @@ -3213,18 +3261,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 @@ -3265,13 +3301,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) @@ -3280,9 +3328,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, @@ -3328,10 +3380,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 @@ -3360,11 +3418,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. @@ -3414,6 +3467,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 /* @@ -3606,8 +3662,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.262.g85cc9f2d1e-goog From nobody Mon Feb 9 19:26:45 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 F35521F78EC for ; Wed, 29 Jan 2025 22:43:00 +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=1738190583; cv=none; b=PoazUlaH0e+nqy0DQyZA4L2O4NuXTDxT39PIPR7VMsjOnFIrconQ9z71ZmqcJdvx2j7aCCJzIu9eJi802pkkCpgG3aJiAf+qGVl75D1IN/mYww0/PptOMF8+hBGuNdf2sCBdQgZUsUYtJJwL2x0fyws18OWjaMley+PEps3wa+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738190583; c=relaxed/simple; bh=ahWO5/NteXAquK3IMZahG/IwBguVO84SNacjgPRLiwI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u9TpMXT7Ekk7HV82NIAlC9hwkU6xPAPuNopmBTsR+3Ub7NszvtMh1GTRPzyQApZ7jG6SOpgzkH8OwkRPLsfqObi5RvZ0Ou7Ff+fDWRmwsTgxhqjjmrSf9/AOyxYGDhRa7LTVP6qARJJ1PKITLu6Jw/zVHNTluCi2Kp7ug/5C4/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=T9BIAbD5; 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="T9BIAbD5" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2178115051dso3216495ad.1 for ; Wed, 29 Jan 2025 14:43:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190580; x=1738795380; 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=aH4nwlMvvSTbckejE0k1f4dtYPUtlX/28RtOz+k6ygU=; b=T9BIAbD5FossZaL+mZWYA100nc886IIS3A3xpI6nuTvSsS2Bc2azspjCnaGe+5tGj/ k166uMApiPKGynGm4PjP0DE5pDK+ilaqQ3p7YdUc1e6jQol7mn9nnBEAaf+t4y33A7bN kAcdTBovsot6WHLIi9YyQ93L3KQzU3j7OGxO0FDTvDuiLiQFYe0BeOROOXuhMUc3hC6Q ev0MEz+vcfCZDUd/Uo31gzSo99pmAIz5vQ/K5hVTe1fK6S88K0qARJh3AapJ7letYO7A jHQXVK/Q+qO9g6VahwRRH8LpTIryF9DsJRy8CPbN5gYCw6/tRAtDCOZYTq2oIEpYhVz4 4mpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190580; x=1738795380; 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=aH4nwlMvvSTbckejE0k1f4dtYPUtlX/28RtOz+k6ygU=; b=IhRo/P7poeLCBTjTZhMy1rPfOJaWVzI8jQKtl+W1usw7a7DRYH3iteI01Bw2W1tFs1 X3yQuit1X9F306SMlF1fTAs9nfHU9F9SOXIFbs3D81lZ/PiVnGUEKtrYHFOYA/Ld1WGZ kbmYBaZEVq9tdJ7K9UGK+7hhvQ61fgmqk/SxlNllCot6VIXGh11Q2avpGfrUrWvMlAz+ sUaAy4sSNO+egQchE2AnSfMOaV/eQwKbrMO7b3H9+n+6uO29uzZC9baX3RwR+eyzpEPi C2WRM4RjCdkFhpAR+4nmsmQ+yyN7E5rtTYfBUjxXv+LI33D8Fn40KhCrodbmsoeNcoY6 2mKA== X-Forwarded-Encrypted: i=1; AJvYcCWhabLy53GFBZJK2LpiMBwnBnZ7YzphnNfqJtGF8lbBSGW5xRZRQjOWHyP8j76UT87onE9IymYwQutLJ/Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwDdLm5ior3kidkcL5M9O0mSQel4jheNRqczWQxybwVCeo31yfy Tv5HbQpErchh2EcPxLhuEcneBCARk763T2sXJUG/wmCvIOWIp3jIJiGJcQrRB5NUx9UJmg== X-Google-Smtp-Source: AGHT+IE+pj10b4z63haeyix4paPMsUGHOoSvFej7RbeeYAi9eA9qHLwGBxnt7Q4A+DDX+iTalohvAtl5 X-Received: from plgm4.prod.google.com ([2002:a17:902:f644:b0:20c:5d5a:9d64]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:188:b0:215:7b06:90ca with SMTP id d9443c01a7336-21dd7c57d68mr67625625ad.17.1738190580369; Wed, 29 Jan 2025 14:43:00 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:57 +0000 In-Reply-To: <20250129224157.2046079-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: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-29-fvdl@google.com> Subject: [PATCH v2 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 accomodate the move. No functional change intended. Signed-off-by: Frank van der Linden --- mm/Makefile | 3 + mm/hugetlb.c | 252 +++------------------------------------------ mm/hugetlb_cma.c | 258 +++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_cma.h | 55 ++++++++++ 4 files changed, 332 insertions(+), 236 deletions(-) create mode 100644 mm/hugetlb_cma.c create mode 100644 mm/hugetlb_cma.h 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 5a3e9f7deaba..6e296f16116d 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -50,19 +50,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]; __initdata unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE]; =20 @@ -129,14 +123,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 @@ -1493,31 +1484,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); @@ -3180,32 +3149,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) { @@ -3220,8 +3176,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; @@ -3666,7 +3620,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; } @@ -3703,7 +3658,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) @@ -4605,14 +4560,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; @@ -4796,8 +4743,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 /* @@ -7867,169 +7813,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..92eb7530fe9e --- /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.262.g85cc9f2d1e-goog