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 227FBE64007 for ; Thu, 21 Nov 2024 19:19:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AE786B0098; Thu, 21 Nov 2024 14:19:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 95D2E6B0099; Thu, 21 Nov 2024 14:19:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84C226B009A; Thu, 21 Nov 2024 14:19:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 646546B0098 for ; Thu, 21 Nov 2024 14:19:14 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 116DC12147B for ; Thu, 21 Nov 2024 19:19:14 +0000 (UTC) X-FDA: 82811064126.15.1413F26 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 01CFD16001B for ; Thu, 21 Nov 2024 19:18:36 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf08.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732216660; 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; bh=2JEVxA48+49grarAT2tPO0jCEujXT35oKOexX8BIfYA=; b=FxAiVXHDEwHcMqfawPCN6GdCnUn9oKv1RXhzrSqVtBjcyL5UU5b1x9cVFMTN0yls71pOah 65l9SIy0+E2QkpcME/J6/z/EmPi9ELwojqIZa+ybzEy0SawjIQM6R2iFaUXVAwp9TbmlzD O8GleMaeTplpfkhklHSRH+RynbjZLX0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf08.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732216660; a=rsa-sha256; cv=none; b=JO4YLmCMTANW3QeRyuF1VuoHsLSmmlPVsF9TMYXrpNUjmu7Eo9aKRMYJn0jwOoAUiT5nOX 5PNdNRJubkJQ5rzTO+cwhp3Z2Td0prHcJuGRmVra93LvGJspiRgOjOhGZXphqNw7oS5UmA 77HIynzYyo4bnXIR/4Kq0KHDXZSBQ7c= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 607475C5876; Thu, 21 Nov 2024 19:18:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B01FC4CECC; Thu, 21 Nov 2024 19:19:08 +0000 (UTC) Date: Thu, 21 Nov 2024 19:19:05 +0000 From: Catalin Marinas To: Steven Rostedt Cc: Alessandro Carminati , Andrew Morton , Sebastian Andrzej Siewior , Clark Williams , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev, Thomas Weissschuh , Alessandro Carminati , Juri Lelli , Gabriele Paoloni , Eric Chanudet Subject: Re: [PATCH] mm/kmemleak: Fix sleeping function called from invalid context in kmemleak_seq_show Message-ID: References: <20241120102325.3538-1-acarmina@redhat.com> <20241120102602.3e17f2d5@gandalf.local.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241120102602.3e17f2d5@gandalf.local.home> X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 01CFD16001B X-Stat-Signature: hew5rc48ywcwkos6r64sr69b3gab991c X-HE-Tag: 1732216716-9637 X-HE-Meta: U2FsdGVkX18+5V+csdMkyAtqGSF5McOVRV5CubxcoyHwhl4/XARAN2sxY9VCGFBcr/qSXFt4TEk8y+9hEpaby9cfE5Ytyi42RA+hT3Hn3doT78omzrJvxe0FYpctsyXaXKaUy6uHEGRXMNiomz4rKbMVT9od7sACCzwinqE4j+MmIJeDH4PnKHy/HhYLhJLKGUuhKUn1pRacz3B37Sz7YdHVWkx6n6SRBZL1uyp+qxF8CUY+wtYN77aGYgYh8OZGzA8YXI5Jqn8u82OYoKNh4uDUec0xA8sNXHkkIIfO4JXR3utdmoUm2LPFV+ZRMZa3EvqyiVkonU2JhrijoV5CTtsE/m2w013Os85s1JcluZpoP+o7zXC4bLhzXXukAF9Ily/5KtFLt0jEuZHad17Nv8zJfEyIQTmD2ElbFlJ7sCg5mUSjJRWnRZNEAApFkhUnAVmRkEXXPWZiqBB96BKaxjpKoAfWT+u25KMtU4148EM0Kjbes5xeqzblaULrBhvNWRsbbsiK6dLc7CkhVQBmAY/Zk/Da2oNy2P/+dzOEHdoX8dWRPZGLUao9ZIQVhOf7ry8UKCi9OmPrjFcMWGl/XZfsDfdNVMNql/3i+on/s9tFOArCvJSmC0VEfIdh3PFxDsNrKU1poW2fNfhkxZMd3MDNKQZ09xvJDqsztRCGh0bBo1IEz0e5yZLXR1YpRTrcyYmg/1mCl+XVVZsQeZVq/AwiOLvmx3K8PiB1hhmxb7ULWqBk2hiapSYFilNxaXK2GcNlhqdzm2navquz4QGZgxcJCMDkYjyW3nRz5w9dlB8+UZdJgTsD+som1NYSGdb/ZmYoz7RLNYTUR1ohGuBrDgr1oq8MtkZuhFXxvk9kqhckkiluPP/26X6rR6Ugi1nApSSi2/QImRlH9FQusUUcEOmFYwWBROwNgR2lIj3WxzSnIru7Oy8xEpPeobAoVtixhA8Dwer6/ma96kqRvZ8 vSJnbLpt xWrPolh4HEKMqdoE/64ENgLzsL5sdPyOs5jTb/OQal2i5BJEoMmirkg4WlaGeM1lM7JwTgfOOKLBbp6yn/r3N6oir3XsSvaJaOH25bDr2sz0U2oVIOMiMuibVYgL07z9QVRCzxQdHjg7zius/fdWOL3s+FY0cnkUkdC6Jz2P3zoGyCJMCr4RFB4+TJIVoH+glE4FkBvQ8k+v21nDPRGJ6qZhHWF/UevErfW9kLGi44a+hWlX/rgtADA9J8ZXo/c+J60H1jFW2uGsJTUGCNbuT4QtkzqmG52yfOMoNfUpL3/U+huhO7R3fiv5fVOdADxf1buuxPpHFiNGquil1ZMMvtBdWYQ70MQSCb5ZKO4XQUwVYKdok461DWvYIxdmhBAhqF1TYqPLiSqkGbpMCqgMXPBbpMHppP2mOtuxL48JrI6sfCjmzvOFlY9R9KIbpJTSOMKk/ayHVJOo8rFNBCXo/Mfndfu5PQHgJJGoil9wJUq25SHR6q7TwVjpmTV2svUb7Gzv3+kUeaW7+JwPam0By8tpRWQ== 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 Wed, Nov 20, 2024 at 10:26:02AM -0500, Steven Rostedt wrote: > On Wed, 20 Nov 2024 14:53:13 +0000 > Catalin Marinas wrote: > > > -static void print_unreferenced(struct seq_file *seq, > > > +static depot_stack_handle_t print_unreferenced(struct seq_file *seq, > > > struct kmemleak_object *object) > > > { > > > - int i; > > > - unsigned long *entries; > > > - unsigned int nr_entries; > > > - > > > - nr_entries = stack_depot_fetch(object->trace_handle, &entries); > > > warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n", > > > object->pointer, object->size); > > > warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n", > > > @@ -371,6 +366,23 @@ static void print_unreferenced(struct seq_file *seq, > > > hex_dump_object(seq, object); > > > warn_or_seq_printf(seq, " backtrace (crc %x):\n", object->checksum); > > > > > > + return object->trace_handle; > > > +} > > > > What I don't fully understand - is this a problem with any seq_printf() > > or just the backtrace pointers from the stack depot that trigger this > > issue? I guess it's something to do with restricted pointers but I'm not > > familiar with the PREEMPT_RT concepts. It would be good to explain, > > ideally both in the commit log and a comment in the code, why we only > > need to do this for the stack dump. > > In PREEMPT_RT, to achieve the ability to preempt in more context, > spin_lock() is converted to a special sleeping mutex. But there's some > places where it can not be converted, and in those cases we use > raw_spin_lock(). kmemleak has been converted to use raw_spin_lock() which > means anything that gets called under that lock can not take a normal > spin_lock(). > > What happened here is that the kmemleak raw spinlock is held and > seq_printf() is called. Normally, this is not an issue, but the behavior of > seq_printf() is dependent on what values is being printed. > > The "%pK" dereferences a pointer and there's some SELinux hooks attached to > that code. The problem is that the SELinux hooks take spinlocks. This would > not have been an issue if it wasn't for that "%pK" in the format. Thanks Steven. That's very useful. > Maybe SELinux locks should be converted to raw? I don't know how long that > lock is held. There are some loops though :-/ > > avc_insert(): > > spin_lock_irqsave(lock, flag); > hlist_for_each_entry(pos, head, list) { > if (pos->ae.ssid == ssid && > pos->ae.tsid == tsid && > pos->ae.tclass == tclass) { > avc_node_replace(node, pos); > goto found; > } > } > hlist_add_head_rcu(&node->list, head); > found: > spin_unlock_irqrestore(lock, flag); > > Perhaps that could be converted to simple RCU? > > As I'm sure there's other places that call vsprintf() under a raw_spin_lock > or non-preemptable context, perhaps this should be the fix we do. My preference would also be to convert SELinux rather than avoiding the issue in kmemleak (and other similar places). -- Catalin