From mboxrd@z Thu Jan 1 00:00:00 1970 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 94159C4167B for ; Mon, 27 Nov 2023 19:42:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 175DE6B02E2; Mon, 27 Nov 2023 14:42:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 125986B02E5; Mon, 27 Nov 2023 14:42:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 013B26B02E9; Mon, 27 Nov 2023 14:42:09 -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 E74F66B02E2 for ; Mon, 27 Nov 2023 14:42:09 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C12E0806E8 for ; Mon, 27 Nov 2023 19:42:09 +0000 (UTC) X-FDA: 81504755178.14.CF0A10A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 0A9C8140013 for ; Mon, 27 Nov 2023 19:42:07 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UzPCXUuj; spf=pass (imf09.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=1701114128; 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:references:dkim-signature; bh=MFZzVrvyAJtc2SlqHRdiv4fw2bU3px82Ljm8ZqJTVGo=; b=or3ym09uK0JAXZxW7NZ0BvyXHVCfSPCf/s27+r1bcxqTenSSWdPQKYcIFc88ENkjoQ9nPF TAWc4kxGCKpsDtNo5wjTP15KFp3C5n/m16H8gUsfEjtvxmo5EmjmeWOPYhPYqqRAxXxHFR TJiKDLr0DgpNgMZvtRqXwUcOSBkzCoY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UzPCXUuj; spf=pass (imf09.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=1701114128; a=rsa-sha256; cv=none; b=w23wA+93eI0RUF/Gdg1nHoflkwNvs0BzpsC7HQucQzqw/ScutwalEKYoJdbIrwi5QBwgnv /PksIEyk1g0TwD6RDgxq8VY5c99fwIkB2o+v6m4uLb7dJfNC8WGHp93HryDztsu5P62uoX HcVKBTOmhmKLdBX5I4Tihn56AfWUS8U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701114127; 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; bh=MFZzVrvyAJtc2SlqHRdiv4fw2bU3px82Ljm8ZqJTVGo=; b=UzPCXUujw9aJPDWZbuLmVRgPJrX5VwN3OO7TXX0QwubnYUDGDDZ81A2DAnv8rJZM9Gkkxm +5e9PYdv9/y7KpTiM2KrCcKetFTr7e4h9m40+bK1jdATSQEShPnP5WjeyerEZotd/wSIfL 2g8TjZ9NNSmwfDJob0Zr5swuLNmKwxY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-613-ja_pP7llNLy2rv8ntPW8Bg-1; Mon, 27 Nov 2023 14:42:05 -0500 X-MC-Unique: ja_pP7llNLy2rv8ntPW8Bg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 26B0A28040B7; Mon, 27 Nov 2023 19:42:05 +0000 (UTC) Received: from llong.com (unknown [10.22.8.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2A3DC15881; Mon, 27 Nov 2023 19:42:04 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Waiman Long Subject: [PATCH] mm/kmemleak: Add cond_resched() to kmemleak_free_percpu() Date: Mon, 27 Nov 2023 14:41:53 -0500 Message-Id: <20231127194153.289626-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Rspamd-Queue-Id: 0A9C8140013 X-Rspam-User: X-Stat-Signature: 36m9rhwgsakj73axf7zrpifft4pyfomf X-Rspamd-Server: rspam01 X-HE-Tag: 1701114127-967072 X-HE-Meta: U2FsdGVkX1+z/6K5gu366izC5wnjjtmBYx3CDojPzf7lO+TTIG1SP8vFUlhT/y9PJ+Gnlsz414nSSHSZGsjG3Fh014qmbyerN8zy8Ae8FjLb//HmAHsXYxIm0ynXIDn2mlJy0LLwSnyK8nlHvxmc6n1QhIvKM9yaftlwft8mZiu5xE0/uvbbIjOCxbvsJ7uff4W1+qVQkdkgQ+NYi31In0d4zhUYjWlf8jGD2J708Te5MBasxtrGucwSJmBa1aigWUoGCTRXtQ4jH7dVIdOHyLxgt667RdG7aIyrqODuj3+nup4rBQIIsWHbq696fYUPsIPs0TAa3owMpd/GYjfOPL9bHy5pSYpHciUmGalIu5Y6qj62yH4+2y4YgbppeCJiix2ABzNa9LuWgSk4uhwbk/Y+kJcYMeqdFlgkld+4ycViL056Xs3Us6QkJkel8dMBSKlgoO8tP45tbLOw/PZfaiPvBvlQ+s43IC/uCvMj3101z/bQclZ6ojtJvn3ZgCmTlkHQG6Gcxa+2VziNWxeyuwOZBfRr/En+Az7bctg5qLMd33kh1vimhzSA31h++sOyWHANPjTUqZ311piQvSlfH5bUfh8tFzGK/NsMNU48Dt+rdY5V5tKv7VxxudvE8lbg78LkC91UuCCjlCCukVttshI4sPksSFe6d3ipJ856nurs3UhQTji39c8paj41g4QpbSG2MZWisM2WjRD5w2MrzHILskNFYCnd1ep+jQ+w1PiFIyomHg3mu/cymJxRFZOj2e4o/76bjtwGLwz1RQYRNTQPudFSWtLSx2X1fj49DqUMrPZ/0WvInHGdmOMFoYpVAxlKMgooH4qBLEAI//BLSZwW7qwM0GIN30YZlRLOIKq9v4wzE6Xgjj8iQjo5GXb1g+UyWh+gvVxKP1/pm0QqQ/tWnVCDn+NQE5WjfwGloEbba38Q7K3fLHWibphZ6EZazJzFJAsnSa6HlVKeIE5 Y7DsXwLH 2gwpd3/FfA9iSxA/r9w4BcVjG9g/m8U1yqJIx6QQ1dcM9Wloo2S+9Vi1QvcQrJC4Zd3HrDLa9qhJcCvUQI3chcouTi82RSWy7ISprOwk9lfEr2yT0se4zJ4SvoiuP0V7N2YoMEMGSAR0vMh0bUriAADJhKXZ2r31qxNsxXRX+kZfs9RTRgC7jbEpDiuvBEHAxROmtzcLamn+dGCjTJJVJUf1/NA== 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: List-Subscribe: List-Unsubscribe: It was found that on systems with large number of CPUs, the following soft lockup splat might sometimes happen: [ 2656.001617] watchdog: BUG: soft lockup - CPU#364 stuck for 21s! [ksoftirqd/364:2206] : [ 2656.141194] RIP: 0010:_raw_spin_unlock_irqrestore+0x3d/0x70 : 2656.241214] Call Trace: [ 2656.243971] [ 2656.246237] ? show_trace_log_lvl+0x1c4/0x2df [ 2656.251152] ? show_trace_log_lvl+0x1c4/0x2df [ 2656.256066] ? kmemleak_free_percpu+0x11f/0x1f0 [ 2656.261173] ? watchdog_timer_fn+0x379/0x470 [ 2656.265984] ? __pfx_watchdog_timer_fn+0x10/0x10 [ 2656.271179] ? __hrtimer_run_queues+0x5f3/0xd00 [ 2656.276283] ? __pfx___hrtimer_run_queues+0x10/0x10 [ 2656.281783] ? ktime_get_update_offsets_now+0x95/0x2c0 [ 2656.287573] ? ktime_get_update_offsets_now+0xdd/0x2c0 [ 2656.293380] ? hrtimer_interrupt+0x2e9/0x780 [ 2656.298221] ? __sysvec_apic_timer_interrupt+0x184/0x640 [ 2656.304211] ? sysvec_apic_timer_interrupt+0x8e/0xc0 [ 2656.309807] [ 2656.312169] [ 2656.326110] kmemleak_free_percpu+0x11f/0x1f0 [ 2656.331015] free_percpu.part.0+0x1b/0xe70 [ 2656.335635] free_vfsmnt+0xb9/0x100 [ 2656.339567] rcu_do_batch+0x3c8/0xe30 [ 2656.363693] rcu_core+0x3de/0x5a0 [ 2656.367433] __do_softirq+0x2d0/0x9a8 [ 2656.381119] run_ksoftirqd+0x36/0x60 [ 2656.385145] smpboot_thread_fn+0x556/0x910 [ 2656.394971] kthread+0x2a4/0x350 [ 2656.402826] ret_from_fork+0x29/0x50 [ 2656.406861] Fix this by adding a cond_resched() call in the percpu freeing loop and defer the freeing of percpu kmemleak objects to a workqueue if it is being called from a non-task context. Signed-off-by: Waiman Long --- mm/kmemleak.c | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 1eacca03bedd..03385f4a8008 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -168,6 +168,14 @@ struct kmemleak_object { char comm[TASK_COMM_LEN]; /* executable name */ }; +/* + * A percpu address to be submitted to a workqueue for being freed. + */ +struct kmemleak_percpu_addr { + struct work_struct work; + const void __percpu *ptr; +}; + /* flag representing the memory block allocation status */ #define OBJECT_ALLOCATED (1 << 0) /* flag set after the first reporting of an unreference object */ @@ -1120,23 +1128,60 @@ void __ref kmemleak_free_part(const void *ptr, size_t size) } EXPORT_SYMBOL_GPL(kmemleak_free_part); +static void __kmemleak_free_percpu(const void __percpu *ptr) +{ + unsigned int cpu; + + for_each_possible_cpu(cpu) { + delete_object_full((unsigned long)per_cpu_ptr(ptr, cpu)); + if (in_task()) + cond_resched(); + } +} + +/* + * Work function for deferred freeing of kmemleak objects associated with + * a freed percpu memory block. + */ +static void kmemleak_free_percpu_workfn(struct work_struct *work) +{ + struct kmemleak_percpu_addr *addr; + + addr = container_of(work, struct kmemleak_percpu_addr, work); + __kmemleak_free_percpu(addr->ptr); + kfree(addr); +} + /** * kmemleak_free_percpu - unregister a previously registered __percpu object * @ptr: __percpu pointer to beginning of the object * * This function is called from the kernel percpu allocator when an object - * (memory block) is freed (free_percpu). + * (memory block) is freed (free_percpu). Since this function is inherently + * slow especially on systems with a large number of CPUs, defer the actual + * removal of kmemleak objects associated with the percpu pointer to a + * workqueue if it is not in a task context. */ void __ref kmemleak_free_percpu(const void __percpu *ptr) { - unsigned int cpu; - pr_debug("%s(0x%px)\n", __func__, ptr); - if (kmemleak_free_enabled && ptr && !IS_ERR(ptr)) - for_each_possible_cpu(cpu) - delete_object_full((unsigned long)per_cpu_ptr(ptr, - cpu)); + if (!kmemleak_free_enabled || !ptr || IS_ERR(ptr)) + return; + + if (!in_task()) { + struct kmemleak_percpu_addr *addr; + + addr = kzalloc(sizeof(*addr), GFP_ATOMIC); + if (addr) { + INIT_WORK(&addr->work, kmemleak_free_percpu_workfn); + addr->ptr = ptr; + queue_work(system_long_wq, &addr->work); + return; + } + /* Fallback to do direct deletion */ + } + __kmemleak_free_percpu(ptr); } EXPORT_SYMBOL_GPL(kmemleak_free_percpu); -- 2.39.3