From patchwork Wed Oct 18 10:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13426730 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 D0780CDB482 for ; Wed, 18 Oct 2023 09:30:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 497CF8D014E; Wed, 18 Oct 2023 05:30:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 420178D014F; Wed, 18 Oct 2023 05:30:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D5B98D014E; Wed, 18 Oct 2023 05:30:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 00E8F8D014A for ; Wed, 18 Oct 2023 05:30:27 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C0917120C3E for ; Wed, 18 Oct 2023 09:30:27 +0000 (UTC) X-FDA: 81358061694.23.7734FB3 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf04.hostedemail.com (Postfix) with ESMTP id 2E4444001D for ; Wed, 18 Oct 2023 09:30:24 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697621425; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SRLMJjdHw559WUkNqfMGixZkAAP6vteLR3osyUIfiHA=; b=SjM8kVi45YhMOLuASiqsNmnzd8gBwOW6+4kfqSlKfGZ4g4dR0jttD0pi0ycswNa2OELNEA SvBNUAwK2zi7fmEJBclbb9/QIcmQq8whtPF4Qaje4Wxk2pLlhG1Zruykf6kXcVCX1nqLGn sYN8dUUSrejpnp2L7SqgBLgDwgu/CQ8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697621425; a=rsa-sha256; cv=none; b=IkymGRtpBGA14eS6j2Jxj7vYk61ZLW+OT5NEesfXizhqJTmnn/NFcMf95a9NXN22fFzm2Y 7PaZIbaycUXRnwfum0XVje+QVSzyvC3xEQSS+kaQ8UcLkpS7aGQisOGOFzcODqKkd45GYK 2Yq0NiidYGsu3EOe9F4sJV7AkJ7TxU0= Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4S9QVd0ZRSz15Nh8; Wed, 18 Oct 2023 17:27:37 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemm500009.china.huawei.com (7.185.36.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Wed, 18 Oct 2023 17:30:19 +0800 From: Liu Shixin To: Catalin Marinas , Patrick Wang , Andrew Morton , Kefeng Wang CC: , , Liu Shixin Subject: [PATCH v3 4/7] mm: kmemleak: split __create_object into two functions Date: Wed, 18 Oct 2023 18:29:49 +0800 Message-ID: <20231018102952.3339837-5-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231018102952.3339837-1-liushixin2@huawei.com> References: <20231018102952.3339837-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 2E4444001D X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 36jgnognk8j16i7b8df4kr8mse89hbmx X-HE-Tag: 1697621424-990728 X-HE-Meta: U2FsdGVkX1+uyVeQ0i/UMT/jYDC/6CQEFXf7trjMUs/gBReguberngpozW4YABBuGLxZhXZkgVHMdBGrySd+Wwh6/O3I19lLJALkjUFcUyiOj8omd/H50PYwgB5/GgYbJg98zih/TLvFp6Md9SmFpFp96hNXNGTlBde0VWQ87Gu+u6EvPxk3M6GEXtcVrbvdIbexkoL5XgTo3VbiBM3IxVDRl6jW6Bvf5Wrmj33clq76GU+FlOHZvf09XmNQu331JkM0FTubELRvtSjuFbltyvgD5Tg3K4kZHFdae1M20xZZVcFHLyzYqd1UP3Mf9Ygd9GNEyqKX5j1Oxj6MbQjM9s9nGE4B9m9jFJvLY3yfMJyg/RuHBaFqKpAuw/wj8sowqqJ2mmfE+b/Ta1FGfi3tGIq3IA0N3VgrsnqHWs4DdknpFqm/vQX4dWz1aBW/N44zlDAmWEqag8lx4pxL+2fKZr9RIo/xx0U1b7zMoIXai7vF6oGj51OVkgoVZ3vzatPgymWEPcepTnRezQiFqXdmGd8RS5pK/Ko3fYZS7XxKurGi4BDtQ2DKKIfHUMfpuNelwcH9hCLuZOhrzxZAUZtSfqbh3zeSlQZH4j+eACIm9PEBSDxUgQAsh6+7v2gALbH2ahNy0xtqjyoVd7E1yVRAXmzwE+goDKtdyNZBVQB/zynwb9cKbSaZ80o6a/c9wPsbEBXr9AYPW8c7uUjWFbwfL24DNjMsl848ZsiJR9KBaCMUVC18KRn6AuNCMZr2fPHjCwTbQxtM/Wp9ODa7tZt2yldeIYzVPsOa0XuPeZ4CCkV2gfJNcX5bVvRO4PbcmFa2TDdLeTtv1brC/yD9Kd4O2JTN0m2zoqVe3JQf+3zx0BMqOIShTSU2e08lSkxPvzGeuxeN+3Agqva5aal+SXI6o7g9CBUfkncwGC5m6B+N3onXwdF6SplnoJn0/hn1pD6XuyMNEiP8ps+MqcXjHoQ Cz+8Xrzu wWf+IvOppM8XW+CCsB3Ryyn0dbBtI8XbcqykrJfXeHPRo3IGVh2e3d2Ja4l88vvwYFKD4HRaVVYjC6lWxAWuK33jha8ekRWO/dyfuZjsnZNhj4DHnpjA9IrZzKtaRXjCuJTXlArtUl1TjFI+cOPLUPT2SOymbJxhY2Xe2LeDsMj04Xld4PmA4Zbxm7hlpmHCAlqElmeZFEEYf583wjl/ILSagOnyR8VCOUBsArzrAYZo3qyE78MuuUWXiPz8B3GdAiIB5 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: __create_object() consists of two part, the first part allocate a kmemleak object and initialize it, the second part insert it into object tree. This function need kmemleak_lock but actually only the second part need lock. Split it into two functions, the first function __alloc_object only allocate a kmemleak object, and the second function __link_object() will initialize the object and insert it into object tree, use the kmemleak_lock to protect __link_object() only. Signed-off-by: Liu Shixin --- mm/kmemleak.c | 61 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 289b3be5ee6e..064fc3695c6b 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -623,25 +623,15 @@ static noinline depot_stack_handle_t set_track_prepare(void) return trace_handle; } -/* - * Create the metadata (struct kmemleak_object) corresponding to an allocated - * memory block and add it to the object_list and object_tree_root (or - * object_phys_tree_root). - */ -static void __create_object(unsigned long ptr, size_t size, - int min_count, gfp_t gfp, bool is_phys) +static struct kmemleak_object * __alloc_object(gfp_t gfp) { - unsigned long flags; - struct kmemleak_object *object, *parent; - struct rb_node **link, *rb_parent; - unsigned long untagged_ptr; - unsigned long untagged_objp; + struct kmemleak_object *object; object = mem_pool_alloc(gfp); if (!object) { pr_warn("Cannot allocate a kmemleak_object structure\n"); kmemleak_disable(); - return; + return NULL; } INIT_LIST_HEAD(&object->object_list); @@ -649,13 +639,8 @@ static void __create_object(unsigned long ptr, size_t size, INIT_HLIST_HEAD(&object->area_list); raw_spin_lock_init(&object->lock); atomic_set(&object->use_count, 1); - object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0); - object->pointer = ptr; - object->size = kfence_ksize((void *)ptr) ?: size; object->excess_ref = 0; - object->min_count = min_count; object->count = 0; /* white color initially */ - object->jiffies = jiffies; object->checksum = 0; object->del_state = 0; @@ -680,7 +665,23 @@ static void __create_object(unsigned long ptr, size_t size, /* kernel backtrace */ object->trace_handle = set_track_prepare(); - raw_spin_lock_irqsave(&kmemleak_lock, flags); + return object; +} + +static void __link_object(struct kmemleak_object *object, unsigned long ptr, + size_t size, int min_count, bool is_phys) +{ + + struct kmemleak_object *parent; + struct rb_node **link, *rb_parent; + unsigned long untagged_ptr; + unsigned long untagged_objp; + + object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0); + object->pointer = ptr; + object->size = kfence_ksize((void *)ptr) ?: size; + object->min_count = min_count; + object->jiffies = jiffies; untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); /* @@ -711,14 +712,32 @@ static void __create_object(unsigned long ptr, size_t size, */ dump_object_info(parent); kmem_cache_free(object_cache, object); - goto out; + return; } } rb_link_node(&object->rb_node, rb_parent, link); rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root : &object_tree_root); list_add_tail_rcu(&object->object_list, &object_list); -out: +} + +/* + * Create the metadata (struct kmemleak_object) corresponding to an allocated + * memory block and add it to the object_list and object_tree_root (or + * object_phys_tree_root). + */ +static void __create_object(unsigned long ptr, size_t size, + int min_count, gfp_t gfp, bool is_phys) +{ + struct kmemleak_object *object; + unsigned long flags; + + object = __alloc_object(gfp); + if (!object) + return; + + raw_spin_lock_irqsave(&kmemleak_lock, flags); + __link_object(object, ptr, size, min_count, is_phys); raw_spin_unlock_irqrestore(&kmemleak_lock, flags); }