The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_shm.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006 Robert N. M. Watson
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * Support for shared swap-backed anonymous memory objects via
   29  * shm_open(2) and shm_unlink(2).  While most of the implementation is
   30  * here, vm_mmap.c contains mapping logic changes.
   31  *
   32  * TODO:
   33  *
   34  * (2) Need to export data to a userland tool via a sysctl.  Should ipcs(1)
   35  *     and ipcrm(1) be expanded or should new tools to manage both POSIX
   36  *     kernel semaphores and POSIX shared memory be written?
   37  *
   38  * (3) Add support for this file type to fstat(1).
   39  *
   40  * (4) Resource limits?  Does this need its own resource limits or are the
   41  *     existing limits in mmap(2) sufficient?
   42  *
   43  * (5) Partial page truncation.  vnode_pager_setsize() will zero any parts
   44  *     of a partially mapped page as a result of ftruncate(2)/truncate(2).
   45  *     We can do the same (with the same pmap evil), but do we need to
   46  *     worry about the bits on disk if the page is swapped out or will the
   47  *     swapper zero the parts of a page that are invalid if the page is
   48  *     swapped back in for us?
   49  *
   50  * (6) Add MAC support in mac_biba(4) and mac_mls(4).
   51  *
   52  * (7) Add a MAC check_create() hook for creating new named objects.
   53  */
   54 
   55 #include <sys/cdefs.h>
   56 __FBSDID("$FreeBSD$");
   57 
   58 #include <sys/param.h>
   59 #include <sys/conf.h>
   60 #include <sys/fcntl.h>
   61 #include <sys/file.h>
   62 #include <sys/filedesc.h>
   63 #include <sys/fnv_hash.h>
   64 #include <sys/kernel.h>
   65 #include <sys/lock.h>
   66 #include <sys/malloc.h>
   67 #include <sys/mman.h>
   68 #include <sys/mutex.h>
   69 #include <sys/proc.h>
   70 #include <sys/refcount.h>
   71 #include <sys/resourcevar.h>
   72 #include <sys/stat.h>
   73 #include <sys/sysctl.h>
   74 #include <sys/sysproto.h>
   75 #include <sys/systm.h>
   76 #include <sys/sx.h>
   77 #include <sys/time.h>
   78 #include <sys/vnode.h>
   79 
   80 #include <security/mac/mac_framework.h>
   81 
   82 #include <vm/vm.h>
   83 #include <vm/vm_param.h>
   84 #include <vm/pmap.h>
   85 #include <vm/vm_extern.h>
   86 #include <vm/vm_map.h>
   87 #include <vm/vm_kern.h>
   88 #include <vm/vm_object.h>
   89 #include <vm/vm_page.h>
   90 #include <vm/vm_pager.h>
   91 #include <vm/swap_pager.h>
   92 
   93 struct shm_mapping {
   94         char            *sm_path;
   95         Fnv32_t         sm_fnv;
   96         struct shmfd    *sm_shmfd;
   97         LIST_ENTRY(shm_mapping) sm_link;
   98 };
   99 
  100 static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
  101 static LIST_HEAD(, shm_mapping) *shm_dictionary;
  102 static struct sx shm_dict_lock;
  103 static struct mtx shm_timestamp_lock;
  104 static u_long shm_hash;
  105 static struct unrhdr *shm_ino_unr;
  106 static dev_t shm_dev_ino;
  107 
  108 #define SHM_HASH(fnv)   (&shm_dictionary[(fnv) & shm_hash])
  109 
  110 static int      shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
  111 static struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
  112 static void     shm_init(void *arg);
  113 static void     shm_drop(struct shmfd *shmfd);
  114 static struct shmfd *shm_hold(struct shmfd *shmfd);
  115 static void     shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
  116 static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
  117 static int      shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
  118 static int      shm_dotruncate(struct shmfd *shmfd, off_t length);
  119 
  120 static fo_rdwr_t        shm_read;
  121 static fo_rdwr_t        shm_write;
  122 static fo_truncate_t    shm_truncate;
  123 static fo_ioctl_t       shm_ioctl;
  124 static fo_poll_t        shm_poll;
  125 static fo_kqfilter_t    shm_kqfilter;
  126 static fo_stat_t        shm_stat;
  127 static fo_close_t       shm_close;
  128 
  129 /* File descriptor operations. */
  130 static struct fileops shm_ops = {
  131         .fo_read = shm_read,
  132         .fo_write = shm_write,
  133         .fo_truncate = shm_truncate,
  134         .fo_ioctl = shm_ioctl,
  135         .fo_poll = shm_poll,
  136         .fo_kqfilter = shm_kqfilter,
  137         .fo_stat = shm_stat,
  138         .fo_close = shm_close,
  139         .fo_flags = DFLAG_PASSABLE
  140 };
  141 
  142 FEATURE(posix_shm, "POSIX shared memory");
  143 
  144 static int
  145 shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
  146     int flags, struct thread *td)
  147 {
  148 
  149         return (EOPNOTSUPP);
  150 }
  151 
  152 static int
  153 shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
  154     int flags, struct thread *td)
  155 {
  156 
  157         return (EOPNOTSUPP);
  158 }
  159 
  160 static int
  161 shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
  162     struct thread *td)
  163 {
  164         struct shmfd *shmfd;
  165 #ifdef MAC
  166         int error;
  167 #endif
  168 
  169         shmfd = fp->f_data;
  170 #ifdef MAC
  171         error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
  172         if (error)
  173                 return (error);
  174 #endif
  175         return (shm_dotruncate(shmfd, length));
  176 }
  177 
  178 static int
  179 shm_ioctl(struct file *fp, u_long com, void *data,
  180     struct ucred *active_cred, struct thread *td)
  181 {
  182 
  183         return (EOPNOTSUPP);
  184 }
  185 
  186 static int
  187 shm_poll(struct file *fp, int events, struct ucred *active_cred,
  188     struct thread *td)
  189 {
  190 
  191         return (EOPNOTSUPP);
  192 }
  193 
  194 static int
  195 shm_kqfilter(struct file *fp, struct knote *kn)
  196 {
  197 
  198         return (EOPNOTSUPP);
  199 }
  200 
  201 static int
  202 shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
  203     struct thread *td)
  204 {
  205         struct shmfd *shmfd;
  206 #ifdef MAC
  207         int error;
  208 #endif
  209 
  210         shmfd = fp->f_data;
  211 
  212 #ifdef MAC
  213         error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
  214         if (error)
  215                 return (error);
  216 #endif
  217         
  218         /*
  219          * Attempt to return sanish values for fstat() on a memory file
  220          * descriptor.
  221          */
  222         bzero(sb, sizeof(*sb));
  223         sb->st_mode = S_IFREG | shmfd->shm_mode;                /* XXX */
  224         sb->st_blksize = PAGE_SIZE;
  225         sb->st_size = shmfd->shm_size;
  226         sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
  227         sb->st_atimespec = shmfd->shm_atime;
  228         sb->st_ctimespec = shmfd->shm_ctime;
  229         sb->st_mtimespec = shmfd->shm_mtime;
  230         sb->st_birthtimespec = shmfd->shm_birthtime;    
  231         sb->st_uid = shmfd->shm_uid;
  232         sb->st_gid = shmfd->shm_gid;
  233         sb->st_dev = shm_dev_ino;
  234         sb->st_ino = shmfd->shm_ino;
  235 
  236         return (0);
  237 }
  238 
  239 static int
  240 shm_close(struct file *fp, struct thread *td)
  241 {
  242         struct shmfd *shmfd;
  243 
  244         shmfd = fp->f_data;
  245         fp->f_data = NULL;
  246         shm_drop(shmfd);
  247 
  248         return (0);
  249 }
  250 
  251 static int
  252 shm_dotruncate(struct shmfd *shmfd, off_t length)
  253 {
  254         vm_object_t object;
  255         vm_page_t m;
  256         vm_pindex_t nobjsize;
  257         vm_ooffset_t delta;
  258 
  259         object = shmfd->shm_object;
  260         VM_OBJECT_LOCK(object);
  261         if (length == shmfd->shm_size) {
  262                 VM_OBJECT_UNLOCK(object);
  263                 return (0);
  264         }
  265         nobjsize = OFF_TO_IDX(length + PAGE_MASK);
  266 
  267         /* Are we shrinking?  If so, trim the end. */
  268         if (length < shmfd->shm_size) {
  269                 /*
  270                  * Disallow any requests to shrink the size if this
  271                  * object is mapped into the kernel.
  272                  */
  273                 if (shmfd->shm_kmappings > 0) {
  274                         VM_OBJECT_UNLOCK(object);
  275                         return (EBUSY);
  276                 }
  277                 delta = ptoa(object->size - nobjsize);
  278 
  279                 /* Toss in memory pages. */
  280                 if (nobjsize < object->size)
  281                         vm_object_page_remove(object, nobjsize, object->size,
  282                             FALSE);
  283 
  284                 /* Toss pages from swap. */
  285                 if (object->type == OBJT_SWAP)
  286                         swap_pager_freespace(object, nobjsize, delta);
  287 
  288                 /* Free the swap accounted for shm */
  289                 swap_release_by_uid(delta, object->uip);
  290                 object->charge -= delta;
  291 
  292                 /*
  293                  * If the last page is partially mapped, then zero out
  294                  * the garbage at the end of the page.  See comments
  295                  * in vnode_pager_setsize() for more details.
  296                  *
  297                  * XXXJHB: This handles in memory pages, but what about
  298                  * a page swapped out to disk?
  299                  */
  300                 if ((length & PAGE_MASK) &&
  301                     (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
  302                     m->valid != 0) {
  303                         int base = (int)length & PAGE_MASK;
  304                         int size = PAGE_SIZE - base;
  305 
  306                         pmap_zero_page_area(m, base, size);
  307 
  308                         /*
  309                          * Update the valid bits to reflect the blocks that
  310                          * have been zeroed.  Some of these valid bits may
  311                          * have already been set.
  312                          */
  313                         vm_page_set_valid(m, base, size);
  314 
  315                         /*
  316                          * Round "base" to the next block boundary so that the
  317                          * dirty bit for a partially zeroed block is not
  318                          * cleared.
  319                          */
  320                         base = roundup2(base, DEV_BSIZE);
  321 
  322                         vm_page_lock_queues();
  323                         vm_page_clear_dirty(m, base, PAGE_SIZE - base);
  324                         vm_page_unlock_queues();
  325                 } else if ((length & PAGE_MASK) &&
  326                     __predict_false(object->cache != NULL)) {
  327                         vm_page_cache_free(object, OFF_TO_IDX(length),
  328                             nobjsize);
  329                 }
  330         } else {
  331 
  332                 /* Attempt to reserve the swap */
  333                 delta = ptoa(nobjsize - object->size);
  334                 if (!swap_reserve_by_uid(delta, object->uip)) {
  335                         VM_OBJECT_UNLOCK(object);
  336                         return (ENOMEM);
  337                 }
  338                 object->charge += delta;
  339         }
  340         shmfd->shm_size = length;
  341         mtx_lock(&shm_timestamp_lock);
  342         vfs_timestamp(&shmfd->shm_ctime);
  343         shmfd->shm_mtime = shmfd->shm_ctime;
  344         mtx_unlock(&shm_timestamp_lock);
  345         object->size = nobjsize;
  346         VM_OBJECT_UNLOCK(object);
  347         return (0);
  348 }
  349 
  350 /*
  351  * shmfd object management including creation and reference counting
  352  * routines.
  353  */
  354 static struct shmfd *
  355 shm_alloc(struct ucred *ucred, mode_t mode)
  356 {
  357         struct shmfd *shmfd;
  358         int ino;
  359 
  360         shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
  361         shmfd->shm_size = 0;
  362         shmfd->shm_uid = ucred->cr_uid;
  363         shmfd->shm_gid = ucred->cr_gid;
  364         shmfd->shm_mode = mode;
  365         shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
  366             shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
  367         KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
  368         VM_OBJECT_LOCK(shmfd->shm_object);
  369         vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
  370         vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
  371         VM_OBJECT_UNLOCK(shmfd->shm_object);
  372         vfs_timestamp(&shmfd->shm_birthtime);
  373         shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
  374             shmfd->shm_birthtime;
  375         ino = alloc_unr(shm_ino_unr);
  376         if (ino == -1)
  377                 shmfd->shm_ino = 0;
  378         else
  379                 shmfd->shm_ino = ino;
  380         refcount_init(&shmfd->shm_refs, 1);
  381 #ifdef MAC
  382         mac_posixshm_init(shmfd);
  383         mac_posixshm_create(ucred, shmfd);
  384 #endif
  385 
  386         return (shmfd);
  387 }
  388 
  389 static struct shmfd *
  390 shm_hold(struct shmfd *shmfd)
  391 {
  392 
  393         refcount_acquire(&shmfd->shm_refs);
  394         return (shmfd);
  395 }
  396 
  397 static void
  398 shm_drop(struct shmfd *shmfd)
  399 {
  400 
  401         if (refcount_release(&shmfd->shm_refs)) {
  402 #ifdef MAC
  403                 mac_posixshm_destroy(shmfd);
  404 #endif
  405                 vm_object_deallocate(shmfd->shm_object);
  406                 if (shmfd->shm_ino != 0)
  407                         free_unr(shm_ino_unr, shmfd->shm_ino);
  408                 free(shmfd, M_SHMFD);
  409         }
  410 }
  411 
  412 /*
  413  * Determine if the credentials have sufficient permissions for a
  414  * specified combination of FREAD and FWRITE.
  415  */
  416 static int
  417 shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
  418 {
  419         accmode_t accmode;
  420 
  421         accmode = 0;
  422         if (flags & FREAD)
  423                 accmode |= VREAD;
  424         if (flags & FWRITE)
  425                 accmode |= VWRITE;
  426         return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
  427             accmode, ucred, NULL));
  428 }
  429 
  430 /*
  431  * Dictionary management.  We maintain an in-kernel dictionary to map
  432  * paths to shmfd objects.  We use the FNV hash on the path to store
  433  * the mappings in a hash table.
  434  */
  435 static void
  436 shm_init(void *arg)
  437 {
  438 
  439         mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
  440         sx_init(&shm_dict_lock, "shm dictionary");
  441         shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
  442         shm_ino_unr = new_unrhdr(1, INT32_MAX, NULL);
  443         KASSERT(shm_ino_unr != NULL, ("shm fake inodes not initialized"));
  444         shm_dev_ino = devfs_alloc_cdp_inode();
  445         KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized"));
  446 }
  447 SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL);
  448 
  449 static struct shmfd *
  450 shm_lookup(char *path, Fnv32_t fnv)
  451 {
  452         struct shm_mapping *map;
  453 
  454         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
  455                 if (map->sm_fnv != fnv)
  456                         continue;
  457                 if (strcmp(map->sm_path, path) == 0)
  458                         return (map->sm_shmfd);
  459         }
  460 
  461         return (NULL);
  462 }
  463 
  464 static void
  465 shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
  466 {
  467         struct shm_mapping *map;
  468 
  469         map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
  470         map->sm_path = path;
  471         map->sm_fnv = fnv;
  472         map->sm_shmfd = shm_hold(shmfd);
  473         shmfd->shm_path = path;
  474         LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
  475 }
  476 
  477 static int
  478 shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
  479 {
  480         struct shm_mapping *map;
  481         int error;
  482 
  483         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
  484                 if (map->sm_fnv != fnv)
  485                         continue;
  486                 if (strcmp(map->sm_path, path) == 0) {
  487 #ifdef MAC
  488                         error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
  489                         if (error)
  490                                 return (error);
  491 #endif
  492                         error = shm_access(map->sm_shmfd, ucred,
  493                             FREAD | FWRITE);
  494                         if (error)
  495                                 return (error);
  496                         map->sm_shmfd->shm_path = NULL;
  497                         LIST_REMOVE(map, sm_link);
  498                         shm_drop(map->sm_shmfd);
  499                         free(map->sm_path, M_SHMFD);
  500                         free(map, M_SHMFD);
  501                         return (0);
  502                 }
  503         }
  504 
  505         return (ENOENT);
  506 }
  507 
  508 /* System calls. */
  509 int
  510 shm_open(struct thread *td, struct shm_open_args *uap)
  511 {
  512         struct filedesc *fdp;
  513         struct shmfd *shmfd;
  514         struct file *fp;
  515         char *path;
  516         Fnv32_t fnv;
  517         mode_t cmode;
  518         int fd, error;
  519 
  520         if ((uap->flags & O_ACCMODE) != O_RDONLY &&
  521             (uap->flags & O_ACCMODE) != O_RDWR)
  522                 return (EINVAL);
  523 
  524         if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
  525                 return (EINVAL);
  526 
  527         fdp = td->td_proc->p_fd;
  528         cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
  529 
  530         error = falloc(td, &fp, &fd);
  531         if (error)
  532                 return (error);
  533 
  534         /* A SHM_ANON path pointer creates an anonymous object. */
  535         if (uap->path == SHM_ANON) {
  536                 /* A read-only anonymous object is pointless. */
  537                 if ((uap->flags & O_ACCMODE) == O_RDONLY) {
  538                         fdclose(fdp, fp, fd, td);
  539                         fdrop(fp, td);
  540                         return (EINVAL);
  541                 }
  542                 shmfd = shm_alloc(td->td_ucred, cmode);
  543         } else {
  544                 path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
  545                 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
  546 
  547                 /* Require paths to start with a '/' character. */
  548                 if (error == 0 && path[0] != '/')
  549                         error = EINVAL;
  550                 if (error) {
  551                         fdclose(fdp, fp, fd, td);
  552                         fdrop(fp, td);
  553                         free(path, M_SHMFD);
  554                         return (error);
  555                 }
  556 
  557                 fnv = fnv_32_str(path, FNV1_32_INIT);
  558                 sx_xlock(&shm_dict_lock);
  559                 shmfd = shm_lookup(path, fnv);
  560                 if (shmfd == NULL) {
  561                         /* Object does not yet exist, create it if requested. */
  562                         if (uap->flags & O_CREAT) {
  563                                 shmfd = shm_alloc(td->td_ucred, cmode);
  564                                 shm_insert(path, fnv, shmfd);
  565                         } else {
  566                                 free(path, M_SHMFD);
  567                                 error = ENOENT;
  568                         }
  569                 } else {
  570                         /*
  571                          * Object already exists, obtain a new
  572                          * reference if requested and permitted.
  573                          */
  574                         free(path, M_SHMFD);
  575                         if ((uap->flags & (O_CREAT | O_EXCL)) ==
  576                             (O_CREAT | O_EXCL))
  577                                 error = EEXIST;
  578                         else {
  579 #ifdef MAC
  580                                 error = mac_posixshm_check_open(td->td_ucred,
  581                                     shmfd);
  582                                 if (error == 0)
  583 #endif
  584                                 error = shm_access(shmfd, td->td_ucred,
  585                                     FFLAGS(uap->flags & O_ACCMODE));
  586                         }
  587 
  588                         /*
  589                          * Truncate the file back to zero length if
  590                          * O_TRUNC was specified and the object was
  591                          * opened with read/write.
  592                          */
  593                         if (error == 0 &&
  594                             (uap->flags & (O_ACCMODE | O_TRUNC)) ==
  595                             (O_RDWR | O_TRUNC)) {
  596 #ifdef MAC
  597                                 error = mac_posixshm_check_truncate(
  598                                         td->td_ucred, fp->f_cred, shmfd);
  599                                 if (error == 0)
  600 #endif
  601                                         shm_dotruncate(shmfd, 0);
  602                         }
  603                         if (error == 0)
  604                                 shm_hold(shmfd);
  605                 }
  606                 sx_xunlock(&shm_dict_lock);
  607 
  608                 if (error) {
  609                         fdclose(fdp, fp, fd, td);
  610                         fdrop(fp, td);
  611                         return (error);
  612                 }
  613         }
  614 
  615         finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
  616 
  617         FILEDESC_XLOCK(fdp);
  618         if (fdp->fd_ofiles[fd] == fp)
  619                 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
  620         FILEDESC_XUNLOCK(fdp);
  621         td->td_retval[0] = fd;
  622         fdrop(fp, td);
  623 
  624         return (0);
  625 }
  626 
  627 int
  628 shm_unlink(struct thread *td, struct shm_unlink_args *uap)
  629 {
  630         char *path;
  631         Fnv32_t fnv;
  632         int error;
  633 
  634         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  635         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
  636         if (error) {
  637                 free(path, M_TEMP);
  638                 return (error);
  639         }
  640 
  641         fnv = fnv_32_str(path, FNV1_32_INIT);
  642         sx_xlock(&shm_dict_lock);
  643         error = shm_remove(path, fnv, td->td_ucred);
  644         sx_xunlock(&shm_dict_lock);
  645         free(path, M_TEMP);
  646 
  647         return (error);
  648 }
  649 
  650 /*
  651  * mmap() helper to validate mmap() requests against shm object state
  652  * and give mmap() the vm_object to use for the mapping.
  653  */
  654 int
  655 shm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
  656     vm_object_t *obj)
  657 {
  658 
  659         /*
  660          * XXXRW: This validation is probably insufficient, and subject to
  661          * sign errors.  It should be fixed.
  662          */
  663         if (foff >= shmfd->shm_size ||
  664             foff + objsize > round_page(shmfd->shm_size))
  665                 return (EINVAL);
  666 
  667         mtx_lock(&shm_timestamp_lock);
  668         vfs_timestamp(&shmfd->shm_atime);
  669         mtx_unlock(&shm_timestamp_lock);
  670         vm_object_reference(shmfd->shm_object);
  671         *obj = shmfd->shm_object;
  672         return (0);
  673 }
  674 
  675 /*
  676  * Helper routines to allow the backing object of a shared memory file
  677  * descriptor to be mapped in the kernel.
  678  */
  679 int
  680 shm_map(struct file *fp, size_t size, off_t offset, void **memp)
  681 {
  682         struct shmfd *shmfd;
  683         vm_offset_t kva, ofs;
  684         vm_object_t obj;
  685         int rv;
  686 
  687         if (fp->f_type != DTYPE_SHM)
  688                 return (EINVAL);
  689         shmfd = fp->f_data;
  690         obj = shmfd->shm_object;
  691         VM_OBJECT_LOCK(obj);
  692         /*
  693          * XXXRW: This validation is probably insufficient, and subject to
  694          * sign errors.  It should be fixed.
  695          */
  696         if (offset >= shmfd->shm_size ||
  697             offset + size > round_page(shmfd->shm_size)) {
  698                 VM_OBJECT_UNLOCK(obj);
  699                 return (EINVAL);
  700         }
  701 
  702         shmfd->shm_kmappings++;
  703         vm_object_reference_locked(obj);
  704         VM_OBJECT_UNLOCK(obj);
  705 
  706         /* Map the object into the kernel_map and wire it. */
  707         kva = vm_map_min(kernel_map);
  708         ofs = offset & PAGE_MASK;
  709         offset = trunc_page(offset);
  710         size = round_page(size + ofs);
  711         rv = vm_map_find(kernel_map, obj, offset, &kva, size,
  712             VMFS_ALIGNED_SPACE, VM_PROT_READ | VM_PROT_WRITE,
  713             VM_PROT_READ | VM_PROT_WRITE, 0);
  714         if (rv == KERN_SUCCESS) {
  715                 rv = vm_map_wire(kernel_map, kva, kva + size,
  716                     VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
  717                 if (rv == KERN_SUCCESS) {
  718                         *memp = (void *)(kva + ofs);
  719                         return (0);
  720                 }
  721                 vm_map_remove(kernel_map, kva, kva + size);
  722         } else
  723                 vm_object_deallocate(obj);
  724 
  725         /* On failure, drop our mapping reference. */
  726         VM_OBJECT_LOCK(obj);
  727         shmfd->shm_kmappings--;
  728         VM_OBJECT_UNLOCK(obj);
  729 
  730         return (vm_mmap_to_errno(rv));
  731 }
  732 
  733 /*
  734  * We require the caller to unmap the entire entry.  This allows us to
  735  * safely decrement shm_kmappings when a mapping is removed.
  736  */
  737 int
  738 shm_unmap(struct file *fp, void *mem, size_t size)
  739 {
  740         struct shmfd *shmfd;
  741         vm_map_entry_t entry;
  742         vm_offset_t kva, ofs;
  743         vm_object_t obj;
  744         vm_pindex_t pindex;
  745         vm_prot_t prot;
  746         boolean_t wired;
  747         vm_map_t map;
  748         int rv;
  749 
  750         if (fp->f_type != DTYPE_SHM)
  751                 return (EINVAL);
  752         shmfd = fp->f_data;
  753         kva = (vm_offset_t)mem;
  754         ofs = kva & PAGE_MASK;
  755         kva = trunc_page(kva);
  756         size = round_page(size + ofs);
  757         map = kernel_map;
  758         rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
  759             &obj, &pindex, &prot, &wired);
  760         if (rv != KERN_SUCCESS)
  761                 return (EINVAL);
  762         if (entry->start != kva || entry->end != kva + size) {
  763                 vm_map_lookup_done(map, entry);
  764                 return (EINVAL);
  765         }
  766         vm_map_lookup_done(map, entry);
  767         if (obj != shmfd->shm_object)
  768                 return (EINVAL);
  769         vm_map_remove(map, kva, kva + size);
  770         VM_OBJECT_LOCK(obj);
  771         KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
  772         shmfd->shm_kmappings--;
  773         VM_OBJECT_UNLOCK(obj);
  774         return (0);
  775 }
  776 
  777 void
  778 shm_path(struct shmfd *shmfd, char *path, size_t size)
  779 {
  780 
  781         if (shmfd->shm_path == NULL)
  782                 return;
  783         sx_slock(&shm_dict_lock);
  784         if (shmfd->shm_path != NULL)
  785                 strlcpy(path, shmfd->shm_path, size);
  786         sx_sunlock(&shm_dict_lock);
  787 }

Cache object: a699cd1d03c13da53b4e4940189a7f99


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.