From patchwork Tue Jul 9 16:30:53 2024 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: 13728327 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 E9632C3DA41 for ; Tue, 9 Jul 2024 16:31:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E7AF6B0098; Tue, 9 Jul 2024 12:31:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 797506B0099; Tue, 9 Jul 2024 12:31:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 637696B009A; Tue, 9 Jul 2024 12:31:05 -0400 (EDT) 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 4627B6B0098 for ; Tue, 9 Jul 2024 12:31:05 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F263541A51 for ; Tue, 9 Jul 2024 16:31:04 +0000 (UTC) X-FDA: 82320753648.20.5E85A59 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 174FB120007 for ; Tue, 9 Jul 2024 16:31:01 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UM6tJUNo; spf=pass (imf29.hostedemail.com: domain of 3xGWNZgQKCAEgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3xGWNZgQKCAEgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720542646; a=rsa-sha256; cv=none; b=AYMhGhVHA/C8SoS+xsygK1puStlMr8+3Kw4I1FqGPxdDo8moZ/q9BvQW1+ch16GftHJWIW I0g4MNQ9iMUeDFMz4E2Enco7xIwFgRU0jiMvKI1/Q+uXCpq6w8oM8WzIwXKC6qlHkTmJAF NuQ/zN2Xk3S0lqg14r8hbCiJpTKIw7c= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UM6tJUNo; spf=pass (imf29.hostedemail.com: domain of 3xGWNZgQKCAEgwemhpphmf.dpnmjovy-nnlwbdl.psh@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3xGWNZgQKCAEgwemhpphmf.dpnmjovy-nnlwbdl.psh@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=1720542646; 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: references:dkim-signature; bh=y5RiI1/WftUv65TYOiAp8vcGWTeflD92QLbJN57DRJo=; b=JDy0sCapSAISb7N7AVGa2+Qu3nyn4iPC06982lV1tWBHYMiWU2shyGKxBeIaU3VAUH2ke9 tKuLS4CSNET6zSnyoJusNRKP43Bl95OfNjNHZzRzcfB3qTAmwxKi3AxUVg5snlOLvDxWXK Tsp21fAKaZdenaAMAMel9iPFCtBS3hY= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1f682664703so41097055ad.1 for ; Tue, 09 Jul 2024 09:31:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720542661; x=1721147461; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=y5RiI1/WftUv65TYOiAp8vcGWTeflD92QLbJN57DRJo=; b=UM6tJUNog746rdFbD6bVl8K41a4gmGcALJvpqIaTfgFDOMTfs5J51ZwiK4YMhol7Yp sxoCgwTE/zeW2IuD6x1hOo5e0zfngxJsb3nkc6EmSW1s0J9Ec9aV7dyyDAGVZKFFqD/p L/xZu3vPGwdIpo+I4FoLlv4x/8w7fBB84TtZGm384oxYbopejBghUnW7MD+KU/GItEmH W7XWNhE7WTbt5L+DapnP40VzywpcmDhu6iM10gYuxRHNh+JdlwPVQhH65ykkDTNI/J6V kPj5mdTH44kwfWAZnG3Gdwi+5axQLI8+kkIOMalFwCv16GNIVteY1nzl7p2hBAwQyfqx O1pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720542661; x=1721147461; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=y5RiI1/WftUv65TYOiAp8vcGWTeflD92QLbJN57DRJo=; b=boO9lomC55dVYVLhSmUiRjcNzPbP8uwnIF9Aa4z4f7QoHFCcCewHX937wzIDWCYUac wTIBpxZWT/ZJprRHRlAotDrYY4jot9qiV038b7KKGa9D8qOt/mfC6Fm/vIeMW6GVIORn 6QJ3FbzgJRcF7Z34pPYk7JzezRxQPqyozsN6/B9tJTJkqyJfcLFDCHBObX/vVvdxSGJi jETFV5NVfuqnoJ17kz881xgIrqlvqOqXnBdAmvPtG3lX0fLrshQQ64agUmoU0Uov0Fpn 43KWYLaxni2aEVguBARdeosT17xAIhec0yv02PR427S5Z5fzIu2pZ26Jg9BFhAvDlvry Pa6A== X-Gm-Message-State: AOJu0Yz0XUdxvRgeXWhj7YH/h3rRfvovfO+AebEiN1pK34gWWaiOBqxb ppSYTLGCJeuk7G/AKDm2NgvW12/pEdKq1NksDnndLAFnUJYpIYJgiZQQIEEB/8D68E80EL44vzo w6oSIDgcCXrzHtaa2gYy9KvUf8ivvbMVuibvjyMa4/v6/vgilNFbeLkUVX7weyKM0gmsKmzN9PA 47TE2REnxzla3F9Al4 X-Google-Smtp-Source: AGHT+IHEPNf6YF/D2lxxo9kzhmYN4T17VfbjdCiJCEulS4pSavl/ccl7H7van8HjCkisBPeqdHwld2D+ X-Received: from fvdl-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:46ab]) (user=fvdl job=sendgmr) by 2002:a17:902:e5ca:b0:1fb:4a21:8d37 with SMTP id d9443c01a7336-1fbb6d69c6dmr2309225ad.11.1720542660621; Tue, 09 Jul 2024 09:31:00 -0700 (PDT) Date: Tue, 9 Jul 2024 16:30:53 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.45.2.803.g4e1b14247a-goog Message-ID: <20240709163053.2514760-1-fvdl@google.com> Subject: [PATCH] mm/cma: export total and free number of pages for CMA areas From: Frank van der Linden To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, Frank van der Linden X-Stat-Signature: 8u7oarx5to5ngozr3ks8tf5rnongakxm X-Rspamd-Queue-Id: 174FB120007 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1720542661-967236 X-HE-Meta: U2FsdGVkX19qYqpZK2DW9aWtfXDcoj0KrNgINP/wb7CeOhZN4QXiLEegaLtkqwo/kc6OANlGePY4A0gRGGA0UJTClKGF8DyAXzSoLVEBWwz3p+v9IilUCNso3noDFgaIGGPIYfp76puruk04TIa/LxhY58yu2GqqzeQf0oHgOX3CY6+0rwIgYlMHptW4l3HeYAb9t/UtY3hGH8sUgR9UzkjLJjBMtp1k3BG0RINbyNM2B/IUG68LOFv0PFBKOauQAtkZVgbfeXcBcQLVf37TmkIDk4zUPjckC78fBP+SKprZVllLJ3qNbLWTYdrECJHFclM2wU4F6C+nZMtdHs8t/eu766dJQq3upPaW6bjW4mZbpFAM388cBlIxw2C0945JfcTLviT3DwGHg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+7InBcUjaEd7urdCzglgMPNvllmMpUwIh8UiUL+uVqh7+2BlivXixZLDsI+B/BEEZ3Ag2OmHSgfCurWgBQ8SY/fEIPbCVPunSkzD6v6AaY+Z+m9KWPwDKkbnfDdFAx4tohDB/8Xlb0Nk68HLoyR5ItoAy6bHCcPpY9cnNybu 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 | 18 +++++++++++------ mm/cma.h | 1 + mm/cma_debug.c | 5 +---- mm/cma_sysfs.c | 20 +++++++++++++++++++ 5 files changed, 47 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 3e9724716bad..727e8c04d53a 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); return; } @@ -198,7 +199,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++; @@ -382,7 +383,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); @@ -394,12 +395,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); } @@ -446,6 +447,10 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, for (;;) { spin_lock_irq(&cma->lock); + 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); @@ -454,6 +459,7 @@ 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 ad61cc6dd439..d111f3d51868 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);