xref: /freebsd/sys/kern/uipc_shm.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18e38aeffSJohn Baldwin /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
415bcf785SRobert Watson  * Copyright (c) 2006, 2011, 2016-2017 Robert N. M. Watson
5d301b358SKonstantin Belousov  * Copyright 2020 The FreeBSD Foundation
68e38aeffSJohn Baldwin  * All rights reserved.
78e38aeffSJohn Baldwin  *
815bcf785SRobert Watson  * Portions of this software were developed by BAE Systems, the University of
915bcf785SRobert Watson  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
1015bcf785SRobert Watson  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
1115bcf785SRobert Watson  * Computing (TC) research program.
1215bcf785SRobert Watson  *
13d301b358SKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
14d301b358SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
15d301b358SKonstantin Belousov  *
168e38aeffSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
178e38aeffSJohn Baldwin  * modification, are permitted provided that the following conditions
188e38aeffSJohn Baldwin  * are met:
198e38aeffSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
208e38aeffSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
218e38aeffSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
228e38aeffSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
238e38aeffSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
248e38aeffSJohn Baldwin  *
258e38aeffSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
268e38aeffSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
278e38aeffSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
288e38aeffSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
298e38aeffSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
308e38aeffSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
318e38aeffSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
328e38aeffSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
338e38aeffSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
348e38aeffSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
358e38aeffSJohn Baldwin  * SUCH DAMAGE.
368e38aeffSJohn Baldwin  */
378e38aeffSJohn Baldwin 
388e38aeffSJohn Baldwin /*
398e38aeffSJohn Baldwin  * Support for shared swap-backed anonymous memory objects via
409afb12baSDavid Bright  * shm_open(2), shm_rename(2), and shm_unlink(2).
419afb12baSDavid Bright  * While most of the implementation is here, vm_mmap.c contains
429afb12baSDavid Bright  * mapping logic changes.
438e38aeffSJohn Baldwin  *
445c066cd2SKonstantin Belousov  * posixshmcontrol(1) allows users to inspect the state of the memory
455c066cd2SKonstantin Belousov  * objects.  Per-uid swap resource limit controls total amount of
465c066cd2SKonstantin Belousov  * memory that user can consume for anonymous objects, including
475c066cd2SKonstantin Belousov  * shared.
488e38aeffSJohn Baldwin  */
498e38aeffSJohn Baldwin 
508e38aeffSJohn Baldwin #include <sys/cdefs.h>
518e38aeffSJohn Baldwin __FBSDID("$FreeBSD$");
528e38aeffSJohn Baldwin 
5312bc222eSJonathan Anderson #include "opt_capsicum.h"
54551a7895SRui Paulo #include "opt_ktrace.h"
5512bc222eSJonathan Anderson 
568e38aeffSJohn Baldwin #include <sys/param.h>
574a144410SRobert Watson #include <sys/capsicum.h>
58610a2b3cSJohn Baldwin #include <sys/conf.h>
598e38aeffSJohn Baldwin #include <sys/fcntl.h>
608e38aeffSJohn Baldwin #include <sys/file.h>
618e38aeffSJohn Baldwin #include <sys/filedesc.h>
622b64ab22SMark Johnston #include <sys/filio.h>
638e38aeffSJohn Baldwin #include <sys/fnv_hash.h>
648e38aeffSJohn Baldwin #include <sys/kernel.h>
6591898857SMark Johnston #include <sys/limits.h>
66551a7895SRui Paulo #include <sys/uio.h>
67551a7895SRui Paulo #include <sys/signal.h>
68cc7b259aSJamie Gritton #include <sys/jail.h>
69551a7895SRui Paulo #include <sys/ktrace.h>
708e38aeffSJohn Baldwin #include <sys/lock.h>
718e38aeffSJohn Baldwin #include <sys/malloc.h>
728e38aeffSJohn Baldwin #include <sys/mman.h>
738e38aeffSJohn Baldwin #include <sys/mutex.h>
749c00bb91SKonstantin Belousov #include <sys/priv.h>
758e38aeffSJohn Baldwin #include <sys/proc.h>
768e38aeffSJohn Baldwin #include <sys/refcount.h>
778e38aeffSJohn Baldwin #include <sys/resourcevar.h>
7889f6b863SAttilio Rao #include <sys/rwlock.h>
7956d0e33eSKonstantin Belousov #include <sys/sbuf.h>
808e38aeffSJohn Baldwin #include <sys/stat.h>
817ee1b208SEd Schouten #include <sys/syscallsubr.h>
828e38aeffSJohn Baldwin #include <sys/sysctl.h>
838e38aeffSJohn Baldwin #include <sys/sysproto.h>
848e38aeffSJohn Baldwin #include <sys/systm.h>
858e38aeffSJohn Baldwin #include <sys/sx.h>
868e38aeffSJohn Baldwin #include <sys/time.h>
87d301b358SKonstantin Belousov #include <sys/vmmeter.h>
888e38aeffSJohn Baldwin #include <sys/vnode.h>
89940cb0e2SKonstantin Belousov #include <sys/unistd.h>
909696feebSJohn Baldwin #include <sys/user.h>
918e38aeffSJohn Baldwin 
9215bcf785SRobert Watson #include <security/audit/audit.h>
938e38aeffSJohn Baldwin #include <security/mac/mac_framework.h>
948e38aeffSJohn Baldwin 
958e38aeffSJohn Baldwin #include <vm/vm.h>
968e38aeffSJohn Baldwin #include <vm/vm_param.h>
978e38aeffSJohn Baldwin #include <vm/pmap.h>
98338e7cf2SJohn Baldwin #include <vm/vm_extern.h>
998e38aeffSJohn Baldwin #include <vm/vm_map.h>
100fb680e16SJohn Baldwin #include <vm/vm_kern.h>
1018e38aeffSJohn Baldwin #include <vm/vm_object.h>
1028e38aeffSJohn Baldwin #include <vm/vm_page.h>
1032971897dSAlan Cox #include <vm/vm_pageout.h>
1048e38aeffSJohn Baldwin #include <vm/vm_pager.h>
1058e38aeffSJohn Baldwin #include <vm/swap_pager.h>
1068e38aeffSJohn Baldwin 
1078e38aeffSJohn Baldwin struct shm_mapping {
1088e38aeffSJohn Baldwin 	char		*sm_path;
1098e38aeffSJohn Baldwin 	Fnv32_t		sm_fnv;
1108e38aeffSJohn Baldwin 	struct shmfd	*sm_shmfd;
1118e38aeffSJohn Baldwin 	LIST_ENTRY(shm_mapping) sm_link;
1128e38aeffSJohn Baldwin };
1138e38aeffSJohn Baldwin 
1148e38aeffSJohn Baldwin static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
1158e38aeffSJohn Baldwin static LIST_HEAD(, shm_mapping) *shm_dictionary;
1168e38aeffSJohn Baldwin static struct sx shm_dict_lock;
1178e38aeffSJohn Baldwin static struct mtx shm_timestamp_lock;
1188e38aeffSJohn Baldwin static u_long shm_hash;
1197883ce1fSMateusz Guzik static struct unrhdr64 shm_ino_unr;
120610a2b3cSJohn Baldwin static dev_t shm_dev_ino;
1218e38aeffSJohn Baldwin 
1228e38aeffSJohn Baldwin #define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
1238e38aeffSJohn Baldwin 
1245be725d7SAndreas Tobler static void	shm_init(void *arg);
1258e38aeffSJohn Baldwin static void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
1268e38aeffSJohn Baldwin static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
1278e38aeffSJohn Baldwin static int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
1287060da62SJamie Gritton static void	shm_doremove(struct shm_mapping *map);
129d301b358SKonstantin Belousov static int	shm_dotruncate_cookie(struct shmfd *shmfd, off_t length,
130d301b358SKonstantin Belousov     void *rl_cookie);
131af755d3eSKyle Evans static int	shm_dotruncate_locked(struct shmfd *shmfd, off_t length,
132af755d3eSKyle Evans     void *rl_cookie);
1332d5603feSDavid Bright static int	shm_copyin_path(struct thread *td, const char *userpath_in,
1342d5603feSDavid Bright     char **path_out);
135454bc887SKa Ho Ng static int	shm_deallocate(struct shmfd *shmfd, off_t *offset,
136454bc887SKa Ho Ng     off_t *length, int flags);
1378e38aeffSJohn Baldwin 
1388e38aeffSJohn Baldwin static fo_rdwr_t	shm_read;
1398e38aeffSJohn Baldwin static fo_rdwr_t	shm_write;
1408e38aeffSJohn Baldwin static fo_truncate_t	shm_truncate;
1412b64ab22SMark Johnston static fo_ioctl_t	shm_ioctl;
1428e38aeffSJohn Baldwin static fo_stat_t	shm_stat;
1438e38aeffSJohn Baldwin static fo_close_t	shm_close;
1449c00bb91SKonstantin Belousov static fo_chmod_t	shm_chmod;
1459c00bb91SKonstantin Belousov static fo_chown_t	shm_chown;
146940cb0e2SKonstantin Belousov static fo_seek_t	shm_seek;
1479696feebSJohn Baldwin static fo_fill_kinfo_t	shm_fill_kinfo;
1487077c426SJohn Baldwin static fo_mmap_t	shm_mmap;
149af755d3eSKyle Evans static fo_get_seals_t	shm_get_seals;
150af755d3eSKyle Evans static fo_add_seals_t	shm_add_seals;
151f1040532SKyle Evans static fo_fallocate_t	shm_fallocate;
152454bc887SKa Ho Ng static fo_fspacectl_t	shm_fspacectl;
1538e38aeffSJohn Baldwin 
1548e38aeffSJohn Baldwin /* File descriptor operations. */
1551bdbd705SKonstantin Belousov struct fileops shm_ops = {
1568e38aeffSJohn Baldwin 	.fo_read = shm_read,
1578e38aeffSJohn Baldwin 	.fo_write = shm_write,
1588e38aeffSJohn Baldwin 	.fo_truncate = shm_truncate,
1592b64ab22SMark Johnston 	.fo_ioctl = shm_ioctl,
1602d69d0dcSJohn Baldwin 	.fo_poll = invfo_poll,
1612d69d0dcSJohn Baldwin 	.fo_kqfilter = invfo_kqfilter,
1628e38aeffSJohn Baldwin 	.fo_stat = shm_stat,
1638e38aeffSJohn Baldwin 	.fo_close = shm_close,
1649c00bb91SKonstantin Belousov 	.fo_chmod = shm_chmod,
1659c00bb91SKonstantin Belousov 	.fo_chown = shm_chown,
166227aaa86SKonstantin Belousov 	.fo_sendfile = vn_sendfile,
167940cb0e2SKonstantin Belousov 	.fo_seek = shm_seek,
1689696feebSJohn Baldwin 	.fo_fill_kinfo = shm_fill_kinfo,
1697077c426SJohn Baldwin 	.fo_mmap = shm_mmap,
170af755d3eSKyle Evans 	.fo_get_seals = shm_get_seals,
171af755d3eSKyle Evans 	.fo_add_seals = shm_add_seals,
172f1040532SKyle Evans 	.fo_fallocate = shm_fallocate,
173454bc887SKa Ho Ng 	.fo_fspacectl = shm_fspacectl,
174d301b358SKonstantin Belousov 	.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE,
1758e38aeffSJohn Baldwin };
1768e38aeffSJohn Baldwin 
1778e38aeffSJohn Baldwin FEATURE(posix_shm, "POSIX shared memory");
1788e38aeffSJohn Baldwin 
179d301b358SKonstantin Belousov static SYSCTL_NODE(_vm, OID_AUTO, largepages, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
180d301b358SKonstantin Belousov     "");
181d301b358SKonstantin Belousov 
182d301b358SKonstantin Belousov static int largepage_reclaim_tries = 1;
183d301b358SKonstantin Belousov SYSCTL_INT(_vm_largepages, OID_AUTO, reclaim_tries,
184d301b358SKonstantin Belousov     CTLFLAG_RWTUN, &largepage_reclaim_tries, 0,
185d301b358SKonstantin Belousov     "Number of contig reclaims before giving up for default alloc policy");
186d301b358SKonstantin Belousov 
1878e38aeffSJohn Baldwin static int
18841cf41fdSKonstantin Belousov uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio)
18941cf41fdSKonstantin Belousov {
19041cf41fdSKonstantin Belousov 	vm_page_t m;
19141cf41fdSKonstantin Belousov 	vm_pindex_t idx;
19241cf41fdSKonstantin Belousov 	size_t tlen;
19341cf41fdSKonstantin Belousov 	int error, offset, rv;
19441cf41fdSKonstantin Belousov 
19541cf41fdSKonstantin Belousov 	idx = OFF_TO_IDX(uio->uio_offset);
19641cf41fdSKonstantin Belousov 	offset = uio->uio_offset & PAGE_MASK;
19741cf41fdSKonstantin Belousov 	tlen = MIN(PAGE_SIZE - offset, len);
19841cf41fdSKonstantin Belousov 
199f72eaaebSJeff Roberson 	rv = vm_page_grab_valid_unlocked(&m, obj, idx,
200f72eaaebSJeff Roberson 	    VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | VM_ALLOC_NOCREAT);
201f72eaaebSJeff Roberson 	if (rv == VM_PAGER_OK)
202f72eaaebSJeff Roberson 		goto found;
20341cf41fdSKonstantin Belousov 
20441cf41fdSKonstantin Belousov 	/*
2056311d7aaSWill Andrews 	 * Read I/O without either a corresponding resident page or swap
2066311d7aaSWill Andrews 	 * page: use zero_region.  This is intended to avoid instantiating
2076311d7aaSWill Andrews 	 * pages on read from a sparse region.
2086311d7aaSWill Andrews 	 */
209f72eaaebSJeff Roberson 	VM_OBJECT_WLOCK(obj);
210f72eaaebSJeff Roberson 	m = vm_page_lookup(obj, idx);
211f72eaaebSJeff Roberson 	if (uio->uio_rw == UIO_READ && m == NULL &&
2126311d7aaSWill Andrews 	    !vm_pager_has_page(obj, idx, NULL, NULL)) {
2136311d7aaSWill Andrews 		VM_OBJECT_WUNLOCK(obj);
214b9062c93SKonstantin Belousov 		return (uiomove(__DECONST(void *, zero_region), tlen, uio));
2156311d7aaSWill Andrews 	}
2166311d7aaSWill Andrews 
2176311d7aaSWill Andrews 	/*
21841cf41fdSKonstantin Belousov 	 * Although the tmpfs vnode lock is held here, it is
21941cf41fdSKonstantin Belousov 	 * nonetheless safe to sleep waiting for a free page.  The
22041cf41fdSKonstantin Belousov 	 * pageout daemon does not need to acquire the tmpfs vnode
22141cf41fdSKonstantin Belousov 	 * lock to page out tobj's pages because tobj is a OBJT_SWAP
22241cf41fdSKonstantin Belousov 	 * type object.
22341cf41fdSKonstantin Belousov 	 */
224c7575748SJeff Roberson 	rv = vm_page_grab_valid(&m, obj, idx,
225a8081778SJeff Roberson 	    VM_ALLOC_NORMAL | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY);
22641cf41fdSKonstantin Belousov 	if (rv != VM_PAGER_OK) {
22741cf41fdSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
2287ec4b29bSKonstantin Belousov 		if (bootverbose) {
2297ec4b29bSKonstantin Belousov 			printf("uiomove_object: vm_obj %p idx %jd "
2307ec4b29bSKonstantin Belousov 			    "pager error %d\n", obj, idx, rv);
2317ec4b29bSKonstantin Belousov 		}
23237aea264SKonstantin Belousov 		return (rv == VM_PAGER_AGAIN ? ENOSPC : EIO);
23341cf41fdSKonstantin Belousov 	}
23441cf41fdSKonstantin Belousov 	VM_OBJECT_WUNLOCK(obj);
235f72eaaebSJeff Roberson 
236f72eaaebSJeff Roberson found:
23741cf41fdSKonstantin Belousov 	error = uiomove_fromphys(&m, offset, tlen, uio);
238a8081778SJeff Roberson 	if (uio->uio_rw == UIO_WRITE && error == 0)
239a8081778SJeff Roberson 		vm_page_set_dirty(m);
240d29f674fSJeff Roberson 	vm_page_activate(m);
241a8081778SJeff Roberson 	vm_page_sunbusy(m);
24241cf41fdSKonstantin Belousov 
24341cf41fdSKonstantin Belousov 	return (error);
24441cf41fdSKonstantin Belousov }
24541cf41fdSKonstantin Belousov 
24641cf41fdSKonstantin Belousov int
24741cf41fdSKonstantin Belousov uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio)
24841cf41fdSKonstantin Belousov {
24941cf41fdSKonstantin Belousov 	ssize_t resid;
25041cf41fdSKonstantin Belousov 	size_t len;
25141cf41fdSKonstantin Belousov 	int error;
25241cf41fdSKonstantin Belousov 
25341cf41fdSKonstantin Belousov 	error = 0;
25441cf41fdSKonstantin Belousov 	while ((resid = uio->uio_resid) > 0) {
25541cf41fdSKonstantin Belousov 		if (obj_size <= uio->uio_offset)
25641cf41fdSKonstantin Belousov 			break;
25741cf41fdSKonstantin Belousov 		len = MIN(obj_size - uio->uio_offset, resid);
25841cf41fdSKonstantin Belousov 		if (len == 0)
25941cf41fdSKonstantin Belousov 			break;
26041cf41fdSKonstantin Belousov 		error = uiomove_object_page(obj, len, uio);
26141cf41fdSKonstantin Belousov 		if (error != 0 || resid == uio->uio_resid)
26241cf41fdSKonstantin Belousov 			break;
26341cf41fdSKonstantin Belousov 	}
26441cf41fdSKonstantin Belousov 	return (error);
26541cf41fdSKonstantin Belousov }
26641cf41fdSKonstantin Belousov 
267d301b358SKonstantin Belousov static u_long count_largepages[MAXPAGESIZES];
268d301b358SKonstantin Belousov 
269d301b358SKonstantin Belousov static int
270d301b358SKonstantin Belousov shm_largepage_phys_populate(vm_object_t object, vm_pindex_t pidx,
271d301b358SKonstantin Belousov     int fault_type, vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
272d301b358SKonstantin Belousov {
2733b5331ddSKonstantin Belousov 	vm_page_t m __diagused;
274d301b358SKonstantin Belousov 	int psind;
275d301b358SKonstantin Belousov 
276d301b358SKonstantin Belousov 	psind = object->un_pager.phys.data_val;
277d301b358SKonstantin Belousov 	if (psind == 0 || pidx >= object->size)
278d301b358SKonstantin Belousov 		return (VM_PAGER_FAIL);
279d301b358SKonstantin Belousov 	*first = rounddown2(pidx, pagesizes[psind] / PAGE_SIZE);
280d301b358SKonstantin Belousov 
281d301b358SKonstantin Belousov 	/*
282d301b358SKonstantin Belousov 	 * We only busy the first page in the superpage run.  It is
283d301b358SKonstantin Belousov 	 * useless to busy whole run since we only remove full
284d301b358SKonstantin Belousov 	 * superpage, and it takes too long to busy e.g. 512 * 512 ==
285d301b358SKonstantin Belousov 	 * 262144 pages constituing 1G amd64 superage.
286d301b358SKonstantin Belousov 	 */
287d301b358SKonstantin Belousov 	m = vm_page_grab(object, *first, VM_ALLOC_NORMAL | VM_ALLOC_NOCREAT);
288d301b358SKonstantin Belousov 	MPASS(m != NULL);
289d301b358SKonstantin Belousov 
290d301b358SKonstantin Belousov 	*last = *first + atop(pagesizes[psind]) - 1;
291d301b358SKonstantin Belousov 	return (VM_PAGER_OK);
292d301b358SKonstantin Belousov }
293d301b358SKonstantin Belousov 
294d301b358SKonstantin Belousov static boolean_t
295d301b358SKonstantin Belousov shm_largepage_phys_haspage(vm_object_t object, vm_pindex_t pindex,
296d301b358SKonstantin Belousov     int *before, int *after)
297d301b358SKonstantin Belousov {
298d301b358SKonstantin Belousov 	int psind;
299d301b358SKonstantin Belousov 
300d301b358SKonstantin Belousov 	psind = object->un_pager.phys.data_val;
301d301b358SKonstantin Belousov 	if (psind == 0 || pindex >= object->size)
302d301b358SKonstantin Belousov 		return (FALSE);
303d301b358SKonstantin Belousov 	if (before != NULL) {
304d301b358SKonstantin Belousov 		*before = pindex - rounddown2(pindex, pagesizes[psind] /
305d301b358SKonstantin Belousov 		    PAGE_SIZE);
306d301b358SKonstantin Belousov 	}
307d301b358SKonstantin Belousov 	if (after != NULL) {
308d301b358SKonstantin Belousov 		*after = roundup2(pindex, pagesizes[psind] / PAGE_SIZE) -
309d301b358SKonstantin Belousov 		    pindex;
310d301b358SKonstantin Belousov 	}
311d301b358SKonstantin Belousov 	return (TRUE);
312d301b358SKonstantin Belousov }
313d301b358SKonstantin Belousov 
314d301b358SKonstantin Belousov static void
315d301b358SKonstantin Belousov shm_largepage_phys_ctor(vm_object_t object, vm_prot_t prot,
316d301b358SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
317d301b358SKonstantin Belousov {
318d301b358SKonstantin Belousov }
319d301b358SKonstantin Belousov 
320d301b358SKonstantin Belousov static void
321d301b358SKonstantin Belousov shm_largepage_phys_dtor(vm_object_t object)
322d301b358SKonstantin Belousov {
323d301b358SKonstantin Belousov 	int psind;
324d301b358SKonstantin Belousov 
325d301b358SKonstantin Belousov 	psind = object->un_pager.phys.data_val;
326d301b358SKonstantin Belousov 	if (psind != 0) {
327d301b358SKonstantin Belousov 		atomic_subtract_long(&count_largepages[psind],
328d301b358SKonstantin Belousov 		    object->size / (pagesizes[psind] / PAGE_SIZE));
329d301b358SKonstantin Belousov 		vm_wire_sub(object->size);
330d301b358SKonstantin Belousov 	} else {
331d301b358SKonstantin Belousov 		KASSERT(object->size == 0,
332d301b358SKonstantin Belousov 		    ("largepage phys obj %p not initialized bit size %#jx > 0",
333d301b358SKonstantin Belousov 		    object, (uintmax_t)object->size));
334d301b358SKonstantin Belousov 	}
335d301b358SKonstantin Belousov }
336d301b358SKonstantin Belousov 
337d474440aSKonstantin Belousov static const struct phys_pager_ops shm_largepage_phys_ops = {
338d301b358SKonstantin Belousov 	.phys_pg_populate =	shm_largepage_phys_populate,
339d301b358SKonstantin Belousov 	.phys_pg_haspage =	shm_largepage_phys_haspage,
340d301b358SKonstantin Belousov 	.phys_pg_ctor =		shm_largepage_phys_ctor,
341d301b358SKonstantin Belousov 	.phys_pg_dtor =		shm_largepage_phys_dtor,
342d301b358SKonstantin Belousov };
343d301b358SKonstantin Belousov 
344d301b358SKonstantin Belousov bool
345d301b358SKonstantin Belousov shm_largepage(struct shmfd *shmfd)
346d301b358SKonstantin Belousov {
347d301b358SKonstantin Belousov 	return (shmfd->shm_object->type == OBJT_PHYS);
348d301b358SKonstantin Belousov }
349d301b358SKonstantin Belousov 
3500919f29dSKonstantin Belousov static void
3510919f29dSKonstantin Belousov shm_pager_freespace(vm_object_t obj, vm_pindex_t start, vm_size_t size)
3520919f29dSKonstantin Belousov {
3530919f29dSKonstantin Belousov 	struct shmfd *shm;
3540919f29dSKonstantin Belousov 	vm_size_t c;
3550919f29dSKonstantin Belousov 
3560919f29dSKonstantin Belousov 	swap_pager_freespace(obj, start, size, &c);
3570919f29dSKonstantin Belousov 	if (c == 0)
3580919f29dSKonstantin Belousov 		return;
3590919f29dSKonstantin Belousov 
3600919f29dSKonstantin Belousov 	shm = obj->un_pager.swp.swp_priv;
3610919f29dSKonstantin Belousov 	if (shm == NULL)
3620919f29dSKonstantin Belousov 		return;
3630919f29dSKonstantin Belousov 	KASSERT(shm->shm_pages >= c,
3640919f29dSKonstantin Belousov 	    ("shm %p pages %jd free %jd", shm,
3650919f29dSKonstantin Belousov 	    (uintmax_t)shm->shm_pages, (uintmax_t)c));
3660919f29dSKonstantin Belousov 	shm->shm_pages -= c;
3670919f29dSKonstantin Belousov }
3680919f29dSKonstantin Belousov 
3690919f29dSKonstantin Belousov static void
3700919f29dSKonstantin Belousov shm_page_inserted(vm_object_t obj, vm_page_t m)
3710919f29dSKonstantin Belousov {
3720919f29dSKonstantin Belousov 	struct shmfd *shm;
3730919f29dSKonstantin Belousov 
3740919f29dSKonstantin Belousov 	shm = obj->un_pager.swp.swp_priv;
3750919f29dSKonstantin Belousov 	if (shm == NULL)
3760919f29dSKonstantin Belousov 		return;
3770919f29dSKonstantin Belousov 	if (!vm_pager_has_page(obj, m->pindex, NULL, NULL))
3780919f29dSKonstantin Belousov 		shm->shm_pages += 1;
3790919f29dSKonstantin Belousov }
3800919f29dSKonstantin Belousov 
3810919f29dSKonstantin Belousov static void
3820919f29dSKonstantin Belousov shm_page_removed(vm_object_t obj, vm_page_t m)
3830919f29dSKonstantin Belousov {
3840919f29dSKonstantin Belousov 	struct shmfd *shm;
3850919f29dSKonstantin Belousov 
3860919f29dSKonstantin Belousov 	shm = obj->un_pager.swp.swp_priv;
3870919f29dSKonstantin Belousov 	if (shm == NULL)
3880919f29dSKonstantin Belousov 		return;
3890919f29dSKonstantin Belousov 	if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) {
3900919f29dSKonstantin Belousov 		KASSERT(shm->shm_pages >= 1,
3910919f29dSKonstantin Belousov 		    ("shm %p pages %jd free 1", shm,
3920919f29dSKonstantin Belousov 		    (uintmax_t)shm->shm_pages));
3930919f29dSKonstantin Belousov 		shm->shm_pages -= 1;
3940919f29dSKonstantin Belousov 	}
3950919f29dSKonstantin Belousov }
3960919f29dSKonstantin Belousov 
3970919f29dSKonstantin Belousov static struct pagerops shm_swap_pager_ops = {
3980919f29dSKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_SWAP,
3990919f29dSKonstantin Belousov 	.pgo_freespace = shm_pager_freespace,
4000919f29dSKonstantin Belousov 	.pgo_page_inserted = shm_page_inserted,
4010919f29dSKonstantin Belousov 	.pgo_page_removed = shm_page_removed,
4020919f29dSKonstantin Belousov };
4030919f29dSKonstantin Belousov static int shmfd_pager_type = -1;
4040919f29dSKonstantin Belousov 
40541cf41fdSKonstantin Belousov static int
406940cb0e2SKonstantin Belousov shm_seek(struct file *fp, off_t offset, int whence, struct thread *td)
407940cb0e2SKonstantin Belousov {
408940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
409940cb0e2SKonstantin Belousov 	off_t foffset;
410940cb0e2SKonstantin Belousov 	int error;
411940cb0e2SKonstantin Belousov 
412940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
413940cb0e2SKonstantin Belousov 	foffset = foffset_lock(fp, 0);
414940cb0e2SKonstantin Belousov 	error = 0;
415940cb0e2SKonstantin Belousov 	switch (whence) {
416940cb0e2SKonstantin Belousov 	case L_INCR:
417940cb0e2SKonstantin Belousov 		if (foffset < 0 ||
418940cb0e2SKonstantin Belousov 		    (offset > 0 && foffset > OFF_MAX - offset)) {
419940cb0e2SKonstantin Belousov 			error = EOVERFLOW;
420940cb0e2SKonstantin Belousov 			break;
421940cb0e2SKonstantin Belousov 		}
422940cb0e2SKonstantin Belousov 		offset += foffset;
423940cb0e2SKonstantin Belousov 		break;
424940cb0e2SKonstantin Belousov 	case L_XTND:
425940cb0e2SKonstantin Belousov 		if (offset > 0 && shmfd->shm_size > OFF_MAX - offset) {
426940cb0e2SKonstantin Belousov 			error = EOVERFLOW;
427940cb0e2SKonstantin Belousov 			break;
428940cb0e2SKonstantin Belousov 		}
429940cb0e2SKonstantin Belousov 		offset += shmfd->shm_size;
430940cb0e2SKonstantin Belousov 		break;
431940cb0e2SKonstantin Belousov 	case L_SET:
432940cb0e2SKonstantin Belousov 		break;
433940cb0e2SKonstantin Belousov 	default:
434940cb0e2SKonstantin Belousov 		error = EINVAL;
435940cb0e2SKonstantin Belousov 	}
436940cb0e2SKonstantin Belousov 	if (error == 0) {
437940cb0e2SKonstantin Belousov 		if (offset < 0 || offset > shmfd->shm_size)
438940cb0e2SKonstantin Belousov 			error = EINVAL;
439940cb0e2SKonstantin Belousov 		else
4406f2b769cSJohn-Mark Gurney 			td->td_uretoff.tdu_off = offset;
441940cb0e2SKonstantin Belousov 	}
442940cb0e2SKonstantin Belousov 	foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
443940cb0e2SKonstantin Belousov 	return (error);
444940cb0e2SKonstantin Belousov }
445940cb0e2SKonstantin Belousov 
446940cb0e2SKonstantin Belousov static int
4478e38aeffSJohn Baldwin shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
4488e38aeffSJohn Baldwin     int flags, struct thread *td)
4498e38aeffSJohn Baldwin {
450940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
451940cb0e2SKonstantin Belousov 	void *rl_cookie;
452940cb0e2SKonstantin Belousov 	int error;
4538e38aeffSJohn Baldwin 
454940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
455940cb0e2SKonstantin Belousov #ifdef MAC
456940cb0e2SKonstantin Belousov 	error = mac_posixshm_check_read(active_cred, fp->f_cred, shmfd);
457940cb0e2SKonstantin Belousov 	if (error)
458940cb0e2SKonstantin Belousov 		return (error);
459940cb0e2SKonstantin Belousov #endif
4606ea906eeSJilles Tjoelker 	foffset_lock_uio(fp, uio, flags);
4616ea906eeSJilles Tjoelker 	rl_cookie = rangelock_rlock(&shmfd->shm_rl, uio->uio_offset,
4626ea906eeSJilles Tjoelker 	    uio->uio_offset + uio->uio_resid, &shmfd->shm_mtx);
463940cb0e2SKonstantin Belousov 	error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio);
464940cb0e2SKonstantin Belousov 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
465940cb0e2SKonstantin Belousov 	foffset_unlock_uio(fp, uio, flags);
466940cb0e2SKonstantin Belousov 	return (error);
4678e38aeffSJohn Baldwin }
4688e38aeffSJohn Baldwin 
4698e38aeffSJohn Baldwin static int
4708e38aeffSJohn Baldwin shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
4718e38aeffSJohn Baldwin     int flags, struct thread *td)
4728e38aeffSJohn Baldwin {
473940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
474940cb0e2SKonstantin Belousov 	void *rl_cookie;
475940cb0e2SKonstantin Belousov 	int error;
4763f07b9d9SKyle Evans 	off_t size;
4778e38aeffSJohn Baldwin 
478940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
479940cb0e2SKonstantin Belousov #ifdef MAC
480940cb0e2SKonstantin Belousov 	error = mac_posixshm_check_write(active_cred, fp->f_cred, shmfd);
481940cb0e2SKonstantin Belousov 	if (error)
482940cb0e2SKonstantin Belousov 		return (error);
483940cb0e2SKonstantin Belousov #endif
484d301b358SKonstantin Belousov 	if (shm_largepage(shmfd) && shmfd->shm_lp_psind == 0)
485d301b358SKonstantin Belousov 		return (EINVAL);
486940cb0e2SKonstantin Belousov 	foffset_lock_uio(fp, uio, flags);
4873f07b9d9SKyle Evans 	if (uio->uio_resid > OFF_MAX - uio->uio_offset) {
4883f07b9d9SKyle Evans 		/*
4893f07b9d9SKyle Evans 		 * Overflow is only an error if we're supposed to expand on
4903f07b9d9SKyle Evans 		 * write.  Otherwise, we'll just truncate the write to the
4913f07b9d9SKyle Evans 		 * size of the file, which can only grow up to OFF_MAX.
4923f07b9d9SKyle Evans 		 */
4933f07b9d9SKyle Evans 		if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0) {
4943f07b9d9SKyle Evans 			foffset_unlock_uio(fp, uio, flags);
4953f07b9d9SKyle Evans 			return (EFBIG);
4963f07b9d9SKyle Evans 		}
4973f07b9d9SKyle Evans 
4983f07b9d9SKyle Evans 		size = shmfd->shm_size;
4993f07b9d9SKyle Evans 	} else {
5003f07b9d9SKyle Evans 		size = uio->uio_offset + uio->uio_resid;
5013f07b9d9SKyle Evans 	}
502940cb0e2SKonstantin Belousov 	if ((flags & FOF_OFFSET) == 0) {
503940cb0e2SKonstantin Belousov 		rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
504940cb0e2SKonstantin Belousov 		    &shmfd->shm_mtx);
505940cb0e2SKonstantin Belousov 	} else {
506940cb0e2SKonstantin Belousov 		rl_cookie = rangelock_wlock(&shmfd->shm_rl, uio->uio_offset,
5073f07b9d9SKyle Evans 		    size, &shmfd->shm_mtx);
508940cb0e2SKonstantin Belousov 	}
5093f07b9d9SKyle Evans 	if ((shmfd->shm_seals & F_SEAL_WRITE) != 0) {
510af755d3eSKyle Evans 		error = EPERM;
5113f07b9d9SKyle Evans 	} else {
5123f07b9d9SKyle Evans 		error = 0;
5133f07b9d9SKyle Evans 		if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0 &&
5143f07b9d9SKyle Evans 		    size > shmfd->shm_size) {
51579783634SKonstantin Belousov 			error = shm_dotruncate_cookie(shmfd, size, rl_cookie);
5163f07b9d9SKyle Evans 		}
5173f07b9d9SKyle Evans 		if (error == 0)
5183f07b9d9SKyle Evans 			error = uiomove_object(shmfd->shm_object,
5193f07b9d9SKyle Evans 			    shmfd->shm_size, uio);
5203f07b9d9SKyle Evans 	}
521940cb0e2SKonstantin Belousov 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
522940cb0e2SKonstantin Belousov 	foffset_unlock_uio(fp, uio, flags);
523940cb0e2SKonstantin Belousov 	return (error);
5248e38aeffSJohn Baldwin }
5258e38aeffSJohn Baldwin 
5268e38aeffSJohn Baldwin static int
5278e38aeffSJohn Baldwin shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
5288e38aeffSJohn Baldwin     struct thread *td)
5298e38aeffSJohn Baldwin {
5308e38aeffSJohn Baldwin 	struct shmfd *shmfd;
5318e38aeffSJohn Baldwin #ifdef MAC
5328e38aeffSJohn Baldwin 	int error;
5338e38aeffSJohn Baldwin #endif
5348e38aeffSJohn Baldwin 
5358e38aeffSJohn Baldwin 	shmfd = fp->f_data;
5368e38aeffSJohn Baldwin #ifdef MAC
5378e38aeffSJohn Baldwin 	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
5388e38aeffSJohn Baldwin 	if (error)
5398e38aeffSJohn Baldwin 		return (error);
5408e38aeffSJohn Baldwin #endif
5413364c323SKonstantin Belousov 	return (shm_dotruncate(shmfd, length));
5428e38aeffSJohn Baldwin }
5438e38aeffSJohn Baldwin 
5442b64ab22SMark Johnston int
5452b64ab22SMark Johnston shm_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
5462b64ab22SMark Johnston     struct thread *td)
5472b64ab22SMark Johnston {
548d301b358SKonstantin Belousov 	struct shmfd *shmfd;
549d301b358SKonstantin Belousov 	struct shm_largepage_conf *conf;
550d301b358SKonstantin Belousov 	void *rl_cookie;
5512b64ab22SMark Johnston 
552d301b358SKonstantin Belousov 	shmfd = fp->f_data;
5532b64ab22SMark Johnston 	switch (com) {
5542b64ab22SMark Johnston 	case FIONBIO:
5552b64ab22SMark Johnston 	case FIOASYNC:
5562b64ab22SMark Johnston 		/*
5572b64ab22SMark Johnston 		 * Allow fcntl(fd, F_SETFL, O_NONBLOCK) to work,
5582b64ab22SMark Johnston 		 * just like it would on an unlinked regular file
5592b64ab22SMark Johnston 		 */
5602b64ab22SMark Johnston 		return (0);
561d301b358SKonstantin Belousov 	case FIOSSHMLPGCNF:
562d301b358SKonstantin Belousov 		if (!shm_largepage(shmfd))
563d301b358SKonstantin Belousov 			return (ENOTTY);
564d301b358SKonstantin Belousov 		conf = data;
565d301b358SKonstantin Belousov 		if (shmfd->shm_lp_psind != 0 &&
566d301b358SKonstantin Belousov 		    conf->psind != shmfd->shm_lp_psind)
567d301b358SKonstantin Belousov 			return (EINVAL);
568d301b358SKonstantin Belousov 		if (conf->psind <= 0 || conf->psind >= MAXPAGESIZES ||
569d301b358SKonstantin Belousov 		    pagesizes[conf->psind] == 0)
570d301b358SKonstantin Belousov 			return (EINVAL);
571d301b358SKonstantin Belousov 		if (conf->alloc_policy != SHM_LARGEPAGE_ALLOC_DEFAULT &&
572d301b358SKonstantin Belousov 		    conf->alloc_policy != SHM_LARGEPAGE_ALLOC_NOWAIT &&
573d301b358SKonstantin Belousov 		    conf->alloc_policy != SHM_LARGEPAGE_ALLOC_HARD)
574d301b358SKonstantin Belousov 			return (EINVAL);
575d301b358SKonstantin Belousov 
576d301b358SKonstantin Belousov 		rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
577d301b358SKonstantin Belousov 		    &shmfd->shm_mtx);
578d301b358SKonstantin Belousov 		shmfd->shm_lp_psind = conf->psind;
579d301b358SKonstantin Belousov 		shmfd->shm_lp_alloc_policy = conf->alloc_policy;
580d301b358SKonstantin Belousov 		shmfd->shm_object->un_pager.phys.data_val = conf->psind;
581d301b358SKonstantin Belousov 		rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
582d301b358SKonstantin Belousov 		return (0);
583d301b358SKonstantin Belousov 	case FIOGSHMLPGCNF:
584d301b358SKonstantin Belousov 		if (!shm_largepage(shmfd))
585d301b358SKonstantin Belousov 			return (ENOTTY);
586d301b358SKonstantin Belousov 		conf = data;
587d301b358SKonstantin Belousov 		rl_cookie = rangelock_rlock(&shmfd->shm_rl, 0, OFF_MAX,
588d301b358SKonstantin Belousov 		    &shmfd->shm_mtx);
589d301b358SKonstantin Belousov 		conf->psind = shmfd->shm_lp_psind;
590d301b358SKonstantin Belousov 		conf->alloc_policy = shmfd->shm_lp_alloc_policy;
591d301b358SKonstantin Belousov 		rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
592d301b358SKonstantin Belousov 		return (0);
5932b64ab22SMark Johnston 	default:
5942b64ab22SMark Johnston 		return (ENOTTY);
5952b64ab22SMark Johnston 	}
5962b64ab22SMark Johnston }
5972b64ab22SMark Johnston 
5988e38aeffSJohn Baldwin static int
5992b68eb8eSMateusz Guzik shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
6008e38aeffSJohn Baldwin {
6018e38aeffSJohn Baldwin 	struct shmfd *shmfd;
6028e38aeffSJohn Baldwin #ifdef MAC
6038e38aeffSJohn Baldwin 	int error;
6048e38aeffSJohn Baldwin #endif
6058e38aeffSJohn Baldwin 
6068e38aeffSJohn Baldwin 	shmfd = fp->f_data;
6078e38aeffSJohn Baldwin 
6088e38aeffSJohn Baldwin #ifdef MAC
6098e38aeffSJohn Baldwin 	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
6108e38aeffSJohn Baldwin 	if (error)
6118e38aeffSJohn Baldwin 		return (error);
6128e38aeffSJohn Baldwin #endif
6138e38aeffSJohn Baldwin 
6148e38aeffSJohn Baldwin 	/*
6158e38aeffSJohn Baldwin 	 * Attempt to return sanish values for fstat() on a memory file
6168e38aeffSJohn Baldwin 	 * descriptor.
6178e38aeffSJohn Baldwin 	 */
6188e38aeffSJohn Baldwin 	bzero(sb, sizeof(*sb));
6198e38aeffSJohn Baldwin 	sb->st_blksize = PAGE_SIZE;
6208e38aeffSJohn Baldwin 	sb->st_size = shmfd->shm_size;
6219c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
622510ea843SEd Schouten 	sb->st_atim = shmfd->shm_atime;
623510ea843SEd Schouten 	sb->st_ctim = shmfd->shm_ctime;
624510ea843SEd Schouten 	sb->st_mtim = shmfd->shm_mtime;
625510ea843SEd Schouten 	sb->st_birthtim = shmfd->shm_birthtime;
6269c00bb91SKonstantin Belousov 	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
6278e38aeffSJohn Baldwin 	sb->st_uid = shmfd->shm_uid;
6288e38aeffSJohn Baldwin 	sb->st_gid = shmfd->shm_gid;
6299c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
630610a2b3cSJohn Baldwin 	sb->st_dev = shm_dev_ino;
631610a2b3cSJohn Baldwin 	sb->st_ino = shmfd->shm_ino;
632e4b77548SKonstantin Belousov 	sb->st_nlink = shmfd->shm_object->ref_count;
6330919f29dSKonstantin Belousov 	if (shm_largepage(shmfd)) {
634d301b358SKonstantin Belousov 		sb->st_blocks = shmfd->shm_object->size /
635d301b358SKonstantin Belousov 		    (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT);
6360919f29dSKonstantin Belousov 	} else {
6370919f29dSKonstantin Belousov 		sb->st_blocks = shmfd->shm_pages;
6380919f29dSKonstantin Belousov 	}
6398e38aeffSJohn Baldwin 
6408e38aeffSJohn Baldwin 	return (0);
6418e38aeffSJohn Baldwin }
6428e38aeffSJohn Baldwin 
6438e38aeffSJohn Baldwin static int
6448e38aeffSJohn Baldwin shm_close(struct file *fp, struct thread *td)
6458e38aeffSJohn Baldwin {
6468e38aeffSJohn Baldwin 	struct shmfd *shmfd;
6478e38aeffSJohn Baldwin 
6488e38aeffSJohn Baldwin 	shmfd = fp->f_data;
6498e38aeffSJohn Baldwin 	fp->f_data = NULL;
6508e38aeffSJohn Baldwin 	shm_drop(shmfd);
6518e38aeffSJohn Baldwin 
6528e38aeffSJohn Baldwin 	return (0);
6538e38aeffSJohn Baldwin }
6548e38aeffSJohn Baldwin 
655af755d3eSKyle Evans static int
6562d5603feSDavid Bright shm_copyin_path(struct thread *td, const char *userpath_in, char **path_out) {
6572d5603feSDavid Bright 	int error;
6582d5603feSDavid Bright 	char *path;
6592d5603feSDavid Bright 	const char *pr_path;
6602d5603feSDavid Bright 	size_t pr_pathlen;
6612d5603feSDavid Bright 
6622d5603feSDavid Bright 	path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
6632d5603feSDavid Bright 	pr_path = td->td_ucred->cr_prison->pr_path;
6642d5603feSDavid Bright 
6652d5603feSDavid Bright 	/* Construct a full pathname for jailed callers. */
6662d5603feSDavid Bright 	pr_pathlen = strcmp(pr_path, "/") ==
6672d5603feSDavid Bright 	    0 ? 0 : strlcpy(path, pr_path, MAXPATHLEN);
6682d5603feSDavid Bright 	error = copyinstr(userpath_in, path + pr_pathlen,
6692d5603feSDavid Bright 	    MAXPATHLEN - pr_pathlen, NULL);
6702d5603feSDavid Bright 	if (error != 0)
6712d5603feSDavid Bright 		goto out;
6722d5603feSDavid Bright 
6732d5603feSDavid Bright #ifdef KTRACE
6742d5603feSDavid Bright 	if (KTRPOINT(curthread, KTR_NAMEI))
6752d5603feSDavid Bright 		ktrnamei(path);
6762d5603feSDavid Bright #endif
6772d5603feSDavid Bright 
6782d5603feSDavid Bright 	/* Require paths to start with a '/' character. */
6792d5603feSDavid Bright 	if (path[pr_pathlen] != '/') {
6802d5603feSDavid Bright 		error = EINVAL;
6812d5603feSDavid Bright 		goto out;
6822d5603feSDavid Bright 	}
6832d5603feSDavid Bright 
6842d5603feSDavid Bright 	*path_out = path;
6852d5603feSDavid Bright 
6862d5603feSDavid Bright out:
6872d5603feSDavid Bright 	if (error != 0)
6882d5603feSDavid Bright 		free(path, M_SHMFD);
6892d5603feSDavid Bright 
6902d5603feSDavid Bright 	return (error);
6912d5603feSDavid Bright }
6922d5603feSDavid Bright 
6932d5603feSDavid Bright static int
694454bc887SKa Ho Ng shm_partial_page_invalidate(vm_object_t object, vm_pindex_t idx, int base,
695454bc887SKa Ho Ng     int end)
696454bc887SKa Ho Ng {
697454bc887SKa Ho Ng 	vm_page_t m;
698454bc887SKa Ho Ng 	int rv;
699454bc887SKa Ho Ng 
700454bc887SKa Ho Ng 	VM_OBJECT_ASSERT_WLOCKED(object);
701454bc887SKa Ho Ng 	KASSERT(base >= 0, ("%s: base %d", __func__, base));
702454bc887SKa Ho Ng 	KASSERT(end - base <= PAGE_SIZE, ("%s: base %d end %d", __func__, base,
703454bc887SKa Ho Ng 	    end));
704454bc887SKa Ho Ng 
705454bc887SKa Ho Ng retry:
706454bc887SKa Ho Ng 	m = vm_page_grab(object, idx, VM_ALLOC_NOCREAT);
707454bc887SKa Ho Ng 	if (m != NULL) {
708454bc887SKa Ho Ng 		MPASS(vm_page_all_valid(m));
709454bc887SKa Ho Ng 	} else if (vm_pager_has_page(object, idx, NULL, NULL)) {
710454bc887SKa Ho Ng 		m = vm_page_alloc(object, idx,
711454bc887SKa Ho Ng 		    VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL);
712454bc887SKa Ho Ng 		if (m == NULL)
713454bc887SKa Ho Ng 			goto retry;
714454bc887SKa Ho Ng 		vm_object_pip_add(object, 1);
715454bc887SKa Ho Ng 		VM_OBJECT_WUNLOCK(object);
716454bc887SKa Ho Ng 		rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
717454bc887SKa Ho Ng 		VM_OBJECT_WLOCK(object);
718454bc887SKa Ho Ng 		vm_object_pip_wakeup(object);
719454bc887SKa Ho Ng 		if (rv == VM_PAGER_OK) {
720454bc887SKa Ho Ng 			/*
721454bc887SKa Ho Ng 			 * Since the page was not resident, and therefore not
722454bc887SKa Ho Ng 			 * recently accessed, immediately enqueue it for
723454bc887SKa Ho Ng 			 * asynchronous laundering.  The current operation is
724454bc887SKa Ho Ng 			 * not regarded as an access.
725454bc887SKa Ho Ng 			 */
726454bc887SKa Ho Ng 			vm_page_launder(m);
727454bc887SKa Ho Ng 		} else {
728454bc887SKa Ho Ng 			vm_page_free(m);
729454bc887SKa Ho Ng 			VM_OBJECT_WUNLOCK(object);
730454bc887SKa Ho Ng 			return (EIO);
731454bc887SKa Ho Ng 		}
732454bc887SKa Ho Ng 	}
733454bc887SKa Ho Ng 	if (m != NULL) {
734454bc887SKa Ho Ng 		pmap_zero_page_area(m, base, end - base);
735454bc887SKa Ho Ng 		KASSERT(vm_page_all_valid(m), ("%s: page %p is invalid",
736454bc887SKa Ho Ng 		    __func__, m));
737454bc887SKa Ho Ng 		vm_page_set_dirty(m);
738454bc887SKa Ho Ng 		vm_page_xunbusy(m);
739454bc887SKa Ho Ng 	}
740454bc887SKa Ho Ng 
741454bc887SKa Ho Ng 	return (0);
742454bc887SKa Ho Ng }
743454bc887SKa Ho Ng 
744454bc887SKa Ho Ng static int
745af755d3eSKyle Evans shm_dotruncate_locked(struct shmfd *shmfd, off_t length, void *rl_cookie)
7468e38aeffSJohn Baldwin {
7478e38aeffSJohn Baldwin 	vm_object_t object;
748454bc887SKa Ho Ng 	vm_pindex_t nobjsize;
7493364c323SKonstantin Belousov 	vm_ooffset_t delta;
750454bc887SKa Ho Ng 	int base, error;
7518e38aeffSJohn Baldwin 
7522a016de1SAlan Cox 	KASSERT(length >= 0, ("shm_dotruncate: length < 0"));
7538e38aeffSJohn Baldwin 	object = shmfd->shm_object;
754af755d3eSKyle Evans 	VM_OBJECT_ASSERT_WLOCKED(object);
755af755d3eSKyle Evans 	rangelock_cookie_assert(rl_cookie, RA_WLOCKED);
756af755d3eSKyle Evans 	if (length == shmfd->shm_size)
7573364c323SKonstantin Belousov 		return (0);
7588e38aeffSJohn Baldwin 	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
7598e38aeffSJohn Baldwin 
7608e38aeffSJohn Baldwin 	/* Are we shrinking?  If so, trim the end. */
7618e38aeffSJohn Baldwin 	if (length < shmfd->shm_size) {
762af755d3eSKyle Evans 		if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0)
763af755d3eSKyle Evans 			return (EPERM);
764af755d3eSKyle Evans 
765fb680e16SJohn Baldwin 		/*
766fb680e16SJohn Baldwin 		 * Disallow any requests to shrink the size if this
767fb680e16SJohn Baldwin 		 * object is mapped into the kernel.
768fb680e16SJohn Baldwin 		 */
769af755d3eSKyle Evans 		if (shmfd->shm_kmappings > 0)
770fb680e16SJohn Baldwin 			return (EBUSY);
7712971897dSAlan Cox 
7722971897dSAlan Cox 		/*
7732971897dSAlan Cox 		 * Zero the truncated part of the last page.
7742971897dSAlan Cox 		 */
7752971897dSAlan Cox 		base = length & PAGE_MASK;
7762971897dSAlan Cox 		if (base != 0) {
777454bc887SKa Ho Ng 			error = shm_partial_page_invalidate(object,
778454bc887SKa Ho Ng 			    OFF_TO_IDX(length), base, PAGE_SIZE);
779454bc887SKa Ho Ng 			if (error)
780454bc887SKa Ho Ng 				return (error);
7812971897dSAlan Cox 		}
7822a016de1SAlan Cox 		delta = IDX_TO_OFF(object->size - nobjsize);
7833364c323SKonstantin Belousov 
7848e38aeffSJohn Baldwin 		if (nobjsize < object->size)
7858e38aeffSJohn Baldwin 			vm_object_page_remove(object, nobjsize, object->size,
7866bbee8e2SAlan Cox 			    0);
7878e38aeffSJohn Baldwin 
7883364c323SKonstantin Belousov 		/* Free the swap accounted for shm */
789ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(delta, object->cred);
7903364c323SKonstantin Belousov 		object->charge -= delta;
7913364c323SKonstantin Belousov 	} else {
792af755d3eSKyle Evans 		if ((shmfd->shm_seals & F_SEAL_GROW) != 0)
793af755d3eSKyle Evans 			return (EPERM);
794af755d3eSKyle Evans 
7952a016de1SAlan Cox 		/* Try to reserve additional swap space. */
7962a016de1SAlan Cox 		delta = IDX_TO_OFF(nobjsize - object->size);
797af755d3eSKyle Evans 		if (!swap_reserve_by_cred(delta, object->cred))
7983364c323SKonstantin Belousov 			return (ENOMEM);
7993364c323SKonstantin Belousov 		object->charge += delta;
8008e38aeffSJohn Baldwin 	}
8018e38aeffSJohn Baldwin 	shmfd->shm_size = length;
8028e38aeffSJohn Baldwin 	mtx_lock(&shm_timestamp_lock);
8038e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_ctime);
8048e38aeffSJohn Baldwin 	shmfd->shm_mtime = shmfd->shm_ctime;
8058e38aeffSJohn Baldwin 	mtx_unlock(&shm_timestamp_lock);
8068e38aeffSJohn Baldwin 	object->size = nobjsize;
8073364c323SKonstantin Belousov 	return (0);
8088e38aeffSJohn Baldwin }
8098e38aeffSJohn Baldwin 
810d301b358SKonstantin Belousov static int
811d301b358SKonstantin Belousov shm_dotruncate_largepage(struct shmfd *shmfd, off_t length, void *rl_cookie)
812d301b358SKonstantin Belousov {
813d301b358SKonstantin Belousov 	vm_object_t object;
814d301b358SKonstantin Belousov 	vm_page_t m;
8153b5331ddSKonstantin Belousov 	vm_pindex_t newobjsz;
8163b5331ddSKonstantin Belousov 	vm_pindex_t oldobjsz __unused;
817d301b358SKonstantin Belousov 	int aflags, error, i, psind, try;
818d301b358SKonstantin Belousov 
819d301b358SKonstantin Belousov 	KASSERT(length >= 0, ("shm_dotruncate: length < 0"));
820d301b358SKonstantin Belousov 	object = shmfd->shm_object;
821d301b358SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
822d301b358SKonstantin Belousov 	rangelock_cookie_assert(rl_cookie, RA_WLOCKED);
823d301b358SKonstantin Belousov 
824d301b358SKonstantin Belousov 	oldobjsz = object->size;
825d301b358SKonstantin Belousov 	newobjsz = OFF_TO_IDX(length);
826d301b358SKonstantin Belousov 	if (length == shmfd->shm_size)
827d301b358SKonstantin Belousov 		return (0);
828d301b358SKonstantin Belousov 	psind = shmfd->shm_lp_psind;
829d301b358SKonstantin Belousov 	if (psind == 0 && length != 0)
830d301b358SKonstantin Belousov 		return (EINVAL);
831d301b358SKonstantin Belousov 	if ((length & (pagesizes[psind] - 1)) != 0)
832d301b358SKonstantin Belousov 		return (EINVAL);
833d301b358SKonstantin Belousov 
834d301b358SKonstantin Belousov 	if (length < shmfd->shm_size) {
835d301b358SKonstantin Belousov 		if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0)
836d301b358SKonstantin Belousov 			return (EPERM);
837d301b358SKonstantin Belousov 		if (shmfd->shm_kmappings > 0)
838d301b358SKonstantin Belousov 			return (EBUSY);
839d301b358SKonstantin Belousov 		return (ENOTSUP);	/* Pages are unmanaged. */
840d301b358SKonstantin Belousov #if 0
841d301b358SKonstantin Belousov 		vm_object_page_remove(object, newobjsz, oldobjsz, 0);
842d301b358SKonstantin Belousov 		object->size = newobjsz;
843d301b358SKonstantin Belousov 		shmfd->shm_size = length;
844d301b358SKonstantin Belousov 		return (0);
845d301b358SKonstantin Belousov #endif
846d301b358SKonstantin Belousov 	}
847d301b358SKonstantin Belousov 
84879783634SKonstantin Belousov 	if ((shmfd->shm_seals & F_SEAL_GROW) != 0)
84979783634SKonstantin Belousov 		return (EPERM);
85079783634SKonstantin Belousov 
851d301b358SKonstantin Belousov 	aflags = VM_ALLOC_NORMAL | VM_ALLOC_ZERO;
852d301b358SKonstantin Belousov 	if (shmfd->shm_lp_alloc_policy == SHM_LARGEPAGE_ALLOC_NOWAIT)
853d301b358SKonstantin Belousov 		aflags |= VM_ALLOC_WAITFAIL;
854d301b358SKonstantin Belousov 	try = 0;
855d301b358SKonstantin Belousov 
856d301b358SKonstantin Belousov 	/*
857d301b358SKonstantin Belousov 	 * Extend shmfd and object, keeping all already fully
858d301b358SKonstantin Belousov 	 * allocated large pages intact even on error, because dropped
859d301b358SKonstantin Belousov 	 * object lock might allowed mapping of them.
860d301b358SKonstantin Belousov 	 */
861d301b358SKonstantin Belousov 	while (object->size < newobjsz) {
862d301b358SKonstantin Belousov 		m = vm_page_alloc_contig(object, object->size, aflags,
863d301b358SKonstantin Belousov 		    pagesizes[psind] / PAGE_SIZE, 0, ~0,
864d301b358SKonstantin Belousov 		    pagesizes[psind], 0,
865d301b358SKonstantin Belousov 		    VM_MEMATTR_DEFAULT);
866d301b358SKonstantin Belousov 		if (m == NULL) {
867d301b358SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
868d301b358SKonstantin Belousov 			if (shmfd->shm_lp_alloc_policy ==
869d301b358SKonstantin Belousov 			    SHM_LARGEPAGE_ALLOC_NOWAIT ||
870d301b358SKonstantin Belousov 			    (shmfd->shm_lp_alloc_policy ==
871d301b358SKonstantin Belousov 			    SHM_LARGEPAGE_ALLOC_DEFAULT &&
872d301b358SKonstantin Belousov 			    try >= largepage_reclaim_tries)) {
873d301b358SKonstantin Belousov 				VM_OBJECT_WLOCK(object);
874d301b358SKonstantin Belousov 				return (ENOMEM);
875d301b358SKonstantin Belousov 			}
876d301b358SKonstantin Belousov 			error = vm_page_reclaim_contig(aflags,
877d301b358SKonstantin Belousov 			    pagesizes[psind] / PAGE_SIZE, 0, ~0,
878d301b358SKonstantin Belousov 			    pagesizes[psind], 0) ? 0 :
879d301b358SKonstantin Belousov 			    vm_wait_intr(object);
880d301b358SKonstantin Belousov 			if (error != 0) {
881d301b358SKonstantin Belousov 				VM_OBJECT_WLOCK(object);
882d301b358SKonstantin Belousov 				return (error);
883d301b358SKonstantin Belousov 			}
884d301b358SKonstantin Belousov 			try++;
885d301b358SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
886d301b358SKonstantin Belousov 			continue;
887d301b358SKonstantin Belousov 		}
888d301b358SKonstantin Belousov 		try = 0;
889d301b358SKonstantin Belousov 		for (i = 0; i < pagesizes[psind] / PAGE_SIZE; i++) {
890d301b358SKonstantin Belousov 			if ((m[i].flags & PG_ZERO) == 0)
891d301b358SKonstantin Belousov 				pmap_zero_page(&m[i]);
892d301b358SKonstantin Belousov 			vm_page_valid(&m[i]);
893d301b358SKonstantin Belousov 			vm_page_xunbusy(&m[i]);
894d301b358SKonstantin Belousov 		}
895d301b358SKonstantin Belousov 		object->size += OFF_TO_IDX(pagesizes[psind]);
896d301b358SKonstantin Belousov 		shmfd->shm_size += pagesizes[psind];
897d301b358SKonstantin Belousov 		atomic_add_long(&count_largepages[psind], 1);
898d301b358SKonstantin Belousov 		vm_wire_add(atop(pagesizes[psind]));
899d301b358SKonstantin Belousov 	}
900d301b358SKonstantin Belousov 	return (0);
901d301b358SKonstantin Belousov }
902d301b358SKonstantin Belousov 
903d301b358SKonstantin Belousov static int
904d301b358SKonstantin Belousov shm_dotruncate_cookie(struct shmfd *shmfd, off_t length, void *rl_cookie)
905d301b358SKonstantin Belousov {
906d301b358SKonstantin Belousov 	int error;
907d301b358SKonstantin Belousov 
908d301b358SKonstantin Belousov 	VM_OBJECT_WLOCK(shmfd->shm_object);
909d301b358SKonstantin Belousov 	error = shm_largepage(shmfd) ? shm_dotruncate_largepage(shmfd,
910d301b358SKonstantin Belousov 	    length, rl_cookie) : shm_dotruncate_locked(shmfd, length,
911d301b358SKonstantin Belousov 	    rl_cookie);
912d301b358SKonstantin Belousov 	VM_OBJECT_WUNLOCK(shmfd->shm_object);
913d301b358SKonstantin Belousov 	return (error);
914d301b358SKonstantin Belousov }
915d301b358SKonstantin Belousov 
916af755d3eSKyle Evans int
917af755d3eSKyle Evans shm_dotruncate(struct shmfd *shmfd, off_t length)
918af755d3eSKyle Evans {
919af755d3eSKyle Evans 	void *rl_cookie;
920af755d3eSKyle Evans 	int error;
921af755d3eSKyle Evans 
922af755d3eSKyle Evans 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
923af755d3eSKyle Evans 	    &shmfd->shm_mtx);
924d301b358SKonstantin Belousov 	error = shm_dotruncate_cookie(shmfd, length, rl_cookie);
925af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
926af755d3eSKyle Evans 	return (error);
927af755d3eSKyle Evans }
928af755d3eSKyle Evans 
9298e38aeffSJohn Baldwin /*
9308e38aeffSJohn Baldwin  * shmfd object management including creation and reference counting
9318e38aeffSJohn Baldwin  * routines.
9328e38aeffSJohn Baldwin  */
9331bdbd705SKonstantin Belousov struct shmfd *
934d301b358SKonstantin Belousov shm_alloc(struct ucred *ucred, mode_t mode, bool largepage)
9358e38aeffSJohn Baldwin {
9368e38aeffSJohn Baldwin 	struct shmfd *shmfd;
9370919f29dSKonstantin Belousov 	vm_object_t obj;
9388e38aeffSJohn Baldwin 
9398e38aeffSJohn Baldwin 	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
9408e38aeffSJohn Baldwin 	shmfd->shm_size = 0;
9418e38aeffSJohn Baldwin 	shmfd->shm_uid = ucred->cr_uid;
9428e38aeffSJohn Baldwin 	shmfd->shm_gid = ucred->cr_gid;
9438e38aeffSJohn Baldwin 	shmfd->shm_mode = mode;
944d301b358SKonstantin Belousov 	if (largepage) {
945d301b358SKonstantin Belousov 		shmfd->shm_object = phys_pager_allocate(NULL,
946d301b358SKonstantin Belousov 		    &shm_largepage_phys_ops, NULL, shmfd->shm_size,
947d301b358SKonstantin Belousov 		    VM_PROT_DEFAULT, 0, ucred);
948d301b358SKonstantin Belousov 		shmfd->shm_lp_alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT;
949d301b358SKonstantin Belousov 	} else {
9500919f29dSKonstantin Belousov 		obj = vm_pager_allocate(shmfd_pager_type, NULL,
9513364c323SKonstantin Belousov 		    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
9520919f29dSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
9530919f29dSKonstantin Belousov 		obj->un_pager.swp.swp_priv = shmfd;
9540919f29dSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
9550919f29dSKonstantin Belousov 		shmfd->shm_object = obj;
956d301b358SKonstantin Belousov 	}
9578e38aeffSJohn Baldwin 	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
9588e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_birthtime);
9598e38aeffSJohn Baldwin 	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
9608e38aeffSJohn Baldwin 	    shmfd->shm_birthtime;
9617883ce1fSMateusz Guzik 	shmfd->shm_ino = alloc_unr64(&shm_ino_unr);
9628e38aeffSJohn Baldwin 	refcount_init(&shmfd->shm_refs, 1);
963940cb0e2SKonstantin Belousov 	mtx_init(&shmfd->shm_mtx, "shmrl", NULL, MTX_DEF);
964940cb0e2SKonstantin Belousov 	rangelock_init(&shmfd->shm_rl);
9658e38aeffSJohn Baldwin #ifdef MAC
9668e38aeffSJohn Baldwin 	mac_posixshm_init(shmfd);
9678e38aeffSJohn Baldwin 	mac_posixshm_create(ucred, shmfd);
9688e38aeffSJohn Baldwin #endif
9698e38aeffSJohn Baldwin 
9708e38aeffSJohn Baldwin 	return (shmfd);
9718e38aeffSJohn Baldwin }
9728e38aeffSJohn Baldwin 
9731bdbd705SKonstantin Belousov struct shmfd *
9748e38aeffSJohn Baldwin shm_hold(struct shmfd *shmfd)
9758e38aeffSJohn Baldwin {
9768e38aeffSJohn Baldwin 
9778e38aeffSJohn Baldwin 	refcount_acquire(&shmfd->shm_refs);
9788e38aeffSJohn Baldwin 	return (shmfd);
9798e38aeffSJohn Baldwin }
9808e38aeffSJohn Baldwin 
9811bdbd705SKonstantin Belousov void
9828e38aeffSJohn Baldwin shm_drop(struct shmfd *shmfd)
9838e38aeffSJohn Baldwin {
9840919f29dSKonstantin Belousov 	vm_object_t obj;
9858e38aeffSJohn Baldwin 
9868e38aeffSJohn Baldwin 	if (refcount_release(&shmfd->shm_refs)) {
9878e38aeffSJohn Baldwin #ifdef MAC
9888e38aeffSJohn Baldwin 		mac_posixshm_destroy(shmfd);
9898e38aeffSJohn Baldwin #endif
990940cb0e2SKonstantin Belousov 		rangelock_destroy(&shmfd->shm_rl);
991940cb0e2SKonstantin Belousov 		mtx_destroy(&shmfd->shm_mtx);
9920919f29dSKonstantin Belousov 		obj = shmfd->shm_object;
9930919f29dSKonstantin Belousov 		if (!shm_largepage(shmfd)) {
9940919f29dSKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
9950919f29dSKonstantin Belousov 			obj->un_pager.swp.swp_priv = NULL;
9960919f29dSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
9970919f29dSKonstantin Belousov 		}
9980919f29dSKonstantin Belousov 		vm_object_deallocate(obj);
9998e38aeffSJohn Baldwin 		free(shmfd, M_SHMFD);
10008e38aeffSJohn Baldwin 	}
10018e38aeffSJohn Baldwin }
10028e38aeffSJohn Baldwin 
10038e38aeffSJohn Baldwin /*
10048e38aeffSJohn Baldwin  * Determine if the credentials have sufficient permissions for a
10058e38aeffSJohn Baldwin  * specified combination of FREAD and FWRITE.
10068e38aeffSJohn Baldwin  */
10071bdbd705SKonstantin Belousov int
10088e38aeffSJohn Baldwin shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
10098e38aeffSJohn Baldwin {
101015bc6b2bSEdward Tomasz Napierala 	accmode_t accmode;
10119c00bb91SKonstantin Belousov 	int error;
10128e38aeffSJohn Baldwin 
101315bc6b2bSEdward Tomasz Napierala 	accmode = 0;
10148e38aeffSJohn Baldwin 	if (flags & FREAD)
101515bc6b2bSEdward Tomasz Napierala 		accmode |= VREAD;
10168e38aeffSJohn Baldwin 	if (flags & FWRITE)
101715bc6b2bSEdward Tomasz Napierala 		accmode |= VWRITE;
10189c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
10199c00bb91SKonstantin Belousov 	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
1020d292b194SMateusz Guzik 	    accmode, ucred);
10219c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
10229c00bb91SKonstantin Belousov 	return (error);
10238e38aeffSJohn Baldwin }
10248e38aeffSJohn Baldwin 
10258e38aeffSJohn Baldwin static void
1026610a2b3cSJohn Baldwin shm_init(void *arg)
10278e38aeffSJohn Baldwin {
1028d301b358SKonstantin Belousov 	char name[32];
1029d301b358SKonstantin Belousov 	int i;
10308e38aeffSJohn Baldwin 
10318e38aeffSJohn Baldwin 	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
10328e38aeffSJohn Baldwin 	sx_init(&shm_dict_lock, "shm dictionary");
10338e38aeffSJohn Baldwin 	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
10347883ce1fSMateusz Guzik 	new_unrhdr64(&shm_ino_unr, 1);
1035610a2b3cSJohn Baldwin 	shm_dev_ino = devfs_alloc_cdp_inode();
1036610a2b3cSJohn Baldwin 	KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized"));
10370919f29dSKonstantin Belousov 	shmfd_pager_type = vm_pager_alloc_dyn_type(&shm_swap_pager_ops,
10380919f29dSKonstantin Belousov 	    OBJT_SWAP);
10390919f29dSKonstantin Belousov 	MPASS(shmfd_pager_type != -1);
1040d301b358SKonstantin Belousov 
1041d301b358SKonstantin Belousov 	for (i = 1; i < MAXPAGESIZES; i++) {
1042d301b358SKonstantin Belousov 		if (pagesizes[i] == 0)
1043d301b358SKonstantin Belousov 			break;
1044d301b358SKonstantin Belousov #define	M	(1024 * 1024)
1045d301b358SKonstantin Belousov #define	G	(1024 * M)
1046d301b358SKonstantin Belousov 		if (pagesizes[i] >= G)
1047d301b358SKonstantin Belousov 			snprintf(name, sizeof(name), "%luG", pagesizes[i] / G);
1048d301b358SKonstantin Belousov 		else if (pagesizes[i] >= M)
1049d301b358SKonstantin Belousov 			snprintf(name, sizeof(name), "%luM", pagesizes[i] / M);
1050d301b358SKonstantin Belousov 		else
1051d301b358SKonstantin Belousov 			snprintf(name, sizeof(name), "%lu", pagesizes[i]);
1052d301b358SKonstantin Belousov #undef G
1053d301b358SKonstantin Belousov #undef M
1054d301b358SKonstantin Belousov 		SYSCTL_ADD_ULONG(NULL, SYSCTL_STATIC_CHILDREN(_vm_largepages),
1055d301b358SKonstantin Belousov 		    OID_AUTO, name, CTLFLAG_RD, &count_largepages[i],
1056d301b358SKonstantin Belousov 		    "number of non-transient largepages allocated");
1057d301b358SKonstantin Belousov 	}
10588e38aeffSJohn Baldwin }
1059610a2b3cSJohn Baldwin SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL);
10608e38aeffSJohn Baldwin 
106125f44824SKonstantin Belousov /*
10627060da62SJamie Gritton  * Remove all shared memory objects that belong to a prison.
10637060da62SJamie Gritton  */
10647060da62SJamie Gritton void
10657060da62SJamie Gritton shm_remove_prison(struct prison *pr)
10667060da62SJamie Gritton {
10677060da62SJamie Gritton 	struct shm_mapping *shmm, *tshmm;
10687060da62SJamie Gritton 	u_long i;
10697060da62SJamie Gritton 
10707060da62SJamie Gritton 	sx_xlock(&shm_dict_lock);
10717060da62SJamie Gritton 	for (i = 0; i < shm_hash + 1; i++) {
10727060da62SJamie Gritton 		LIST_FOREACH_SAFE(shmm, &shm_dictionary[i], sm_link, tshmm) {
10737060da62SJamie Gritton 			if (shmm->sm_shmfd->shm_object->cred &&
10747060da62SJamie Gritton 			    shmm->sm_shmfd->shm_object->cred->cr_prison == pr)
10757060da62SJamie Gritton 				shm_doremove(shmm);
10767060da62SJamie Gritton 		}
10777060da62SJamie Gritton 	}
10787060da62SJamie Gritton 	sx_xunlock(&shm_dict_lock);
10797060da62SJamie Gritton }
10807060da62SJamie Gritton 
10817060da62SJamie Gritton /*
108225f44824SKonstantin Belousov  * Dictionary management.  We maintain an in-kernel dictionary to map
108325f44824SKonstantin Belousov  * paths to shmfd objects.  We use the FNV hash on the path to store
108425f44824SKonstantin Belousov  * the mappings in a hash table.
108525f44824SKonstantin Belousov  */
10868e38aeffSJohn Baldwin static struct shmfd *
10878e38aeffSJohn Baldwin shm_lookup(char *path, Fnv32_t fnv)
10888e38aeffSJohn Baldwin {
10898e38aeffSJohn Baldwin 	struct shm_mapping *map;
10908e38aeffSJohn Baldwin 
10918e38aeffSJohn Baldwin 	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
10928e38aeffSJohn Baldwin 		if (map->sm_fnv != fnv)
10938e38aeffSJohn Baldwin 			continue;
10948e38aeffSJohn Baldwin 		if (strcmp(map->sm_path, path) == 0)
10958e38aeffSJohn Baldwin 			return (map->sm_shmfd);
10968e38aeffSJohn Baldwin 	}
10978e38aeffSJohn Baldwin 
10988e38aeffSJohn Baldwin 	return (NULL);
10998e38aeffSJohn Baldwin }
11008e38aeffSJohn Baldwin 
11018e38aeffSJohn Baldwin static void
11028e38aeffSJohn Baldwin shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
11038e38aeffSJohn Baldwin {
11048e38aeffSJohn Baldwin 	struct shm_mapping *map;
11058e38aeffSJohn Baldwin 
11068e38aeffSJohn Baldwin 	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
11078e38aeffSJohn Baldwin 	map->sm_path = path;
11088e38aeffSJohn Baldwin 	map->sm_fnv = fnv;
11098e38aeffSJohn Baldwin 	map->sm_shmfd = shm_hold(shmfd);
1110e506e182SJohn Baldwin 	shmfd->shm_path = path;
11118e38aeffSJohn Baldwin 	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
11128e38aeffSJohn Baldwin }
11138e38aeffSJohn Baldwin 
11148e38aeffSJohn Baldwin static int
11158e38aeffSJohn Baldwin shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
11168e38aeffSJohn Baldwin {
11178e38aeffSJohn Baldwin 	struct shm_mapping *map;
11188e38aeffSJohn Baldwin 	int error;
11198e38aeffSJohn Baldwin 
11208e38aeffSJohn Baldwin 	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
11218e38aeffSJohn Baldwin 		if (map->sm_fnv != fnv)
11228e38aeffSJohn Baldwin 			continue;
11238e38aeffSJohn Baldwin 		if (strcmp(map->sm_path, path) == 0) {
11248e38aeffSJohn Baldwin #ifdef MAC
11258e38aeffSJohn Baldwin 			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
11268e38aeffSJohn Baldwin 			if (error)
11278e38aeffSJohn Baldwin 				return (error);
11288e38aeffSJohn Baldwin #endif
11298e38aeffSJohn Baldwin 			error = shm_access(map->sm_shmfd, ucred,
11308e38aeffSJohn Baldwin 			    FREAD | FWRITE);
11318e38aeffSJohn Baldwin 			if (error)
11328e38aeffSJohn Baldwin 				return (error);
11337060da62SJamie Gritton 			shm_doremove(map);
11348e38aeffSJohn Baldwin 			return (0);
11358e38aeffSJohn Baldwin 		}
11368e38aeffSJohn Baldwin 	}
11378e38aeffSJohn Baldwin 
11388e38aeffSJohn Baldwin 	return (ENOENT);
11398e38aeffSJohn Baldwin }
11408e38aeffSJohn Baldwin 
11417060da62SJamie Gritton static void
11427060da62SJamie Gritton shm_doremove(struct shm_mapping *map)
11437060da62SJamie Gritton {
11447060da62SJamie Gritton 	map->sm_shmfd->shm_path = NULL;
11457060da62SJamie Gritton 	LIST_REMOVE(map, sm_link);
11467060da62SJamie Gritton 	shm_drop(map->sm_shmfd);
11477060da62SJamie Gritton 	free(map->sm_path, M_SHMFD);
11487060da62SJamie Gritton 	free(map, M_SHMFD);
11497060da62SJamie Gritton }
11507060da62SJamie Gritton 
11518e38aeffSJohn Baldwin int
1152535b1df9SKyle Evans kern_shm_open2(struct thread *td, const char *userpath, int flags, mode_t mode,
1153535b1df9SKyle Evans     int shmflags, struct filecaps *fcaps, const char *name __unused)
11548e38aeffSJohn Baldwin {
115585078b85SConrad Meyer 	struct pwddesc *pdp;
11568e38aeffSJohn Baldwin 	struct shmfd *shmfd;
11578e38aeffSJohn Baldwin 	struct file *fp;
11588e38aeffSJohn Baldwin 	char *path;
11590cd95859SKyle Evans 	void *rl_cookie;
11608e38aeffSJohn Baldwin 	Fnv32_t fnv;
11618e38aeffSJohn Baldwin 	mode_t cmode;
1162535b1df9SKyle Evans 	int error, fd, initial_seals;
1163d301b358SKonstantin Belousov 	bool largepage;
1164535b1df9SKyle Evans 
1165d301b358SKonstantin Belousov 	if ((shmflags & ~(SHM_ALLOW_SEALING | SHM_GROW_ON_WRITE |
1166d301b358SKonstantin Belousov 	    SHM_LARGEPAGE)) != 0)
1167535b1df9SKyle Evans 		return (EINVAL);
1168535b1df9SKyle Evans 
1169535b1df9SKyle Evans 	initial_seals = F_SEAL_SEAL;
1170535b1df9SKyle Evans 	if ((shmflags & SHM_ALLOW_SEALING) != 0)
1171535b1df9SKyle Evans 		initial_seals &= ~F_SEAL_SEAL;
11728e38aeffSJohn Baldwin 
117312bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
117412bc222eSJonathan Anderson 	/*
117512bc222eSJonathan Anderson 	 * shm_open(2) is only allowed for anonymous objects.
117612bc222eSJonathan Anderson 	 */
11777ee1b208SEd Schouten 	if (IN_CAPABILITY_MODE(td) && (userpath != SHM_ANON))
117812bc222eSJonathan Anderson 		return (ECAPMODE);
117912bc222eSJonathan Anderson #endif
118012bc222eSJonathan Anderson 
118115bcf785SRobert Watson 	AUDIT_ARG_FFLAGS(flags);
118215bcf785SRobert Watson 	AUDIT_ARG_MODE(mode);
118315bcf785SRobert Watson 
11847ee1b208SEd Schouten 	if ((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR)
11858e38aeffSJohn Baldwin 		return (EINVAL);
11868e38aeffSJohn Baldwin 
11877ee1b208SEd Schouten 	if ((flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC)) != 0)
11888e38aeffSJohn Baldwin 		return (EINVAL);
11898e38aeffSJohn Baldwin 
1190d301b358SKonstantin Belousov 	largepage = (shmflags & SHM_LARGEPAGE) != 0;
119178257765SMark Johnston 	if (largepage && !PMAP_HAS_LARGEPAGES)
1192d301b358SKonstantin Belousov 		return (ENOTTY);
1193d301b358SKonstantin Belousov 
11940cd95859SKyle Evans 	/*
11950cd95859SKyle Evans 	 * Currently only F_SEAL_SEAL may be set when creating or opening shmfd.
11960cd95859SKyle Evans 	 * If the decision is made later to allow additional seals, care must be
11970cd95859SKyle Evans 	 * taken below to ensure that the seals are properly set if the shmfd
11980cd95859SKyle Evans 	 * already existed -- this currently assumes that only F_SEAL_SEAL can
11990cd95859SKyle Evans 	 * be set and doesn't take further precautions to ensure the validity of
12000cd95859SKyle Evans 	 * the seals being added with respect to current mappings.
12010cd95859SKyle Evans 	 */
12020cd95859SKyle Evans 	if ((initial_seals & ~F_SEAL_SEAL) != 0)
12030cd95859SKyle Evans 		return (EINVAL);
12040cd95859SKyle Evans 
120585078b85SConrad Meyer 	pdp = td->td_proc->p_pd;
120685078b85SConrad Meyer 	cmode = (mode & ~pdp->pd_cmask) & ACCESSPERMS;
12078e38aeffSJohn Baldwin 
1208b5a7ac99SKyle Evans 	/*
1209b5a7ac99SKyle Evans 	 * shm_open(2) created shm should always have O_CLOEXEC set, as mandated
1210b5a7ac99SKyle Evans 	 * by POSIX.  We allow it to be unset here so that an in-kernel
1211b5a7ac99SKyle Evans 	 * interface may be written as a thin layer around shm, optionally not
1212b5a7ac99SKyle Evans 	 * setting CLOEXEC.  For shm_open(2), O_CLOEXEC is set unconditionally
1213b5a7ac99SKyle Evans 	 * in sys_shm_open() to keep this implementation compliant.
1214b5a7ac99SKyle Evans 	 */
1215b5a7ac99SKyle Evans 	error = falloc_caps(td, &fp, &fd, flags & O_CLOEXEC, fcaps);
12168e38aeffSJohn Baldwin 	if (error)
12178e38aeffSJohn Baldwin 		return (error);
12188e38aeffSJohn Baldwin 
12198e38aeffSJohn Baldwin 	/* A SHM_ANON path pointer creates an anonymous object. */
12207ee1b208SEd Schouten 	if (userpath == SHM_ANON) {
12218e38aeffSJohn Baldwin 		/* A read-only anonymous object is pointless. */
12227ee1b208SEd Schouten 		if ((flags & O_ACCMODE) == O_RDONLY) {
122390f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
12248e38aeffSJohn Baldwin 			fdrop(fp, td);
12258e38aeffSJohn Baldwin 			return (EINVAL);
12268e38aeffSJohn Baldwin 		}
1227d301b358SKonstantin Belousov 		shmfd = shm_alloc(td->td_ucred, cmode, largepage);
12280cd95859SKyle Evans 		shmfd->shm_seals = initial_seals;
12295dd47b52SKyle Evans 		shmfd->shm_flags = shmflags;
12308e38aeffSJohn Baldwin 	} else {
12312d5603feSDavid Bright 		error = shm_copyin_path(td, userpath, &path);
12322d5603feSDavid Bright 		if (error != 0) {
123390f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
12348e38aeffSJohn Baldwin 			fdrop(fp, td);
12358e38aeffSJohn Baldwin 			return (error);
12368e38aeffSJohn Baldwin 		}
12378e38aeffSJohn Baldwin 
123815bcf785SRobert Watson 		AUDIT_ARG_UPATH1_CANON(path);
12398e38aeffSJohn Baldwin 		fnv = fnv_32_str(path, FNV1_32_INIT);
12408e38aeffSJohn Baldwin 		sx_xlock(&shm_dict_lock);
12418e38aeffSJohn Baldwin 		shmfd = shm_lookup(path, fnv);
12428e38aeffSJohn Baldwin 		if (shmfd == NULL) {
12438e38aeffSJohn Baldwin 			/* Object does not yet exist, create it if requested. */
12447ee1b208SEd Schouten 			if (flags & O_CREAT) {
12459b6dd12eSRobert Watson #ifdef MAC
12469b6dd12eSRobert Watson 				error = mac_posixshm_check_create(td->td_ucred,
12479b6dd12eSRobert Watson 				    path);
12489b6dd12eSRobert Watson 				if (error == 0) {
12499b6dd12eSRobert Watson #endif
1250d301b358SKonstantin Belousov 					shmfd = shm_alloc(td->td_ucred, cmode,
1251d301b358SKonstantin Belousov 					    largepage);
12520cd95859SKyle Evans 					shmfd->shm_seals = initial_seals;
12535dd47b52SKyle Evans 					shmfd->shm_flags = shmflags;
12548e38aeffSJohn Baldwin 					shm_insert(path, fnv, shmfd);
12559b6dd12eSRobert Watson #ifdef MAC
12569b6dd12eSRobert Watson 				}
12579b6dd12eSRobert Watson #endif
12588e38aeffSJohn Baldwin 			} else {
12598e38aeffSJohn Baldwin 				free(path, M_SHMFD);
12608e38aeffSJohn Baldwin 				error = ENOENT;
12618e38aeffSJohn Baldwin 			}
12628e38aeffSJohn Baldwin 		} else {
12630cd95859SKyle Evans 			rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
12640cd95859SKyle Evans 			    &shmfd->shm_mtx);
12650cd95859SKyle Evans 
12660cd95859SKyle Evans 			/*
12670cd95859SKyle Evans 			 * kern_shm_open() likely shouldn't ever error out on
12680cd95859SKyle Evans 			 * trying to set a seal that already exists, unlike
12690cd95859SKyle Evans 			 * F_ADD_SEALS.  This would break terribly as
12700cd95859SKyle Evans 			 * shm_open(2) actually sets F_SEAL_SEAL to maintain
12710cd95859SKyle Evans 			 * historical behavior where the underlying file could
12720cd95859SKyle Evans 			 * not be sealed.
12730cd95859SKyle Evans 			 */
12740cd95859SKyle Evans 			initial_seals &= ~shmfd->shm_seals;
12750cd95859SKyle Evans 
12768e38aeffSJohn Baldwin 			/*
12778e38aeffSJohn Baldwin 			 * Object already exists, obtain a new
12788e38aeffSJohn Baldwin 			 * reference if requested and permitted.
12798e38aeffSJohn Baldwin 			 */
12808e38aeffSJohn Baldwin 			free(path, M_SHMFD);
12810cd95859SKyle Evans 
12820cd95859SKyle Evans 			/*
12830cd95859SKyle Evans 			 * initial_seals can't set additional seals if we've
12840cd95859SKyle Evans 			 * already been set F_SEAL_SEAL.  If F_SEAL_SEAL is set,
12850cd95859SKyle Evans 			 * then we've already removed that one from
12860cd95859SKyle Evans 			 * initial_seals.  This is currently redundant as we
12870cd95859SKyle Evans 			 * only allow setting F_SEAL_SEAL at creation time, but
12880cd95859SKyle Evans 			 * it's cheap to check and decreases the effort required
12890cd95859SKyle Evans 			 * to allow additional seals.
12900cd95859SKyle Evans 			 */
12910cd95859SKyle Evans 			if ((shmfd->shm_seals & F_SEAL_SEAL) != 0 &&
12920cd95859SKyle Evans 			    initial_seals != 0)
12930cd95859SKyle Evans 				error = EPERM;
12940cd95859SKyle Evans 			else if ((flags & (O_CREAT | O_EXCL)) ==
12950cd95859SKyle Evans 			    (O_CREAT | O_EXCL))
12968e38aeffSJohn Baldwin 				error = EEXIST;
12975dd47b52SKyle Evans 			else if (shmflags != 0 && shmflags != shmfd->shm_flags)
12985dd47b52SKyle Evans 				error = EINVAL;
12998e38aeffSJohn Baldwin 			else {
13008e38aeffSJohn Baldwin #ifdef MAC
13018e38aeffSJohn Baldwin 				error = mac_posixshm_check_open(td->td_ucred,
13027ee1b208SEd Schouten 				    shmfd, FFLAGS(flags & O_ACCMODE));
13038e38aeffSJohn Baldwin 				if (error == 0)
13048e38aeffSJohn Baldwin #endif
13058e38aeffSJohn Baldwin 				error = shm_access(shmfd, td->td_ucred,
13067ee1b208SEd Schouten 				    FFLAGS(flags & O_ACCMODE));
13078e38aeffSJohn Baldwin 			}
13088e38aeffSJohn Baldwin 
13098e38aeffSJohn Baldwin 			/*
13108e38aeffSJohn Baldwin 			 * Truncate the file back to zero length if
13118e38aeffSJohn Baldwin 			 * O_TRUNC was specified and the object was
13128e38aeffSJohn Baldwin 			 * opened with read/write.
13138e38aeffSJohn Baldwin 			 */
13148e38aeffSJohn Baldwin 			if (error == 0 &&
13157ee1b208SEd Schouten 			    (flags & (O_ACCMODE | O_TRUNC)) ==
13168e38aeffSJohn Baldwin 			    (O_RDWR | O_TRUNC)) {
13170cd95859SKyle Evans 				VM_OBJECT_WLOCK(shmfd->shm_object);
13188e38aeffSJohn Baldwin #ifdef MAC
13198e38aeffSJohn Baldwin 				error = mac_posixshm_check_truncate(
13208e38aeffSJohn Baldwin 					td->td_ucred, fp->f_cred, shmfd);
13218e38aeffSJohn Baldwin 				if (error == 0)
13228e38aeffSJohn Baldwin #endif
13230cd95859SKyle Evans 					error = shm_dotruncate_locked(shmfd, 0,
13240cd95859SKyle Evans 					    rl_cookie);
13250cd95859SKyle Evans 				VM_OBJECT_WUNLOCK(shmfd->shm_object);
13268e38aeffSJohn Baldwin 			}
13270cd95859SKyle Evans 			if (error == 0) {
13280cd95859SKyle Evans 				/*
13290cd95859SKyle Evans 				 * Currently we only allow F_SEAL_SEAL to be
13300cd95859SKyle Evans 				 * set initially.  As noted above, this would
13310cd95859SKyle Evans 				 * need to be reworked should that change.
13320cd95859SKyle Evans 				 */
13330cd95859SKyle Evans 				shmfd->shm_seals |= initial_seals;
13348e38aeffSJohn Baldwin 				shm_hold(shmfd);
13358e38aeffSJohn Baldwin 			}
13360cd95859SKyle Evans 			rangelock_unlock(&shmfd->shm_rl, rl_cookie,
13370cd95859SKyle Evans 			    &shmfd->shm_mtx);
13380cd95859SKyle Evans 		}
13398e38aeffSJohn Baldwin 		sx_xunlock(&shm_dict_lock);
13408e38aeffSJohn Baldwin 
13418e38aeffSJohn Baldwin 		if (error) {
134290f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
13438e38aeffSJohn Baldwin 			fdrop(fp, td);
13448e38aeffSJohn Baldwin 			return (error);
13458e38aeffSJohn Baldwin 		}
13468e38aeffSJohn Baldwin 	}
13478e38aeffSJohn Baldwin 
13487ee1b208SEd Schouten 	finit(fp, FFLAGS(flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
13498e38aeffSJohn Baldwin 
13508e38aeffSJohn Baldwin 	td->td_retval[0] = fd;
13518e38aeffSJohn Baldwin 	fdrop(fp, td);
13528e38aeffSJohn Baldwin 
13538e38aeffSJohn Baldwin 	return (0);
13548e38aeffSJohn Baldwin }
13558e38aeffSJohn Baldwin 
13567ee1b208SEd Schouten /* System calls. */
1357a9ac5e14SKyle Evans #ifdef COMPAT_FREEBSD12
13587ee1b208SEd Schouten int
1359a9ac5e14SKyle Evans freebsd12_shm_open(struct thread *td, struct freebsd12_shm_open_args *uap)
13607ee1b208SEd Schouten {
13617ee1b208SEd Schouten 
1362535b1df9SKyle Evans 	return (kern_shm_open(td, uap->path, uap->flags | O_CLOEXEC,
1363535b1df9SKyle Evans 	    uap->mode, NULL));
13647ee1b208SEd Schouten }
1365a9ac5e14SKyle Evans #endif
13667ee1b208SEd Schouten 
13678e38aeffSJohn Baldwin int
13688451d0ddSKip Macy sys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
13698e38aeffSJohn Baldwin {
13708e38aeffSJohn Baldwin 	char *path;
13718e38aeffSJohn Baldwin 	Fnv32_t fnv;
13728e38aeffSJohn Baldwin 	int error;
13738e38aeffSJohn Baldwin 
13742d5603feSDavid Bright 	error = shm_copyin_path(td, uap->path, &path);
13752d5603feSDavid Bright 	if (error != 0)
13768e38aeffSJohn Baldwin 		return (error);
13772d5603feSDavid Bright 
137815bcf785SRobert Watson 	AUDIT_ARG_UPATH1_CANON(path);
13798e38aeffSJohn Baldwin 	fnv = fnv_32_str(path, FNV1_32_INIT);
13808e38aeffSJohn Baldwin 	sx_xlock(&shm_dict_lock);
13818e38aeffSJohn Baldwin 	error = shm_remove(path, fnv, td->td_ucred);
13828e38aeffSJohn Baldwin 	sx_xunlock(&shm_dict_lock);
13834cf919edSMark Johnston 	free(path, M_SHMFD);
13848e38aeffSJohn Baldwin 
13858e38aeffSJohn Baldwin 	return (error);
13868e38aeffSJohn Baldwin }
13878e38aeffSJohn Baldwin 
13888e38aeffSJohn Baldwin int
13899afb12baSDavid Bright sys_shm_rename(struct thread *td, struct shm_rename_args *uap)
13909afb12baSDavid Bright {
13919afb12baSDavid Bright 	char *path_from = NULL, *path_to = NULL;
13929afb12baSDavid Bright 	Fnv32_t fnv_from, fnv_to;
13939afb12baSDavid Bright 	struct shmfd *fd_from;
13949afb12baSDavid Bright 	struct shmfd *fd_to;
13959afb12baSDavid Bright 	int error;
13969afb12baSDavid Bright 	int flags;
13979afb12baSDavid Bright 
13989afb12baSDavid Bright 	flags = uap->flags;
13992d5603feSDavid Bright 	AUDIT_ARG_FFLAGS(flags);
14009afb12baSDavid Bright 
14019afb12baSDavid Bright 	/*
14029afb12baSDavid Bright 	 * Make sure the user passed only valid flags.
14039afb12baSDavid Bright 	 * If you add a new flag, please add a new term here.
14049afb12baSDavid Bright 	 */
14059afb12baSDavid Bright 	if ((flags & ~(
14069afb12baSDavid Bright 	    SHM_RENAME_NOREPLACE |
14079afb12baSDavid Bright 	    SHM_RENAME_EXCHANGE
14089afb12baSDavid Bright 	    )) != 0) {
14099afb12baSDavid Bright 		error = EINVAL;
14109afb12baSDavid Bright 		goto out;
14119afb12baSDavid Bright 	}
14129afb12baSDavid Bright 
14139afb12baSDavid Bright 	/*
14149afb12baSDavid Bright 	 * EXCHANGE and NOREPLACE don't quite make sense together. Let's
14159afb12baSDavid Bright 	 * force the user to choose one or the other.
14169afb12baSDavid Bright 	 */
14179afb12baSDavid Bright 	if ((flags & SHM_RENAME_NOREPLACE) != 0 &&
14189afb12baSDavid Bright 	    (flags & SHM_RENAME_EXCHANGE) != 0) {
14199afb12baSDavid Bright 		error = EINVAL;
14209afb12baSDavid Bright 		goto out;
14219afb12baSDavid Bright 	}
14229afb12baSDavid Bright 
14232d5603feSDavid Bright 	/* Renaming to or from anonymous makes no sense */
14242d5603feSDavid Bright 	if (uap->path_from == SHM_ANON || uap->path_to == SHM_ANON) {
14252d5603feSDavid Bright 		error = EINVAL;
14262d5603feSDavid Bright 		goto out;
14272d5603feSDavid Bright 	}
14282d5603feSDavid Bright 
14292d5603feSDavid Bright 	error = shm_copyin_path(td, uap->path_from, &path_from);
14302d5603feSDavid Bright 	if (error != 0)
14319afb12baSDavid Bright 		goto out;
14329afb12baSDavid Bright 
14332d5603feSDavid Bright 	error = shm_copyin_path(td, uap->path_to, &path_to);
14342d5603feSDavid Bright 	if (error != 0)
14359afb12baSDavid Bright 		goto out;
14369afb12baSDavid Bright 
14372d5603feSDavid Bright 	AUDIT_ARG_UPATH1_CANON(path_from);
14382d5603feSDavid Bright 	AUDIT_ARG_UPATH2_CANON(path_to);
14392d5603feSDavid Bright 
14409afb12baSDavid Bright 	/* Rename with from/to equal is a no-op */
14412d5603feSDavid Bright 	if (strcmp(path_from, path_to) == 0)
14429afb12baSDavid Bright 		goto out;
14439afb12baSDavid Bright 
14449afb12baSDavid Bright 	fnv_from = fnv_32_str(path_from, FNV1_32_INIT);
14459afb12baSDavid Bright 	fnv_to = fnv_32_str(path_to, FNV1_32_INIT);
14469afb12baSDavid Bright 
14479afb12baSDavid Bright 	sx_xlock(&shm_dict_lock);
14489afb12baSDavid Bright 
14499afb12baSDavid Bright 	fd_from = shm_lookup(path_from, fnv_from);
14509afb12baSDavid Bright 	if (fd_from == NULL) {
14519afb12baSDavid Bright 		error = ENOENT;
14522d5603feSDavid Bright 		goto out_locked;
14539afb12baSDavid Bright 	}
14549afb12baSDavid Bright 
14559afb12baSDavid Bright 	fd_to = shm_lookup(path_to, fnv_to);
14569afb12baSDavid Bright 	if ((flags & SHM_RENAME_NOREPLACE) != 0 && fd_to != NULL) {
14579afb12baSDavid Bright 		error = EEXIST;
14582d5603feSDavid Bright 		goto out_locked;
14599afb12baSDavid Bright 	}
14609afb12baSDavid Bright 
14619afb12baSDavid Bright 	/*
14629afb12baSDavid Bright 	 * Unconditionally prevents shm_remove from invalidating the 'from'
14639afb12baSDavid Bright 	 * shm's state.
14649afb12baSDavid Bright 	 */
14659afb12baSDavid Bright 	shm_hold(fd_from);
14669afb12baSDavid Bright 	error = shm_remove(path_from, fnv_from, td->td_ucred);
14679afb12baSDavid Bright 
14689afb12baSDavid Bright 	/*
14699afb12baSDavid Bright 	 * One of my assumptions failed if ENOENT (e.g. locking didn't
14709afb12baSDavid Bright 	 * protect us)
14719afb12baSDavid Bright 	 */
14729afb12baSDavid Bright 	KASSERT(error != ENOENT, ("Our shm disappeared during shm_rename: %s",
14739afb12baSDavid Bright 	    path_from));
14742d5603feSDavid Bright 	if (error != 0) {
14759afb12baSDavid Bright 		shm_drop(fd_from);
14762d5603feSDavid Bright 		goto out_locked;
14779afb12baSDavid Bright 	}
14789afb12baSDavid Bright 
14799afb12baSDavid Bright 	/*
14809afb12baSDavid Bright 	 * If we are exchanging, we need to ensure the shm_remove below
14819afb12baSDavid Bright 	 * doesn't invalidate the dest shm's state.
14829afb12baSDavid Bright 	 */
14839afb12baSDavid Bright 	if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL)
14849afb12baSDavid Bright 		shm_hold(fd_to);
14859afb12baSDavid Bright 
14869afb12baSDavid Bright 	/*
14879afb12baSDavid Bright 	 * NOTE: if path_to is not already in the hash, c'est la vie;
14889afb12baSDavid Bright 	 * it simply means we have nothing already at path_to to unlink.
14899afb12baSDavid Bright 	 * That is the ENOENT case.
14909afb12baSDavid Bright 	 *
14919afb12baSDavid Bright 	 * If we somehow don't have access to unlink this guy, but
14929afb12baSDavid Bright 	 * did for the shm at path_from, then relink the shm to path_from
14939afb12baSDavid Bright 	 * and abort with EACCES.
14949afb12baSDavid Bright 	 *
14959afb12baSDavid Bright 	 * All other errors: that is weird; let's relink and abort the
14969afb12baSDavid Bright 	 * operation.
14979afb12baSDavid Bright 	 */
14989afb12baSDavid Bright 	error = shm_remove(path_to, fnv_to, td->td_ucred);
14992d5603feSDavid Bright 	if (error != 0 && error != ENOENT) {
15009afb12baSDavid Bright 		shm_insert(path_from, fnv_from, fd_from);
15019afb12baSDavid Bright 		shm_drop(fd_from);
15029afb12baSDavid Bright 		/* Don't free path_from now, since the hash references it */
15039afb12baSDavid Bright 		path_from = NULL;
15042d5603feSDavid Bright 		goto out_locked;
15059afb12baSDavid Bright 	}
15069afb12baSDavid Bright 
15072d5603feSDavid Bright 	error = 0;
15082d5603feSDavid Bright 
15099afb12baSDavid Bright 	shm_insert(path_to, fnv_to, fd_from);
15109afb12baSDavid Bright 
15119afb12baSDavid Bright 	/* Don't free path_to now, since the hash references it */
15129afb12baSDavid Bright 	path_to = NULL;
15139afb12baSDavid Bright 
15149afb12baSDavid Bright 	/* We kept a ref when we removed, and incremented again in insert */
15159afb12baSDavid Bright 	shm_drop(fd_from);
15169afb12baSDavid Bright 	KASSERT(fd_from->shm_refs > 0, ("Expected >0 refs; got: %d\n",
15179afb12baSDavid Bright 	    fd_from->shm_refs));
15189afb12baSDavid Bright 
15199afb12baSDavid Bright 	if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL) {
15209afb12baSDavid Bright 		shm_insert(path_from, fnv_from, fd_to);
15219afb12baSDavid Bright 		path_from = NULL;
15229afb12baSDavid Bright 		shm_drop(fd_to);
15239afb12baSDavid Bright 		KASSERT(fd_to->shm_refs > 0, ("Expected >0 refs; got: %d\n",
15249afb12baSDavid Bright 		    fd_to->shm_refs));
15259afb12baSDavid Bright 	}
15269afb12baSDavid Bright 
15272d5603feSDavid Bright out_locked:
15289afb12baSDavid Bright 	sx_xunlock(&shm_dict_lock);
15299afb12baSDavid Bright 
15309afb12baSDavid Bright out:
15319afb12baSDavid Bright 	free(path_from, M_SHMFD);
15329afb12baSDavid Bright 	free(path_to, M_SHMFD);
15339afb12baSDavid Bright 	return (error);
15349afb12baSDavid Bright }
15359afb12baSDavid Bright 
1536d301b358SKonstantin Belousov static int
1537d301b358SKonstantin Belousov shm_mmap_large(struct shmfd *shmfd, vm_map_t map, vm_offset_t *addr,
1538d301b358SKonstantin Belousov     vm_size_t size, vm_prot_t prot, vm_prot_t max_prot, int flags,
153979783634SKonstantin Belousov     vm_ooffset_t foff, struct thread *td)
1540d301b358SKonstantin Belousov {
1541d301b358SKonstantin Belousov 	struct vmspace *vms;
1542d301b358SKonstantin Belousov 	vm_map_entry_t next_entry, prev_entry;
1543d301b358SKonstantin Belousov 	vm_offset_t align, mask, maxaddr;
1544d301b358SKonstantin Belousov 	int docow, error, rv, try;
1545d301b358SKonstantin Belousov 	bool curmap;
1546d301b358SKonstantin Belousov 
1547d301b358SKonstantin Belousov 	if (shmfd->shm_lp_psind == 0)
1548d301b358SKonstantin Belousov 		return (EINVAL);
1549d301b358SKonstantin Belousov 
1550d301b358SKonstantin Belousov 	/* MAP_PRIVATE is disabled */
1551d301b358SKonstantin Belousov 	if ((flags & ~(MAP_SHARED | MAP_FIXED | MAP_EXCL |
1552d301b358SKonstantin Belousov 	    MAP_NOCORE |
1553d301b358SKonstantin Belousov #ifdef MAP_32BIT
1554d301b358SKonstantin Belousov 	    MAP_32BIT |
1555d301b358SKonstantin Belousov #endif
1556d301b358SKonstantin Belousov 	    MAP_ALIGNMENT_MASK)) != 0)
1557d301b358SKonstantin Belousov 		return (EINVAL);
1558d301b358SKonstantin Belousov 
1559d301b358SKonstantin Belousov 	vms = td->td_proc->p_vmspace;
1560d301b358SKonstantin Belousov 	curmap = map == &vms->vm_map;
1561d301b358SKonstantin Belousov 	if (curmap) {
1562d301b358SKonstantin Belousov 		error = kern_mmap_racct_check(td, map, size);
1563d301b358SKonstantin Belousov 		if (error != 0)
1564d301b358SKonstantin Belousov 			return (error);
1565d301b358SKonstantin Belousov 	}
1566d301b358SKonstantin Belousov 
1567d301b358SKonstantin Belousov 	docow = shmfd->shm_lp_psind << MAP_SPLIT_BOUNDARY_SHIFT;
1568d301b358SKonstantin Belousov 	docow |= MAP_INHERIT_SHARE;
1569d301b358SKonstantin Belousov 	if ((flags & MAP_NOCORE) != 0)
1570d301b358SKonstantin Belousov 		docow |= MAP_DISABLE_COREDUMP;
1571d301b358SKonstantin Belousov 
1572d301b358SKonstantin Belousov 	mask = pagesizes[shmfd->shm_lp_psind] - 1;
1573d301b358SKonstantin Belousov 	if ((foff & mask) != 0)
1574d301b358SKonstantin Belousov 		return (EINVAL);
1575d301b358SKonstantin Belousov 	maxaddr = vm_map_max(map);
1576d301b358SKonstantin Belousov #ifdef MAP_32BIT
1577d301b358SKonstantin Belousov 	if ((flags & MAP_32BIT) != 0 && maxaddr > MAP_32BIT_MAX_ADDR)
1578d301b358SKonstantin Belousov 		maxaddr = MAP_32BIT_MAX_ADDR;
1579d301b358SKonstantin Belousov #endif
1580d301b358SKonstantin Belousov 	if (size == 0 || (size & mask) != 0 ||
1581d301b358SKonstantin Belousov 	    (*addr != 0 && ((*addr & mask) != 0 ||
1582d301b358SKonstantin Belousov 	    *addr + size < *addr || *addr + size > maxaddr)))
1583d301b358SKonstantin Belousov 		return (EINVAL);
1584d301b358SKonstantin Belousov 
1585d301b358SKonstantin Belousov 	align = flags & MAP_ALIGNMENT_MASK;
1586d301b358SKonstantin Belousov 	if (align == 0) {
1587d301b358SKonstantin Belousov 		align = pagesizes[shmfd->shm_lp_psind];
1588d301b358SKonstantin Belousov 	} else if (align == MAP_ALIGNED_SUPER) {
1589d301b358SKonstantin Belousov 		if (shmfd->shm_lp_psind != 1)
1590d301b358SKonstantin Belousov 			return (EINVAL);
1591d301b358SKonstantin Belousov 		align = pagesizes[1];
1592d301b358SKonstantin Belousov 	} else {
1593d301b358SKonstantin Belousov 		align >>= MAP_ALIGNMENT_SHIFT;
1594d301b358SKonstantin Belousov 		align = 1ULL << align;
1595d301b358SKonstantin Belousov 		/* Also handles overflow. */
1596d301b358SKonstantin Belousov 		if (align < pagesizes[shmfd->shm_lp_psind])
1597d301b358SKonstantin Belousov 			return (EINVAL);
1598d301b358SKonstantin Belousov 	}
1599d301b358SKonstantin Belousov 
1600d301b358SKonstantin Belousov 	vm_map_lock(map);
1601d301b358SKonstantin Belousov 	if ((flags & MAP_FIXED) == 0) {
1602d301b358SKonstantin Belousov 		try = 1;
1603d301b358SKonstantin Belousov 		if (curmap && (*addr == 0 ||
1604d301b358SKonstantin Belousov 		    (*addr >= round_page((vm_offset_t)vms->vm_taddr) &&
1605d301b358SKonstantin Belousov 		    *addr < round_page((vm_offset_t)vms->vm_daddr +
1606d301b358SKonstantin Belousov 		    lim_max(td, RLIMIT_DATA))))) {
1607d301b358SKonstantin Belousov 			*addr = roundup2((vm_offset_t)vms->vm_daddr +
1608d301b358SKonstantin Belousov 			    lim_max(td, RLIMIT_DATA),
1609d301b358SKonstantin Belousov 			    pagesizes[shmfd->shm_lp_psind]);
1610d301b358SKonstantin Belousov 		}
1611d301b358SKonstantin Belousov again:
1612d301b358SKonstantin Belousov 		rv = vm_map_find_aligned(map, addr, size, maxaddr, align);
1613d301b358SKonstantin Belousov 		if (rv != KERN_SUCCESS) {
1614d301b358SKonstantin Belousov 			if (try == 1) {
1615d301b358SKonstantin Belousov 				try = 2;
1616d301b358SKonstantin Belousov 				*addr = vm_map_min(map);
1617d301b358SKonstantin Belousov 				if ((*addr & mask) != 0)
1618d301b358SKonstantin Belousov 					*addr = (*addr + mask) & mask;
1619d301b358SKonstantin Belousov 				goto again;
1620d301b358SKonstantin Belousov 			}
1621d301b358SKonstantin Belousov 			goto fail1;
1622d301b358SKonstantin Belousov 		}
1623d301b358SKonstantin Belousov 	} else if ((flags & MAP_EXCL) == 0) {
1624d301b358SKonstantin Belousov 		rv = vm_map_delete(map, *addr, *addr + size);
1625d301b358SKonstantin Belousov 		if (rv != KERN_SUCCESS)
1626d301b358SKonstantin Belousov 			goto fail1;
1627d301b358SKonstantin Belousov 	} else {
1628d301b358SKonstantin Belousov 		error = ENOSPC;
1629d301b358SKonstantin Belousov 		if (vm_map_lookup_entry(map, *addr, &prev_entry))
1630d301b358SKonstantin Belousov 			goto fail;
1631d301b358SKonstantin Belousov 		next_entry = vm_map_entry_succ(prev_entry);
1632d301b358SKonstantin Belousov 		if (next_entry->start < *addr + size)
1633d301b358SKonstantin Belousov 			goto fail;
1634d301b358SKonstantin Belousov 	}
1635d301b358SKonstantin Belousov 
1636d301b358SKonstantin Belousov 	rv = vm_map_insert(map, shmfd->shm_object, foff, *addr, *addr + size,
1637d301b358SKonstantin Belousov 	    prot, max_prot, docow);
1638d301b358SKonstantin Belousov fail1:
1639d301b358SKonstantin Belousov 	error = vm_mmap_to_errno(rv);
1640d301b358SKonstantin Belousov fail:
1641d301b358SKonstantin Belousov 	vm_map_unlock(map);
1642d301b358SKonstantin Belousov 	return (error);
1643d301b358SKonstantin Belousov }
1644d301b358SKonstantin Belousov 
1645d301b358SKonstantin Belousov static int
16467077c426SJohn Baldwin shm_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t objsize,
16477077c426SJohn Baldwin     vm_prot_t prot, vm_prot_t cap_maxprot, int flags,
16487077c426SJohn Baldwin     vm_ooffset_t foff, struct thread *td)
16498e38aeffSJohn Baldwin {
16507077c426SJohn Baldwin 	struct shmfd *shmfd;
16517077c426SJohn Baldwin 	vm_prot_t maxprot;
16527077c426SJohn Baldwin 	int error;
1653dca52ab4SKyle Evans 	bool writecnt;
1654af755d3eSKyle Evans 	void *rl_cookie;
16557077c426SJohn Baldwin 
16567077c426SJohn Baldwin 	shmfd = fp->f_data;
16577077c426SJohn Baldwin 	maxprot = VM_PROT_NONE;
16587077c426SJohn Baldwin 
1659af755d3eSKyle Evans 	rl_cookie = rangelock_rlock(&shmfd->shm_rl, 0, objsize,
1660af755d3eSKyle Evans 	    &shmfd->shm_mtx);
16617077c426SJohn Baldwin 	/* FREAD should always be set. */
16627077c426SJohn Baldwin 	if ((fp->f_flag & FREAD) != 0)
16637077c426SJohn Baldwin 		maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
166458366f05SKyle Evans 
166558366f05SKyle Evans 	/*
166658366f05SKyle Evans 	 * If FWRITE's set, we can allow VM_PROT_WRITE unless it's a shared
1667c7841c6bSMark Johnston 	 * mapping with a write seal applied.  Private mappings are always
1668c7841c6bSMark Johnston 	 * writeable.
166958366f05SKyle Evans 	 */
1670c7841c6bSMark Johnston 	if ((flags & MAP_SHARED) == 0) {
1671c7841c6bSMark Johnston 		cap_maxprot |= VM_PROT_WRITE;
16727077c426SJohn Baldwin 		maxprot |= VM_PROT_WRITE;
1673c7841c6bSMark Johnston 		writecnt = false;
1674c7841c6bSMark Johnston 	} else {
1675c7841c6bSMark Johnston 		if ((fp->f_flag & FWRITE) != 0 &&
1676c7841c6bSMark Johnston 		    (shmfd->shm_seals & F_SEAL_WRITE) == 0)
1677c7841c6bSMark Johnston 			maxprot |= VM_PROT_WRITE;
1678af755d3eSKyle Evans 
167951a16c84SKyle Evans 		/*
168051a16c84SKyle Evans 		 * Any mappings from a writable descriptor may be upgraded to
168151a16c84SKyle Evans 		 * VM_PROT_WRITE with mprotect(2), unless a write-seal was
168251a16c84SKyle Evans 		 * applied between the open and subsequent mmap(2).  We want to
168351a16c84SKyle Evans 		 * reject application of a write seal as long as any such
168451a16c84SKyle Evans 		 * mapping exists so that the seal cannot be trivially bypassed.
168551a16c84SKyle Evans 		 */
168651a16c84SKyle Evans 		writecnt = (maxprot & VM_PROT_WRITE) != 0;
168751a16c84SKyle Evans 		if (!writecnt && (prot & VM_PROT_WRITE) != 0) {
1688af755d3eSKyle Evans 			error = EACCES;
1689af755d3eSKyle Evans 			goto out;
1690af755d3eSKyle Evans 		}
1691c7841c6bSMark Johnston 	}
16927077c426SJohn Baldwin 	maxprot &= cap_maxprot;
16937077c426SJohn Baldwin 
1694987ff181SKonstantin Belousov 	/* See comment in vn_mmap(). */
1695987ff181SKonstantin Belousov 	if (
1696987ff181SKonstantin Belousov #ifdef _LP64
1697987ff181SKonstantin Belousov 	    objsize > OFF_MAX ||
1698987ff181SKonstantin Belousov #endif
1699f9cc8410SEric van Gyzen 	    foff > OFF_MAX - objsize) {
1700af755d3eSKyle Evans 		error = EINVAL;
1701af755d3eSKyle Evans 		goto out;
1702af755d3eSKyle Evans 	}
1703987ff181SKonstantin Belousov 
17047077c426SJohn Baldwin #ifdef MAC
17057077c426SJohn Baldwin 	error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, flags);
17067077c426SJohn Baldwin 	if (error != 0)
1707af755d3eSKyle Evans 		goto out;
17087077c426SJohn Baldwin #endif
17098e38aeffSJohn Baldwin 
17108e38aeffSJohn Baldwin 	mtx_lock(&shm_timestamp_lock);
17118e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_atime);
17128e38aeffSJohn Baldwin 	mtx_unlock(&shm_timestamp_lock);
17138e38aeffSJohn Baldwin 	vm_object_reference(shmfd->shm_object);
17147077c426SJohn Baldwin 
1715d301b358SKonstantin Belousov 	if (shm_largepage(shmfd)) {
171679783634SKonstantin Belousov 		writecnt = false;
1717d301b358SKonstantin Belousov 		error = shm_mmap_large(shmfd, map, addr, objsize, prot,
171879783634SKonstantin Belousov 		    maxprot, flags, foff, td);
1719d301b358SKonstantin Belousov 	} else {
172079783634SKonstantin Belousov 		if (writecnt) {
172179783634SKonstantin Belousov 			vm_pager_update_writecount(shmfd->shm_object, 0,
172279783634SKonstantin Belousov 			    objsize);
172379783634SKonstantin Belousov 		}
17247077c426SJohn Baldwin 		error = vm_mmap_object(map, addr, objsize, prot, maxprot, flags,
1725dca52ab4SKyle Evans 		    shmfd->shm_object, foff, writecnt, td);
1726d301b358SKonstantin Belousov 	}
1727dca52ab4SKyle Evans 	if (error != 0) {
1728dca52ab4SKyle Evans 		if (writecnt)
1729dca52ab4SKyle Evans 			vm_pager_release_writecount(shmfd->shm_object, 0,
1730dca52ab4SKyle Evans 			    objsize);
17317077c426SJohn Baldwin 		vm_object_deallocate(shmfd->shm_object);
1732dca52ab4SKyle Evans 	}
1733af755d3eSKyle Evans out:
1734af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
173534d3e89fSKonstantin Belousov 	return (error);
17368e38aeffSJohn Baldwin }
17379c00bb91SKonstantin Belousov 
17389c00bb91SKonstantin Belousov static int
17399c00bb91SKonstantin Belousov shm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
17409c00bb91SKonstantin Belousov     struct thread *td)
17419c00bb91SKonstantin Belousov {
17429c00bb91SKonstantin Belousov 	struct shmfd *shmfd;
17439c00bb91SKonstantin Belousov 	int error;
17449c00bb91SKonstantin Belousov 
17459c00bb91SKonstantin Belousov 	error = 0;
17469c00bb91SKonstantin Belousov 	shmfd = fp->f_data;
17479c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
17489c00bb91SKonstantin Belousov 	/*
17499c00bb91SKonstantin Belousov 	 * SUSv4 says that x bits of permission need not be affected.
17509c00bb91SKonstantin Belousov 	 * Be consistent with our shm_open there.
17519c00bb91SKonstantin Belousov 	 */
17529c00bb91SKonstantin Belousov #ifdef MAC
17539c00bb91SKonstantin Belousov 	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
17549c00bb91SKonstantin Belousov 	if (error != 0)
17559c00bb91SKonstantin Belousov 		goto out;
17569c00bb91SKonstantin Belousov #endif
1757d292b194SMateusz Guzik 	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
1758d292b194SMateusz Guzik 	    VADMIN, active_cred);
17599c00bb91SKonstantin Belousov 	if (error != 0)
17609c00bb91SKonstantin Belousov 		goto out;
17619c00bb91SKonstantin Belousov 	shmfd->shm_mode = mode & ACCESSPERMS;
17629c00bb91SKonstantin Belousov out:
17639c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
17649c00bb91SKonstantin Belousov 	return (error);
17659c00bb91SKonstantin Belousov }
17669c00bb91SKonstantin Belousov 
17679c00bb91SKonstantin Belousov static int
17689c00bb91SKonstantin Belousov shm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
17699c00bb91SKonstantin Belousov     struct thread *td)
17709c00bb91SKonstantin Belousov {
17719c00bb91SKonstantin Belousov 	struct shmfd *shmfd;
17729c00bb91SKonstantin Belousov 	int error;
17739c00bb91SKonstantin Belousov 
177468889ed6SKonstantin Belousov 	error = 0;
17759c00bb91SKonstantin Belousov 	shmfd = fp->f_data;
17769c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
17779c00bb91SKonstantin Belousov #ifdef MAC
17789c00bb91SKonstantin Belousov 	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
17799c00bb91SKonstantin Belousov 	if (error != 0)
17809c00bb91SKonstantin Belousov 		goto out;
17819c00bb91SKonstantin Belousov #endif
17829c00bb91SKonstantin Belousov 	if (uid == (uid_t)-1)
17839c00bb91SKonstantin Belousov 		uid = shmfd->shm_uid;
17849c00bb91SKonstantin Belousov 	if (gid == (gid_t)-1)
17859c00bb91SKonstantin Belousov                  gid = shmfd->shm_gid;
17869c00bb91SKonstantin Belousov 	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
17879c00bb91SKonstantin Belousov 	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
1788cc426dd3SMateusz Guzik 	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
17899c00bb91SKonstantin Belousov 		goto out;
17909c00bb91SKonstantin Belousov 	shmfd->shm_uid = uid;
17919c00bb91SKonstantin Belousov 	shmfd->shm_gid = gid;
17929c00bb91SKonstantin Belousov out:
17939c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
17949c00bb91SKonstantin Belousov 	return (error);
17959c00bb91SKonstantin Belousov }
1796fb680e16SJohn Baldwin 
1797fb680e16SJohn Baldwin /*
1798fb680e16SJohn Baldwin  * Helper routines to allow the backing object of a shared memory file
1799fb680e16SJohn Baldwin  * descriptor to be mapped in the kernel.
1800fb680e16SJohn Baldwin  */
1801fb680e16SJohn Baldwin int
1802fb680e16SJohn Baldwin shm_map(struct file *fp, size_t size, off_t offset, void **memp)
1803fb680e16SJohn Baldwin {
1804fb680e16SJohn Baldwin 	struct shmfd *shmfd;
1805fb680e16SJohn Baldwin 	vm_offset_t kva, ofs;
1806fb680e16SJohn Baldwin 	vm_object_t obj;
1807fb680e16SJohn Baldwin 	int rv;
1808fb680e16SJohn Baldwin 
1809fb680e16SJohn Baldwin 	if (fp->f_type != DTYPE_SHM)
1810fb680e16SJohn Baldwin 		return (EINVAL);
1811fb680e16SJohn Baldwin 	shmfd = fp->f_data;
1812fb680e16SJohn Baldwin 	obj = shmfd->shm_object;
181389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1814fb680e16SJohn Baldwin 	/*
1815fb680e16SJohn Baldwin 	 * XXXRW: This validation is probably insufficient, and subject to
1816fb680e16SJohn Baldwin 	 * sign errors.  It should be fixed.
1817fb680e16SJohn Baldwin 	 */
1818fb680e16SJohn Baldwin 	if (offset >= shmfd->shm_size ||
1819fb680e16SJohn Baldwin 	    offset + size > round_page(shmfd->shm_size)) {
182089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1821fb680e16SJohn Baldwin 		return (EINVAL);
1822fb680e16SJohn Baldwin 	}
1823fb680e16SJohn Baldwin 
1824fb680e16SJohn Baldwin 	shmfd->shm_kmappings++;
1825fb680e16SJohn Baldwin 	vm_object_reference_locked(obj);
182689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1827fb680e16SJohn Baldwin 
1828fb680e16SJohn Baldwin 	/* Map the object into the kernel_map and wire it. */
1829fb680e16SJohn Baldwin 	kva = vm_map_min(kernel_map);
1830fb680e16SJohn Baldwin 	ofs = offset & PAGE_MASK;
1831fb680e16SJohn Baldwin 	offset = trunc_page(offset);
1832fb680e16SJohn Baldwin 	size = round_page(size + ofs);
1833edb572a3SJohn Baldwin 	rv = vm_map_find(kernel_map, obj, offset, &kva, size, 0,
18345e3a17c0SJohn Baldwin 	    VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
1835fb680e16SJohn Baldwin 	    VM_PROT_READ | VM_PROT_WRITE, 0);
1836fb680e16SJohn Baldwin 	if (rv == KERN_SUCCESS) {
1837fb680e16SJohn Baldwin 		rv = vm_map_wire(kernel_map, kva, kva + size,
1838fb680e16SJohn Baldwin 		    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
1839fb680e16SJohn Baldwin 		if (rv == KERN_SUCCESS) {
1840fb680e16SJohn Baldwin 			*memp = (void *)(kva + ofs);
1841fb680e16SJohn Baldwin 			return (0);
1842fb680e16SJohn Baldwin 		}
1843fb680e16SJohn Baldwin 		vm_map_remove(kernel_map, kva, kva + size);
1844fb680e16SJohn Baldwin 	} else
1845fb680e16SJohn Baldwin 		vm_object_deallocate(obj);
1846fb680e16SJohn Baldwin 
1847fb680e16SJohn Baldwin 	/* On failure, drop our mapping reference. */
184889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1849fb680e16SJohn Baldwin 	shmfd->shm_kmappings--;
185089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1851fb680e16SJohn Baldwin 
1852338e7cf2SJohn Baldwin 	return (vm_mmap_to_errno(rv));
1853fb680e16SJohn Baldwin }
1854fb680e16SJohn Baldwin 
1855fb680e16SJohn Baldwin /*
1856fb680e16SJohn Baldwin  * We require the caller to unmap the entire entry.  This allows us to
1857fb680e16SJohn Baldwin  * safely decrement shm_kmappings when a mapping is removed.
1858fb680e16SJohn Baldwin  */
1859fb680e16SJohn Baldwin int
1860fb680e16SJohn Baldwin shm_unmap(struct file *fp, void *mem, size_t size)
1861fb680e16SJohn Baldwin {
1862fb680e16SJohn Baldwin 	struct shmfd *shmfd;
1863fb680e16SJohn Baldwin 	vm_map_entry_t entry;
1864fb680e16SJohn Baldwin 	vm_offset_t kva, ofs;
1865fb680e16SJohn Baldwin 	vm_object_t obj;
1866fb680e16SJohn Baldwin 	vm_pindex_t pindex;
1867fb680e16SJohn Baldwin 	vm_prot_t prot;
1868fb680e16SJohn Baldwin 	boolean_t wired;
1869fb680e16SJohn Baldwin 	vm_map_t map;
1870fb680e16SJohn Baldwin 	int rv;
1871fb680e16SJohn Baldwin 
1872fb680e16SJohn Baldwin 	if (fp->f_type != DTYPE_SHM)
1873fb680e16SJohn Baldwin 		return (EINVAL);
1874fb680e16SJohn Baldwin 	shmfd = fp->f_data;
1875fb680e16SJohn Baldwin 	kva = (vm_offset_t)mem;
1876fb680e16SJohn Baldwin 	ofs = kva & PAGE_MASK;
1877fb680e16SJohn Baldwin 	kva = trunc_page(kva);
1878fb680e16SJohn Baldwin 	size = round_page(size + ofs);
1879fb680e16SJohn Baldwin 	map = kernel_map;
1880fb680e16SJohn Baldwin 	rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
1881fb680e16SJohn Baldwin 	    &obj, &pindex, &prot, &wired);
1882fb680e16SJohn Baldwin 	if (rv != KERN_SUCCESS)
1883fb680e16SJohn Baldwin 		return (EINVAL);
1884fb680e16SJohn Baldwin 	if (entry->start != kva || entry->end != kva + size) {
1885fb680e16SJohn Baldwin 		vm_map_lookup_done(map, entry);
1886fb680e16SJohn Baldwin 		return (EINVAL);
1887fb680e16SJohn Baldwin 	}
1888fb680e16SJohn Baldwin 	vm_map_lookup_done(map, entry);
1889fb680e16SJohn Baldwin 	if (obj != shmfd->shm_object)
1890fb680e16SJohn Baldwin 		return (EINVAL);
1891fb680e16SJohn Baldwin 	vm_map_remove(map, kva, kva + size);
189289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1893fb680e16SJohn Baldwin 	KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
1894fb680e16SJohn Baldwin 	shmfd->shm_kmappings--;
189589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1896fb680e16SJohn Baldwin 	return (0);
1897fb680e16SJohn Baldwin }
1898e506e182SJohn Baldwin 
18999696feebSJohn Baldwin static int
190056d0e33eSKonstantin Belousov shm_fill_kinfo_locked(struct shmfd *shmfd, struct kinfo_file *kif, bool list)
1901e506e182SJohn Baldwin {
1902cc7b259aSJamie Gritton 	const char *path, *pr_path;
1903cc7b259aSJamie Gritton 	size_t pr_pathlen;
190456d0e33eSKonstantin Belousov 	bool visible;
1905e506e182SJohn Baldwin 
190656d0e33eSKonstantin Belousov 	sx_assert(&shm_dict_lock, SA_LOCKED);
19079696feebSJohn Baldwin 	kif->kf_type = KF_TYPE_SHM;
190856d0e33eSKonstantin Belousov 	kif->kf_un.kf_file.kf_file_mode = S_IFREG | shmfd->shm_mode;
19099696feebSJohn Baldwin 	kif->kf_un.kf_file.kf_file_size = shmfd->shm_size;
19109696feebSJohn Baldwin 	if (shmfd->shm_path != NULL) {
191144c16975SJamie Gritton 		if (shmfd->shm_path != NULL) {
1912cc7b259aSJamie Gritton 			path = shmfd->shm_path;
1913cc7b259aSJamie Gritton 			pr_path = curthread->td_ucred->cr_prison->pr_path;
191444c16975SJamie Gritton 			if (strcmp(pr_path, "/") != 0) {
191544c16975SJamie Gritton 				/* Return the jail-rooted pathname. */
1916cc7b259aSJamie Gritton 				pr_pathlen = strlen(pr_path);
191756d0e33eSKonstantin Belousov 				visible = strncmp(path, pr_path, pr_pathlen)
191856d0e33eSKonstantin Belousov 				    == 0 && path[pr_pathlen] == '/';
191956d0e33eSKonstantin Belousov 				if (list && !visible)
192056d0e33eSKonstantin Belousov 					return (EPERM);
192156d0e33eSKonstantin Belousov 				if (visible)
1922cc7b259aSJamie Gritton 					path += pr_pathlen;
1923cc7b259aSJamie Gritton 			}
1924cc7b259aSJamie Gritton 			strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
1925cc7b259aSJamie Gritton 		}
1926e506e182SJohn Baldwin 	}
19279696feebSJohn Baldwin 	return (0);
19289696feebSJohn Baldwin }
192956d0e33eSKonstantin Belousov 
193056d0e33eSKonstantin Belousov static int
193156d0e33eSKonstantin Belousov shm_fill_kinfo(struct file *fp, struct kinfo_file *kif,
193256d0e33eSKonstantin Belousov     struct filedesc *fdp __unused)
193356d0e33eSKonstantin Belousov {
193456d0e33eSKonstantin Belousov 	int res;
193556d0e33eSKonstantin Belousov 
193656d0e33eSKonstantin Belousov 	sx_slock(&shm_dict_lock);
193756d0e33eSKonstantin Belousov 	res = shm_fill_kinfo_locked(fp->f_data, kif, false);
193856d0e33eSKonstantin Belousov 	sx_sunlock(&shm_dict_lock);
193956d0e33eSKonstantin Belousov 	return (res);
194056d0e33eSKonstantin Belousov }
194156d0e33eSKonstantin Belousov 
194256d0e33eSKonstantin Belousov static int
1943af755d3eSKyle Evans shm_add_seals(struct file *fp, int seals)
1944af755d3eSKyle Evans {
1945af755d3eSKyle Evans 	struct shmfd *shmfd;
1946af755d3eSKyle Evans 	void *rl_cookie;
1947af755d3eSKyle Evans 	vm_ooffset_t writemappings;
1948af755d3eSKyle Evans 	int error, nseals;
1949af755d3eSKyle Evans 
1950af755d3eSKyle Evans 	error = 0;
1951af755d3eSKyle Evans 	shmfd = fp->f_data;
1952af755d3eSKyle Evans 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
1953af755d3eSKyle Evans 	    &shmfd->shm_mtx);
1954af755d3eSKyle Evans 
1955af755d3eSKyle Evans 	/* Even already-set seals should result in EPERM. */
1956af755d3eSKyle Evans 	if ((shmfd->shm_seals & F_SEAL_SEAL) != 0) {
1957af755d3eSKyle Evans 		error = EPERM;
1958af755d3eSKyle Evans 		goto out;
1959af755d3eSKyle Evans 	}
1960af755d3eSKyle Evans 	nseals = seals & ~shmfd->shm_seals;
1961af755d3eSKyle Evans 	if ((nseals & F_SEAL_WRITE) != 0) {
196279783634SKonstantin Belousov 		if (shm_largepage(shmfd)) {
196379783634SKonstantin Belousov 			error = ENOTSUP;
196479783634SKonstantin Belousov 			goto out;
196579783634SKonstantin Belousov 		}
196679783634SKonstantin Belousov 
1967af755d3eSKyle Evans 		/*
1968af755d3eSKyle Evans 		 * The rangelock above prevents writable mappings from being
1969af755d3eSKyle Evans 		 * added after we've started applying seals.  The RLOCK here
1970af755d3eSKyle Evans 		 * is to avoid torn reads on ILP32 arches as unmapping/reducing
1971af755d3eSKyle Evans 		 * writemappings will be done without a rangelock.
1972af755d3eSKyle Evans 		 */
1973af755d3eSKyle Evans 		VM_OBJECT_RLOCK(shmfd->shm_object);
1974af755d3eSKyle Evans 		writemappings = shmfd->shm_object->un_pager.swp.writemappings;
1975af755d3eSKyle Evans 		VM_OBJECT_RUNLOCK(shmfd->shm_object);
1976af755d3eSKyle Evans 		/* kmappings are also writable */
1977af755d3eSKyle Evans 		if (writemappings > 0) {
1978af755d3eSKyle Evans 			error = EBUSY;
1979af755d3eSKyle Evans 			goto out;
1980af755d3eSKyle Evans 		}
1981af755d3eSKyle Evans 	}
1982af755d3eSKyle Evans 	shmfd->shm_seals |= nseals;
1983af755d3eSKyle Evans out:
1984af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
1985af755d3eSKyle Evans 	return (error);
1986af755d3eSKyle Evans }
1987af755d3eSKyle Evans 
1988af755d3eSKyle Evans static int
1989af755d3eSKyle Evans shm_get_seals(struct file *fp, int *seals)
1990af755d3eSKyle Evans {
1991af755d3eSKyle Evans 	struct shmfd *shmfd;
1992af755d3eSKyle Evans 
1993af755d3eSKyle Evans 	shmfd = fp->f_data;
1994af755d3eSKyle Evans 	*seals = shmfd->shm_seals;
1995af755d3eSKyle Evans 	return (0);
1996af755d3eSKyle Evans }
1997af755d3eSKyle Evans 
1998af755d3eSKyle Evans static int
1999454bc887SKa Ho Ng shm_deallocate(struct shmfd *shmfd, off_t *offset, off_t *length, int flags)
2000454bc887SKa Ho Ng {
2001454bc887SKa Ho Ng 	vm_object_t object;
2002454bc887SKa Ho Ng 	vm_pindex_t pistart, pi, piend;
2003454bc887SKa Ho Ng 	vm_ooffset_t off, len;
2004454bc887SKa Ho Ng 	int startofs, endofs, end;
2005454bc887SKa Ho Ng 	int error;
2006454bc887SKa Ho Ng 
2007454bc887SKa Ho Ng 	off = *offset;
2008454bc887SKa Ho Ng 	len = *length;
2009454bc887SKa Ho Ng 	KASSERT(off + len <= (vm_ooffset_t)OFF_MAX, ("off + len overflows"));
20101eaa3652SKa Ho Ng 	if (off + len > shmfd->shm_size)
20111eaa3652SKa Ho Ng 		len = shmfd->shm_size - off;
2012454bc887SKa Ho Ng 	object = shmfd->shm_object;
2013454bc887SKa Ho Ng 	startofs = off & PAGE_MASK;
2014454bc887SKa Ho Ng 	endofs = (off + len) & PAGE_MASK;
2015454bc887SKa Ho Ng 	pistart = OFF_TO_IDX(off);
2016454bc887SKa Ho Ng 	piend = OFF_TO_IDX(off + len);
2017454bc887SKa Ho Ng 	pi = OFF_TO_IDX(off + PAGE_MASK);
2018454bc887SKa Ho Ng 	error = 0;
2019454bc887SKa Ho Ng 
20205c1428d2SKa Ho Ng 	/* Handle the case when offset is on or beyond shm size. */
20215c1428d2SKa Ho Ng 	if ((off_t)len <= 0) {
20221eaa3652SKa Ho Ng 		*length = 0;
20231eaa3652SKa Ho Ng 		return (0);
20241eaa3652SKa Ho Ng 	}
20251eaa3652SKa Ho Ng 
2026454bc887SKa Ho Ng 	VM_OBJECT_WLOCK(object);
2027454bc887SKa Ho Ng 
2028454bc887SKa Ho Ng 	if (startofs != 0) {
2029454bc887SKa Ho Ng 		end = pistart != piend ? PAGE_SIZE : endofs;
2030454bc887SKa Ho Ng 		error = shm_partial_page_invalidate(object, pistart, startofs,
2031454bc887SKa Ho Ng 		    end);
2032454bc887SKa Ho Ng 		if (error)
2033454bc887SKa Ho Ng 			goto out;
2034454bc887SKa Ho Ng 		off += end - startofs;
2035454bc887SKa Ho Ng 		len -= end - startofs;
2036454bc887SKa Ho Ng 	}
2037454bc887SKa Ho Ng 
2038454bc887SKa Ho Ng 	if (pi < piend) {
2039454bc887SKa Ho Ng 		vm_object_page_remove(object, pi, piend, 0);
2040454bc887SKa Ho Ng 		off += IDX_TO_OFF(piend - pi);
2041454bc887SKa Ho Ng 		len -= IDX_TO_OFF(piend - pi);
2042454bc887SKa Ho Ng 	}
2043454bc887SKa Ho Ng 
2044454bc887SKa Ho Ng 	if (endofs != 0 && pistart != piend) {
2045454bc887SKa Ho Ng 		error = shm_partial_page_invalidate(object, piend, 0, endofs);
2046454bc887SKa Ho Ng 		if (error)
2047454bc887SKa Ho Ng 			goto out;
2048454bc887SKa Ho Ng 		off += endofs;
2049454bc887SKa Ho Ng 		len -= endofs;
2050454bc887SKa Ho Ng 	}
2051454bc887SKa Ho Ng 
2052454bc887SKa Ho Ng out:
2053454bc887SKa Ho Ng 	VM_OBJECT_WUNLOCK(shmfd->shm_object);
2054454bc887SKa Ho Ng 	*offset = off;
2055454bc887SKa Ho Ng 	*length = len;
2056454bc887SKa Ho Ng 	return (error);
2057454bc887SKa Ho Ng }
2058454bc887SKa Ho Ng 
2059454bc887SKa Ho Ng static int
2060454bc887SKa Ho Ng shm_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length, int flags,
2061454bc887SKa Ho Ng     struct ucred *active_cred, struct thread *td)
2062454bc887SKa Ho Ng {
2063454bc887SKa Ho Ng 	void *rl_cookie;
2064454bc887SKa Ho Ng 	struct shmfd *shmfd;
2065454bc887SKa Ho Ng 	off_t off, len;
2066454bc887SKa Ho Ng 	int error;
2067454bc887SKa Ho Ng 
20688c9aa94bSKa Ho Ng 	KASSERT(cmd == SPACECTL_DEALLOC, ("shm_fspacectl: Invalid cmd"));
20698c9aa94bSKa Ho Ng 	KASSERT((flags & ~SPACECTL_F_SUPPORTED) == 0,
20708c9aa94bSKa Ho Ng 	    ("shm_fspacectl: non-zero flags"));
20718c9aa94bSKa Ho Ng 	KASSERT(*offset >= 0 && *length > 0 && *length <= OFF_MAX - *offset,
20728c9aa94bSKa Ho Ng 	    ("shm_fspacectl: offset/length overflow or underflow"));
2073454bc887SKa Ho Ng 	error = EINVAL;
2074454bc887SKa Ho Ng 	shmfd = fp->f_data;
2075454bc887SKa Ho Ng 	off = *offset;
2076454bc887SKa Ho Ng 	len = *length;
2077454bc887SKa Ho Ng 
2078454bc887SKa Ho Ng 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, off, off + len,
2079454bc887SKa Ho Ng 	    &shmfd->shm_mtx);
2080454bc887SKa Ho Ng 	switch (cmd) {
2081454bc887SKa Ho Ng 	case SPACECTL_DEALLOC:
2082454bc887SKa Ho Ng 		if ((shmfd->shm_seals & F_SEAL_WRITE) != 0) {
2083454bc887SKa Ho Ng 			error = EPERM;
2084454bc887SKa Ho Ng 			break;
2085454bc887SKa Ho Ng 		}
2086454bc887SKa Ho Ng 		error = shm_deallocate(shmfd, &off, &len, flags);
2087454bc887SKa Ho Ng 		*offset = off;
2088454bc887SKa Ho Ng 		*length = len;
2089454bc887SKa Ho Ng 		break;
2090454bc887SKa Ho Ng 	default:
2091454bc887SKa Ho Ng 		__assert_unreachable();
2092454bc887SKa Ho Ng 	}
2093454bc887SKa Ho Ng 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
2094454bc887SKa Ho Ng 	return (error);
2095454bc887SKa Ho Ng }
2096454bc887SKa Ho Ng 
2097454bc887SKa Ho Ng 
2098454bc887SKa Ho Ng static int
2099f1040532SKyle Evans shm_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td)
2100f1040532SKyle Evans {
2101f1040532SKyle Evans 	void *rl_cookie;
2102f1040532SKyle Evans 	struct shmfd *shmfd;
2103f1040532SKyle Evans 	size_t size;
2104f1040532SKyle Evans 	int error;
2105f1040532SKyle Evans 
2106f1040532SKyle Evans 	/* This assumes that the caller already checked for overflow. */
2107f1040532SKyle Evans 	error = 0;
2108f1040532SKyle Evans 	shmfd = fp->f_data;
2109f1040532SKyle Evans 	size = offset + len;
211039eae263SKyle Evans 
211139eae263SKyle Evans 	/*
211239eae263SKyle Evans 	 * Just grab the rangelock for the range that we may be attempting to
211339eae263SKyle Evans 	 * grow, rather than blocking read/write for regions we won't be
211439eae263SKyle Evans 	 * touching while this (potential) resize is in progress.  Other
211539eae263SKyle Evans 	 * attempts to resize the shmfd will have to take a write lock from 0 to
211639eae263SKyle Evans 	 * OFF_MAX, so this being potentially beyond the current usable range of
211739eae263SKyle Evans 	 * the shmfd is not necessarily a concern.  If other mechanisms are
211839eae263SKyle Evans 	 * added to grow a shmfd, this may need to be re-evaluated.
211939eae263SKyle Evans 	 */
212039eae263SKyle Evans 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, offset, size,
2121f1040532SKyle Evans 	    &shmfd->shm_mtx);
2122d301b358SKonstantin Belousov 	if (size > shmfd->shm_size)
2123d301b358SKonstantin Belousov 		error = shm_dotruncate_cookie(shmfd, size, rl_cookie);
2124f1040532SKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
2125f1040532SKyle Evans 	/* Translate to posix_fallocate(2) return value as needed. */
2126f1040532SKyle Evans 	if (error == ENOMEM)
2127f1040532SKyle Evans 		error = ENOSPC;
2128f1040532SKyle Evans 	return (error);
2129f1040532SKyle Evans }
2130f1040532SKyle Evans 
2131f1040532SKyle Evans static int
213256d0e33eSKonstantin Belousov sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS)
213356d0e33eSKonstantin Belousov {
213456d0e33eSKonstantin Belousov 	struct shm_mapping *shmm;
213556d0e33eSKonstantin Belousov 	struct sbuf sb;
213656d0e33eSKonstantin Belousov 	struct kinfo_file kif;
213756d0e33eSKonstantin Belousov 	u_long i;
213856d0e33eSKonstantin Belousov 	int error, error2;
213956d0e33eSKonstantin Belousov 
214056d0e33eSKonstantin Belousov 	sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file) * 5, req);
214156d0e33eSKonstantin Belousov 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
214256d0e33eSKonstantin Belousov 	error = 0;
214356d0e33eSKonstantin Belousov 	sx_slock(&shm_dict_lock);
214456d0e33eSKonstantin Belousov 	for (i = 0; i < shm_hash + 1; i++) {
214556d0e33eSKonstantin Belousov 		LIST_FOREACH(shmm, &shm_dictionary[i], sm_link) {
214656d0e33eSKonstantin Belousov 			error = shm_fill_kinfo_locked(shmm->sm_shmfd,
214756d0e33eSKonstantin Belousov 			    &kif, true);
2148747a4726SJamie Gritton 			if (error == EPERM) {
2149747a4726SJamie Gritton 				error = 0;
215056d0e33eSKonstantin Belousov 				continue;
2151747a4726SJamie Gritton 			}
215256d0e33eSKonstantin Belousov 			if (error != 0)
215356d0e33eSKonstantin Belousov 				break;
215456d0e33eSKonstantin Belousov 			pack_kinfo(&kif);
215556d0e33eSKonstantin Belousov 			error = sbuf_bcat(&sb, &kif, kif.kf_structsize) == 0 ?
215656d0e33eSKonstantin Belousov 			    0 : ENOMEM;
215756d0e33eSKonstantin Belousov 			if (error != 0)
215856d0e33eSKonstantin Belousov 				break;
215956d0e33eSKonstantin Belousov 		}
216056d0e33eSKonstantin Belousov 	}
216156d0e33eSKonstantin Belousov 	sx_sunlock(&shm_dict_lock);
216256d0e33eSKonstantin Belousov 	error2 = sbuf_finish(&sb);
216356d0e33eSKonstantin Belousov 	sbuf_delete(&sb);
216456d0e33eSKonstantin Belousov 	return (error != 0 ? error : error2);
216556d0e33eSKonstantin Belousov }
216656d0e33eSKonstantin Belousov 
216756d0e33eSKonstantin Belousov SYSCTL_PROC(_kern_ipc, OID_AUTO, posix_shm_list,
2168d7c4ea7dSJamie Gritton     CTLFLAG_RD | CTLFLAG_PRISON | CTLFLAG_MPSAFE | CTLTYPE_OPAQUE,
216956d0e33eSKonstantin Belousov     NULL, 0, sysctl_posix_shm_list, "",
217056d0e33eSKonstantin Belousov     "POSIX SHM list");
217120f70576SKyle Evans 
217220f70576SKyle Evans int
2173535b1df9SKyle Evans kern_shm_open(struct thread *td, const char *path, int flags, mode_t mode,
2174535b1df9SKyle Evans     struct filecaps *caps)
217520f70576SKyle Evans {
217620f70576SKyle Evans 
2177535b1df9SKyle Evans 	return (kern_shm_open2(td, path, flags, mode, 0, caps, NULL));
217820f70576SKyle Evans }
217920f70576SKyle Evans 
218020f70576SKyle Evans /*
218120f70576SKyle Evans  * This version of the shm_open() interface leaves CLOEXEC behavior up to the
218220f70576SKyle Evans  * caller, and libc will enforce it for the traditional shm_open() call.  This
218320f70576SKyle Evans  * allows other consumers, like memfd_create(), to opt-in for CLOEXEC.  This
218420f70576SKyle Evans  * interface also includes a 'name' argument that is currently unused, but could
218520f70576SKyle Evans  * potentially be exported later via some interface for debugging purposes.
218620f70576SKyle Evans  * From the kernel's perspective, it is optional.  Individual consumers like
218720f70576SKyle Evans  * memfd_create() may require it in order to be compatible with other systems
218820f70576SKyle Evans  * implementing the same function.
218920f70576SKyle Evans  */
219020f70576SKyle Evans int
219120f70576SKyle Evans sys_shm_open2(struct thread *td, struct shm_open2_args *uap)
219220f70576SKyle Evans {
219320f70576SKyle Evans 
219420f70576SKyle Evans 	return (kern_shm_open2(td, uap->path, uap->flags, uap->mode,
2195535b1df9SKyle Evans 	    uap->shmflags, NULL, uap->name));
219620f70576SKyle Evans }
2197