From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752172AbbDUF7j (ORCPT ); Tue, 21 Apr 2015 01:59:39 -0400 Received: from mail-la0-f47.google.com ([209.85.215.47]:34046 "EHLO mail-la0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750737AbbDUF7h (ORCPT ); Tue, 21 Apr 2015 01:59:37 -0400 Date: Tue, 21 Apr 2015 07:59:32 +0200 From: Jens Wiklander To: Jason Gunthorpe Cc: Russell King - ARM Linux , valentin.manea@huawei.com, devicetree@vger.kernel.org, javier@javigon.com, emmanuel.michel@st.com, Herbert Xu , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, jean-michel.delorme@st.com, tpmdd-devel@lists.sourceforge.net, linux-arm-kernel@lists.infradead.org Subject: Re: [tpmdd-devel] [RFC PATCH 1/2] tee: generic TEE subsystem Message-ID: <20150421055932.GA7760@ermac> References: <1429257057-7935-1-git-send-email-jens.wiklander@linaro.org> <1429257057-7935-2-git-send-email-jens.wiklander@linaro.org> <20150417163054.GA28241@obsidianresearch.com> <20150418090147.GF12732@n2100.arm.linux.org.uk> <20150418172923.GA10605@obsidianresearch.com> <20150420130203.GB25054@ermac> <20150420175515.GA31958@obsidianresearch.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20150420175515.GA31958@obsidianresearch.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 20, 2015 at 11:55:15AM -0600, Jason Gunthorpe wrote: > On Mon, Apr 20, 2015 at 03:02:03PM +0200, Jens Wiklander wrote: > > > It appeared to me this driver was copying TPM's old architecture, > > > which is very much known to be broken. > > > > The struct tee_device holds a shared memory pool from which shared > > memory objects are allocated. These shared memory objects can be mapped > > both by user space and secure world. > > So this is a whole other set of problems besides what was already > brought up. > > You need to figure out a lifetime model for this shared memory that > works. > > > To come around the problem with what should happen when the driver > > is removed I'm increasing the refcount on the driver for each > > allocated shared memory object and created file pointers. As long as > > any resource is in use by either user space or secure world the > > driver can't be unloaded. > > This isn't how the kernel works. The module refcount effects module > unload (it protects the .text) - it does not interact with driver > detatch. Userspace can trigger driver detatch (which results in > tee_unregister being called) at any time via sysfs. > > If you properly design for that case then module unload sequencing > works properly for free. > > Based on what I gather, I would suggest the following sequence in > tee_unregister > - unregister all sysfs and char dev registrations. > - Write lock ops and set to null. This will error future cdev ioctls, > and guarentees no driver ops callbacks are in progress, or will be > started in future. > - Wait until all client accesses to shared memory are > released. > - Command the driver to release it's side of the > shared memory and wait for that to complete > - Free the shared memory > - deref the tee_device's struct device (match ref in tee_register) > > Then in your struct tee_device's release function free the tee_device > memory. > > Replace all the module locking code with an active count in struct > tee_device (see something like kernfs_drain for an example). > > > * Change to use the pattern (with a struct device etc) as described > > above. > > Yes, I think Greg confirmed you need to use a struct device, and purge > misc_device from the mid layer. > > > I can't protect the ops with just a mutex since tee_ioctl_cmd() needs to > > be multithreaded. > > Then use a sleeping read/write lock - aka an active count. Thanks for the clarification, I got it now. Regards, Jens