From owner-freebsd-net@freebsd.org Thu Jul 30 15:00:55 2015 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D7B9C9AFAE3 for ; Thu, 30 Jul 2015 15:00:55 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B4EC21B32; Thu, 30 Jul 2015 15:00:55 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (75-48-78-19.lightspeed.cncrca.sbcglobal.net [75.48.78.19]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 9CE2FB9AD; Thu, 30 Jul 2015 11:00:53 -0400 (EDT) From: John Baldwin To: "K. Macy" Cc: John-Mark Gurney , Laurie Jennings , "freebsd-net@freebsd.org" Subject: Re: Locking Memory Question Date: Thu, 30 Jul 2015 07:16:02 -0700 Message-ID: <5314312.Bb3l71uHLc@ralph.baldwin.cx> User-Agent: KMail/4.14.3 (FreeBSD/10.2-PRERELEASE; KDE/4.14.3; amd64; ; ) In-Reply-To: References: <20150729232522.GN78154@funkthat.com> <20150730044603.GQ78154@funkthat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Thu, 30 Jul 2015 11:00:53 -0400 (EDT) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jul 2015 15:00:55 -0000 On Wednesday, July 29, 2015 11:28:03 PM K. Macy wrote: > >> > >> 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. You can do this in FreeBSD by calling vm_mmap() with a NULL handle pointer to simulate a MAP_ANON mapping. Something like this: vm_mmap(&curproc->p_vmspace->vm_map, &addr, , VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, MAP_SHARED, OBJT_DEFAULT, NULL, 0); It's not great for a true shared memory buffer, but is fine for a one-time copy. -- John Baldwin