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 E97F6C54798 for ; Tue, 5 Mar 2024 03:21:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 471036B0080; Mon, 4 Mar 2024 22:21:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 421B96B0081; Mon, 4 Mar 2024 22:21:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E9676B0082; Mon, 4 Mar 2024 22:21:06 -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 20AB06B0080 for ; Mon, 4 Mar 2024 22:21:06 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E048A14020A for ; Tue, 5 Mar 2024 03:21:05 +0000 (UTC) X-FDA: 81861534090.22.F2D7689 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf05.hostedemail.com (Postfix) with ESMTP id EBE8910000B for ; Tue, 5 Mar 2024 03:21:03 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=d2CQENP3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf05.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1709608864; 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:dkim-signature; bh=6h7TTCoR/Jp0ooBmr/f4+J3rHYWSV3rW1/afOQUBqeg=; b=6q6gqtKj+RGyC/UL19gwahVFuP4vt7HHw3SpLQXlTS+vwc+IXe1T+NQSq4eVP/fIJrVcuO GnTib5S8P+IKbU2h8Dg8vsrGR2B6TlBBsODJVJSXgwPMyc2rf+Euzb+Cmm9mS/EYc6QHXu LmdYsaAlmIaWP9UNxVUvulB7fSleLVQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=d2CQENP3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf05.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709608864; a=rsa-sha256; cv=none; b=5uZf/BlFZ27AdAbPhXaG6Bv3iK2e0EO9ARMLJkeC9CHCMLPMe/rmIYXMavdnQRgbdVaRn9 hIJzT1lq3zdQSqjOLHttle31Tki4Q+Z8X4reV1mSfj3thT/a1/eZ16jPPGgCTznHWL54F+ J4y0UE7HpgRx2fGsLQ4gwBEEL3ulN0o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709608863; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6h7TTCoR/Jp0ooBmr/f4+J3rHYWSV3rW1/afOQUBqeg=; b=d2CQENP3L3CMuVKR+0xEklk+FQl2qjxV6kgmTXE9edhWjmANMHpeurevMFDFyvNTGqaBxa sgeqr3MH1jfRNjAW9E/zTVPJgZPlcHeGn8AJZuSPhNm3D4xjvLPMDeld8zqNPk8aac9dKi biXbMTwhG4h6Wcaldjs2D4Pq+b/9B0U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-383-Gei35NVMMYGpMismXOy2zg-1; Mon, 04 Mar 2024 22:20:58 -0500 X-MC-Unique: Gei35NVMMYGpMismXOy2zg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 9107310726A4; Tue, 5 Mar 2024 03:20:58 +0000 (UTC) Received: from [10.22.8.66] (unknown [10.22.8.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 481612166B31; Tue, 5 Mar 2024 03:20:58 +0000 (UTC) Message-ID: <147b6e99-dc5a-4b40-a1b2-8b957459e76d@redhat.com> Date: Mon, 4 Mar 2024 22:20:58 -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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EBE8910000B X-Stat-Signature: sp7q57c17aimc7y99fdgf58mpfaz6nmk X-HE-Tag: 1709608863-12091 X-HE-Meta: U2FsdGVkX19+Hw2sf9UMUJHsKKIcgCCrqpswKaDb/sY+QZkiFZ7awC7XNSQT7oG9j+OOYKdmlq+XqI4Wcofb6TGs4VDDAphJU1Gxa/Ghgu2hNa3eP7vu+yZiANkhT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SeeiV5VntE129tD6UZuTvMSmHZ6+YgdVkHaKPuFMi3re8A== 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: On 3/1/24 09:49, Catalin Marinas wrote: > On Thu, Feb 29, 2024 at 10:55:38AM -0500, Waiman Long wrote: >> 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. > Yeah, it's unlikely. I think it can only happen if there's a bug in > kmemleak (or slab) and the insertion fails because of the same object we > try to dump. But I suspect lockdep will complain either way. > >> 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. > Could you please share the whole lockdep warning? IIUC, it's not the > printk() code allocating memory but somewhere down the line in the tty > layer. Yes, I will do that in the next version. > > Anyway, I had a look again at the kmemleak locking (I've been meaning to > simplify it for some time, drop the object->lock altogether). The only > time we nest object->lock within kmemleak_lock is during scan_block(). > If we are unlucky to get some error on another CPU and dump that exact > object with printk(), it could lead to deadlock. > > There's the dump_str_object_info() case as well triggered by a sysfs > write but luckily this takes the scan_mutex (same as during > scan_block()), so it solves the nesting problem. > > I think in those error cases we can even ignore the object->lock when > dumping the info. Yeah, it can race, maybe not showing exactly the > precise data in some rare cases, but in those OOM scenarios it's > probably the least of our problem. I was thinking about not taking the object->lock too. You are right that under OOM, a little bit of racing doesn't really matter. Will do that in the next version. Cheers, Longman