From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) (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 AEEE233D4E5 for ; Thu, 8 Jan 2026 23:27:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.67 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767914827; cv=none; b=F2rwPlsKCWMmPp+CwQH6O7zRsPqC2OgpacNWbpEDrBi38TcpTv2htHYiS/b3IKBacxKHVe2sDt1Tb3oRKeClM9pIyx2e7KqOGky1perylYMNPb4RIgxquEzOmJXG8fLktAF4DPQhVwP37ENw0xG6/4a4YvcoyMyEVEfLO7T+HAI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767914827; 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=HpHTd3aYl8cMotcbRPEGkUK3woyjqzUSToeqCLulznk887uCrdTw14cLAuWyKI3LlEfSye33Cf7YDKP17yHVlT9jkJ3zpRNla2JSxJBR31RPli5Trv4LUegJ8c9piGNzBVG9CeJEADv70kE3cphnuCe65m/k/BMw8zOB54J50oY= 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=cxc+JbMF; arc=none smtp.client-ip=209.85.128.67 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="cxc+JbMF" Received: by mail-wm1-f67.google.com with SMTP id 5b1f17b1804b1-4775ae77516so40876065e9.1 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=lists.linux.dev; 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=cxc+JbMFKJ0Ebgtc3APIfiBV5TtU+ty3n4pSynHO/0cJ3RGaRZFA6SzOGrCYqMCL89 zF3SFGGAjIIBa/fIOdNrXot6pQPDQKZ8OizbuU6+qrHuq2mux7Xr3BwhuE0iaAJMYoTl nGb+nymZZrlrOfpcg2OjvU3V/P5Kmn6/twLFm9se/YtfeUaeClOMswJhE+QvaZianwBs 4JgaFF2K88d8GAvA+uu1PwOwYnBh5BzqjaHpPRMG9D8cj9AViViLwf4wv/kVHyJtLmM/ AqgDKD5rFZRRXIOjZ+ZHJA2S+WZ5b0MK+YXMQOd+5qxAHLvk5f76NJ3PSec7gDZ4MCyd D5Tw== 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=t3NHHFx2daTl4R9PG5OIz4qxheYI+qQMURcCgegJSmzdK1BFT8DaiUuR4JMlDF3hl5 vkRae/k40eWRZPO2kpsTTMYu3ntQSn4npYc+ml1KQa6F6K6f2n4byOQjkfZHsPDQwqYk 8BgMcf8QdnmfqWBz3y98dPqpjmIheQ7UsO5wLWIijPdNg/KgjsxseJFq35Fshsr9q9OS +GA9o6nMgI1XwHsPS3QvHgGo8s3afrHkiFnjIMiRsm0/WLYURQ5UBotoR0bdSJXTLeFa 7GiQEKGn3UEQ4G5teUeRI6yRZqQpTnX7EVw/j0wjtBbl1Z4tXTTzvD547iQgTSr4wZQN aWuQ== X-Forwarded-Encrypted: i=1; AJvYcCWsZ3NlVKq7zKjHH28YIydP7eS7XknLzRDmcPAgWIrr4w8BUdzGQ6+SvxiDCQ5QZQ93maD8@lists.linux.dev X-Gm-Message-State: AOJu0Yw2TtrwmP99mPOhtQg+ZMN7i44L8kTIWOXAUwAi7hpK49xMmONu vtOHYyFo8SDedG1SXRRadi3PqasLnnyPsEd7xzdckrZXw8rU2v0cXLho8hceUwzs6w== X-Gm-Gg: AY/fxX71Szew6T54SbsStt6zu8RQ3TMxlD5Z/oAcHdhQet8VyueXJv6jETqfgqfLUQc LU4uS6FuKa4f5A29qHdOMxJfVssLV8P25dgQZcFQAPgLKAc2Drvw2FJSz+Vzd8ZE0uRqo35/ztJ GQpd8JRjzq4Zf2SaPidX+Ihn4YQw/0A2y+1FQvyEgh4piSFpe1cYfrpLYmKT3pCT+KkQ6WxVR+v qWhbYXWvDreKQ/eg7OUkIfjEt7W/1oLQcsAnR6q76bmGp0he/s1rRR93T9o93tbzzgMo8pyCgqw 5ss4d+bjPvrF8Rm2IBySizprwXaS67o4goPR2oCMACOD2iB60uUmRxGiU9wUnv40dKFb7r2pRuV WqKxlRi+479KdxcDfNf7nMK0I3bcXOzTgUfvZUi8H7esAkrWvO2zejQrRbgVC+pb0KSxr7jLKEO /EXuOVXwajCUEeIu2eb3VkvvB0tD1znTjCPy2LPMFYjsaDe2p5 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: llvm@lists.linux.dev 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;