From patchwork Wed Nov 2 12:06:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 13028071 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 A708DC433FE for ; Wed, 2 Nov 2022 12:07:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C7B68E0002; Wed, 2 Nov 2022 08:07:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0780C8E0001; Wed, 2 Nov 2022 08:07:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E81968E0002; Wed, 2 Nov 2022 08:07:06 -0400 (EDT) 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 D7CF68E0001 for ; Wed, 2 Nov 2022 08:07:06 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 835DB1A03EE for ; Wed, 2 Nov 2022 12:07:06 +0000 (UTC) X-FDA: 80088376452.12.CF3D972 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by imf19.hostedemail.com (Postfix) with ESMTP id 742141A0005 for ; Wed, 2 Nov 2022 12:07:04 +0000 (UTC) Received: from SHSend.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by SHSQR01.spreadtrum.com with ESMTPS id 2A2C6eDg084573 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Wed, 2 Nov 2022 20:06:40 +0800 (CST) (envelope-from zhaoyang.huang@unisoc.com) Received: from bj03382pcu.spreadtrum.com (10.0.74.65) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Wed, 2 Nov 2022 20:06:41 +0800 From: "zhaoyang.huang" To: Andrew Morton , Vlastimil Babka , Zhaoyang Huang , , , , Subject: [RFC PATCH] mm: introduce accounting of page_owner via backtrace Date: Wed, 2 Nov 2022 20:06:21 +0800 Message-ID: <1667390781-17515-1-git-send-email-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-Originating-IP: [10.0.74.65] X-ClientProxiedBy: SHCAS01.spreadtrum.com (10.0.1.201) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 2A2C6eDg084573 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667390825; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references; bh=7LUIjp4vczzrzamc4IACx613LrYFWrZ3YIaCcdoUO5k=; b=G4KzmVHd7LRFO2ZNOBQg9XMpCIDKBoJL0bXypM4dslw996jWs4tQAmcnFPSyJunNA9b5lB vqIpA+Tvea1kPabdkMoTVbtr58SN3HUwlRfOF9y24j/FRY+90FrPi6zOBd0J9cugevTZDx 13q21Yv3oYQz+dK7YX3000DoiK3+9IA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667390825; a=rsa-sha256; cv=none; b=VCobgEhH4wKGSxqHBG17CWozoKVR45Nvl94T5F3SebuO+bJ1fv+36zFyTqL/7hUmp2cA7k m0Kb18AUkeYDtgrWAUKASKEtbcNR76APASpKAR8HOAM3KjD8pbg9C0bP46xsd0MesQ0WaL zRPMoSZjREwpC0Ht92j0b2mV03N67Eo= Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com; dmarc=none X-Stat-Signature: azwd833t36ffesfog7uux7z8pp6enyjt X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 742141A0005 X-HE-Tag: 1667390824-458590 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: From: Zhaoyang Huang Page_owner could be accounted and sort via its backtrace, which could simplify the output. Signed-off-by: Zhaoyang Huang --- include/linux/page_owner.h | 7 +++ mm/page_owner.c | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/include/linux/page_owner.h b/include/linux/page_owner.h index 119a0c9..c86a342 100644 --- a/include/linux/page_owner.h +++ b/include/linux/page_owner.h @@ -3,6 +3,13 @@ #define __LINUX_PAGE_OWNER_H #include +#include + +struct hash_object { + int count; + depot_stack_handle_t trace_hash; + struct rb_node rb_node; +}; #ifdef CONFIG_PAGE_OWNER extern struct static_key_false page_owner_inited; diff --git a/mm/page_owner.c b/mm/page_owner.c index e4c6f3f..3d014fd 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -42,6 +42,12 @@ struct page_owner { static depot_stack_handle_t early_handle; static void init_early_allocated_pages(void); +static void store_user_hash(depot_stack_handle_t trace_hash, int count); +static DEFINE_SPINLOCK(stack_hash_lock); +static struct hash_object ho_pool[8192]; +static int ho_pool_free_count = ARRAY_SIZE(ho_pool); +struct rb_root user_hash_root = RB_ROOT; + static int __init early_page_owner_param(char *buf) { @@ -152,6 +158,8 @@ void __reset_page_owner(struct page *page, unsigned short order) page_owner->free_handle = handle; page_owner->free_ts_nsec = free_ts_nsec; page_ext = page_ext_next(page_ext); + if (!i) + store_user_hash(page_owner->handle, -(1 << order)); } } @@ -190,6 +198,7 @@ noinline void __set_page_owner(struct page *page, unsigned short order, return; handle = save_stack(gfp_mask); + store_user_hash(handle, 1 << order); __set_page_owner_handle(page_ext, handle, order, gfp_mask); } @@ -570,6 +579,105 @@ void __dump_page_owner(const struct page *page) return 0; } +static void store_user_hash(depot_stack_handle_t trace_hash, int count) +{ + struct rb_node *rb_parent; + unsigned int trace_hash_parent; + struct rb_node **link = &user_hash_root.rb_node; + struct hash_object *hash_parent = NULL; + struct hash_object *hash_object = NULL; + unsigned long flags; + + spin_lock_irqsave(&stack_hash_lock, flags); + while (*link) { + rb_parent = *link; + hash_parent = rb_entry(rb_parent, struct hash_object, rb_node); + trace_hash_parent = hash_parent->trace_hash; + if (trace_hash < trace_hash_parent) + link = &hash_parent->rb_node.rb_left; + else if (trace_hash_parent < trace_hash) + link = &hash_parent->rb_node.rb_right; + else { + hash_object = rb_entry(*link, struct hash_object, rb_node); + hash_object->count += count; + if (!RB_EMPTY_ROOT(&user_hash_root) + && !hash_object->count) { + rb_erase(&hash_object->rb_node, &user_hash_root); + } + spin_unlock_irqrestore(&stack_hash_lock, flags); + return; + } + } + /* + * hash_object is the 1st node represent this trace_hash + * insert it to user_hash_root + */ + hash_object = ho_pool_free_count ? &ho_pool[--ho_pool_free_count] : NULL; + if (!hash_object) { + spin_unlock_irqrestore(&stack_hash_lock, flags); + return; + } + hash_object->trace_hash = trace_hash; + /* add the node to rb tree*/ + rb_link_node(&hash_object->rb_node, rb_parent, link); + rb_insert_color(&hash_object->rb_node, &user_hash_root); + spin_unlock_irqrestore(&stack_hash_lock, flags); + return; +} +static ssize_t print_owner_stack(struct file *file, char __user *buf, size_t count, loff_t *ppos) +{ + struct hash_object *object; + struct rb_node *rb; + unsigned long *entries; + unsigned int nr_entries; + char *kbuf; + int i, ret = 0; + unsigned long flags; + + count = min_t(size_t, count, PAGE_SIZE); + kbuf = kmalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + rb = file->private_data ? (struct rb_node *)file->private_data : rb_first(&user_hash_root); + if(!rb) + return 0; + spin_lock_irqsave(&stack_hash_lock, flags); + object = rb_entry(rb, struct hash_object, rb_node); + if (object) { + while (!object->trace_hash || object->count <= 0) { + rb = rb_next(rb); + object = rb ? rb_entry(rb, struct hash_object, rb_node) : NULL; + if (!object) { + spin_unlock_irqrestore(&stack_hash_lock, flags); + kfree(kbuf); + return 0; + } + } + nr_entries = stack_depot_fetch(object->trace_hash, &entries); + ret += snprintf(kbuf + ret, count - ret, "count %d\n", object->count); + if (ret >= count) + goto err; + for (i = 0; i < nr_entries; i++) { + void *ptr = (void *)entries[i]; + ret += snprintf(kbuf + ret, count - ret, " [<%p>] %pS\n", ptr, ptr); + if (ret >= count) + goto err; + } + } + file->private_data = (void *)rb_next(rb); + spin_unlock_irqrestore(&stack_hash_lock, flags); + if (copy_to_user(buf, kbuf, ret)) + ret = -EFAULT; + + kfree(kbuf); + return ret; +err: + spin_unlock_irqrestore(&stack_hash_lock, flags); + kfree(kbuf); + return -ENOMEM; +} + static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) { unsigned long pfn = zone->zone_start_pfn; @@ -661,6 +769,9 @@ static void init_early_allocated_pages(void) static const struct file_operations proc_page_owner_operations = { .read = read_page_owner, }; +static const struct file_operations proc_page_owner_simple_operations = { + .read = print_owner_stack, +}; static int __init pageowner_init(void) { @@ -671,6 +782,8 @@ static int __init pageowner_init(void) debugfs_create_file("page_owner", 0400, NULL, NULL, &proc_page_owner_operations); + debugfs_create_file("page_owner_simple", 0400, NULL, NULL, + &proc_page_owner_simple_operations); return 0; }