Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752758AbbG1OPi (ORCPT ); Tue, 28 Jul 2015 10:15:38 -0400 Received: from galahad.ideasonboard.com ([185.26.127.97]:54142 "EHLO galahad.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752115AbbG1OPh (ORCPT ); Tue, 28 Jul 2015 10:15:37 -0400 From: Laurent Pinchart To: Tejun Heo Cc: Dan Williams , Linux Kernel Mailing List Subject: Re: Is devm_* broken ? Date: Tue, 28 Jul 2015 17:16:16 +0300 Message-ID: <1643821.fnPPMYQxCt@avalon> User-Agent: KMail/4.14.8 (Linux/4.0.5-gentoo; KDE/4.14.8; x86_64; ; ) In-Reply-To: <20150715180355.GH15934@mtj.duckdns.org> References: <1503739.gVWYM3p8QD@avalon> <20150715180355.GH15934@mtj.duckdns.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2420 Lines: 51 Hi Teejun, On Wednesday 15 July 2015 14:03:55 Tejun Heo wrote: > Hello, > > On Wed, Jul 15, 2015 at 10:00:54AM -0700, Dan Williams wrote: > > Sounds like a real problem. The drivers I've used devm with have an > > upper layer that prevents this crash, but that's not much consolation. > > I think adding lifetime to devm allocations would be useful that way > > ->probe() and open() can do a devres_get() while ->remove() and > > close() can do a devres_put(). Perhaps I'm also missing something > > obvious though... > > Hmmm... so this really is a general lifetime management problem and > also why sysfs implements revoke semantics. As memory allocated by > devm_kmalloc() isn't tied to any specific hardware, it seems a bit > murky here but if you consider any other resources, this is clear - a > driver must not access any resources once detach is complete. These > aren't resources which can be detached and then held while draining > existing userland references. They immediately conflict with the next > driver which is gonna attach to the device. > > A driver should isolate and drain on-going accesses from userland > before finishing detaching one way or another. No resources attached > to the hardware side can't be held once detaching is complete. If a > piece of memory isn't attached to the harware side but the userland > interface side which gets isolated and drained after detachment, that > shouldn't be allocated via devm - it has "dev" in its name for a > reason. Then that's a message we should start hammering in. There's plenty of drivers that have happily switched to devm_kzalloc() to allocate the driver private data structure, and that structure can't be freed before the last reference from userspace gets dropped. I'd even argue that this is the main use case of devm_kzalloc() in drivers. Using devm_kzalloc() in such a way has value though, and reverting drivers to the pre-devm memory allocation code would make error handling and cleanup code paths more complex again. Should we introduce a managed allocator for that purpose that would have a lifespan explicitly handled by drivers ? -- Regards, Laurent Pinchart -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/