From patchwork Tue Feb 18 18:16:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank van der Linden X-Patchwork-Id: 13980400 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0A6DC021AF for ; Tue, 18 Feb 2025 18:17:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58C24280177; Tue, 18 Feb 2025 13:17:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C56D280176; Tue, 18 Feb 2025 13:17:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22BC8280177; Tue, 18 Feb 2025 13:17:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F0823280176 for ; Tue, 18 Feb 2025 13:17:12 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A29D01C6FF7 for ; Tue, 18 Feb 2025 18:17:12 +0000 (UTC) X-FDA: 83133872304.15.D0EC6A8 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf21.hostedemail.com (Postfix) with ESMTP id E336A1C0011 for ; Tue, 18 Feb 2025 18:17:09 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1+N0SRaJ; spf=pass (imf21.hostedemail.com: domain of 3pM60ZwQKCFk6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3pM60ZwQKCFk6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739902630; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YFMDP9y7jE/B7rMGFu3HCD39n6UMlP9JUwWE0fLegnI=; b=awU833F2rtgQYvCyUm8kOrfxPgjahMC4ywDoElKLGNdpHl8+e47BlGpT8Bc0GWfwPW0j2A KrEVGVJyuQpRq6Dx3fQxfpIvSMUva3h1A1VvBZDM0qsTIPqUV0FDaSL3hKiS+5XS8qssYG SXOUMt3xOIpnQAfrvMQDzEVAGubhjEk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739902630; a=rsa-sha256; cv=none; b=oF7FSDXVxg3X8HZli3WIjjmic58HMEV3HWNLaXEzKOUNbvpzNZsn0U1Gut+9uqXQdd/aiH cBMEjs4kFlxqZQSqDDTFhCYJM+vjP5qdFvFsDao6HSLlXlhEiLzDG6H22OlyDOnUx814DH rh05fdoURYnwiFU3f7wtHj20EdpSta8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1+N0SRaJ; spf=pass (imf21.hostedemail.com: domain of 3pM60ZwQKCFk6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3pM60ZwQKCFk6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fc1eadf5a8so11121064a91.3 for ; Tue, 18 Feb 2025 10:17:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739902628; x=1740507428; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YFMDP9y7jE/B7rMGFu3HCD39n6UMlP9JUwWE0fLegnI=; b=1+N0SRaJhbo6b+IWDWiKnMCrhREcAgwP3Xme4yO73R8B56Uxp6zM7u5Iy5JE6Uxa8S DiB2CntKKeqZvkyiCiCmjHR2Lwc2d7teck8dFzQzsxsojDVg7PwP3J4XsTBePkYpbk9V FRxsI+4iJT+aHmjMxJz4nQahDwhlNeu7YA8VF/j0pSmP1lgm4a8Y1THmp4OuhjZFfN3f K4lvAA91uaTr5oz7UsoXCkKV8g0USE9GCTgz/+8/bhm3yl5KxLARsC8waZXpYzwOl44s 7qjmNMlXHXnrEGRZBfeObXyBZs766de5LY8fQ97lC2zGqpq3FBB8hxQ6FHSnfSVxFBNw jZyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739902628; x=1740507428; 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=YFMDP9y7jE/B7rMGFu3HCD39n6UMlP9JUwWE0fLegnI=; b=PDJHefiIenQ/pjV1fYqu+W7P0bcunsqUNBvuJJoPfvSwEO+kDqNUeJ8gbUiLRLeQpp +JLeWCwYVJ6dXPgn+rj5VDjRW/7oLwUgcVYCyqMz+3nUIvaIjvqkWxpgka70dLB/3w1a ck+Mz0worU2OxYcqxGHvdjDUaGlcExyZpJFDzBY8vwxkRUtNqtokGRszpgVSp/AOFTm7 q9ZWPy3mg7fNG2c0Kgjf7bEz5bYWTgZlSqwkzobSwhh7nw39+DDvSsvAlkFWEDlPEyA3 wNpRA4R5O+9aTWUmrPZCinqDZ7V2mMfEsQuo+Ltgw9D/aOco0V/PwAW/0wQvbveWXNzM Yiuw== X-Forwarded-Encrypted: i=1; AJvYcCV1J8RnFPq41n5doPkf/heweB8aLJuRxFR1gm2QJ7SLHMXev8/eDQy/lREQr9NBTjrQ5o5Uhr6idw==@kvack.org X-Gm-Message-State: AOJu0YytBviL9Rfk5m/R4bKtJkRiMPkbW/ooW4CZ9MAxjVo/0AUTpZlz lxs6jm2Uwsn7DQNRzvTyg9ztnFqyrf6pO6tguANsde8iui4PP8EIFqytBUDUrFF8btCCEA== X-Google-Smtp-Source: AGHT+IFKNXDQlFw3p4kB+WyHMQa70HsCZ8AgEKV5M11liys6mRWRTzYQbQMsUg+5jC7sM7FrCTb3toTw X-Received: from pfbji12.prod.google.com ([2002:a05:6a00:8f0c:b0:730:50c0:136d]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1301:b0:730:8a0a:9f06 with SMTP id d2e1a72fcca58-7329df4e95dmr405449b3a.16.1739902628294; Tue, 18 Feb 2025 10:17:08 -0800 (PST) Date: Tue, 18 Feb 2025 18:16:29 +0000 In-Reply-To: <20250218181656.207178-1-fvdl@google.com> Mime-Version: 1.0 References: <20250218181656.207178-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250218181656.207178-2-fvdl@google.com> Subject: [PATCH v4 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, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden , Oscar Salvador X-Rspam-User: X-Rspamd-Queue-Id: E336A1C0011 X-Rspamd-Server: rspam07 X-Stat-Signature: oume3jm67ccsm6kf1zt49rym5gnuq8i6 X-HE-Tag: 1739902629-361875 X-HE-Meta: U2FsdGVkX19YrgB9v20WG0snjGfRQZmCqd0WyZmACYtySG8z2rAWX6EkLjPiaWfg+6ElDy83vT03CRr7zNVzwYYCAuYomen9kvOyJ/RZxGyJDUUktfImL/CbCCaOphKU9rfaLno+srfSoxjiGdy2eGjkIrbK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V5y/npQL5YMBKu/EDhL0k121pPne3xE+3xiNoxYL/PR4gmIpwBDMhnO2uEvIzT415KUNEklfeXtTmUN/pN7EbeYV82E5LxGhwVMA7/ZvJBf5QMOasZdGXeaox6+z+rf0T7R1V0BgHBzuwyCaWIu1rNGhh1RoOhPo27jq11z8HgNLeAq4VQqRD38E9EcujX/rMuhhCNUxO31TCtbA/keOK1f0xarmMMG5ep5mOLYJEajS4RWMTCE+ANH6fw5hlDKp5Zz3OFGAlX8g/dB0+ef9YZmNvo0AvWlrFBJdtUaE4jwRBPafE9rptLI9kUBcb5wB1YixAkYQn9uBCQ9SFFOLbeGL7sStIFCLlGjCCQW95yNOkfdmMVu6nFt6s5du/+e1c18jPg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Reviewed-by: Oscar Salvador 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 long pfn, spin_lock_irqsave(&cma->lock, flags); bitmap_clear(cma->bitmap, bitmap_no, bitmap_count); + cma->available_count += count; spin_unlock_irqrestore(&cma->lock, flags); } @@ -133,7 +134,7 @@ static void __init cma_activate_area(struct cma *cma) free_reserved_page(pfn_to_page(pfn)); } totalcma_pages -= cma->count; - cma->count = 0; + cma->available_count = cma->count = 0; pr_err("CMA area %s could not be activated\n", cma->name); } @@ -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); cma->base_pfn = PFN_DOWN(base); - cma->count = size >> PAGE_SHIFT; + cma->available_count = cma->count = size >> PAGE_SHIFT; cma->order_per_bit = order_per_bit; *res_cma = 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 = 0; - unsigned long nr_part, nr_total = 0; + unsigned long nr_part; unsigned long nbits = cma_bitmap_maxno(cma); spin_lock_irq(&cma->lock); @@ -402,12 +403,12 @@ static void cma_debug_show_areas(struct cma *cma) next_set_bit = find_next_bit(cma->bitmap, nbits, next_zero_bit); nr_zero = next_set_bit - next_zero_bit; nr_part = 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 += nr_part; start = next_zero_bit + nr_zero; } - pr_cont("=> %lu free of %lu total pages\n", nr_total, cma->count); + pr_cont("=> %lu free of %lu total pages\n", cma->available_count, + cma->count); spin_unlock_irq(&cma->lock); } @@ -444,6 +445,14 @@ static struct page *__cma_alloc(struct cma *cma, unsigned long count, 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 = 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, unsigned long count, break; } bitmap_set(cma->bitmap, bitmap_no, bitmap_count); + cma->available_count -= 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 = data; - unsigned long used; spin_lock_irq(&cma->lock); - /* pages counter is smaller than sizeof(int) */ - used = bitmap_weight(cma->bitmap, (int)cma_bitmap_maxno(cma)); + *val = cma->count - cma->available_count; spin_unlock_irq(&cma->lock); - *val = (u64)used << cma->order_per_bit; 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); +static ssize_t total_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct cma *cma = 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 = 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 = cma_from_kobj(kobj); @@ -75,6 +93,8 @@ static struct attribute *cma_attrs[] = { &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);