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: 14203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3581334wru; Wed, 2 Nov 2022 05:14:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7JgpvCaPa2IhTESkli9KtBLvQT+mkI4Vm0aKPSaZAw4Pn6c+hvF3v8Pv9lTTzuqD0hGjeH X-Received: by 2002:a17:90a:1946:b0:212:f926:5382 with SMTP id 6-20020a17090a194600b00212f9265382mr25060487pjh.218.1667391294137; Wed, 02 Nov 2022 05:14:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667391294; cv=none; d=google.com; s=arc-20160816; b=xppv/9y4VCddCQUk8tkUiphBqgKzRGFdAq7AKFojXDL8YOrnjt2EOf4KJUorSC7LSs bu/rLvn+NIdLTUcjL3Gy2ESZL0RfdTQwb4dvXI9Hy6XMzYf4GW0WfovuVxIxrZA+gYiq n65rSuHoL5gydQqotROAGGWW94bSSqV1Xz+mlyDGTq/XXYY3RXb+1W35rEfH/bLCw3Hu P2o4TrujQ/orlTrsKJdqxhkQLbr+7kk9KQKOubhtqe0YFSPY2ZhB/IFLuLc9020CCO7z 5G4M91LT8cqbahSSadGgwi8fpCWJ6hB8ck+S7Cx/78RigD6nsWyNkvgmARvMIOYeXZHk Ndtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:to:from; bh=7LUIjp4vczzrzamc4IACx613LrYFWrZ3YIaCcdoUO5k=; b=1BgpPsTRVyyit02voxAE6QLeW1vfG6X1EF4dww9SL4ZnV7J4S4oDpV0SU82HlNipQh 29dM23tE5aKBJbzKXHHDNyE4r0nWR+HgG9AzV33rEXc1GEl4TlpHJRKJJuKfBl1AymSA nk9mYSwbajma8Cey+Z7mKc8/7ZiEquNsrgDiBwPO7F6n3iiTLpHPN3Me51coiUyGqSBh QvN2RmRJE2MG9jXMhaQU7WGWQV18rIN3M2OjmmOxy6kzsn4bHhXvfzXkgITiH0qImeS2 Q32UxHjr+sage7sVeuaKZIDC/93Y3FhfT97CUAnD3BubpwbiF+PmFWOa1Wubl1soTJx9 I6FQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a5-20020a63e845000000b0046f3717229fsi17324186pgk.302.2022.11.02.05.14.41; Wed, 02 Nov 2022 05:14:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230229AbiKBMHY (ORCPT + 99 others); Wed, 2 Nov 2022 08:07:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbiKBMHX (ORCPT ); Wed, 2 Nov 2022 08:07:23 -0400 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3669D64F7 for ; Wed, 2 Nov 2022 05:07:21 -0700 (PDT) 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 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748386493464580317?= X-GMAIL-MSGID: =?utf-8?q?1748386493464580317?= 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; }