]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/sysv_shm.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / sysv_shm.c
index 7e778e60d97158e60ad4e25393ec5b2225dded6b..a3a6ea9330897713f2c53339de19b197bd42fc34 100644 (file)
 #if SYSV_SHM
 static int shminit(void);
 
-static lck_grp_t       *sysv_shm_subsys_lck_grp;
-static lck_grp_attr_t  *sysv_shm_subsys_lck_grp_attr;
-static lck_attr_t      *sysv_shm_subsys_lck_attr;
-static lck_mtx_t        sysv_shm_subsys_mutex;
+static LCK_GRP_DECLARE(sysv_shm_subsys_lck_grp, "sysv_shm_subsys_lock");
+static LCK_MTX_DECLARE(sysv_shm_subsys_mutex, &sysv_shm_subsys_lck_grp);
 
 #define SYSV_SHM_SUBSYS_LOCK() lck_mtx_lock(&sysv_shm_subsys_mutex)
 #define SYSV_SHM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_shm_subsys_mutex)
@@ -183,8 +181,6 @@ struct shminfo shminfo = {
 
 #endif /* __APPLE_API_PRIVATE */
 
-void sysv_shm_lock_init(void);
-
 static __inline__ time_t
 sysv_shmtime(void)
 {
@@ -277,7 +273,7 @@ shm_deallocate_segment(struct shmid_kernel *shmseg)
            shm_handle = shm_handle_next) {
                shm_handle_next = shm_handle->shm_handle_next;
                mach_memory_entry_port_release(shm_handle->shm_object);
-               FREE(shm_handle, M_SHM);
+               kheap_free(KM_SHM, shm_handle, sizeof(struct shm_handle));
        }
        shmseg->u.shm_internal = USER_ADDR_NULL;                /* tunnel */
        size = vm_map_round_page(shmseg->u.shm_segsz,
@@ -421,7 +417,7 @@ shmat(struct proc *p, struct shmat_args *uap, user_addr_t *retval)
                        goto shmat_out;
                }
 
-               MALLOC(shmmap_s, struct shmmap_state *, size, M_SHM, M_WAITOK | M_NULL);
+               shmmap_s = kheap_alloc(KM_SHM, size, Z_WAITOK);
                if (shmmap_s == NULL) {
                        shmat_ret = ENOMEM;
                        goto shmat_out;
@@ -838,7 +834,7 @@ shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode,
                        goto out;
                }
 
-               MALLOC(shm_handle, struct shm_handle *, sizeof(struct shm_handle), M_SHM, M_WAITOK);
+               shm_handle = kheap_alloc(KM_SHM, sizeof(struct shm_handle), Z_WAITOK);
                if (shm_handle == NULL) {
                        kret = KERN_NO_SPACE;
                        mach_memory_entry_port_release(mem_object);
@@ -891,7 +887,7 @@ out:
                    shm_handle = shm_handle_next) {
                        shm_handle_next = shm_handle->shm_handle_next;
                        mach_memory_entry_port_release(shm_handle->shm_object);
-                       FREE(shm_handle, M_SHM);
+                       kheap_free(KM_SHM, shm_handle, sizeof(struct shm_handle));
                }
                shmseg->u.shm_internal = USER_ADDR_NULL; /* tunnel */
        }
@@ -1006,7 +1002,7 @@ shmfork(struct proc *p1, struct proc *p2)
                ret = 1;
                goto shmfork_out;
        }
-       MALLOC(shmmap_s, struct shmmap_state *, size, M_SHM, M_WAITOK);
+       shmmap_s = kheap_alloc(KM_SHM, size, Z_WAITOK);
        if (shmmap_s == NULL) {
                ret = 1;
                goto shmfork_out;
@@ -1029,11 +1025,14 @@ static void
 shmcleanup(struct proc *p, int deallocate)
 {
        struct shmmap_state *shmmap_s;
+       size_t size = 0;
+       int nsegs = 0;
 
        SYSV_SHM_SUBSYS_LOCK();
 
        shmmap_s = (struct shmmap_state *)p->vm_shm;
        for (; shmmap_s->shmid != SHMID_SENTINEL; shmmap_s++) {
+               nsegs++;
                if (SHMID_IS_VALID(shmmap_s->shmid)) {
                        /*
                         * XXX: Should the MAC framework enforce
@@ -1043,8 +1042,10 @@ shmcleanup(struct proc *p, int deallocate)
                }
        }
 
-       FREE(p->vm_shm, M_SHM);
-       p->vm_shm = NULL;
+       if (os_add_and_mul_overflow(nsegs, 1, sizeof(struct shmmap_state), &size)) {
+               panic("shmcleanup: p->vm_shm buffer was correupted\n");
+       }
+       kheap_free(KM_SHM, p->vm_shm, size);
        SYSV_SHM_SUBSYS_UNLOCK();
 }
 
@@ -1084,7 +1085,7 @@ shminit(void)
                        return ENOMEM;
                }
 
-               MALLOC(shmsegs, struct shmid_kernel *, sz, M_SHM, M_WAITOK | M_ZERO);
+               shmsegs = zalloc_permanent(sz, ZALIGN_PTR);
                if (shmsegs == NULL) {
                        return ENOMEM;
                }
@@ -1104,18 +1105,6 @@ shminit(void)
        return 0;
 }
 
-/* Initialize the mutex governing access to the SysV shm subsystem */
-__private_extern__ void
-sysv_shm_lock_init( void )
-{
-       sysv_shm_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
-
-       sysv_shm_subsys_lck_grp = lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_shm_subsys_lck_grp_attr);
-
-       sysv_shm_subsys_lck_attr = lck_attr_alloc_init();
-       lck_mtx_init(&sysv_shm_subsys_mutex, sysv_shm_subsys_lck_grp, sysv_shm_subsys_lck_attr);
-}
-
 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
  *       struct sysctl_req *req) */
 static int