From patchwork Thu Feb 6 18:50:41 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: 13963549 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 D4F9CC0219B for ; Thu, 6 Feb 2025 18:51:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71CAF6B0085; Thu, 6 Feb 2025 13:51:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 67F63280001; Thu, 6 Feb 2025 13:51:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D1726B008A; Thu, 6 Feb 2025 13:51:21 -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 2E7396B0085 for ; Thu, 6 Feb 2025 13:51:21 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DE406A143E for ; Thu, 6 Feb 2025 18:51:20 +0000 (UTC) X-FDA: 83090412720.10.8765D98 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf05.hostedemail.com (Postfix) with ESMTP id 124A8100019 for ; Thu, 6 Feb 2025 18:51:18 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HNJLIqvk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3pgSlZwQKCIcq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3pgSlZwQKCIcq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738867879; a=rsa-sha256; cv=none; b=n4sA7dSkld+kU2LwtumAGyRhIwID8JiuNWVxmgSJ+46FyyID4nSY1zpVN9W90fOoSjx5wj NUEZLi1kSrYSdHBi8sDPL8Wt/oXiU0a167/sRPPovB28TZrj4nfWL8jQY9tc5XFTbe716l dDw8zNJe7ypncBDCij7P83CoWXz/e8M= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HNJLIqvk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3pgSlZwQKCIcq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3pgSlZwQKCIcq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738867879; 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=NJPFpuaCa1L0JdfMq2mnIjylFDa72i51MvIbj3cBAAk=; b=NNCSvqa4hmyxXADuVV29tLEzdTakBe+aI+8uQzbXufDJCMo67Fv3TOwwMZGmNKTkyaJYn0 +7OEcg7FFPRznVGyEV3+aRiMPiyJJM68+g0xQVvDgmLodqGOGBFRijQ6y/5NwzOimwxaU4 psSsWrrPn7d2Qk6EgpgcnaM9CWNIQMw= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f46b7851fcso3581513a91.1 for ; Thu, 06 Feb 2025 10:51:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867878; x=1739472678; 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=NJPFpuaCa1L0JdfMq2mnIjylFDa72i51MvIbj3cBAAk=; b=HNJLIqvkmkmzl+FvX/hCOdGbRDL5wR6CpmbCVr5Tdj9wADZynLwAB5kX3kcQ7D4Yy7 BNT1cmgUlchaMOmzkR9E7nHVKmbuVsoLrYYkCQ69BY9MW+LDBj8euqO56B+pcFiLyv6d bdA9Inwlaa6sjzfNRiPUJ/Slr/yp2vs3JBidlcQrnW2EHTbwmmoaz6iQngzr1WGVv8le Uv09KT92HWa/l1FevYC7j5URwyssn8vmPhyfHc2kj7gln/ANx762LMg0bnO/v7tv0m2I B7Xe2PebuQBAsclKBsMYVkeWiZDPG6hcIIfHv1oeMN22n55njMwXBuf1sG3211ASIGC4 m9zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867878; x=1739472678; 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=NJPFpuaCa1L0JdfMq2mnIjylFDa72i51MvIbj3cBAAk=; b=CODcaHkrRf9RZF7e2yJZCMB1lYnisjPKIwWq6x2Q8faU3t5dMqTAoJNgkmKxFvLDtA Xjwx73Vof1ovx0qgukJtg+sdF1pcSvHX1fWf4RTKQyAmze/XLrEjotkOAQtJWKH8cMh0 cTImEeJsCIpmxyNiv3P49ay3gsLtVrTcoFy0aP52kO4OaHLAVcwL4DadIKRZE4TmRjjC 8fcSuHpGdm4U3Y0itQOMbRUMRAFeLPyzMYyph1jInpjdDzIpU59ngd55ihkW5df03p5F wOgWO5pAZb2Gf2HEPCaXDASmTdmOCTc0SwKzh9VUGG+M0FvO4kC7UoD9uw4te/tkboPO j/ww== X-Forwarded-Encrypted: i=1; AJvYcCVKPaJWsE+Xd5Mt3r0gjSRPksC3iebY/ZD3oAS1x6TKfDv9Fg7fALJbWUTY2yaCrhvj6Mtxrl7pZA==@kvack.org X-Gm-Message-State: AOJu0YzzagvoaDrADqRgqfcJtFOLAPGYgJnMF0Jk3FlPDO4fKWKtNqd+ yvmyxZD/tV8jrsdCuHeU12NQRZYczz9VFgE8QClh44aw5PkLhra84PhVUdv95T5avMCblg== X-Google-Smtp-Source: AGHT+IE32zObckxQH9j+bb16bIAOnvT1L5KvON9aMD8I/isc7mdoYBPbnHRBSRCen9t4NlEX4I7KXcS/ X-Received: from pjbpm18.prod.google.com ([2002:a17:90b:3c52:b0:2ea:448a:8cd1]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d03:b0:2ea:bf1c:1e3a with SMTP id 98e67ed59e1d1-2fa24069ec6mr240439a91.12.1738867878036; Thu, 06 Feb 2025 10:51:18 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:41 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-2-fvdl@google.com> Subject: [PATCH v3 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: rspam02 X-Rspamd-Queue-Id: 124A8100019 X-Stat-Signature: dy8xwwwo1oc7mqtfiooay8eqdicpcwxk X-Rspam-User: X-HE-Tag: 1738867878-688959 X-HE-Meta: U2FsdGVkX1//+3CRRACdrsRWf2MAfbNAK43ws2suJio3jDqw0GhI3gbbf5VzJ2D8aaMVFwunxrvvpydoklmTwAsdvYf5eqSSbYs4OKAa9npmeNgEsQqx/iVKd3Juf+KWyzGcfd8eFTTEeEQqkP8hAR1kmECjaEMG22gC1unQ1aFJ5jETu457HsS3P9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nqDwkTTtjNjTyxoQkzpSpABJN9cetQyrF+15HJXEFDEsdTAKmjpGHTn3KTlqFur94qotPddPBTb67wibAH6gzpPV0HKKtYSYvU35SuAtmhdw33hekf6B/o1QF7BM4mDFWxqBYxoAPASo81WxRpFUSCA2ClO55Gb6JcPoXyeweoQ1YC8qamyTk81gacBAigtppFyNn3JS8VB31arcJ4sVdCN4gzodASUZTOb4jwBtFkLxKSplmZnNXT6zHNNFqT/LVo479YFhaRlVV/BbA8fNET1veCtIhK8XiXKQ8uI6qsvCLYpe0ZGhsySL2GrG/YJBjRZq+nEFmPj00gVEkzM4SU= 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 Reviewed-by: Oscar Salvador --- 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);