From nobody Mon Feb 9 19:31:11 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 917811DDC03 for ; Mon, 27 Jan 2025 23:22:25 +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=1738020147; cv=none; b=kZTYaG4nV5BHyaXkSZudfY8sXpKaCdA/nyhEtj99iZ81XhV8SOg/59cZYYiuQuNSbPXykpxlv4kcrHdFEwYDCPxq/aayJU2m6LgL03PvFbCCpOuHwM6EMkLbZwTkl6lao5gStfIVgeURKFdscnVpuSdhth76EzklJZ9XmlL5qh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020147; c=relaxed/simple; bh=GHBWu9lNt2ri18ecsXuz9716IJyH4boBN0tW9QvMgJU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hGAzJcx4vVdnX2peTooKjx7y0lZ3rOGJDoAke2T0tcDvYNZdkewgkV8kS9HSCOM/GwaQfBBzyyXEdh5MS65nWxMNO2vvpP/NLrvHBbHdL7cgK9QT466NyVZT1WnCirTBHz0j1NTHhOozFJH46NI86zfygdA2+Pcbh1SeJNo+uF4= 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=4f6zS9nR; 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="4f6zS9nR" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee5668e09bso10148391a91.3 for ; Mon, 27 Jan 2025 15:22:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738020145; x=1738624945; 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=4f6zS9nRt3cPVCCSlU2DU7ZgrYEXMXYYwJxYbKb7ISq63ivnUFEQxBn0VmuVOQf8ao +xeyFW7eku6yLfcJCPWY7AhXCsdLb8h3PbIBf3k/QsKpNL2x7K3Pfae5naRiWcLWDyqA 9cxVx3o9SP6El9EXryKwUH/q7hUpQtd/vJnQOUCVX1o61hesW8ooVL+2eu2gk3Ehkp6f fwJLSllFAg5BqXZdBR5g2e2ocf2vUzWjgSeFjkOKVXN3gNZ+wefbWulsBj9xRhgncdvl gOpEhxkFRiY7y+sy8vIa27Ye4/6JRQBmaPcAgb4+9npOs8ntfeQONbqQmxgW/VTzgiev QGEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738020145; x=1738624945; 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=mWj56QeuOZW4Yus3pHpXv/OTVe7smkzIprjrl7AnVGR5GVqOvVNaggaM+dYsJpNJtf zbnaY/GKh+ZHthQhTTLk6oP4pGUviMeBnkuSKO8lFCQSPYQNdaLSHc6LJ/aDOhx70hfZ pz4JL4DKkJkTXn8JtxlmZ1/tCHurrVL4mddbixkFlb7QPrUmGXyi6kjI4XHxRzQCXw+p gQIRjx+n+awg1zlRR7Q7e9HSjYBdeVP3cXppaqPylUPO4Lf97Ei3iqMRXb+X8cM/nsgW axWL2d77XJJTnnlKwvCNDA/qC1UfDdLazqhwnpkF3dcS5Ekb3QmaTIx1Pqzf/XmRDkuF 771Q== X-Forwarded-Encrypted: i=1; AJvYcCXBYZcuhdmFAdiVhWDTC56keu6vecgWfzLaU/3wuZCMsJVMRdo0A/IFtBXBbP4FeCGLbavaR4x2whITOJ0=@vger.kernel.org X-Gm-Message-State: AOJu0YwQq36GEurewjjvHiixuQNmRbtMdLlT3KycHgeTnB4y6MK5RGCJ sE53DIQkmrlqC5KmKXv2q+eyJr2vSht2EsAmf9WV98qkCP5Y8/GgN4X5WXUX7mV+9b6deA== X-Google-Smtp-Source: AGHT+IE2OfAZCY1fEw5GNZxTe6dAQr83OcZeh4iYNLGi7VR8UZal+A177NtIooH/8fQU83vYRYrstMLL X-Received: from pfbjc17.prod.google.com ([2002:a05:6a00:6c91:b0:725:eb13:8be8]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:ad8a:b0:72d:8fa2:99ac with SMTP id d2e1a72fcca58-72daf975d73mr60337775b3a.13.1738020144748; Mon, 27 Jan 2025 15:22:24 -0800 (PST) Date: Mon, 27 Jan 2025 23:21:41 +0000 In-Reply-To: <20250127232207.3888640-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: <20250127232207.3888640-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250127232207.3888640-2-fvdl@google.com> Subject: [PATCH 01/27] 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, usama.arif@bytedance.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