Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 Feb 2015 09:19:27 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r279272 - stable/10/sys/kern
Message-ID:  <201502250919.t1P9JRQm086084@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Wed Feb 25 09:19:26 2015
New Revision: 279272
URL: https://svnweb.freebsd.org/changeset/base/279272

Log:
  MFC r278963:
  If malloc() sleeps, Giant is dropped.  Recheck for another thread
  doing our work.
  
  Remove unneeded check for failed M_WAITOK allocation.

Modified:
  stable/10/sys/kern/sysv_shm.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/sys/kern/sysv_shm.c
==============================================================================
--- stable/10/sys/kern/sysv_shm.c	Wed Feb 25 08:39:48 2015	(r279271)
+++ stable/10/sys/kern/sysv_shm.c	Wed Feb 25 09:19:26 2015	(r279272)
@@ -358,9 +358,22 @@ kern_shmat(td, shmid, shmaddr, shmflg)
 	if (shmmap_s == NULL) {
 		shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
 		    M_SHM, M_WAITOK);
-		for (i = 0; i < shminfo.shmseg; i++)
-			shmmap_s[i].shmid = -1;
-		p->p_vmspace->vm_shm = shmmap_s;
+
+		/*
+		 * If malloc() above sleeps, the Giant lock is
+		 * temporarily dropped, which allows another thread to
+		 * allocate shmmap_state and set vm_shm.  Recheck
+		 * vm_shm and free the new shmmap_state if another one
+		 * is already allocated.
+		 */
+		if (p->p_vmspace->vm_shm != NULL) {
+			free(shmmap_s, M_SHM);
+			shmmap_s = p->p_vmspace->vm_shm;
+		} else {
+			for (i = 0; i < shminfo.shmseg; i++)
+				shmmap_s[i].shmid = -1;
+			p->p_vmspace->vm_shm = shmmap_s;
+		}
 	}
 	shmseg = shm_find_segment_by_shmid(shmid);
 	if (shmseg == NULL) {
@@ -827,8 +840,6 @@ shmrealloc(void)
 		return;
 
 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
-	if (newsegs == NULL)
-		return;
 	for (i = 0; i < shmalloced; i++)
 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
 	for (; i < shminfo.shmmni; i++) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201502250919.t1P9JRQm086084>