From patchwork Tue Sep 13 06:10:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 12974432 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 76114C6FA82 for ; Tue, 13 Sep 2022 06:12:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C98146B0073; Tue, 13 Sep 2022 02:12:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C47A06B0074; Tue, 13 Sep 2022 02:12:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B37636B0075; Tue, 13 Sep 2022 02:12:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A47C96B0073 for ; Tue, 13 Sep 2022 02:12:21 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 820DC40426 for ; Tue, 13 Sep 2022 06:12:21 +0000 (UTC) X-FDA: 79906042482.10.10A286A Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by imf21.hostedemail.com (Postfix) with ESMTP id BD6871C00A9 for ; Tue, 13 Sep 2022 06:12:19 +0000 (UTC) Received: from SHSend.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by SHSQR01.spreadtrum.com with ESMTPS id 28D6B6RY050568 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Tue, 13 Sep 2022 14:11:07 +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; Tue, 13 Sep 2022 14:11:06 +0800 From: "zhaoyang.huang" To: Andrew Morton , Catalin Marinas , Matthew Wilcox , Zhaoyang Huang , , , Subject: [PATCH 1/2] mm: fix logic error of bulkfree_pcp_prepare Date: Tue, 13 Sep 2022 14:10:45 +0800 Message-ID: <1663049446-22310-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 28D6B6RY050568 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663049540; 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=Fk0K6sg3Ugu7cax/xJq/euasAiU3yIasU7phlif83cQ=; b=aXqVi5azL2RzCeIsrWtafaA56XKmNp71yosU0Q9o10UcM+ttU2JQiUkv9sDRVEPoQHPO5H O5TIDqUKrnZO7aUnKgl0VfMD1CCmIO5sHOObjVb0YIhFwwb5GqQXYuwmEvu8/BbL1BCPB9 RxGPfIfQGmfYwH+IoJKg+uyjdrSRHOA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.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=1663049540; a=rsa-sha256; cv=none; b=2M1Jfyt7WL9soNXqh8AyXjDUjBzhsRLuBCB3Er89NpNY0nhANmcaMay81VusjoXIyOjvLi sCenXgmUQAp5ejV3nEVOtWd4kxfG3FyDf2LHsD5hTQTMSyH9TAmMoUxgyhpr4oualbhhv3 2JiTJm3kXNwZAhmOCei+f3IhkQ1Xj5s= X-Stat-Signature: 3mmqbk4rf1pn44nf8og5w6srdty9y3o1 X-Rspamd-Queue-Id: BD6871C00A9 Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.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-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1663049539-116891 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 free_pages_check return 0 when result is ok while bulkfree_pcp_prepare treat it as false wrongly. Signed-off-by: Zhaoyang Huang Signed-off-by: Andrew Morton --- mm/page_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3d..69b15e3 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1424,7 +1424,7 @@ static bool free_pcp_prepare(struct page *page, unsigned int order) static bool bulkfree_pcp_prepare(struct page *page) { if (debug_pagealloc_enabled_static()) - return check_free_page(page); + return !check_free_page(page); else return false; } @@ -1445,7 +1445,7 @@ static bool free_pcp_prepare(struct page *page, unsigned int order) static bool bulkfree_pcp_prepare(struct page *page) { - return check_free_page(page); + return !check_free_page(page); } #endif /* CONFIG_DEBUG_VM */ From patchwork Tue Sep 13 06:10:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 12974433 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 9C8DBC54EE9 for ; Tue, 13 Sep 2022 06:12:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25F256B0074; Tue, 13 Sep 2022 02:12:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 20E176B0075; Tue, 13 Sep 2022 02:12:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D6938D0001; Tue, 13 Sep 2022 02:12:23 -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 001DB6B0074 for ; Tue, 13 Sep 2022 02:12:22 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D2171403CB for ; Tue, 13 Sep 2022 06:12:22 +0000 (UTC) X-FDA: 79906042524.21.1A0DA17 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by imf13.hostedemail.com (Postfix) with ESMTP id 0654120098 for ; Tue, 13 Sep 2022 06:12:21 +0000 (UTC) Received: from SHSend.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by SHSQR01.spreadtrum.com with ESMTPS id 28D6BAJZ050936 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Tue, 13 Sep 2022 14:11:10 +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; Tue, 13 Sep 2022 14:11:10 +0800 From: "zhaoyang.huang" To: Andrew Morton , Catalin Marinas , Matthew Wilcox , Zhaoyang Huang , , , Subject: [PATCH 2/2] mm: introduce __GFP_TRACKLEAK to track in-kernel allocation Date: Tue, 13 Sep 2022 14:10:46 +0800 Message-ID: <1663049446-22310-2-git-send-email-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1663049446-22310-1-git-send-email-zhaoyang.huang@unisoc.com> References: <1663049446-22310-1-git-send-email-zhaoyang.huang@unisoc.com> 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 28D6BAJZ050936 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663049542; 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:in-reply-to:references:references; bh=aNdv8qCcH5LYukQg1r6sPjV4CXDzeR8nmuL554nqKwo=; b=ipDI3KkQfsI4uc+TavsvmrbiFzJdX3/SwcD1fm2OEeOcUnNXS6KxBaBPKjqZknZi1Boknx P4bCG80r8WXQ6ZCZykw7mMJPR6GDFCrPDMlr+XGFSmz3ME5FPlJ3nVF094OLj85ysTKbtS rPzSdAYKFkvXyhWQswdBEiUCDRyLsi8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1663049542; a=rsa-sha256; cv=none; b=WBZ2aHRmG5DreKVsFrFOF753XKjfTPogSWcXVGtIGD7Ef+RiOA+i0FODlV4KVtcm7qg/H+ uNECifuAfC0gu5YrxNRMXHPy6FRFpQBE2UC6F3RTJAPlxyl+2EZRKxMBXB/460FOOKuJVj bEZSh9V+oDIgvhec8Lc5A2Z+fgUdqgc= X-Stat-Signature: kifxs8j3yyq6f1ububdzwxxiuikr9zb1 X-Rspamd-Queue-Id: 0654120098 Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.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-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1663049541-953063 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 Kthread and drivers could fetch memory via alloc_pages directly which make them hard to debug when leaking. Solve this by introducing __GFP_TRACELEAK and reuse kmemleak mechanism which unified most of kernel cosuming pages into kmemleak. This patch has been tested with alloc_pages(__GFP_TRACKLEAK) & (__GFP_TRACKLEAK|__GFP_COMP) and got proved as effective. unreferenced object 0xffffff807c620000 (size 65536): comm "allocator@4.0-s", pid 745, jiffies 4294906308 (age 5136.616s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<00000000ffefbfdf>] __alloc_pages_nodemask+0x108/0x3a4 [<0000000083595277>] ion_page_pool_alloc+0x178/0x234 [<000000008267995a>] ion_system_heap_allocate+0x13c/0x708 [<00000000d4df5a5e>] ion_buffer_create+0x98/0x67c [<0000000043fa6683>] ion_dmabuf_alloc+0xcc/0x1c0 [<000000000d1db17e>] ion_ioctl+0x150/0x350 [<00000000a2b89048>] do_vfs_ioctl+0x5d4/0xa94 [<000000008e9b61d3>] __arm64_sys_ioctl+0x14c/0x164 [<00000000114425a9>] el0_svc_common+0xd0/0x23c [<00000000ec9cb1b1>] el0_svc_handler+0x2c/0x3c [<00000000e44a2c21>] el0_svc+0x8/0x100 unreferenced object 0xffffff807c189000 (size 4096): comm "allocator@4.0-s", pid 745, jiffies 4294906309 (age 5136.612s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<00000000ffefbfdf>] __alloc_pages_nodemask+0x108/0x3a4 [<0000000083595277>] ion_page_pool_alloc+0x178/0x234 [<00000000b30c4562>] ion_system_heap_allocate+0x160/0x708 [<00000000d4df5a5e>] ion_buffer_create+0x98/0x67c [<0000000043fa6683>] ion_dmabuf_alloc+0xcc/0x1c0 [<000000000d1db17e>] ion_ioctl+0x150/0x350 [<00000000a2b89048>] do_vfs_ioctl+0x5d4/0xa94 [<000000008e9b61d3>] __arm64_sys_ioctl+0x14c/0x164 [<00000000114425a9>] el0_svc_common+0xd0/0x23c [<00000000ec9cb1b1>] el0_svc_handler+0x2c/0x3c [<00000000e44a2c21>] el0_svc+0x8/0x100 Signed-off-by: Zhaoyang Huang --- v2: code update v3: update code and Documentation --- --- Documentation/dev-tools/kmemleak.rst | 5 ++++- include/linux/gfp.h | 8 +++++++- include/linux/page-flags.h | 3 +++ mm/page_alloc.c | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Documentation/dev-tools/kmemleak.rst b/Documentation/dev-tools/kmemleak.rst index 1c935f4..b1128fe 100644 --- a/Documentation/dev-tools/kmemleak.rst +++ b/Documentation/dev-tools/kmemleak.rst @@ -81,7 +81,7 @@ Basic Algorithm --------------- The memory allocations via :c:func:`kmalloc`, :c:func:`vmalloc`, -:c:func:`kmem_cache_alloc` and +:c:func:`kmem_cache_alloc`, :c:func:`alloc_pages(__GFP_TRACKLEAK)` (1)and friends are traced and the pointers, together with additional information like size and stack trace, are stored in a rbtree. The corresponding freeing function calls are tracked and the pointers @@ -257,3 +257,6 @@ memory leaks``. Then read the file to see then:: Removing the module with ``rmmod kmemleak_test`` should also trigger some kmemleak results. + +(1)Don't use __GFP_TRACKLEAK when getting pages for vm_iomap_memory which map +physical address from kernel to userspace. diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 2d2ccae..53464c6 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -68,6 +68,11 @@ #else #define ___GFP_NOLOCKDEP 0 #endif +#ifdef CONFIG_HAVE_DEBUG_KMEMLEAK +#define ___GFP_TRACKLEAK 0x10000000u +#else +#define ___GFP_TRACKLEAK 0 +#endif /* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -259,12 +264,13 @@ #define __GFP_SKIP_ZERO ((__force gfp_t)___GFP_SKIP_ZERO) #define __GFP_SKIP_KASAN_UNPOISON ((__force gfp_t)___GFP_SKIP_KASAN_UNPOISON) #define __GFP_SKIP_KASAN_POISON ((__force gfp_t)___GFP_SKIP_KASAN_POISON) +#define __GFP_TRACKLEAK ((__force gfp_t)___GFP_TRACKLEAK) /* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP) /* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (27 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (27 + IS_ENABLED(CONFIG_LOCKDEP) + IS_ENABLED(CONFIG_HAVE_DEBUG_KMEMLEAK)) #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1)) /** diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index e66f7aa..ef0f814 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -942,6 +942,7 @@ static inline bool is_page_hwpoison(struct page *page) #define PG_offline 0x00000100 #define PG_table 0x00000200 #define PG_guard 0x00000400 +#define PG_trackleak 0x00000800 #define PageType(page, flag) \ ((page->page_type & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE) @@ -1012,6 +1013,8 @@ static inline int page_has_type(struct page *page) */ PAGE_TYPE_OPS(Guard, guard) +PAGE_TYPE_OPS(Trackleak, trackleak) + extern bool is_free_buddy_page(struct page *page); PAGEFLAG(Isolated, isolated, PF_ANY); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 69b15e3..ebc1dc5 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1357,6 +1357,10 @@ static __always_inline bool free_pages_prepare(struct page *page, (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; } } + if (PageTrackleak(page)) { + __ClearPageTrackleak(page); + kmemleak_free(page_address(page)); + } if (PageMappingFlags(page)) page->mapping = NULL; if (memcg_kmem_enabled() && PageMemcgKmem(page)) @@ -1521,6 +1525,11 @@ static void free_pcppages_bulk(struct zone *zone, int count, if (unlikely(isolated_pageblocks)) mt = get_pageblock_migratetype(page); + if (PageTrackleak(page)) { + __ClearPageTrackleak(page); + kmemleak_free(page_address(page)); + } + __free_one_page(page, page_to_pfn(page), zone, order, mt, FPI_NONE); trace_mm_page_pcpu_drain(page, order, mt); } while (count > 0 && !list_empty(list)); @@ -2468,6 +2477,11 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags set_page_pfmemalloc(page); else clear_page_pfmemalloc(page); + + if (gfp_flags & __GFP_TRACKLEAK) { + kmemleak_alloc(page_address(page), PAGE_SIZE << order, 1, gfp_flags & ~__GFP_TRACKLEAK); + __SetPageTrackleak(page); + } } /*