Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp150447lqs; Mon, 4 Mar 2024 19:21:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV+FsQlUyR8Q3LxK0WEFm9ggc+Kzrc+I+KAD12Iwl4gnyd1hA+7NW9XCLBhIqT9N+Psqqr9TjvBjJG9NS5RN3EWmrRfc8JmwyQxqrM2iA== X-Google-Smtp-Source: AGHT+IGA8rPqMNr7TZ0I2Ugu9qGd94zyZMGRvKdb2XUnxia4Z1YAgWEwWQlc5si9MkCvqVcnRpFo X-Received: by 2002:a25:83cd:0:b0:dd0:453b:485e with SMTP id v13-20020a2583cd000000b00dd0453b485emr3289355ybm.58.1709608886575; Mon, 04 Mar 2024 19:21:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709608886; cv=pass; d=google.com; s=arc-20160816; b=tTsVTd+zuMLRLrnb5cs0seHuNkMJ5kwoMkaoumSehFajIwhY1tTz8DeASZT/s3pCpf 4GhjfGXJ/ArJAWjoB0kOQVNbjpxVhtNJfOKQ8MqQRw7Nf5XC2lnsPChReTqeui57kduq ikIZeF9FWVolXnbr35ux3u8154dTXkQ96zoSmoUrYDszK05Y04vjZ6trdaV/2hrtPV1m 1Z7BGM8piFEyi1hpJ7w5jHZgVJCQO6eexjsQktyQaWDu7nqpuQB/uqp8Xka1xw466dup ALpFvzPObhR2XGK5NUPO7tKwZZUaBFva3vJ6G4Qmg+UJzDw4dahP/1JjjEin/9dJbtR1 Ugng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=6h7TTCoR/Jp0ooBmr/f4+J3rHYWSV3rW1/afOQUBqeg=; fh=QR31G3F4jUDtkBjbN2x8WBRE3D97Qpo3I57VcDu4BSE=; b=w5U4pMD988W94Lv6DlWrAehxyUk4feuXkRQHAWsX5rZPhNHS2p5jdAWN+xDugOF6vt i/mkMW2Pq1dNk50t+17fCUkNxFbLovrrF9rbwePfJjQwVcXlurjf86e9TD6hM0GXB/+6 WFDRAxSnpYssCVFcu3MQ80Tnbt5/8fI3Fd+SpYP49BesHepXMj4uWlbHAIGp5jOUJykH p5cbpY5FDQoPQ714AS/AgSWNiYp0CPbckBWb5e2j+jwbckbUB8Xq/0bBmhMN9t1CWaad qUiAH9ilx9ZpdNHk65c4stTCaDA2PUUTpSNozTbhnDECFVInR2r2NdyHMn4ua0HHnesq ZRSQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d2CQENP3; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-91607-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91607-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d2-20020a05622a15c200b0042ee97deb36si5025134qty.600.2024.03.04.19.21.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 19:21:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91607-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d2CQENP3; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-91607-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91607-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 510CF1C215D4 for ; Tue, 5 Mar 2024 03:21:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D6183D0B8; Tue, 5 Mar 2024 03:21:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="d2CQENP3" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 45DCB2942A for ; Tue, 5 Mar 2024 03:21:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709608869; cv=none; b=lKzCQLTcI50KrNCphC2OfFho1SS/KI1tLWzfLxOTWJA+iDELlDqbNVFhyrcG8XCgVXfLyF98KpdTpPspzEd63fi74Piwt77yJ4sJ6RZ2UrqTfeJXxdhI0s11A1eikzphJQkbr9wfRBguyxBU4ADRfTsJ/V+FsTdIsXwdtbBIkhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709608869; c=relaxed/simple; bh=BAIZttYr9CFZu/K50YIXtIb0ZZE7+4WLZelPCaIjosg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=D/gRRJ60gZ31gw81cns4JgwePLts7erxlIiiZA/RWlq+8ZaA0T43OMh1GblhjhNB570CGOzT3CSiJiHAGjQtZ2MCrUf1KKMdUK2w4mUXmlQO8j0FjakhUbtY34NuqSea7fehJbDHfBdOvUocr5ZoGTsbdabaV1d1aMnN1n2krXk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=d2CQENP3; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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