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 E9785C48BF6 for ; Thu, 29 Feb 2024 15:55:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D77E6B0095; Thu, 29 Feb 2024 10:55:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 587806B0096; Thu, 29 Feb 2024 10:55:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44F186B0099; Thu, 29 Feb 2024 10:55:47 -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 366966B0095 for ; Thu, 29 Feb 2024 10:55:47 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F40EC4036A for ; Thu, 29 Feb 2024 15:55:46 +0000 (UTC) X-FDA: 81845291892.04.9ED5BE3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf18.hostedemail.com (Postfix) with ESMTP id AD86A1C0016 for ; Thu, 29 Feb 2024 15:55:43 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QItl6eU9; spf=pass (imf18.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=1709222143; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DRt7MLjxEJ9+seZvRPDe7++aJyRHtDVk+O+fjsvjrnk=; b=I4/jZYl39J0MfnprF1ZY5lIPy4hrmdNSrBrkAHQkqc1BlF2jn4FBwW85x8+L1t2iCW1X2n aXfjJlkFNn08Vt+3rzTNQyJdu1ZyAomtfxosZXdS8X0m3te4m9xtyCXRcPH9her8Zt0lKJ Uv9gpzGfKVeLt5+xFkST7QKuPMGitUA= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QItl6eU9; spf=pass (imf18.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=1709222143; a=rsa-sha256; cv=none; b=l9Y3EkWOpxaAQmlbGJ+a3gm8C39TmtPTa33FS2BUFKNS8dplSPyYA9yP7R1klqQbNKx/nY x8p+nvvInkP5CMU44gZ//o+XuOi1wAJ/3IH+954nUMPj6toUVcbgShpXDYdLPTrkAIUVVj 7bv73ttkp/sj64pB8ehNWheYfj0wPfE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709222142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DRt7MLjxEJ9+seZvRPDe7++aJyRHtDVk+O+fjsvjrnk=; b=QItl6eU9f9nMcjxPlquq0EyTPzaWp3yLeRuyRvnwQLrSZ5Ps0iUu9/IXERVP8reC4dVQDD s1BXwpT95geHOB8W3ZH8nPS5uo4QJlez5HlI1oC1aq/gjXeylrGopeoj28rAtHah5yYa+c ZMoyJq3MKuXlro4nC+rpecHIzcG7NXM= 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-361-QfvGaCbGOI-E_7QxclcMOg-1; Thu, 29 Feb 2024 10:55:39 -0500 X-MC-Unique: QfvGaCbGOI-E_7QxclcMOg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 11E913C025BB; Thu, 29 Feb 2024 15:55:39 +0000 (UTC) Received: from [10.22.8.117] (unknown [10.22.8.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id ADC5E492BFA; Thu, 29 Feb 2024 15:55:38 +0000 (UTC) Content-Type: multipart/alternative; boundary="------------BE1SzJBSp0pGsxHJVPP3BOnW" Message-ID: Date: Thu, 29 Feb 2024 10:55:38 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm/kmemleak: Don't hold kmemleak_lock when calling printk() Content-Language: en-US To: Catalin Marinas Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Audra Mitchell References: <20240228191444.481048-1-longman@redhat.com> From: Waiman Long In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Rspamd-Queue-Id: AD86A1C0016 X-Rspam-User: X-Stat-Signature: arewyb6qmca3qufawrbt6yf8mmuw1md8 X-Rspamd-Server: rspam01 X-HE-Tag: 1709222143-43496 X-HE-Meta: U2FsdGVkX18ESkM0gffK8g076W0suvfHAEhyeAeZm9GBX1A2kQ2Hpp3XLhSW1ZxXaX3Q4dCtoZe1TCg9Do2P81w+A/kEQcZ0BRUTRsseWafUYH+AAnH2gIUVoTE5yB5Ag/C1FufhKSA4/sEgvB3MwT+iEawa2Pf0ZiGENxvi0PUODkQQepU5wjkbY5H3oCMBPA7b1yR+xn91BC8EqdCEizs4QtK1+/7PEKZi3iStJBvGanQbyCdpDRxP/oN1nPlgJv5o1WyA2tFuwwYEikHq+SNlhE7YoMH6oLXiy28+KtAJxHpF35WP/XaMMtiz5WOZUeznhribZM44K9xQtTo9UME1IluhC7HCFU4pJYZHXh7RTdyAMrCfYQRGyGYWXqIC3Mm8b9Xu1ONCArUnXk0SoPG3Z4qpyzcuwYo0jNcKN6eDWSr7LmKpII+lvPFwJ3RqsIWxMqmB96Bc0C8VCC515PeA/Mdz/4v4182/SWi7Q2ZiUSGZLx55Wuq5yKN8s3BtOdBPeRwSfwie4MravRC+IYGBxo01kTVsM8vmdAoNH86bYeatwyzSH27ey/IqCT6Q9dNx7mz79dcyxyRfxCJ/N9bvhb+P4cDQFfxFToxyFuLksd/+fQ9nnwXTEOOvuPEIbpTB60efCR6tuschhuF8smcv7r7lV2f4T4fOWbTHTZVjUml4c8hBqzMY+em6VOnU/U+O4PZCDt5Ba3xid23rcHS5Q3VEf9DsDzXUggHMRtAN8HB2tiB438ww+1+xq8clzXB211GzHLnwh5RzPhaWBA51o82wsr/4UaQUTADoQiElnwLynbbMMBCvtCF03DlEZArkDYLZkE7N1SR0/ZAAhYTG3bR23a21eKpVX56VlqB1+HZvqut5JlrKP0lw7qupLD8IY5y1aby67vGVsXbdR1IkY6ISMy4fGU4VhM430A3w5td19snohx3iKlQ5CkBWI3z9rtH/c7DukcHkafn 8pfsCwin PDZijNKz9E9AztUY= 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: This is a multi-part message in MIME format. --------------BE1SzJBSp0pGsxHJVPP3BOnW Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/29/24 10:25, Catalin Marinas wrote: > On Wed, Feb 28, 2024 at 02:14:44PM -0500, Waiman Long wrote: >> When some error conditions happen (like OOM), some kmemleak functions >> call printk() to dump out some useful debugging information while holding >> the kmemleak_lock. This may cause deadlock as the printk() function >> may need to allocate additional memory leading to a create_object() >> call acquiring kmemleak_lock again. >> >> Fix this deadlock issue by making sure that printk() is only called >> after releasing the kmemleak_lock. > I can't say I'm familiar with the printk() code but I always thought it > uses some ring buffers as it can be called from all kind of contexts and > allocation is not guaranteed. > > If printk() ends up taking kmemleak_lock through the slab allocator, I > wonder whether we have bigger problems. The lock order is always > kmemleak_lock -> object->lock but if printk() triggers a callback into > kmemleak, we can also get object->lock -> kmemleak_lock ordering, so > another potential deadlock. object->lock is per object whereas kmemleak_lock is global. When taking object->lock and doing a data dump leading to a call that takes the kmemlock, it is highly unlikely the it will need to take that particular object->lock again. I do agree that lockdep may still warn about it if that happens as all the object->lock's are likely to be treated to be in the same class. I should probably clarify in the change log that the lockdep splat is actually, [ 3991.452558] Chain exists of: [ 3991.452559] console_owner -> &port->lock --> kmemleak_lock So if kmemleak calls printk() acquiring either console_owner or port->lock. It may cause deadlock. Cheers, Longman --------------BE1SzJBSp0pGsxHJVPP3BOnW Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit


On 2/29/24 10:25, Catalin Marinas wrote:
On Wed, Feb 28, 2024 at 02:14:44PM -0500, Waiman Long wrote:
When some error conditions happen (like OOM), some kmemleak functions
call printk() to dump out some useful debugging information while holding
the kmemleak_lock. This may cause deadlock as the printk() function
may need to allocate additional memory leading to a create_object()
call acquiring kmemleak_lock again.

Fix this deadlock issue by making sure that printk() is only called
after releasing the kmemleak_lock.
I can't say I'm familiar with the printk() code but I always thought it
uses some ring buffers as it can be called from all kind of contexts and
allocation is not guaranteed.

If printk() ends up taking kmemleak_lock through the slab allocator, I
wonder whether we have bigger problems. The lock order is always
kmemleak_lock -> object->lock but if printk() triggers a callback into
kmemleak, we can also get object->lock -> kmemleak_lock ordering, so
another potential deadlock.

object->lock is per object whereas kmemleak_lock is global. When taking object->lock and doing a data dump leading to a call that takes the kmemlock, it is highly unlikely the it will need to take that particular object->lock again. I do agree that lockdep may still warn about it if that happens as all the object->lock's are likely to be treated to be in the same class.

I should probably clarify in the change log that the lockdep splat is actually,

[ 3991.452558] Chain exists of: [ 3991.452559] console_owner -> &port->lock --> kmemleak_lock

So if kmemleak calls printk() acquiring either console_owner or port->lock. It may cause deadlock.

Cheers, Longman

--------------BE1SzJBSp0pGsxHJVPP3BOnW--