From owner-svn-src-head@freebsd.org Wed Sep 9 22:20:37 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 095EE3DEB5E; Wed, 9 Sep 2020 22:20:37 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BmxKw6Vydz3yfT; Wed, 9 Sep 2020 22:20:36 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C279887F6; Wed, 9 Sep 2020 22:20:36 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 089MKaJd015889; Wed, 9 Sep 2020 22:20:36 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 089MKabx015887; Wed, 9 Sep 2020 22:20:36 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202009092220.089MKabx015887@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 9 Sep 2020 22:20:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365524 - head/lib/libc/sys X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/lib/libc/sys X-SVN-Commit-Revision: 365524 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Sep 2020 22:20:37 -0000 Author: kib Date: Wed Sep 9 22:20:36 2020 New Revision: 365524 URL: https://svnweb.freebsd.org/changeset/base/365524 Log: Add shm_create_largepage(3) helper for creation and configuration of largepage shm objects. And since we can, add memfd_create(MFD_HUGETLB) support, hopefully close enough to the Linux feature. Reviewed by: markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D24652 Modified: head/lib/libc/sys/Symbol.map head/lib/libc/sys/shm_open.c Modified: head/lib/libc/sys/Symbol.map ============================================================================== --- head/lib/libc/sys/Symbol.map Wed Sep 9 22:18:44 2020 (r365523) +++ head/lib/libc/sys/Symbol.map Wed Sep 9 22:20:36 2020 (r365524) @@ -411,6 +411,7 @@ FBSD_1.6 { getfhat; funlinkat; memfd_create; + shm_create_largepage; shm_rename; }; @@ -919,6 +920,7 @@ FBSDprivate_1.0 { __sys_setuid; _shm_open; __sys_shm_open; + __sys_shm_open2; _shm_unlink; __sys_shm_unlink; _shmat; Modified: head/lib/libc/sys/shm_open.c ============================================================================== --- head/lib/libc/sys/shm_open.c Wed Sep 9 22:18:44 2020 (r365523) +++ head/lib/libc/sys/shm_open.c Wed Sep 9 22:20:36 2020 (r365524) @@ -31,14 +31,17 @@ #include __FBSDID("$FreeBSD$"); -#include +#include +#include #include #include #include #include +#include #include #include +#include #include "libc_private.h" @@ -54,6 +57,51 @@ shm_open(const char *path, int flags, mode_t mode) return (__sys_shm_open2(path, flags | O_CLOEXEC, mode, 0, NULL)); } +int +shm_create_largepage(const char *path, int flags, int psind, int alloc_policy, + mode_t mode) +{ + struct shm_largepage_conf slc; + int error, fd, saved_errno; + + fd = __sys_shm_open2(path, flags | O_CREAT, mode, SHM_LARGEPAGE, NULL); + if (error == -1) + return (-1); + + memset(&slc, 0, sizeof(slc)); + slc.psind = psind; + slc.alloc_policy = alloc_policy; + error = ioctl(fd, FIOSSHMLPGCNF, &slc); + if (error == -1) { + saved_errno = errno; + close(fd); + errno = saved_errno; + return (-1); + } + return (fd); +} + +#define K(x) ((size_t)(x) * 1024) +#define M(x) (K(x) * 1024) +#define G(x) (M(x) * 1024) +static const struct { + int mask; + size_t pgsize; +} mfd_huge_sizes[] = { + { .mask = MFD_HUGE_64KB, .pgsize = K(64) }, + { .mask = MFD_HUGE_512KB, .pgsize = K(512) }, + { .mask = MFD_HUGE_1MB, .pgsize = M(1) }, + { .mask = MFD_HUGE_2MB, .pgsize = M(2) }, + { .mask = MFD_HUGE_8MB, .pgsize = M(8) }, + { .mask = MFD_HUGE_16MB, .pgsize = M(16) }, + { .mask = MFD_HUGE_32MB, .pgsize = M(32) }, + { .mask = MFD_HUGE_256MB, .pgsize = M(256) }, + { .mask = MFD_HUGE_512MB, .pgsize = M(512) }, + { .mask = MFD_HUGE_1GB, .pgsize = G(1) }, + { .mask = MFD_HUGE_2GB, .pgsize = G(2) }, + { .mask = MFD_HUGE_16GB, .pgsize = G(16) }, +}; + /* * The path argument is passed to the kernel, but the kernel doesn't currently * do anything with it. Linux exposes it in linprocfs for debugging purposes @@ -63,8 +111,9 @@ int memfd_create(const char *name, unsigned int flags) { char memfd_name[NAME_MAX + 1]; - size_t namelen; - int oflags, shmflags; + size_t namelen, *pgs; + struct shm_largepage_conf slc; + int error, fd, i, npgs, oflags, pgidx, saved_errno, shmflags; if (name == NULL) return (EBADF); @@ -75,11 +124,9 @@ memfd_create(const char *name, unsigned int flags) MFD_HUGE_MASK)) != 0) return (EINVAL); /* Size specified but no HUGETLB. */ - if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0) + if (((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0) || + __bitcount(flags & MFD_HUGE_MASK) > 1) return (EINVAL); - /* We don't actually support HUGETLB. */ - if ((flags & MFD_HUGETLB) != 0) - return (ENOSYS); /* We've already validated that we're sufficiently sized. */ snprintf(memfd_name, NAME_MAX + 1, "%s%s", MEMFD_NAME_PREFIX, name); @@ -89,5 +136,57 @@ memfd_create(const char *name, unsigned int flags) oflags |= O_CLOEXEC; if ((flags & MFD_ALLOW_SEALING) != 0) shmflags |= SHM_ALLOW_SEALING; - return (__sys_shm_open2(SHM_ANON, oflags, 0, shmflags, memfd_name)); + if ((flags & MFD_HUGETLB) == 0) + shmflags |= SHM_LARGEPAGE; + fd = __sys_shm_open2(SHM_ANON, oflags, 0, shmflags, memfd_name); + if (fd == -1 || (flags & MFD_HUGETLB) == 0) + return (fd); + + pgs = NULL; + npgs = getpagesizes(NULL, 0); + if (npgs == -1) + goto clean; + pgs = calloc(npgs, sizeof(size_t)); + if (pgs == NULL) + goto clean; + error = getpagesizes(pgs, npgs); + if (error == -1) + goto clean; + if ((flags & MFD_HUGE_MASK) == 0) { + if (npgs == 1) { + errno = EOPNOTSUPP; + goto clean; + } + pgidx = 1; + } else { + for (i = 0; i < nitems(mfd_huge_sizes); i++) { + if (mfd_huge_sizes[i].mask == (flags & MFD_HUGE_MASK)) + break; + } + for (pgidx = 0; pgidx < npgs; pgidx++) { + if (mfd_huge_sizes[i].pgsize == pgs[pgidx]) + break; + } + if (pgidx == npgs) { + errno = EOPNOTSUPP; + goto clean; + } + } + free(pgs); + pgs = NULL; + + memset(&slc, 0, sizeof(slc)); + slc.psind = pgidx; + slc.alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT; + error = ioctl(fd, FIOSSHMLPGCNF, &slc); + if (error == -1) + goto clean; + return (fd); + +clean: + saved_errno = errno; + close(fd); + free(pgs); + errno = saved_errno; + return (-1); }