Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp340370lqg; Fri, 1 Mar 2024 06:49:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWuQa1pe4qj7Jax9Q/6vQvTtwWNIXVQMNB11+CqjuLmoKEXMXTNGDz4kgFvQrtjc89wnYSHRYtbkvix2b5piir2xPNR+/KIGhah+Xm4Dw== X-Google-Smtp-Source: AGHT+IFrfMnsiXN+HDVqUt7DY34lAuFPc7dI1rKadNJAoPCwnPi1IhB8jIol/iB3Yjp6zngeJFcS X-Received: by 2002:a17:906:d209:b0:a44:5927:3e67 with SMTP id w9-20020a170906d20900b00a4459273e67mr1567996ejz.23.1709304598837; Fri, 01 Mar 2024 06:49:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709304598; cv=pass; d=google.com; s=arc-20160816; b=nlEOhzGoHUxZPccnPOlAPzjjjiIJnfuhsSJpgypsvVQDvA+Ms3u2COWrQFxIoONn+s oVcfrKaEXW1mTn9LdRvzTzeaoI3Ou/zsstYXrSEPwcsjaGUQLpOUf5VURfKCLEBajKyu jonTIDJpET4ZSSQ0JHnYu35y9u+baczcTLs76AFU4naTDJKg20/WWNbmXsEwN15REX6o 6OxzDuxVKyo9dzRljDnBv4hKduS/+LgzBt0KpU+4QJ93RFKpQAAFbNCYwuqX8c8eJsze z+AT9Lr2rSsvAw+u2BtopOfKcwJxSrC4vATv0SuIC375VHBgrXcnAPD2uamN1ShgxZUt kwQQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=G2kL+ivjF8ErY/HQSmm1g/px7C6SaUoXPcrYPci/nnU=; fh=VpO+0gcKzSS0x09o7aIL0+6GbjT6NBeM0ac+CmcIPt4=; b=ru08Y/xxFlRw26IgQqPDrhmxiixRUm3R4EoujqFE1hLP98a4/262fsrVRbrJ8cxt5s 3Q5RgEN1T0Mjcr6qs6TWyafpLdYvvmiWRCxhH7r4lPnVU7EuNC6KoHrnJ71MITeZPx0Y KRmWRIsWuOSwReRaV61rXvKELSNC+/YjGi/3YE3LmTyyHpdtA/liEJ7WTFM2VAnGVPiD U3ihMY3aQ9lUZeDr1xGQCgtjJTUc8efcIp5UqlQvyspoNLsxxUOD51udCdIpCf4+vv7J gOH3b+/t2WYbzpTXvoLBVgDLMLdTnJy2g3p1P4epRGkR4QKO3kY/yzUPTu6bzXmj7dgD i1Cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-88583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88583-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q7-20020a170906360700b00a446c76ea5asi851186ejb.909.2024.03.01.06.49.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 06:49:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-88583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88583-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 725181F24990 for ; Fri, 1 Mar 2024 14:49:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 511F339FE4; Fri, 1 Mar 2024 14:49:52 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0EAE257A for ; Fri, 1 Mar 2024 14:49:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709304591; cv=none; b=n8GEZbExCnBFyfdfNMBHu++NouXSFZJMd62hbqZGmH+3RxYuk9IlK+RwwzzuWRwX72hitm4/HQom2kMCrON901hRgqmhb7Ojkf44ldix6XUl9g2/KZCMWWqDVtWY6yUj+u41b38TytKzx+BETFCv/yRAGwF/rL5k1mwsSL0dXio= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709304591; c=relaxed/simple; bh=Dl3AlUfjo/I+0JM0QbC8JfNyVl7VSlKqagBWJbQ+S4o=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L3SCRXFh4S7PJuWS/m9ic7s02eGUVkzePMizinonnVk3oWh2pVdnIXeI0rkQmya1JxPFCpZKM7pscR+O0kbZz6UAqhXO/7vr99GyPu+HgIYNmmtH2vfwrKmTdPjJA7AVqN9QyT16AZOGm8U9GAOW4MNymCjmFz3ExStS+d+0fAI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12348C433F1; Fri, 1 Mar 2024 14:49:49 +0000 (UTC) Date: Fri, 1 Mar 2024 14:49:47 +0000 From: Catalin Marinas To: Waiman Long Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Audra Mitchell Subject: Re: [PATCH] mm/kmemleak: Don't hold kmemleak_lock when calling printk() Message-ID: References: <20240228191444.481048-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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. 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. -- Catalin