From patchwork Thu Dec 15 16:02:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13074312 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 11B08C2D0CC for ; Thu, 15 Dec 2022 16:03:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 899228E0002; Thu, 15 Dec 2022 11:03:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8150B8E0006; Thu, 15 Dec 2022 11:03:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5536D8E0003; Thu, 15 Dec 2022 11:03:19 -0500 (EST) 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 3C6CB8E0002 for ; Thu, 15 Dec 2022 11:03:19 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DD6F180FB0 for ; Thu, 15 Dec 2022 16:03:18 +0000 (UTC) X-FDA: 80245010076.13.B254EF4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id F09C1180055 for ; Thu, 15 Dec 2022 16:03:13 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WMTDqiuW; spf=pass (imf06.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1671120196; a=rsa-sha256; cv=none; b=6EH1WNITJS4/U+bIkv0eJp/g0tl8q24FZNbGr2IoF6qCMuDMxfDiuLqyVBLXtIedAWnOAX svxX2tpolwsihjOH52yIpfwvmBrjR7BWHMceM3S8LwhyfG3fKyVFtgvzwrl7uskMuV4A8m QEXg5B7KbCqWuCDA1mc8lMESnlAsbTM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WMTDqiuW; spf=pass (imf06.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1671120196; 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=M5QTAurX6ikbn8SjRcPs6LkM71sg8or7RhwzUgFKYEGYTuI5XXPSp15YBgIo1D7HCDglha qslDvNj/tVNvFCquRi8npG+YAgyvXYVSTqlINRwF05l5iNVW7vgnyMnQjXVByZIRHP16S0 /UqaFtiwi56L8V0OBuoILffmUhrDBFk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671120193; 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=WMTDqiuWmKCTPoNxzpWhsVYxai7Ffd09BRzmYKqL18fg0OfrwRh/J7Kz7j6Z7A8s6I1CNl rP7+EOQGtQFV40nmQ6zpQNL3FcIs6Q4aNuxbTHZtYiOJiYKNZBSyBbODonCJcWrDZi8R4l 4RtDRCep4uDBGHYlrHK0TadgUknbC0o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-453-b2qMVZfhMqyIfPZeJDNE2g-1; Thu, 15 Dec 2022 11:03:09 -0500 X-MC-Unique: b2qMVZfhMqyIfPZeJDNE2g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E7EF8100F901; Thu, 15 Dec 2022 16:03:08 +0000 (UTC) Received: from llong.com (unknown [10.22.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id A4BBE4085720; Thu, 15 Dec 2022 16:03:08 +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: [PATCH v2 1/2] mm/kmemleak: Simplify kmemleak_cond_resched() usage Date: Thu, 15 Dec 2022 11:02:58 -0500 Message-Id: <20221215160259.261136-2-longman@redhat.com> In-Reply-To: <20221215160259.261136-1-longman@redhat.com> References: <20221215160259.261136-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Queue-Id: F09C1180055 X-Rspamd-Server: rspam01 X-Stat-Signature: 7a3epggj4k85ca9nm7dc3typafmyi8s9 X-HE-Tag: 1671120193-109946 X-HE-Meta: U2FsdGVkX19Ivyrp03AyFsJ21c7qqwADSOCBkMSvL6ELOvHFJz9FOorjs9ApYxQAiWZ2cPfjgrQXxSSxL5/qPut8VRFH+E4zOON5LCflp2GyRbVSBgZOe1TxEVqR6bzE3f70IHgsllnmBI/5ueM03Fk7yxCli65ntib/tPeSV9CK51W5gmzhlJab+6Y/3pOBwMHRRgmB7pCApFuKr7GRDCf71irgEygUugyNMEHGiISKWg1318r80E/Y8/xdWBoSI/MAa07LgqwrideAfHvsNHDTDkd7bmYNz0f2wuXKYW7xc8fhwGmfYGBMvpCMAKoNULRYTJr8WvPvSY17mo+jr/frX5N7VY2BTd5iMc3WsaCPWySfu4gFW9M1XE7cKUZmd6LpJwBqcuiA/EeSWV2GCOrfk3ljeemjZ/VkgiBrdwIUOHRBzJMJK7tx0baVKAc5H0uoQgUrFNbLIJcdFufFj4DExl+QWK02blrDrX57qejp22Gw36eZtp3G6I1rLQpW2ajkyzxhp6C1rDeUKUERkmf4YiVtR+BIstxcCO1hoGbgWSzs4wfdQV1sDxdFP4yL+GnYvpzgks8+sPaZWsaxGCMY7gMmgdtxmtBdQjgIz9QD9JCeZ45SQ86C7w/skWkC6EAnFmgq31qeD2jJ2AI9cayYZaGcMXtKMEWmWM3l91iGu2RlJGsJxTfKOXQ+1IDgQKNloccgkOdxPmlSGAYgJ3wR7FsO8UXH8QdwiMhEjhvlCIkKs5TzRNyhjk4ryrJjMbA4nQwEcvdBNqhDBMN9LPAope2FMgKK4N/ozKzOF58BsYicSzf2DpJ4wknJlD3CayvYYdjLkycTgVr4MwUtB8z9NI7UStnekFYa1ZssX9WCtBRSe1NfJRZWvtPBVi0DUyiKS8CVwMes7nZkGwoJp59MctKHAdV/HEJZu1H5t9OWtszynfcbofJShcpzQKgmAHhJgRmCZMg+CKnWGNv Uvw== 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 Dec 15 16:02:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13074313 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 7836CC4332F for ; Thu, 15 Dec 2022 16:03:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30CC98E0006; Thu, 15 Dec 2022 11:03:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BB568E0003; Thu, 15 Dec 2022 11:03:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A9AD8E0006; Thu, 15 Dec 2022 11:03:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id F40DD8E0003 for ; Thu, 15 Dec 2022 11:03:20 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CACB21A0FFC for ; Thu, 15 Dec 2022 16:03:20 +0000 (UTC) X-FDA: 80245010160.17.752ADF1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id DB99C14003E for ; Thu, 15 Dec 2022 16:03:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GXWX9rFK; spf=pass (imf26.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=1671120196; 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=hU8XPnEOQIrXJm1f8LjuXWxGasqPI3FiV6KWdOox3kBmklV24aXA0Ywcj40+EicWsQ6RlW VxRBWIcfRof/lM7Un+gMcrw91y98IyZMnDomu2Tj2kX5SVtZIVFcZULohOsVcNu84Yrx7D x7E714V6j2gXbc8x6wzLrsHgWiNQ1as= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GXWX9rFK; spf=pass (imf26.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=1671120196; a=rsa-sha256; cv=none; b=57GM9ggP/ObH7IrVLJNaT/CH6W6KqMWTJYfGhiyiJ9ooECu0Do2ZUPu/EdDho96ToUjnnQ A2CdY5/bnoCX/phs++AE8Tm7odnb8wvDFwGPSgikrCI+E5LfK5260ai6aTE0FWl4sIJhaV I6uhzeRaKu1+z+aAeos1yWSiQYtsbns= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671120195; 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=GXWX9rFKJx+pkcgQjak20S8RHHN/1Ooe4v4S+NHGJnAKeGdfpZvZ2ifAz35h/hR0QrPxBr r3Ef0mLuymf3O4NM96On/NVUv75hYDoHnMMn0pxBp34smR4Dem6hjRm+4knkgAXFSe1z6L 4q/igSlYdYWo96hAf+uVtkJXLw71xuU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-74--JlN4jZHNsufKEb23_Ljsg-1; Thu, 15 Dec 2022 11:03:09 -0500 X-MC-Unique: -JlN4jZHNsufKEb23_Ljsg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 459BA857F82; Thu, 15 Dec 2022 16:03:09 +0000 (UTC) Received: from llong.com (unknown [10.22.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0123C4085720; Thu, 15 Dec 2022 16:03:08 +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: [PATCH v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Date: Thu, 15 Dec 2022 11:02:59 -0500 Message-Id: <20221215160259.261136-3-longman@redhat.com> In-Reply-To: <20221215160259.261136-1-longman@redhat.com> References: <20221215160259.261136-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DB99C14003E X-Stat-Signature: sexhrbb4okp665hbjn969zh5cfmyjh5b X-HE-Tag: 1671120195-774361 X-HE-Meta: U2FsdGVkX1/JVSNxvVdoSBHw1dS/RD2ekZw9xHEefZ1lAM0LGtVhDHU50Eg748j4r8LNnWpLb4RL+nHeDSc1ozexVvg6fQF0r0WjUfaMds8/fdp/FaS84R03zI6kUEyW+NIKEh9tB9PZZGOwOMOT3G+kpVMRnvEoqJVL+ErYfKuRFP5GJg1VfgBgKnM4Ohzx1nJ83XNIH7X/1/QYPxWFID/HKZptkNsgIaEva0pK6kcIYi0JuUMk/Us3o1T8BJGENyJRcSOa08oEb8/3tYgDd/tI9cTYDH6wDls5Nl2XDH8c8o9bD08c6msfG1e4T1eY8QQcVxwJ1GFOmQD4wueze/LA8NxTVpjPjKe0b31wb2fVdJg/u/4nCjo/VBpST/mZyeUqqPOm8NOmgXGeGX1oz1chXNU6aS3EHNoiEdcvgre/jjEFStCwezjUkPgvAI6IlHROjtpa8qemkr2UNhoYoOKfREy+kXLp5AXLqdGTtOJQ8zE3SOg5ruHFTmLKj0RNCrZ0J+w0hBXV7MSwbDdW3pJMZ2GEXad1pzh1K0YNvtc77rh6rAyqEwyhW8hYP9NhqDQprReKHAb2dGLoaf1ppldmmbAJuXaib1RS/waDbnJDCfBxql8bnbpuuN23qhVpqqajKo1RLqme4bhkdvgNUgSSnqsG/o+7yUp91KJd3G8qdRdjNg6i4EgvDje59eOo3SLfAvSHQHwRdnz1Z3XEfYH/RSG9UyoaMaX4ud3rDF6qIGyXL5WekiyHIoYKtdNWmIzJ38C7q7BcS7sXXyIbHNqPGXRIz/TOL4DUWFXCaxCO7Bhp9I3H/JQFtm+kKV1QmUu6mtHHDdVH+V2QheeDUfD47oL/XbSudovKyi+ba7DbE6hwB6IEWXF2Sbc0GZvLK25Botyr2NaXPERyeyufVfTxEuRak8vWD8oDsAokWG/Z/GebxUfCKAmS8C1IxVOA44c0W52j+zQ= 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 --- 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); }