From owner-freebsd-hackers@FreeBSD.ORG Tue Feb 7 00:56: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 B4CE716A420 for ; Tue, 7 Feb 2006 00:56:56 +0000 (GMT) (envelope-from jmg@hydrogen.funkthat.com) Received: from hydrogen.funkthat.com (gate.funkthat.com [69.17.45.168]) by mx1.FreeBSD.org (Postfix) with ESMTP id B66F843D49 for ; Tue, 7 Feb 2006 00:56:53 +0000 (GMT) (envelope-from jmg@hydrogen.funkthat.com) Received: from hydrogen.funkthat.com (tf4e2gi81vskc9h7@localhost.funkthat.com [127.0.0.1]) by hydrogen.funkthat.com (8.13.3/8.13.3) with ESMTP id k170t6EO070976; Mon, 6 Feb 2006 16:55:06 -0800 (PST) (envelope-from jmg@hydrogen.funkthat.com) Received: (from jmg@localhost) by hydrogen.funkthat.com (8.13.3/8.13.3/Submit) id k170t6OZ070975; Mon, 6 Feb 2006 16:55:06 -0800 (PST) (envelope-from jmg) Date: Mon, 6 Feb 2006 16:55:06 -0800 From: John-Mark Gurney To: Raaf Message-ID: <20060207005506.GL69162@funkthat.com> Mail-Followup-To: Raaf , freebsd-hackers@freebsd.org References: <43E7C583.3040904@luna.afraid.org> <20060206225649.GJ69162@funkthat.com> <43E7ED06.4030403@luna.afraid.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <43E7ED06.4030403@luna.afraid.org> User-Agent: Mutt/1.4.2.1i X-Operating-System: FreeBSD 5.4-RELEASE-p6 i386 X-PGP-Fingerprint: B7 EC EF F8 AE ED A7 31 96 7A 22 B3 D8 56 36 F4 X-Files: The truth is out there X-URL: http://resnet.uoregon.edu/~gurney_j/ X-Resume: http://resnet.uoregon.edu/~gurney_j/resume.html Cc: freebsd-hackers@freebsd.org Subject: Re: Freeing mmapped memory X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: John-Mark Gurney 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 00:56:56 -0000 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 -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."