From owner-freebsd-hackers@freebsd.org Sat Jul 30 23:40:41 2016 Return-Path: Delivered-To: freebsd-hackers@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 9C3F3BA9624 for ; Sat, 30 Jul 2016 23:40:41 +0000 (UTC) (envelope-from brooks@spindle.one-eyed-alien.net) Received: from spindle.one-eyed-alien.net (spindle.one-eyed-alien.net [199.48.129.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7D7D21230 for ; Sat, 30 Jul 2016 23:40:41 +0000 (UTC) (envelope-from brooks@spindle.one-eyed-alien.net) Received: by spindle.one-eyed-alien.net (Postfix, from userid 3001) id 2167E5A9F27; Sat, 30 Jul 2016 23:32:24 +0000 (UTC) Date: Sat, 30 Jul 2016 23:32:24 +0000 From: Brooks Davis To: Adam Starak Cc: freebsd-hackers@freebsd.org Subject: Re: Modify user space from kernel. Message-ID: <20160730233224.GB98042@spindle.one-eyed-alien.net> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="x+6KMIRAuhnl3hBn" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 30 Jul 2016 23:40:41 -0000 --x+6KMIRAuhnl3hBn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jul 29, 2016 at 03:11:25PM +0200, Adam Starak wrote: > Hello! >=20 > My name is Adam. I participate in Google Summer of Code this year. I came > up with a big problem, which doesn't allow me to go further in my project. >=20 > I made a new syscall, which is going to retrieve sysctl data and put it > inside the nvlist. And here my problem is. I need to move somehow this da= ta > (packed nvlist) into the user space. Is there any chance to pass data from > kernel to user space without knowing the size of it? >=20 > Right now, the implementation of __sysctl() function requests void pointer > and size in order to get data. If allocated memory is too low, it returns > ENOMEM and you need to realloc the data. I wanted to avoid this situation. If you want a memory based interface, that is what how it should be. If the size won't change, then you can output the required size on failure. For example, if your syscall has a definition like: int give_me_an_nvlist(void *buf, size_t *buflen); On ENOMEM you'd write the required size to buflen. If the list really don't change size, requiring two system calls would be a lot cheaper and easier to handle than a socket as someone else proposed. -- Brooks P.S. Purely as an FYI, there is technically another option. You could inject a set of page mappings into the process, write the nvlist to them, and then return a pointer to them. This would be fragile, prone to hard to diagnose memory leaks, and would probably break all sorts of tools like valgrind, but it's theoretically possible. I seriously doubt such an interface would be acceptable to the project. --x+6KMIRAuhnl3hBn Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJXnTkHAAoJEKzQXbSebgfAuzYH/1qM5Mt3c8ipz3GSmajtWWpl U26v7nQhFSMujDFvcJSickWGYuBmMjt+SCkVjOCafY11iQ+9aCSLCKsmwSnp8JJh DeIDJKV3icD7Owx9STkRW3Uw0n5PaFjp15LVWsZI6EraBN5JUPIZTxJUcfksp/gA +UBg2kW5oqzh1BisIOf+I3kxVVNJHO8H0g9wTBul5siSJ/hsdDdijYHPspm2X50/ HBDNuEtwv4xENtuwa4ZXn6Y26smtwTH0wJ6dvI+rrDONIIgIeWgamvm0E6UbhYng 7A2Bzgr+lvLVYkVyi6MngUgSQNFXM4ulfErx3OUIgARCxJwyjkl2Yl0XoQXR4n0= =LnlL -----END PGP SIGNATURE----- --x+6KMIRAuhnl3hBn--