From patchwork Fri Feb 28 18:29:02 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: 13996900 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 4D8B4C282C6 for ; Fri, 28 Feb 2025 18:29:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB0CC6B0083; Fri, 28 Feb 2025 13:29:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D60C36B0085; Fri, 28 Feb 2025 13:29:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDD10280001; Fri, 28 Feb 2025 13:29:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A044F6B0083 for ; Fri, 28 Feb 2025 13:29:53 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 455F61C8ACF for ; Fri, 28 Feb 2025 18:29:53 +0000 (UTC) X-FDA: 83170192266.29.A9EEA13 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf27.hostedemail.com (Postfix) with ESMTP id 8621140018 for ; Fri, 28 Feb 2025 18:29:51 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pmii0ehb; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3ngDCZwQKCOsSiQYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3ngDCZwQKCOsSiQYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--fvdl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740767391; a=rsa-sha256; cv=none; b=09ASECOqzwbDRT2jQDHjjuYyeDteMrsDqx3DwPqbjH+kMQkKmKkEOS2Gf/A7XqjGFv74PC cX72VpsoKa0SZVqrqXlhZZJlrhUJ0mUjiX8ucZq2uu9G0SuPyjeOlD4D9MV89YdNAWt8fU J6XwB/Yp/3S2TEumtg47L8NOBMnwRtk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pmii0ehb; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3ngDCZwQKCOsSiQYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3ngDCZwQKCOsSiQYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--fvdl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740767391; 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=kKtMLmlpe+m6qN9ca3/progHrAqe0123z61YwAW9d3s=; b=zNqhuaGJdm9KoC+TLh3VgnBxchZifclX4zFhpcWWzoY9/PFMN3fYhNCGDxz53SL1X5DcbW zUShHCj+3U6f3+/IIfm8Wv+adrgFcdPbxviTBjF5Fu57880Nv39Rhd2oh1yVqdGHt9wvK4 sMYRarc6r+wyCYRzXIUx++Cjkzpp6XE= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fec3e38b60so1478603a91.0 for ; Fri, 28 Feb 2025 10:29:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740767390; x=1741372190; 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=kKtMLmlpe+m6qN9ca3/progHrAqe0123z61YwAW9d3s=; b=pmii0ehbXNF12C/WQWvnJZvHGoKrjNhHkZ3J2l5D/fjSZvx2p8W4xhRJWPDTaiFGUM QLAwBAILn6TBU4HKMcjsmdjyXxKd2aOYoimsVFvYaFoIgx/rg+W9Fsbo3FCUtCiewHrU 0PUQlw54BUFb0C7A1aXOtb1LRFiuFgbqvLnEQwzBA0UVm5zDaUxP0KMKc/V/6tl8UTDV 36qQhL50X8y5TCIQzdi7pi6vHPs64WTqCYWDxv0oeDtvCjixHiw0eY2l2CPMAy8eiK6H kQfZIzaPKI8vFfLwxMRW7z2Wz5TJVn+db5eBgbXo67weFqGVWHql3IG4mvHsPWfxbzGN ayqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740767390; x=1741372190; 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=kKtMLmlpe+m6qN9ca3/progHrAqe0123z61YwAW9d3s=; b=Svg077FF+/tGH+HpdRrlZVfbcL1DZ0XZ8RUdgRnvCWmNa6q18T+n73hBLaaRiLVbR9 rP6lFBjwZ+1cYINTNBCfOkHAkdtDF4EQOpWsqq1LU/yg58ubqaND3dAOQBOfwkNOO2Nk VjN7/6zY1nS0Z/KkU8p1BYUwg49xx7yoJGcREw+mKL1R0Nzvvew6/O7UDkXStmvBL1bv +K2MTYNvm/uGoFJofES1MMYpKDMYstsF/kaDAUPX7YBqe3oGTRbVEZ1QsZI3m+dGacEi Z6ZJUp/Qpyh1h1pcttdOqQkZFQU490w/adniAZ59CFIg2sycwWvMMoL10S4vlp+7nLX2 QNcQ== X-Forwarded-Encrypted: i=1; AJvYcCVw3bJROn4eGzLB84F8xNUUfWZgBdOER6Spht1Xc0RgR0JHex0y/mOlwASnemJBjN1ZOXnARiL9Hg==@kvack.org X-Gm-Message-State: AOJu0YyN2l61LqfHGZbmtnR72ExJFStImLwRRs4Qc6RMnCM5Ie72XEsC U8KkG7rnRt+BMZo231iWJjJLpxqpGjJYiM6Bb48XkhVe/BUp0TRhHZzdweI5C+xlFpVyPA== X-Google-Smtp-Source: AGHT+IHqJuYtYkzOhZDMCMvoR+05mDEEDRM0zr7QytAhww8Je1Y3UD1lMlTzOEV4CRBkoQI5L703TwyQ X-Received: from pjbsr11.prod.google.com ([2002:a17:90b:4e8b:b0:2ef:82a8:7171]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3a89:b0:2ef:e0bb:1ef2 with SMTP id 98e67ed59e1d1-2febab71ec6mr6563503a91.19.1740767390505; Fri, 28 Feb 2025 10:29:50 -0800 (PST) Date: Fri, 28 Feb 2025 18:29:02 +0000 In-Reply-To: <20250228182928.2645936-1-fvdl@google.com> Mime-Version: 1.0 References: <20250228182928.2645936-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250228182928.2645936-2-fvdl@google.com> Subject: [PATCH v5 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, ziy@nvidia.com, david@redhat.com, Frank van der Linden , Oscar Salvador X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8621140018 X-Stat-Signature: 7dea87fcr7zcidwt4dxz6a9q9jo1xc1k X-HE-Tag: 1740767391-317122 X-HE-Meta: U2FsdGVkX1+2pJ0DfHFTKJNo68QRiS9tbbzliwWsbj0fdFJ4acHwICqx7haGyV07yWmCgFIxaNxtLGUNAZB2+DaSzjm1qEchjD9G0P3mF/imGXyPL3p1m+VCKUtqHImxQVIsh0wz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rw+MmXzx1umuvKwS0J2DDX16GTEikjrj9qLFekzcYh9W5QuycNtUagtdq2vnnzZZ9cw0CAZSsM4hmgXxGebVu0Tp7tXZU2wcdtxuNewoU+al9nCJxL27A7A6c1Iv8qXcz4MDo7pqoFsiIRE8NBG3AMm3MP6VpbnajLq82g54HLck0b7E9Ne8f/sZdPFt/qnhPhf+4wgmRm/cKjCXPLuY3fGOBSZmkGzyEITTo/cuU/psYlwUPtUDdH5fUwBGiOWHEFr797Ms25a9PVkj+seCQeNzP4mJGOVng3DXUuS63a3bBGQMmPaka/SbltPrHBUzZ6V0u9tULOKMbdlF1tnfw2Q+tIvfFEAmGhdqgbOEmUw+7lkLIZ/r0+BhxMgxDl/risBVxc5L/cbyAqdNF0vJSimcNk0WoKLuOMO8= 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);