From patchwork Tue Aug 8 08:30:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= X-Patchwork-Id: 13345818 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 4187CC04A94 for ; Tue, 8 Aug 2023 08:31:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA66A6B0071; Tue, 8 Aug 2023 04:31:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C08F96B0075; Tue, 8 Aug 2023 04:31:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9992A8D0002; Tue, 8 Aug 2023 04:31:04 -0400 (EDT) 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 855896B0071 for ; Tue, 8 Aug 2023 04:31:04 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 533A7C0331 for ; Tue, 8 Aug 2023 08:31:04 +0000 (UTC) X-FDA: 81100267248.18.D700F33 Received: from mailgw02.mediatek.com (mailgw02.mediatek.com [216.200.240.185]) by imf17.hostedemail.com (Postfix) with ESMTP id 058A540016 for ; Tue, 8 Aug 2023 08:31:01 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=apSLlkmn; dmarc=pass (policy=quarantine) header.from=mediatek.com; spf=pass (imf17.hostedemail.com: domain of kuan-ying.lee@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=kuan-ying.lee@mediatek.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691483462; 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=Z/V/cug+yRVLoW4a8oKn71hM6nDUiC44a/iWC7mxXGg=; b=tnyoqEp4zEegPUtmzIeGDL3tvHE2AqL56CKAq+lJTGzE9Ub0TPTHSDwzsLY0g0gk1/VCLm VCEweeER2Z/oeEi6q9er6UBkk54H9fKkcUqSaCCWLvYbpyB6FedMCkaxzpugtba+9+PED1 Nlp18spwLdrorREozvW4UqCJeaz5yIc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=apSLlkmn; dmarc=pass (policy=quarantine) header.from=mediatek.com; spf=pass (imf17.hostedemail.com: domain of kuan-ying.lee@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=kuan-ying.lee@mediatek.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691483462; a=rsa-sha256; cv=none; b=Bo0ipBMsL75VELs2gEgRJPY/YPsiGEi/lbHC0XjHp2FbpBM/18huMbrSd8YpuNSFDZ3YUw 1wnw4TXnp0e4lGvVoPw7qBT7+fKT/BNjq25cks7SxQ1gPy10kEbe9SfIfHnjUi8aW+DpF7 aABFObqHwyIoleeMgehotJsqKke2ZvI= X-UUID: e4b3668e35c511ee83ed1395ce914268-20230808 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=Z/V/cug+yRVLoW4a8oKn71hM6nDUiC44a/iWC7mxXGg=; b=apSLlkmnSGmmDUfpyzp+Gh3oErlUdheBYpcAJECWkuGF7Q2JsR7fYe/3I7U/5ha2PlFostjoP50oH9YsHOqoRKa2m651cQwiuvtSBnSWHFqQXCd/4VUxBGYD2nKRN5wlIT3/ZYbdw2oC0F+fbGKRpeL++jxWRIqKDLjmsS9vksc=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31,REQID:4756ccae-c881-4f9c-b71b-1ec8b81b3c81,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:100,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:100 X-CID-INFO: VERSION:1.1.31,REQID:4756ccae-c881-4f9c-b71b-1ec8b81b3c81,IP:0,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:100,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTIO N:quarantine,TS:100 X-CID-META: VersionHash:0ad78a4,CLOUDID:21e8cfd2-cd77-4e67-bbfd-aa4eaace762f,B ulkID:230808163045V7DYIVP9,BulkQuantity:2,Recheck:0,SF:19|48|38|29|28|17,T C:nil,Content:0,EDM:-3,IP:nil,URL:11|1,File:nil,Bulk:40,QS:nil,BEC:nil,COL :0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_ASC,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD,TF_CID_SPAM_ULN, TF_CID_SPAM_SNR,TF_CID_SPAM_SDM X-UUID: e4b3668e35c511ee83ed1395ce914268-20230808 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 404750527; Tue, 08 Aug 2023 01:30:55 -0700 Received: from mtkmbs13n2.mediatek.inc (172.21.101.108) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Tue, 8 Aug 2023 16:30:43 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Tue, 8 Aug 2023 16:30:43 +0800 From: Kuan-Ying Lee To: Jan Kiszka , Kieran Bingham , Matthias Brugger , AngeloGioacchino Del Regno CC: , , , , , , , Kuan-Ying Lee , , Subject: [PATCH v2 6/8] scripts/gdb/page_owner: add page owner support Date: Tue, 8 Aug 2023 16:30:16 +0800 Message-ID: <20230808083020.22254-7-Kuan-Ying.Lee@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230808083020.22254-1-Kuan-Ying.Lee@mediatek.com> References: <20230808083020.22254-1-Kuan-Ying.Lee@mediatek.com> MIME-Version: 1.0 X-MTK: N X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 058A540016 X-Stat-Signature: 759xqgfa8jeaeoawtngsbbqkwx5fgw53 X-HE-Tag: 1691483461-505750 X-HE-Meta: U2FsdGVkX1//B68Mgb/dFhKSJsQoQztjAeB7S1ofYD05vjE3AOm3l9bU4lX2hMjM/1xh9SpeA8NGv15JrblMxAEGP+9K/4iiYCdEhSB2qeEjrsrFIb4NxKqfZ2YsLQ6GRHl5zH/SiNxntSdAlMtUPpZkcrtzW+c2S26iZXrPQYeUiQhzSZuT/kxXk3QHJ2l1DYG9Mof1VUib+ku1swIu9zvPXB03iTC6jjcjFWqH/Y6IzTfp4HWI6qsMQfLY/1Znf3suZYQwBr83tPR48myrtFbZ38L9OO2vH9ASym/1QiCCwOZe42dhIzLWpHlyo0o+3rKnnRO2wlcmZQSet5GwtZDZjtpuqMvAWLSA1MSqqcNncgTeA7zyEh0Wn4uNaYEDzY26icvtkZCwBHx1kMsYSMl9AKzkH2TicHfDq3xI/sFXsy4WOmfWjAihyZ09Ro9k0WqoJHZBVGV3GJxRH5BsMLpFHEr9h97nY1H00ZqXelvmGnjFZcQvbQxArGwaJm/mD7QWOUOTvDmdQqPGIbz/15zhHhJCev/YCvuWpI3ft5XV9406AULWS5nD8yoV/X4iTA/OWswdRvbDdEaUYXg9a1nhf0uFzhnML0YbtwBEMY7V31TXvVOxKl1I6/PStJkQh+Rgxv142yxczVUWDUnKOwNcXURVAhVnI2l19qYNjCD0OZkNE/vwsIGvViLLky6SPlwxB2Kq0prxj9S1Z8VQWTwhnfmKPwW6gqcNEGYX4ap4CsN+/PvAPNq4ln/Bbw+MPda6HJxkPzbDY3pSs0zNHfYjx5g9dHjU6vAT5zYZy7+Ee6lHQl8fWT7jP3GsWAS27UoQ4sl2g4tI4mbRqSGuJvsgp6f4zW2AZndDtA4K307e6ODfULVtohqI+z/AeVA9I0y8iGygacZxX92GDx+G9Q47rQxg5BN5KgyrBmf0AgdHbXOztCZAYtVZHY+xho7Vhi6QD1lY7AGKUPIYcS6 fyx9takQ 6/LW0Vi1k52tw/cQyONnddc+wFel8ri3IxYurreTr65aze1qhTZOMew4OjGicReHuBUxwWNCBf7HTGQE5QitY8RbS28XrFX0WxcfL5czRdPX81jdD1H0LaWNHAPgybI5UI3/uco1z1551/b+yXdGnyerHeSOgRF6OaNcP/cRKzHlHqoZYRCn4BgPMN9cE7DRx4zpXjPaw/Wl0ucLTd2FJ9qGFvCxLrDZPJ8H7btUqdsHYrQtKr7PFdD+mnrCKEKU1Z0NG0bzAkdCML2fnBpQn7s7yHqXsKMpNnh39 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: This GDB scripts print page owner information for user to analyze the memory usage or memory corruption issue. Example output from an aarch64 system: (gdb) lx-dump-page-owner --pfn 655360 page_owner tracks the page as allocated Page last allocated via order 0, gfp_mask: 0x8, pid: 1, tgid: 1 ("swapper/0\000\000\000\000\000\000"), ts 1295948880 ns, free_ts 1011852016 ns PFN: 655360, Flags: 0x3fffc0000000000 0xffff8000086ab964 : ldp x19, x20, [sp, #16] 0xffff80000862e4e0 : cbnz w22, 0xffff80000862e57c 0xffff8000086370c4 : mov x0, x27 0xffff8000086bc1cc : mov x24, x0 0xffff80000877d6d8 : mov w1, w0 0xffff8000082c8d18 : ldr x19, [sp, #16] 0xffff8000082ce0e8 : mov x19, x0 0xffff80000c1e41b4 <__dma_atomic_pool_init+172>: Cannot access memory at address 0xffff80000c1e41b4 0xffff80000c1e4298 : Cannot access memory at address 0xffff80000c1e4298 0xffff8000080161d4 : mov w21, w0 0xffff80000c1c1b50 : Cannot access memory at address 0xffff80000c1c1b50 0xffff80000acf87dc : bl 0xffff8000081ab100 0xffff800008018d00 : mrs x28, sp_el0 page last free stack trace: 0xffff8000086a6e8c : mov w2, w23 0xffff8000086aee1c : tst w0, #0xff 0xffff8000086af3f8 <__free_pages+292>: ldp x19, x20, [sp, #16] 0xffff80000c1f3214 : Cannot access memory at address 0xffff80000c1f3214 0xffff80000c20363c : Cannot access memory at address 0xffff80000c20363c 0xffff8000080161d4 : mov w21, w0 0xffff80000c1c1b50 : Cannot access memory at address 0xffff80000c1c1b50 0xffff80000acf87dc : bl 0xffff8000081ab100 0xffff800008018d00 : mrs x28, sp_el0 Signed-off-by: Kuan-Ying Lee --- scripts/gdb/linux/constants.py.in | 7 ++ scripts/gdb/linux/page_owner.py | 190 ++++++++++++++++++++++++++++++ scripts/gdb/vmlinux-gdb.py | 1 + 3 files changed, 198 insertions(+) create mode 100644 scripts/gdb/linux/page_owner.py diff --git a/scripts/gdb/linux/constants.py.in b/scripts/gdb/linux/constants.py.in index f33be26253d2..52f61d65f430 100644 --- a/scripts/gdb/linux/constants.py.in +++ b/scripts/gdb/linux/constants.py.in @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -89,6 +90,11 @@ LX_GDBPARSED(RADIX_TREE_MAP_SIZE) LX_GDBPARSED(RADIX_TREE_MAP_SHIFT) LX_GDBPARSED(RADIX_TREE_MAP_MASK) +/* linux/page_ext.h */ +if IS_BUILTIN(CONFIG_PAGE_OWNER): + LX_GDBPARSED(PAGE_EXT_OWNER) + LX_GDBPARSED(PAGE_EXT_OWNER_ALLOCATED) + /* Kernel Configs */ LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS) LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) @@ -129,3 +135,4 @@ if IS_BUILTIN(CONFIG_NUMA): LX_VALUE(CONFIG_NODES_SHIFT) LX_CONFIG(CONFIG_DEBUG_VIRTUAL) LX_CONFIG(CONFIG_STACKDEPOT) +LX_CONFIG(CONFIG_PAGE_OWNER) diff --git a/scripts/gdb/linux/page_owner.py b/scripts/gdb/linux/page_owner.py new file mode 100644 index 000000000000..844fd5d0c912 --- /dev/null +++ b/scripts/gdb/linux/page_owner.py @@ -0,0 +1,190 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2023 MediaTek Inc. +# +# Authors: +# Kuan-Ying Lee +# + +import gdb +from linux import utils, stackdepot, constants, mm + +if constants.LX_CONFIG_PAGE_OWNER: + page_ext_t = utils.CachedType('struct page_ext') + page_owner_t = utils.CachedType('struct page_owner') + + PAGE_OWNER_STACK_DEPTH = 16 + PAGE_EXT_OWNER = constants.LX_PAGE_EXT_OWNER + PAGE_EXT_INVALID = 0x1 + PAGE_EXT_OWNER_ALLOCATED = constants.LX_PAGE_EXT_OWNER_ALLOCATED + +def help(): + t = """Usage: lx-dump-page-owner [Option] + Option: + --pfn [Decimal pfn] + Example: + lx-dump-page-owner --pfn 655360\n""" + gdb.write("Unrecognized command\n") + raise gdb.GdbError(t) + +class DumpPageOwner(gdb.Command): + """Dump page owner""" + + min_pfn = None + max_pfn = None + p_ops = None + migrate_reason_names = None + + def __init__(self): + super(DumpPageOwner, self).__init__("lx-dump-page-owner", gdb.COMMAND_SUPPORT) + + def invoke(self, args, from_tty): + if not constants.LX_CONFIG_PAGE_OWNER: + raise gdb.GdbError('CONFIG_PAGE_OWNER does not enable') + + page_owner_inited = gdb.parse_and_eval('page_owner_inited') + if page_owner_inited['key']['enabled']['counter'] != 0x1: + raise gdb.GdbError('page_owner_inited is not enabled') + + self.p_ops = mm.page_ops().ops + self.get_page_owner_info() + argv = gdb.string_to_argv(args) + if len(argv) == 0: + self.read_page_owner() + elif len(argv) == 2: + if argv[0] == "--pfn": + pfn = int(argv[1]) + self.read_page_owner_by_addr(self.p_ops.pfn_to_page(pfn)) + else: + help() + else: + help() + + def get_page_owner_info(self): + self.min_pfn = int(gdb.parse_and_eval("min_low_pfn")) + self.max_pfn = int(gdb.parse_and_eval("max_pfn")) + self.page_ext_size = int(gdb.parse_and_eval("page_ext_size")) + self.migrate_reason_names = gdb.parse_and_eval('migrate_reason_names') + + def page_ext_invalid(self, page_ext): + if page_ext == gdb.Value(0): + return True + if page_ext.cast(utils.get_ulong_type()) & PAGE_EXT_INVALID == PAGE_EXT_INVALID: + return True + return False + + def get_entry(self, base, index): + return (base.cast(utils.get_ulong_type()) + self.page_ext_size * index).cast(page_ext_t.get_type().pointer()) + + def lookup_page_ext(self, page): + pfn = self.p_ops.page_to_pfn(page) + section = self.p_ops.pfn_to_section(pfn) + page_ext = section["page_ext"] + if self.page_ext_invalid(page_ext): + return gdb.Value(0) + return self.get_entry(page_ext, pfn) + + def page_ext_get(self, page): + page_ext = self.lookup_page_ext(page) + if page_ext != gdb.Value(0): + return page_ext + else: + return gdb.Value(0) + + def get_page_owner(self, page_ext): + addr = page_ext.cast(utils.get_ulong_type()) + gdb.parse_and_eval("page_owner_ops")["offset"].cast(utils.get_ulong_type()) + return addr.cast(page_owner_t.get_type().pointer()) + + def read_page_owner_by_addr(self, struct_page_addr): + page = gdb.Value(struct_page_addr).cast(utils.get_page_type().pointer()) + pfn = self.p_ops.page_to_pfn(page) + + if pfn < self.min_pfn or pfn > self.max_pfn or (not self.p_ops.pfn_valid(pfn)): + gdb.write("pfn is invalid\n") + return + + page = self.p_ops.pfn_to_page(pfn) + page_ext = self.page_ext_get(page) + + if page_ext == gdb.Value(0): + gdb.write("page_ext is null\n") + return + + if not (page_ext['flags'] & (1 << PAGE_EXT_OWNER)): + gdb.write("page_owner flag is invalid\n") + raise gdb.GdbError('page_owner info is not present (never set?)\n') + + if mm.test_bit(PAGE_EXT_OWNER_ALLOCATED, page_ext['flags'].address): + gdb.write('page_owner tracks the page as allocated\n') + else: + gdb.write('page_owner tracks the page as freed\n') + + if not (page_ext['flags'] & (1 << PAGE_EXT_OWNER_ALLOCATED)): + gdb.write("page_owner is not allocated\n") + + try: + page_owner = self.get_page_owner(page_ext) + gdb.write("Page last allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\ + (page_owner["order"], page_owner["gfp_mask"],\ + page_owner["pid"], page_owner["tgid"], page_owner["comm"],\ + page_owner["ts_nsec"], page_owner["free_ts_nsec"])) + gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags'])) + if page_owner["handle"] == 0: + gdb.write('page_owner allocation stack trace missing\n') + else: + stackdepot.stack_depot_print(page_owner["handle"]) + + if page_owner["free_handle"] == 0: + gdb.write('page_owner free stack trace missing\n') + else: + gdb.write('page last free stack trace:\n') + stackdepot.stack_depot_print(page_owner["free_handle"]) + if page_owner['last_migrate_reason'] != -1: + gdb.write('page has been migrated, last migrate reason: %s\n' % self.migrate_reason_names[page_owner['last_migrate_reason']]) + except: + gdb.write("\n") + + def read_page_owner(self): + pfn = self.min_pfn + + # Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area + while ((not self.p_ops.pfn_valid(pfn)) and (pfn & (self.p_ops.MAX_ORDER_NR_PAGES - 1))) != 0: + pfn += 1 + + while pfn < self.max_pfn: + # + # If the new page is in a new MAX_ORDER_NR_PAGES area, + # validate the area as existing, skip it if not + # + if ((pfn & (self.p_ops.MAX_ORDER_NR_PAGES - 1)) == 0) and (not self.p_ops.pfn_valid(pfn)): + pfn += (self.p_ops.MAX_ORDER_NR_PAGES - 1) + continue; + + page = self.p_ops.pfn_to_page(pfn) + page_ext = self.page_ext_get(page) + if page_ext == gdb.Value(0): + pfn += 1 + continue + + if not (page_ext['flags'] & (1 << PAGE_EXT_OWNER)): + pfn += 1 + continue + if not (page_ext['flags'] & (1 << PAGE_EXT_OWNER_ALLOCATED)): + pfn += 1 + continue + + try: + page_owner = self.get_page_owner(page_ext) + gdb.write("Page allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\ + (page_owner["order"], page_owner["gfp_mask"],\ + page_owner["pid"], page_owner["tgid"], page_owner["comm"],\ + page_owner["ts_nsec"], page_owner["free_ts_nsec"])) + gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags'])) + stackdepot.stack_depot_print(page_owner["handle"]) + pfn += (1 << page_owner["order"]) + continue + except: + gdb.write("\n") + pfn += 1 + +DumpPageOwner() diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py index 24f149694b81..89465f0de548 100644 --- a/scripts/gdb/vmlinux-gdb.py +++ b/scripts/gdb/vmlinux-gdb.py @@ -46,3 +46,4 @@ else: import linux.interrupts import linux.mm import linux.stackdepot + import linux.page_owner