From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFEC433D50E for ; Thu, 8 Jan 2026 23:27:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.65 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767914828; cv=none; b=NKFTtqn1/bmiMg3rFFAe4BqcYmK8RCa2EUbP9ndJY4Dw4bm814jlAoYFfW/YweLKzpO12+jrhQaRmCwnaSPsCKHY+W5QZN/Vg2gSSjFExmWT2fPb6AXYBXFYILfEU3pYa3o6NuAH+ENjBv0k8j440VisyR6Wv2Y3zBOWGfVGhO8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767914828; c=relaxed/simple; bh=q/w2OHcDBmglSw3DmZWycMkP40pwy6rwq9Ljdbui14k=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fFHN4JGv2xaKUUsu3N+HR4joW0LkRtc7Z61LlDQO3PRSp6k3geND3c68GUYz5qOQPX9jbi4aQUro9CtHQnIJzqKVof7CNuLwus1aqONFMsG1A7L9lMVajNGL4Kloww/STGShqvrfdS0bLmRluRnvCoBL1ZCI+dNiSe8PmMSjzq8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GiYbvqxv; arc=none smtp.client-ip=209.85.128.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GiYbvqxv" Received: by mail-wm1-f65.google.com with SMTP id 5b1f17b1804b1-477a2ab455fso34376495e9.3 for ; Thu, 08 Jan 2026 15:27:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767914824; x=1768519624; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=VJVnWCv1er1yEKW3Zzjqu5S3R8api15dEl8a9iGdG3k=; b=GiYbvqxveXHimZB0cY0IPQGwtddGMXUFMQUq4vFCoukkq7vITxdgRtEVA1LjOdhDlb EBQBGkY1rMMkjm+FSUmKwkAh5KnO21QhLSlgv2J0nPv55u6niA1zqm4U4Uo8WDI4XdKq /6dsit2B4hHHyJrjVQA6IRBvVafZo1fwswK4gRdLEe1tcqpy/lH89/u4+LxJ91TccgZB WC4ZnwLsgsY4DEYJQvCg+twqXCOAVkpieIZE0R+e5H2UiaAL+h6T/OteGJcfPXsUUe2z YAhekXP47hmDWItii+2w7hUcyn56P3vWU5skC7b8UAu2sTNGTZcgl3rT5j1sVJIux09t aS7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767914824; x=1768519624; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=VJVnWCv1er1yEKW3Zzjqu5S3R8api15dEl8a9iGdG3k=; b=pvqfQW44z3Wvx/5zjMJN6wOYed37ZL8+o6lGVYmjqaSUVaFUdO9eyCx0/Cg8riVrFG SqAcBAyPSNpYRVvweYKhGBfmmwbafHr0J4MYVnIY6F3dS4BuobsOdSYZrCmj0tD3Y96Z 9BaPlvQB9JlTJTVxnpxVqx+9qj9PvKK67a1aSqdDc/acRuNwQ92P5LbOrpIzPAyNvHFQ GLLSt/+CDbaTAnX6kUqvLu2Etrzf8ap53hKQDK5vJ+b/jtEnD10aCk8vPi1XLHAorJSd LrydFHibtyoNhdI5zKgpglRYt5UCQConSXS3yfAYVCmjEj5Mjnv0+EzGenThrwfnbrCX QCrQ== X-Forwarded-Encrypted: i=1; AJvYcCW7aYrwDNFQOlJ6/CHD21onT1I1HYsJtLji/0JMIUZBcoaNb/JKYKKkN6No1a59bBLHxb4+vFyrKd6uvGXLiA==@vger.kernel.org X-Gm-Message-State: AOJu0YyQwen4w7UkoaXsP8pSRsi/Cmgj2/w9GzothifyXeg/qBs3/wEC nL7YgOOMkhdYvOddK9pKLHLf51CGXy9GPbZTg1YclNJjaOWzD1SCoHfhLQAshX12rA== X-Gm-Gg: AY/fxX5qRmPt6tXiK5/8Nm3jfcxktIvqL2gg7M+m4e9hVo5SVJ/OZO1TUPaqDSL1Arl VLga3ehJeVaqw3LCjf+22lN+k3/Uqd6HgarNbAza+6wgwDgHah40Nxh0L7kho14GHjmcWPl3xTG pR506CD8DAXl2InhI7KLQZZXOLD7Lz96e4Uqx4P+1vIdMDi1Oz1gT4qsI28i6aWe/eWfmE0iSgz D1+t0z7YS3nSIzaCRcjqEj98CFq8MH4oUjradmAzn6vyV1CRrRx3Bs4OS32CNIPbcwqS5TA/Z2p bGpV64qOoksAqxrLHJZv3n8OCIkxNF0B5+V2Evj5dS/OsPcdkIyS1Oqb0cKA8/rRFITMNH2p3k8 al2UA2mZiuiVdgbQziOiIjzHD5tLSXXxf+JR0fUJF4orLwfDIumt6QC9TeYMBPijKMjYRJLhCUO jzojlSDTiJD/BixOCPCkHSpZwjxb5R/yAK8ACkcydI+wcqkjm0 X-Google-Smtp-Source: AGHT+IHhn5oqqXEXdXppHSooBMMRAzilyWn+PndcRbByujPILqHWwWxt63fu3OjbgMJmLJjeeFi/pw== X-Received: by 2002:a05:600c:4ed4:b0:471:14f5:126f with SMTP id 5b1f17b1804b1-47d84b41181mr100523185e9.33.1767914823835; Thu, 08 Jan 2026 15:27:03 -0800 (PST) Received: from elver.google.com ([2a00:79e0:2834:9:384b:578b:5b8e:a6f3]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d871a1e11sm46386595e9.19.2026.01.08.15.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jan 2026 15:27:03 -0800 (PST) Date: Fri, 9 Jan 2026 00:26:55 +0100 From: Marco Elver To: Bart Van Assche Cc: Peter Zijlstra , Boqun Feng , Ingo Molnar , Will Deacon , "David S. Miller" , Luc Van Oostenryck , Chris Li , "Paul E. McKenney" , Alexander Potapenko , Arnd Bergmann , Christoph Hellwig , Dmitry Vyukov , Eric Dumazet , Frederic Weisbecker , Greg Kroah-Hartman , Herbert Xu , Ian Rogers , Jann Horn , Joel Fernandes , Johannes Berg , Jonathan Corbet , Josh Triplett , Justin Stitt , Kees Cook , Kentaro Takeda , Lukas Bulwahn , Mark Rutland , Mathieu Desnoyers , Miguel Ojeda , Nathan Chancellor , Neeraj Upadhyay , Nick Desaulniers , Steven Rostedt , Tetsuo Handa , Thomas Gleixner , Thomas Graf , Uladzislau Rezki , Waiman Long , kasan-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-sparse@vger.kernel.org, linux-wireless@vger.kernel.org, llvm@lists.linux.dev, rcu@vger.kernel.org Subject: Re: [PATCH v5 10/36] locking/mutex: Support Clang's context analysis Message-ID: References: <20251219154418.3592607-1-elver@google.com> <20251219154418.3592607-11-elver@google.com> <57062131-e79e-42c2-aa0b-8f931cb8cac2@acm.org> Precedence: bulk X-Mailing-List: linux-wireless@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: <57062131-e79e-42c2-aa0b-8f931cb8cac2@acm.org> User-Agent: Mutt/2.2.13 (2024-03-09) On Thu, Jan 08, 2026 at 02:10PM -0800, 'Bart Van Assche' via kasan-dev wrote: > On 12/19/25 8:39 AM, Marco Elver wrote: > > diff --git a/include/linux/mutex.h b/include/linux/mutex.h > > index bf535f0118bb..89977c215cbd 100644 > > --- a/include/linux/mutex.h > > +++ b/include/linux/mutex.h > > @@ -62,6 +62,7 @@ do { \ > > static struct lock_class_key __key; \ > > \ > > __mutex_init((mutex), #mutex, &__key); \ > > + __assume_ctx_lock(mutex); \ > > } while (0) > > The above type of change probably will have to be reverted. If I enable > context analysis for the entire kernel tree, drivers/base/devcoredump.c > doesn't build. The following error is reported: > > drivers/base/devcoredump.c:406:2: error: acquiring mutex '_res->mutex' that > is already held [-Werror,-Wthread-safety-analysis] > 406 | mutex_lock(&devcd->mutex); > | ^ > > dev_coredumpm_timeout() calls mutex_init() and mutex_lock() from the same > function. The above type of change breaks compilation of all code > that initializes and locks a synchronization object from the same > function. My understanding of dev_coredumpm_timeout() is that there is a > good reason for calling both mutex_init() and mutex_lock() from that > function. Possible solutions are disabling context analysis for that > function or removing __assume_ctx_lock() again from mutex_init(). Does > anyone want to share their opinion about this? Probably the most idiomatic option is to just factor out construction. Clearly separating complex object construction from use also helps readability regardless, esp. where concurrency is involved. We could document such advice somewhere. For the above case, this seems cleanest and also clearer to me: diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c index 55bdc7f5e59d..56ac8aa41608 100644 --- a/drivers/base/devcoredump.c +++ b/drivers/base/devcoredump.c @@ -339,6 +339,40 @@ void dev_coredump_put(struct device *dev) } EXPORT_SYMBOL_GPL(dev_coredump_put); +static struct devcd_entry * +dev_coredumpm_init(struct device *dev, struct module *owner, void *data, + size_t datalen, gfp_t gfp, + ssize_t (*read)(char *buffer, loff_t offset, size_t count, + void *data, size_t datalen), + void (*free)(void *data)) +{ + static atomic_t devcd_count = ATOMIC_INIT(0); + struct devcd_entry *devcd; + + devcd = kzalloc(sizeof(*devcd), gfp); + if (!devcd) + return NULL; + + devcd->owner = owner; + devcd->data = data; + devcd->datalen = datalen; + devcd->read = read; + devcd->free = free; + devcd->failing_dev = get_device(dev); + devcd->deleted = false; + + mutex_init(&devcd->mutex); + device_initialize(&devcd->devcd_dev); + + dev_set_name(&devcd->devcd_dev, "devcd%d", + atomic_inc_return(&devcd_count)); + devcd->devcd_dev.class = &devcd_class; + + dev_set_uevent_suppress(&devcd->devcd_dev, true); + + return devcd; +} + /** * dev_coredumpm_timeout - create device coredump with read/free methods with a * custom timeout. @@ -364,7 +398,6 @@ void dev_coredumpm_timeout(struct device *dev, struct module *owner, void (*free)(void *data), unsigned long timeout) { - static atomic_t devcd_count = ATOMIC_INIT(0); struct devcd_entry *devcd; struct device *existing; @@ -381,27 +414,10 @@ void dev_coredumpm_timeout(struct device *dev, struct module *owner, if (!try_module_get(owner)) goto free; - devcd = kzalloc(sizeof(*devcd), gfp); + devcd = dev_coredumpm_init(dev, owner, data, datalen, gfp, read, free); if (!devcd) goto put_module; - devcd->owner = owner; - devcd->data = data; - devcd->datalen = datalen; - devcd->read = read; - devcd->free = free; - devcd->failing_dev = get_device(dev); - devcd->deleted = false; - - mutex_init(&devcd->mutex); - device_initialize(&devcd->devcd_dev); - - dev_set_name(&devcd->devcd_dev, "devcd%d", - atomic_inc_return(&devcd_count)); - devcd->devcd_dev.class = &devcd_class; - - dev_set_uevent_suppress(&devcd->devcd_dev, true); - /* devcd->mutex prevents devcd_del() completing until init finishes */ mutex_lock(&devcd->mutex); devcd->init_completed = false;