Locking Memory Question

K. Macy kmacy at freebsd.org
Thu Jul 30 06:28:04 UTC 2015


>>
>> Im not clear how I'd do that. the data being passed up from the kernel is a variable size. To use copyout I'd have to pass a
>> pointer with a static buffer, right?
>
> Correct, you can pass along the size, and if it's not large enough
> try again... Less than ideal...
>
>> Is there a way to malloc user space memory from within an ioctl call?
>
> Well, it is possible that you could do the equivalent of mmap, and pass
> the address back along w/ a length, and leave it up to the user to
> munmap it...  This is probably the nicest method if you the size is
> really largely variable, and it's expensive if the userland process
> allocated too much memory...  The down side is that this is more
> complex to code...
>


Mach has the ability to send large "out of line messages". For smaller
messages where it doesn't do VM tricks to avoid copying it does
exactly this. In the receive path the kernel calls vm_allocate (which
is essentially just a wrapper for mmap) then copies the buffer in to
the newly allocated address space. The message itself contains the
allocated address and size of the allocation. The receiver is expected
to call vm_deallocate (munmap) when it's done with the data.

The implementation is mixed in with enough other code that it may not
be a useful reference. Nonetheless, I wanted to point at that this
isn't as strange as it might sound.

-K


More information about the freebsd-net mailing list