From owner-freebsd-hackers@FreeBSD.ORG Tue Feb 7 15:15:56 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BA3DC16A420 for ; Tue, 7 Feb 2006 15:15:56 +0000 (GMT) (envelope-from jhb@freebsd.org) Received: from speedfactory.net (mail6.speedfactory.net [66.23.216.219]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8559043D77 for ; Tue, 7 Feb 2006 15:15:46 +0000 (GMT) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (unverified [66.23.211.162]) by speedfactory.net (SurgeMail 3.5b3) with ESMTP id 7933710 for multiple; Tue, 07 Feb 2006 10:16:32 -0500 Received: from localhost (john@localhost [127.0.0.1]) by server.baldwin.cx (8.13.4/8.13.4) with ESMTP id k17FFfZM048627; Tue, 7 Feb 2006 10:15:43 -0500 (EST) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-hackers@freebsd.org, John-Mark Gurney Date: Tue, 7 Feb 2006 10:15:53 -0500 User-Agent: KMail/1.9.1 References: <43E7C583.3040904@luna.afraid.org> <43E7ED06.4030403@luna.afraid.org> <20060207005506.GL69162@funkthat.com> In-Reply-To: <20060207005506.GL69162@funkthat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200602071015.55935.jhb@freebsd.org> X-Virus-Scanned: ClamAV 0.87.1/1280/Tue Feb 7 05:11:53 2006 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-1.4 required=4.2 tests=ALL_TRUSTED autolearn=failed version=3.1.0 X-Spam-Checker-Version: SpamAssassin 3.1.0 (2005-09-13) on server.baldwin.cx X-Server: High Performance Mail Server - http://surgemail.com r=1653887525 Cc: Raaf Subject: Re: Freeing mmapped memory X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Feb 2006 15:15:56 -0000 On Monday 06 February 2006 19:55, John-Mark Gurney wrote: > Raaf wrote this message on Tue, Feb 07, 2006 at 01:42 +0100: > > John-Mark Gurney wrote: > > > Raaf wrote this message on Mon, Feb 06, 2006 at 22:54 +0100: > > >> Hi, i am working on a usb driver that allocates some memory when > > >> the device is opened using malloc. > > >> > > >> Now i want user processes to be able to access this memory using > > >> mmap and i want to free this memory when it is no longer needed. > > >> > > >> The problem is that there seems to be no way of knowing for my > > >> driver at what time the memory is no longer mapped in a process > > >> address space so that i can safely free this memory. > > > > > > why not at close time? I would imagine that the device won't be closed > > > until all the mmap's that are backed by the device are unmapped.. it > > > shouldn't be hard to test... the mapping should hold a reference to > > > the device until it's munmapped.. > > > > The problem is that it is perfectly legal to access the mapped memory > > after a close, consider following code: > > > > fd = open() > > mem = mmap() > > close(fd) > > process_data(mem) > > > > Unfortunately the mapping doesn't seem to hold a reference to the > > related fileobject, so the close in above code actually ends up > > in the close function of my driver but the mapping is still there. > > and you've tested that the mmap function still gets called after the > close function of your device driver is? If this is the case, we need > to fix FreeBSD.. the mmap should increase the ref count on the device, > and the close shouldn't initiate the close until the mapping goes away.. > > fd = open() # ref cnt = 1 > mem = mmap() # ref cnt++ > close(fd) # ref cnt-- > process_data(mem) # valid ref cnt > 0 > munmap(mem) # ref cnt-- and close sine ref cnt == 0 No, it doesn't work quite like that since the device_pager is system-wide and not per-process. That is, you can't have d_mmap() return different mappings for different processes, the mapping is global and shared, at least not w/o jumping through a lot of hoops. It might be nice to support different mappings for different proccesses. OS X does it nicely via IOUserClient::clientMemoryForType(), but our device_pager stuff would need a redesign to handle that sort of thing. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" = http://www.FreeBSD.org