From owner-freebsd-net@freebsd.org Thu Jul 30 15:35:09 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 35B3A9AE0DA for ; Thu, 30 Jul 2015 15:35:09 +0000 (UTC) (envelope-from laurie_jennings_1977@yahoo.com) Received: from nm10-vm0.bullet.mail.bf1.yahoo.com (nm10-vm0.bullet.mail.bf1.yahoo.com [98.139.213.147]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E892CE05 for ; Thu, 30 Jul 2015 15:35:08 +0000 (UTC) (envelope-from laurie_jennings_1977@yahoo.com) Received: from [98.139.215.141] by nm10.bullet.mail.bf1.yahoo.com with NNFMP; 30 Jul 2015 15:35:02 -0000 Received: from [98.139.212.210] by tm12.bullet.mail.bf1.yahoo.com with NNFMP; 30 Jul 2015 15:35:02 -0000 Received: from [127.0.0.1] by omp1019.mail.bf1.yahoo.com with NNFMP; 30 Jul 2015 15:35:02 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 468517.1293.bm@omp1019.mail.bf1.yahoo.com Received: (qmail 81705 invoked by uid 60001); 30 Jul 2015 15:35:02 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1438270502; bh=Qhgio2Z/vMQGECinjlKqFaaqminu8Ff8qaW5AOW5Rqk=; h=Message-ID:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=BVdcSOyK1iO4f2oSYebPuzfXzLF/jl0NhlVM3bFRv6pSP7sTIpmmIC+ipMt+E645N3niYaUXwiCbRcA/YKEVtW5nRxRDON9HbA03f/uwKAjTPKISbQ+GRtHz4d+4zMMtmwn4hPlrsB5vQfxw2odEVunpD6LJj98O8MUfJhFT7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CAiTGF1cmllIEplbm4BMAEBAQE- X-Mailer: YahooMailBasic/600 YahooMailWebService/0.8.203.802 Message-ID: <1438270502.37685.YahooMailBasic@web141506.mail.bf1.yahoo.com> Date: Thu, 30 Jul 2015 08:35:02 -0700 From: Laurie Jennings Subject: Re: Locking Memory Question To: "K. Macy" , John Baldwin Cc: "freebsd-net@freebsd.org" , John-Mark Gurney In-Reply-To: <5314312.Bb3l71uHLc@ralph.baldwin.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable 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:35:09 -0000 -------------------------------------------- On Thu, 7/30/15, John Baldwin wrote: Subject: Re: Locking Memory Question To: "K. Macy" Cc: "freebsd-net@freebsd.org" , "John-Mark Gurney= " , "Laurie Jennings" Date: Thursday, July 30, 2015, 10:16 AM =20 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...=A0 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...=A0 The down side is that this is more > > complex to code... > > >=20 >=20 > 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. >=20 > 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. =20 You can do this in FreeBSD by calling vm_mmap() with a NULL handle pointer to simulate a MAP_ANON mapping.=A0 Something like this: =20 =A0=A0=A0 vm_mmap(&curproc->p_vmspace->vm_map, &addr, , VM_PROT_READ | =A0=A0=A0 =A0 =A0 VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, MAP_SHARED, OBJT_DEFAULT, =A0=A0=A0 =A0=A0=A0 NULL, 0); =20 It's not great for a true shared memory buffer, but is fine for a one-time copy. =20 -- ------------ Thanks everyone for the help on this. I decided to implement it with copyou= t(), mainly for maintainability going forward. I only have a couple of commands that require a larger than normal buffer, and I c= an always do a double fetch if the initial buffer is too small.=20 the big problem with the VM stuff (aside from making my head spin around) i= s that it's not documented, so it's difficult to debug and someone else working on the code will have no idea what's going on.=20 Thank again, Laurie