From patchwork Thu Jan 19 04:01:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13107347 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 57DA3C00A5A for ; Thu, 19 Jan 2023 04:01:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFCC36B0073; Wed, 18 Jan 2023 23:01:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B85CE6B0074; Wed, 18 Jan 2023 23:01:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FFBC6B0075; Wed, 18 Jan 2023 23:01:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 883756B0073 for ; Wed, 18 Jan 2023 23:01:27 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4A70C1C1D5F for ; Thu, 19 Jan 2023 04:01:27 +0000 (UTC) X-FDA: 80370199014.27.1166E50 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id BFE3B4001B for ; Thu, 19 Jan 2023 04:01:25 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dQVaQqWM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674100885; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YFRn2G3kICVUTv0CVX/GON7X4LfKNre2jK8Ma2QSF+o=; b=5kLN4ifPHDALvLZs6Wy1SHDCBF5WL33CpmaEBqHLs/G5yQmlp2IxnW3PYLvKYQ44p6uCys AYwOxwMgR1VfmhVC0BUVrk6AN3JF8BkweHmHgdX3OABbqHrtfmnvtYpDnFhWaCw2F87m0h MpPBIRcmFrb8H8sHit+LUIDP7ze+VJ0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dQVaQqWM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674100885; a=rsa-sha256; cv=none; b=Zi7dNyhOAakfMjm8RiC6LC0QO7MT1dQmIokphlYQ/SkuwxYZos70TaLwB2p7blIHLhodix NIMQwP0F6JhWhOeCMXQTsTvYpsJEUEwk3tdt/Hr7Wkj/FkQe5i3FjXqkK3u4y64G/sIrW4 BwWmUcfd0CiNv3nO+V9UO3PYERq2GJ8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674100885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YFRn2G3kICVUTv0CVX/GON7X4LfKNre2jK8Ma2QSF+o=; b=dQVaQqWMplEk2zVszHKXq+Z4TKjHI8hHMwFslIeF/iCfVWMf3GlFwd+7tZx4336jPFKHzw Cq0fy7iXf/LzWjbds4Ku2WRiK6N0pgOmJBMNEpEV1VPKGx+dRnvoO0hf+rUVhJrq5N094x 35YH/i5fWPYPgE+gq4X11ihLHjubhB8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-584-1DQLsC-QPwm5vFLMtYRjPg-1; Wed, 18 Jan 2023 23:01:21 -0500 X-MC-Unique: 1DQLsC-QPwm5vFLMtYRjPg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0E2E53814950; Thu, 19 Jan 2023 04:01:21 +0000 (UTC) Received: from llong.com (unknown [10.22.8.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6CE551E5; Thu, 19 Jan 2023 04:01:20 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Waiman Long Subject: [RESEND PATCH v2 1/2] mm/kmemleak: Simplify kmemleak_cond_resched() usage Date: Wed, 18 Jan 2023 23:01:10 -0500 Message-Id: <20230119040111.350923-2-longman@redhat.com> In-Reply-To: <20230119040111.350923-1-longman@redhat.com> References: <20230119040111.350923-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: BFE3B4001B X-Stat-Signature: iw5ad5tfuxtxzw3hu7yuwi1np1fkr599 X-HE-Tag: 1674100885-471987 X-HE-Meta: U2FsdGVkX183PG6iTqO08dHYSiN5V2S+d4Z6k4pEvmkTnRIY3GgvhskRW3orsYB1sYUr0ecqepEXwqpRai6u/Ck2g9HcqTEAKXhyNRauMqT48oWic1YBnLzN3CsbTTkZjarmZLcOTz12f/dkHwfizKlGEY/iLovpp1DoZWkutLBCIRT+0J8mO9PQyxnj0aO36dAXgMuUxl6F6u7ilTSgpvcH15KBhy0nBUmj5U6JanM2IRQ9W4iflIDd/22oPr6cjtWnSHBXl0ktUc6S+4J5SsZR0CTdrFNPMmaLfytpBEs3PA7p5cH4F9XJo4isQ+FeDkOgGwQhBYzMiNEWOEgO3G9X36GNcOq59HJSJEzPovrZOnwXAc0S+gIk+OB/42+3k8g6EHy/oQBW2zouhfzj05G3cavyNTjTf59jxch4gM4jfbkMTC2ufFX85C143NYIokZJUYzmyOht4NQPRyrjN0PQMxYiKk93ZP9sih5PCPigMeCZHF524M+PdffSSIInnFADp/o5bZqBLEv7X9pXCNm2et2gQ7xW8bxlrz1gwy8YCoCnivYv6CNvHy8FjRMaQcjA7rRxdS8UgVhVfAHeVXyZRhx2CUorDTzwCHHqI46Ap9HzGfqMeEpG+xQxT/kREHvMffcpvO2Aw45w3x08g6qBkgdSJ+9TqHG6RZe9ym3EBilCD0K8BUwe+7bDcSnEaBqDPzgQfc5Z158/O9J2347c1InY57YwV4OWAO9YJISdZ/ZZrvKBiOeOJqDDW56fDhAh68NHpojn4Haug1dXBY59dnimku8nlfNuR2r0lTW67kVZBEMaIudw8NRSgjvfxpXehXUUwN6smY+ZTEnVAtEQZE+f9H7c45qjjxViAHw/SSor0YUwoEn3LyjjtTuGR/LlngDg3qqTL8ZNd04x6n6uIUCvLC9xYOXp99HKY05xJ2f/JB8/tdXvSTm1f9jScZ8DWaZ4HoA8slpsXbw O4A== 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: The presence of a pinned argument and the 64k loop count make kmemleak_cond_resched() a bit more complex to read. The pinned argument is used only by first kmemleak_scan() loop. Simplify the usage of kmemleak_cond_resched() by removing the pinned argument and always do a get_object()/put_object() sequence. In addition, the 64k loop is removed by using need_resched() to decide if kmemleak_cond_resched() should be called. Signed-off-by: Waiman Long Reviewed-by: Catalin Marinas --- mm/kmemleak.c | 48 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 267332904354..e7cb521236bf 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1463,22 +1463,17 @@ static void scan_gray_list(void) /* * Conditionally call resched() in an object iteration loop while making sure * that the given object won't go away without RCU read lock by performing a - * get_object() if !pinned. - * - * Return: false if can't do a cond_resched() due to get_object() failure - * true otherwise + * get_object() if necessaary. */ -static bool kmemleak_cond_resched(struct kmemleak_object *object, bool pinned) +static void kmemleak_cond_resched(struct kmemleak_object *object) { - if (!pinned && !get_object(object)) - return false; + if (!get_object(object)) + return; /* Try next object */ rcu_read_unlock(); cond_resched(); rcu_read_lock(); - if (!pinned) - put_object(object); - return true; + put_object(object); } /* @@ -1492,15 +1487,12 @@ static void kmemleak_scan(void) struct zone *zone; int __maybe_unused i; int new_leaks = 0; - int loop_cnt = 0; jiffies_last_scan = jiffies; /* prepare the kmemleak_object's */ rcu_read_lock(); list_for_each_entry_rcu(object, &object_list, object_list) { - bool obj_pinned = false; - raw_spin_lock_irq(&object->lock); #ifdef DEBUG /* @@ -1526,19 +1518,13 @@ static void kmemleak_scan(void) /* reset the reference count (whiten the object) */ object->count = 0; - if (color_gray(object) && get_object(object)) { + if (color_gray(object) && get_object(object)) list_add_tail(&object->gray_list, &gray_list); - obj_pinned = true; - } raw_spin_unlock_irq(&object->lock); - /* - * Do a cond_resched() every 64k objects to avoid soft lockup. - */ - if (!(++loop_cnt & 0xffff) && - !kmemleak_cond_resched(object, obj_pinned)) - loop_cnt--; /* Try again on next object */ + if (need_resched()) + kmemleak_cond_resched(object); } rcu_read_unlock(); @@ -1605,14 +1591,9 @@ static void kmemleak_scan(void) * scan and color them gray until the next scan. */ rcu_read_lock(); - loop_cnt = 0; list_for_each_entry_rcu(object, &object_list, object_list) { - /* - * Do a cond_resched() every 64k objects to avoid soft lockup. - */ - if (!(++loop_cnt & 0xffff) && - !kmemleak_cond_resched(object, false)) - loop_cnt--; /* Try again on next object */ + if (need_resched()) + kmemleak_cond_resched(object); /* * This is racy but we can save the overhead of lock/unlock @@ -1647,14 +1628,9 @@ static void kmemleak_scan(void) * Scanning result reporting. */ rcu_read_lock(); - loop_cnt = 0; list_for_each_entry_rcu(object, &object_list, object_list) { - /* - * Do a cond_resched() every 64k objects to avoid soft lockup. - */ - if (!(++loop_cnt & 0xffff) && - !kmemleak_cond_resched(object, false)) - loop_cnt--; /* Try again on next object */ + if (need_resched()) + kmemleak_cond_resched(object); /* * This is racy but we can save the overhead of lock/unlock From patchwork Thu Jan 19 04:01:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13107346 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 35DF4C38142 for ; Thu, 19 Jan 2023 04:01:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 769056B0072; Wed, 18 Jan 2023 23:01:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 718BD6B0073; Wed, 18 Jan 2023 23:01:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E0836B0074; Wed, 18 Jan 2023 23:01:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4F56B6B0072 for ; Wed, 18 Jan 2023 23:01:26 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 21D82AB221 for ; Thu, 19 Jan 2023 04:01:26 +0000 (UTC) X-FDA: 80370198972.06.1759FE5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 527FB40005 for ; Thu, 19 Jan 2023 04:01:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WssVKNz8; spf=pass (imf12.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674100884; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Eek5YPddR+rdvDYmWL3WCwvTt/kh6fJNelSHfFXusYg=; b=xjNzvJYGJtUTSlCOjA1JRqtfSbbxgsPss76LmOs7KXfn461oNm6BuaOiJsKVaeuy2NgIq3 sDenvRXy1ndT8F4Lc2bojoW4/Pz0uW2ZVC7GsMYooW53CxXlJ4g+GZ5+E9TS7kLMs/RlHw WdY+qnOVeFspLwyH81uQjvP03M7Vf/c= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WssVKNz8; spf=pass (imf12.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674100884; a=rsa-sha256; cv=none; b=reVHa9hjlFWr75w1Q9yuKEPIjKlEI6VgE1jFW2vhCtIfEll9HiaDlv0MR3ZUw/6CAZfIch 3C/rTbYM5/cpyy2dP8xkLfppPTDWvqZfEq6yAhUoBzMyfjKWF9kgq8gEtwffKA1vQzGsAc C+BEGwoPiDl2jD+Pu6DGZtQKHuJ8/yw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674100883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Eek5YPddR+rdvDYmWL3WCwvTt/kh6fJNelSHfFXusYg=; b=WssVKNz8gmd16G6s+XUoT2PmCw5rr7VzwPhUOJEJFUh7I7q7DzFUZ2fQlqzL5mhpkBlBZ8 9ONpzMgxyjFlzjBsoqST50L2wtmo69N7prymQ/nToYmpgBdti4efq56rxYnOML3f+XNB+5 AV8kFxl/kNN5eVUi5tTq8cPxDyQeS60= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-550-JseQALo8MkyN83fBd0phlQ-1; Wed, 18 Jan 2023 23:01:21 -0500 X-MC-Unique: JseQALo8MkyN83fBd0phlQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 657E52A5956F; Thu, 19 Jan 2023 04:01:21 +0000 (UTC) Received: from llong.com (unknown [10.22.8.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C65751E5; Thu, 19 Jan 2023 04:01:21 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Waiman Long Subject: [RESEND PATCH v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Date: Wed, 18 Jan 2023 23:01:11 -0500 Message-Id: <20230119040111.350923-3-longman@redhat.com> In-Reply-To: <20230119040111.350923-1-longman@redhat.com> References: <20230119040111.350923-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 527FB40005 X-Stat-Signature: 111e5pk9hruykffjqdctrm3k9swd59fo X-Rspam-User: X-HE-Tag: 1674100884-159746 X-HE-Meta: U2FsdGVkX19wF68UHzadCRVN2TtaO8HZ1q3pv0BH70DnxU5KNocau2ADVl0zgXlZECnFINooVJBuUUb55nfi+/JWC1EmAdKzQn1zbHAMJFs/JukYgliHueqHD9DPTtEMI4ozkr6JYxP5W4/wjnGfzRRCaj3BGZ3KTUf4Aaz78ZN8hRdBWzKCIqs97CYhkID4ezZcxPF2bQUIgvECHWJUFepS2MQSR3CHdaH6c7gU/dArXKVRlp4czTjmy/pvYmXEJpy4IpBNrmyNJvdQ4mBCdk/fXqEMgE8iZ+mNfz3QZ4U+HAUFcDIs+g5LpMpnSq+fTznUiVSp896xb2R8tVCXUMuETIyyFvWLQNb3qf4ZQV7p7aERiWJvnL5aXX+VNDAVBYN4aUeCPJGsk3UENxzqOmXU7SKIRr30MFOTwptjkIsvPqgE7AgmMEylHRcP+xdd5/0R7aH1Yxb65Y5JfdlT88dw1ujINwN8oKcQ1VtskikIZUEOju6SiYeOMFD3TqdLV5hWatr7iYVZUXAcGqHtMVl8KZnGTpBbVbgCqEe81sna2M3KqjafcUjD5BTs6DpZxv1iEAO5D8FxlF3hWy7Fj6XiMg9deHTjEsoRfvpW229AH30Y+EdQEexnSHFoV83q26e+0Ky6aodoyVv2Gz08yegzwEELur+xwJOX5S+g5b+9Ko6L35Q/Vnx+gaNnJ84KLBgdpY/sMLHokBlGXGqXij/7yD8GWFWzHAew+6m3L0J87PFWQuLIo84K59TG3Sls300qrY6RTp5wmGy+JMMU/Aq8/m6RbjSKHohK62W0iRh4d4eK/cdG+cOi6ERYXWSGZRAiJ3hjHz0tD3LjlNir9+sO6MccbGr+bQWi1p42kxtWXdfe8n434heJeLX8AQZQYx18KmIzQzOHXeMMD7QF0EfNx78koil7PGeAlU5Kr1oFcYJRyPQrhG/khJOvJ+1JJ92NyTuzHhI= 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: Commit 6edda04ccc7c ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") fixes soft lockup problem in kmemleak_scan() by periodically doing a cond_resched(). It does take a reference of the current object before doing it. Unfortunately, if the object has been deleted from the object_list, the next object pointed to by its next pointer may no longer be valid after coming back from cond_resched(). This can result in use-after-free and other nasty problem. Fix this problem by adding a del_state flag into kmemleak_object structure to synchronize the object deletion process between kmemleak_cond_resched() and __remove_object() to make sure that the object remained in the object_list in the duration of the cond_resched() call. Fixes: 6edda04ccc7c ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") Signed-off-by: Waiman Long Reviewed-by: Catalin Marinas --- mm/kmemleak.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index e7cb521236bf..0ece170fc9ef 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -13,11 +13,12 @@ * * The following locks and mutexes are used by kmemleak: * - * - kmemleak_lock (raw_spinlock_t): protects the object_list modifications and - * accesses to the object_tree_root (or object_phys_tree_root). The - * object_list is the main list holding the metadata (struct kmemleak_object) - * for the allocated memory blocks. The object_tree_root and object_phys_tree_root - * are red black trees used to look-up metadata based on a pointer to the + * - kmemleak_lock (raw_spinlock_t): protects the object_list as well as + * del_state modifications and accesses to the object_tree_root (or + * object_phys_tree_root). The object_list is the main list holding the + * metadata (struct kmemleak_object) for the allocated memory blocks. + * The object_tree_root and object_phys_tree_root are red + * black trees used to look-up metadata based on a pointer to the * corresponding memory block. The object_phys_tree_root is for objects * allocated with physical address. The kmemleak_object structures are * added to the object_list and object_tree_root (or object_phys_tree_root) @@ -147,6 +148,7 @@ struct kmemleak_object { struct rcu_head rcu; /* object_list lockless traversal */ /* object usage count; object freed when use_count == 0 */ atomic_t use_count; + unsigned int del_state; /* deletion state */ unsigned long pointer; size_t size; /* pass surplus references to this pointer */ @@ -177,6 +179,11 @@ struct kmemleak_object { /* flag set for object allocated with physical address */ #define OBJECT_PHYS (1 << 4) +/* set when __remove_object() called */ +#define DELSTATE_REMOVED (1 << 0) +/* set to temporarily prevent deletion from object_list */ +#define DELSTATE_NO_DELETE (1 << 1) + #define HEX_PREFIX " " /* number of bytes to print per line; must be 16 or 32 */ #define HEX_ROW_SIZE 16 @@ -567,7 +574,9 @@ static void __remove_object(struct kmemleak_object *object) rb_erase(&object->rb_node, object->flags & OBJECT_PHYS ? &object_phys_tree_root : &object_tree_root); - list_del_rcu(&object->object_list); + if (!(object->del_state & DELSTATE_NO_DELETE)) + list_del_rcu(&object->object_list); + object->del_state |= DELSTATE_REMOVED; } /* @@ -633,6 +642,7 @@ static void __create_object(unsigned long ptr, size_t size, object->count = 0; /* white color initially */ object->jiffies = jiffies; object->checksum = 0; + object->del_state = 0; /* task information */ if (in_hardirq()) { @@ -1470,9 +1480,22 @@ static void kmemleak_cond_resched(struct kmemleak_object *object) if (!get_object(object)) return; /* Try next object */ + raw_spin_lock_irq(&kmemleak_lock); + if (object->del_state & DELSTATE_REMOVED) + goto unlock_put; /* Object removed */ + object->del_state |= DELSTATE_NO_DELETE; + raw_spin_unlock_irq(&kmemleak_lock); + rcu_read_unlock(); cond_resched(); rcu_read_lock(); + + raw_spin_lock_irq(&kmemleak_lock); + if (object->del_state & DELSTATE_REMOVED) + list_del_rcu(&object->object_list); + object->del_state &= ~DELSTATE_NO_DELETE; +unlock_put: + raw_spin_unlock_irq(&kmemleak_lock); put_object(object); }