From patchwork Fri Sep 2 10:59:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 12964034 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 25234C38145 for ; Fri, 2 Sep 2022 10:59:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84547800D0; Fri, 2 Sep 2022 06:59:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F4F48D001B; Fri, 2 Sep 2022 06:59:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E3F2800D0; Fri, 2 Sep 2022 06:59:57 -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 5EEE48D001B for ; Fri, 2 Sep 2022 06:59:57 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 27726140D55 for ; Fri, 2 Sep 2022 10:59:57 +0000 (UTC) X-FDA: 79866850434.06.37E5630 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by imf16.hostedemail.com (Postfix) with ESMTP id E555618004A for ; Fri, 2 Sep 2022 10:59:55 +0000 (UTC) Received: from SHSend.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by SHSQR01.spreadtrum.com with ESMTPS id 282AxQN9047151 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NO); Fri, 2 Sep 2022 18:59:26 +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; Fri, 2 Sep 2022 18:59:26 +0800 From: "zhaoyang.huang" To: Andrew Morton , Catalin Marinas , Zhaoyang Huang , , , Subject: [Resend RFC PATCH] mm: introduce __GFP_TRACKLEAK to track in-kernel allocation Date: Fri, 2 Sep 2022 18:59:07 +0800 Message-ID: <1662116347-17649-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: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 282AxQN9047151 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662116396; a=rsa-sha256; cv=none; b=X0X/1FYlRk0bYNKv5LFTm6inNPSvMP21W851PWm0dyb1MEAJqsWcU61/3te0VDqvn+dB+Y mpRAfFekQ4hWt0Kgzx4ZvVJxRQcyAg5AIdB4GKowuelpevfJZOlPuUKXqBR1519Sv5//Ub lUv1OpXwfMYTU2XjcltriTc1x2ScOmY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; spf=pass (imf16.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662116396; 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=oNRVLevbLTNTAz722W6CQAiPOmKu2ycgEWV3wSlVAbs=; b=L7y/C4BRX9p7LBgBxQTt/+wqspxUK7R07Z2NvwmK1OIPAtlB/OLSnz9ti4ly+G9KUYymAM 6dqRijPGq1Pht8BALE56NRv9IWdFVTGyeNs6F2YmLM/2tqV0H5xWrmVMEkGdce11A9Ld/x fepEYv6FGhSZpYKOu/C347spRXLfO8o= Authentication-Results: imf16.hostedemail.com; dkim=none; spf=pass (imf16.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: rspam10 X-Rspamd-Queue-Id: E555618004A X-Stat-Signature: mzdxxormex9esdncw788ysztebbxgjxu X-HE-Tag: 1662116395-714067 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. Signed-off-by: Zhaoyang Huang --- include/linux/gfp.h | 4 +++- include/linux/page-flags.h | 3 +++ mm/kmemleak.c | 2 +- mm/page_alloc.c | 6 ++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 2d2ccae..081ab54 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -68,6 +68,7 @@ #else #define ___GFP_NOLOCKDEP 0 #endif +#define ___GFP_TRACKLEAK 0x10000000u /* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -259,12 +260,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 (28 + IS_ENABLED(CONFIG_LOCKDEP)) #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/kmemleak.c b/mm/kmemleak.c index 422f28f..a182f5d 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1471,7 +1471,7 @@ static void kmemleak_scan(void) if (page_zone(page) != zone) continue; /* only scan if page is in use */ - if (page_count(page) == 0 || PageReserved(page)) + if (page_count(page) == 0) continue; scan_block(page, page + 1, NULL); if (!(pfn & 63)) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3d..d8995c6 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1361,6 +1361,8 @@ static __always_inline bool free_pages_prepare(struct page *page, page->mapping = NULL; if (memcg_kmem_enabled() && PageMemcgKmem(page)) __memcg_kmem_uncharge_page(page, order); + if (PageTrackleak(page)) + kmemleak_free(page); if (check_free) bad += check_free_page(page); if (bad) @@ -5444,6 +5446,10 @@ struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid, __free_pages(page, order); page = NULL; } + if (gfp & __GFP_TRACKLEAK) { + kmemleak_alloc(page_address(page), PAGE_SIZE << order, 1, gfp & ~__GFP_TRACKLEAK); + __SetPageTrackleak(page); + } trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype);