Re: Freeing mmapped memory



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 <jhb@xxxxxxxxxxx> <>< http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve" = http://www.FreeBSD.org
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: sharing memory map between processes (same parent)
    ... | 'warm the cache' by getting the inode into memory. ... It might make a difference if the memory remained mapped to some process ... How about we take a look at the approximate steps that mmap needs to ... So, whether the mapping is transferred to a second process, or the second ...
    (comp.unix.programmer)
  • Re: Problem mapping small PCI memory space.
    ... If I try to expose it through mmap, ... > access that 512 bytes memory by using ioremapon the physical address ... > Are there any lower limits on the size of a PCI memory region? ... You impliment mmapin your driver. ...
    (Linux-Kernel)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)
  • How to use "offset" parameter in mmap() system call
    ... I wrote a device driver, it supports mmap(), but I met some ... How to use the offset parameter of mmapcorrectly? ... My driver is a char device driver, when it is loaded, it allocates ... 64 MB contiguous physical memory using contigmalloc, ...
    (freebsd-hackers)
  • Re: [PATCH] drivers/base: export gpl (un)register_memory_notifier
    ... If a driver doesn't know anything else about the mapping structure, ... the normal solution in kernel for this type of problem is a multi ... This doesn't sound right to be implemented in a device driver. ... When the memory is in a particular configuration (range of memory ...
    (Linux-Kernel)