From patchwork Mon Jan 27 23:21: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: 13951842 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 F10D6C02188 for ; Mon, 27 Jan 2025 23:22:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80E6B2801BF; Mon, 27 Jan 2025 18:22:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BC032801BC; Mon, 27 Jan 2025 18:22:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 638DF2801BF; Mon, 27 Jan 2025 18:22:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 431AD2801BC for ; Mon, 27 Jan 2025 18:22:28 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BFFA01C7519 for ; Mon, 27 Jan 2025 23:22:27 +0000 (UTC) X-FDA: 83054807934.08.8FAEE7E Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf07.hostedemail.com (Postfix) with ESMTP id EC9D540007 for ; Mon, 27 Jan 2025 23:22:25 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gto8hhyI; spf=pass (imf07.hostedemail.com: domain of 3MBWYZwQKCP0k0iqlttlqj.htrqnsz2-rrp0fhp.twl@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3MBWYZwQKCP0k0iqlttlqj.htrqnsz2-rrp0fhp.twl@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=1738020146; 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=FRyClOaMnw3w8HyJ58my/pxRPLRK0xuLkwHKgqmlEUDCu3SglBYUIseGD7qi4XNOvUp+6v x8pZiTYmPE+kC2o7EBuFsWc5Tf+hwD4IhOyLwFk2CkRV04hIOmWcb6iAr/j0MhTBxrdUlj IGAYjqURQUl/19D78Imi+Dlb5H5mHiE= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gto8hhyI; spf=pass (imf07.hostedemail.com: domain of 3MBWYZwQKCP0k0iqlttlqj.htrqnsz2-rrp0fhp.twl@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3MBWYZwQKCP0k0iqlttlqj.htrqnsz2-rrp0fhp.twl@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738020146; a=rsa-sha256; cv=none; b=PTEATG2BuYQbpZPq24Y3IpHntdAs28EBdLsMWAP7+3wSDuvPCcUlNoIOd/WreIaEMcbQxh Sh2lEzXrp/r5R5Svt9RDDpPHhvRl7zf+ggPyoRR9eh7eju5r0vg0CzxB32bIZjM741UEqf pjwTbteEogbtEUxZ00NusVgNRJivwXU= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee8ced572eso10193303a91.0 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=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=gto8hhyIzQsA2RW2EzYOY7Ktsj96IVPJiIj6XZn5vFEL7Eo1mHuQJ2cWvz2cJHIbyG zufNh3QjP7iSW/VeXtSevrQKW8VrvMoTBisXf6Q6QBcrARV/a+qDMUVZHrvEZZbYmfL4 PzF1+1dK8UZsEdVv75A6FrDEbRywji2L5gGKtz5ng08q55xNFmNcP3vMCvZRmWHeMtCP SjEvkO7hhYllAfVykyfuCkUUqLShsVXOmTfgsA11W2KCVMfiF4R77tDDvW7Tvi369Lxr Z+SfYMmPQuyi0OEMzD572SYyev9fz+/ttkbO+TvO2GzinEiCJOqOwdMOuzBVERPWSSMI H32w== 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=Q8pcqTinTd9ZX3ibjS8PizCyyyHBT07S0lK0o0K6zaPZ/VEHqg+EfK3Y/hDkihJPHr onAFHWLBt8FY60lomsCFG3zPmxsMDZohx7/9jUhV8mopYxlh36BXLSu1ty/eZ1nFGei7 O2U4BJe2YibwZIHaCvdE3KGB2dMCXW/hrGrAaSYL7bDZc0B+LfHcNaw65NgcH2otfYgV 1Xq1JQn35qikyXnc9hc8NRFgfgn3JV8I57YvZOeXSp0Xgvlv6Bcnyu5zyBO1z9GQLl9X QgdSaCILeGdIqohbwT8eNyfwTw2lZac7apv9xA7VCr87sSHuk5/lyAAbbTKFHhziDGq8 weWw== X-Forwarded-Encrypted: i=1; AJvYcCVX3XYyJYFDy89ajCj733Gct51qrgSnNhqVsMl9mDPNf3wBcoQye6KCb2qOV1MuYZlsXEXJIO5Wbg==@kvack.org X-Gm-Message-State: AOJu0YwhDL7f+1SS1ylbx2QJCC5in+zHZPvY4DNQCXYrgH8tDXL3Kf8S U7iDPb0CDp4i5a3HKFq5YuZtlNYyEP57OKUb30gOAbOc49nKe1Id8OcK7LVTUQhuVLszFg== 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> 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 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: EC9D540007 X-Stat-Signature: 8jszf8u949w88minedds63bw7iqstbt7 X-Rspam-User: X-HE-Tag: 1738020145-467515 X-HE-Meta: U2FsdGVkX18GWk2fj62kZt6IXc6MTGSlPzR8WELJVXlpxyAOcge6Q3jq8hpZyhiHBIGjmJ/3Ddc1f+GWUITZKZ+qkL3rgVVl1D/ve4l8gNdUCMfvm6VItfIBst2RLZppTdja/Xlilq2Zoz/4sIoKhbdtQZikgdgOEzTJ/YjFN2nA73U1qR1A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d8U0PFTs4CkH2jjtoWmfaWpNIfI9vJ44nJDUSYDMrCY4lJjyE9cx+V+Kjpgbt+dvcw77aar6c5MT6SjPyythAFkra5RnfbWEKfOjlXNNsERtHPK0O689HtFSw6fJ16HYVJYv+GOOmhVCwqA7BodjOs0rkPuuslcLy8tXcw4I0DBrFezMLgx7yUkiPmbuhsRv1ZbMegAmZPNj9QgGz+CyeDdQ3w0jL/zuG2EqOHV7fwjpbXkC0T5QFdX9WyUh7qfrdD6yszMQZJBcp5qwwHdwFqaTHMBt68P/PUVErBtJvQbyC8rUAWo+m45TdDBk/W6FTjI4xRSuINi5dPDOroN2nnFcaYO9IWlmzJu1yNNcc= 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);