From patchwork Wed Jan 29 22:41:30 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: 13954198 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 0074BC02190 for ; Wed, 29 Jan 2025 22:42:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2659D280093; Wed, 29 Jan 2025 17:42:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 16ED9280091; Wed, 29 Jan 2025 17:42:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB633280093; Wed, 29 Jan 2025 17:42:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C9101280091 for ; Wed, 29 Jan 2025 17:42:21 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 79BE4804C6 for ; Wed, 29 Jan 2025 22:42:21 +0000 (UTC) X-FDA: 83061964482.13.79AC8AE Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf11.hostedemail.com (Postfix) with ESMTP id AEC004000B for ; Wed, 29 Jan 2025 22:42:19 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0iFOhYjO; spf=pass (imf11.hostedemail.com: domain of 3yq6aZwQKCNU6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3yq6aZwQKCNU6M4C7FF7C5.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=1738190539; 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=juYA+sItEByN7zxk1dgdSJwdSPC9Jk78U+kCT9wOvNQ=; b=c478r6uUDEMkWw1MHqvjg0zssUSJJpMJO+ljlqVx9jyxPAUApm6iOeSNkFGHVt7/Ya7HW7 wVSko+i4z03RwesKRfNbk7lwVFU4Qq4rJkEX7G6FZClzbm9owXRHVZzq2PV7GMWWsdPNJB /Pcfn69rW8Wi/LdK0Pv5fQyG1VOQ8S0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0iFOhYjO; spf=pass (imf11.hostedemail.com: domain of 3yq6aZwQKCNU6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3yq6aZwQKCNU6M4C7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738190539; a=rsa-sha256; cv=none; b=elwU8zdrOUxr7U/v9DrWdfaUMCbDr4ZxouY41hiGepmvsE7rLiJ2ipWaW+i4ze1xpn9Bjk gCOs60XP6LTJVxrx7v/PMCUdZ8Se26P8ba2DPsWD35U5MSxZEVYWQuqahNx3HlqYTkSosh KL0XQOqcB373QEBeh62ekgjGC1GFyh8= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef909597d9so322955a91.3 for ; Wed, 29 Jan 2025 14:42:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190538; x=1738795338; 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=juYA+sItEByN7zxk1dgdSJwdSPC9Jk78U+kCT9wOvNQ=; b=0iFOhYjO9KBU0ftkQEWJWoMBxtrEOlwsy0gt8CbcbY45LMEzpDt2EGAUxvjneqzpWs /ZTF4rXY6NTOvgKe1LTtuX7dZTrtlipPyHpdBHScBsqVw1DmrAexvJDCcAUlXvJOOfrz a/uCYzwxYq3iiGd+O9QpE7txW8/kn71TwWQnixcoM1ESkWLvmjVM1JXIdiCNo/jTT6DJ b9yHfMyHxhYnKlg/LDpJalCd6GisUZkfDFilionhqUZfF2u+7iEHZw7Syy/UnxJ2yVJL 2bTKngMC6jaIUNbY0hoJsnRhmtGPqG9XBkFKgybn8D0+//YcCethD+8Uu3Iv590YBVq6 VsnA== 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=kSaYX3t2bXzxxLR6ZOAFlplaEWZ95+p/tvVqyzS8s9SJMb7PatSCB+pMq4b/tk2a20 +NJZ6wD8cZaYdiF3eGSxT5C0dOd3mkfzP8hxqy4h0ug4TRMkYr/8jr85xhkU9DyN245O UdCECeZ271HMoXHgNLZzFz5k0Q4b73tzyGso5kcKjmFmO5OK0gXzjg8M3w1qxV516yqx d5jOSq/oS2Vv7gUQiF2OZMnNIgXvmBa4cW5rDihiro0E/31Df+4LnKr3aLtYyntjzxwV Spxly/jgpRQDiWp6ccpkEyhY3aT2LCs0JzBNtv5zdrABVGKkTst/wNAZLkQpeJNZ8TBU CkEg== X-Forwarded-Encrypted: i=1; AJvYcCVusjJ9j4bOifhImXO0otBvj7LME5O1iKbGCGWezO1OKS9T/x4BB+f2N087QGdEcXag57c/e/q5Yw==@kvack.org X-Gm-Message-State: AOJu0YzeBphKX8W09h256dwXrM3Ccdx58dn8qpgS1F4BOSHxrXVZvSg5 WE6ajt+LLXRgf/ubDsZCLITlwFhev/uW4CsPRcIGNvuMTD2y7P275T8wrI1Y8Gf19wMOBQ== 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> 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 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AEC004000B X-Stat-Signature: r6xjajymio9m7oh1igo71zzf9kot4iqp X-Rspam-User: X-HE-Tag: 1738190539-512609 X-HE-Meta: U2FsdGVkX18qFiEL68HCtlQjRJU2OW8aCSMw3DdPwaoR1GjsrwN+Fon/awRcNO4T8bJugIQPvdA+YHYgW35OyQ/wmBMBTJsb14pGOB5AyBiMjYON6ncaRcgQYycGchhOEQRoGyscBjGgD2ASTtytXMajxvwIzCswC+PyXYAJdHL1wPGbj//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sZ61dWZIK9Qiz0g2rFjszi+jRue/XhByEO+rGbZTV3Mc+AHkAvSk7jLumriOiG3JnJ6sZifNQYFjTFVyb0GRGJzq/2VNR9ZfutPjIrmsYNHhAsicYtvFTPZrMERO6m3F4xsb71fL6k4GmF2Pj9xOAd7ncE+eUj28tSOKy22dvIHeppNcACj6Y+XdRg92WC/Lq4dHR37xmWhmBv+y0YBGPpcVG4cK3MEsi+BmeRxH00G7K3wfBkj/fWyPEiQVg+YzPNo68BqUmB/hUnPD/PS7o2MkKBuy03TGkSoN/+ZYBEERml3O7nPODlBcDwHvi5JqLzjjSBB3VKN6GBsVnPvu12y7QaEgM= 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. 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);