Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Dec 2018 22:42:06 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r342250 - in stable/12/sys: fs/tmpfs kern sys
Message-ID:  <201812192242.wBJMg6CE048490@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mjg
Date: Wed Dec 19 22:42:06 2018
New Revision: 342250
URL: https://svnweb.freebsd.org/changeset/base/342250

Log:
  MFC r340676,r340677,r340679,r340747,r340749,r341682
  
  Implement unr64
  pipe: use unr64
  tmpfs: use unr64 for inode numbers
  uipc_shm: use unr64 for inode numbers
  uipc_usrreq: fix inode number assignment
  unr64: use locked variant if not __LP64__
  
  Sponsored by:	The FreeBSD Foundation

Modified:
  stable/12/sys/fs/tmpfs/tmpfs.h
  stable/12/sys/fs/tmpfs/tmpfs_subr.c
  stable/12/sys/fs/tmpfs/tmpfs_vfsops.c
  stable/12/sys/kern/subr_unit.c
  stable/12/sys/kern/sys_pipe.c
  stable/12/sys/kern/uipc_shm.c
  stable/12/sys/kern/uipc_usrreq.c
  stable/12/sys/sys/systm.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/fs/tmpfs/tmpfs.h
==============================================================================
--- stable/12/sys/fs/tmpfs/tmpfs.h	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/fs/tmpfs/tmpfs.h	Wed Dec 19 22:42:06 2018	(r342250)
@@ -353,7 +353,7 @@ struct tmpfs_mount {
 	ino_t			tm_nodes_max;
 
 	/* unrhdr used to allocate inode numbers */
-	struct unrhdr *		tm_ino_unr;
+	struct unrhdr64		tm_ino_unr;
 
 	/* Number of nodes currently that are in use. */
 	ino_t			tm_nodes_inuse;

Modified: stable/12/sys/fs/tmpfs/tmpfs_subr.c
==============================================================================
--- stable/12/sys/fs/tmpfs/tmpfs_subr.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/fs/tmpfs/tmpfs_subr.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -230,7 +230,7 @@ tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount 
 	nnode->tn_uid = uid;
 	nnode->tn_gid = gid;
 	nnode->tn_mode = mode;
-	nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
+	nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
 	nnode->tn_refcount = 1;
 
 	/* Type-specific initialization. */
@@ -368,13 +368,6 @@ tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct
 		panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
 	}
 
-	/*
-	 * If we are unmounting there is no need for going through the overhead
-	 * of freeing the inodes from the unr individually, so free them all in
-	 * one go later.
-	 */
-	if (!detach)
-		free_unr(tmp->tm_ino_unr, node->tn_id);
 	uma_zfree(tmp->tm_node_pool, node);
 	TMPFS_LOCK(tmp);
 	tmpfs_free_tmp(tmp);

Modified: stable/12/sys/fs/tmpfs/tmpfs_vfsops.c
==============================================================================
--- stable/12/sys/fs/tmpfs/tmpfs_vfsops.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/fs/tmpfs/tmpfs_vfsops.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -231,7 +231,7 @@ tmpfs_mount(struct mount *mp)
 
 	tmp->tm_pages_max = pages;
 	tmp->tm_pages_used = 0;
-	tmp->tm_ino_unr = new_unrhdr(2, INT_MAX, &tmp->tm_allnode_lock);
+	new_unrhdr64(&tmp->tm_ino_unr, 2);
 	tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
 	    sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
 	    UMA_ALIGN_PTR, 0);
@@ -248,7 +248,6 @@ tmpfs_mount(struct mount *mp)
 	if (error != 0 || root == NULL) {
 		uma_zdestroy(tmp->tm_node_pool);
 		uma_zdestroy(tmp->tm_dirent_pool);
-		delete_unrhdr(tmp->tm_ino_unr);
 		free(tmp, M_TMPFSMNT);
 		return (error);
 	}
@@ -343,8 +342,6 @@ tmpfs_free_tmp(struct tmpfs_mount *tmp)
 
 	uma_zdestroy(tmp->tm_dirent_pool);
 	uma_zdestroy(tmp->tm_node_pool);
-	clear_unrhdr(tmp->tm_ino_unr);
-	delete_unrhdr(tmp->tm_ino_unr);
 
 	mtx_destroy(&tmp->tm_allnode_lock);
 	MPASS(tmp->tm_pages_used == 0);

Modified: stable/12/sys/kern/subr_unit.c
==============================================================================
--- stable/12/sys/kern/subr_unit.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/kern/subr_unit.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -98,6 +98,19 @@ static struct mtx unitmtx;
 
 MTX_SYSINIT(unit, &unitmtx, "unit# allocation", MTX_DEF);
 
+#ifdef UNR64_LOCKED
+uint64_t
+alloc_unr64(struct unrhdr64 *unr64)
+{
+	uint64_t item;
+
+	mtx_lock(&unitmtx);
+	item = unr64->counter++;
+	mtx_unlock(&unitmtx);
+	return (item);
+}
+#endif
+
 #else /* ...USERLAND */
 
 #include <bitstring.h>

Modified: stable/12/sys/kern/sys_pipe.c
==============================================================================
--- stable/12/sys/kern/sys_pipe.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/kern/sys_pipe.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -244,7 +244,7 @@ static int	pipe_zone_init(void *mem, int size, int fla
 static void	pipe_zone_fini(void *mem, int size);
 
 static uma_zone_t pipe_zone;
-static struct unrhdr *pipeino_unr;
+static struct unrhdr64 pipeino_unr;
 static dev_t pipedev_ino;
 
 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL);
@@ -257,8 +257,7 @@ pipeinit(void *dummy __unused)
 	    pipe_zone_ctor, NULL, pipe_zone_init, pipe_zone_fini,
 	    UMA_ALIGN_PTR, 0);
 	KASSERT(pipe_zone != NULL, ("pipe_zone not initialized"));
-	pipeino_unr = new_unrhdr(1, INT32_MAX, NULL);
-	KASSERT(pipeino_unr != NULL, ("pipe fake inodes not initialized"));
+	new_unrhdr64(&pipeino_unr, 1);
 	pipedev_ino = devfs_alloc_cdp_inode();
 	KASSERT(pipedev_ino > 0, ("pipe dev inode not initialized"));
 }
@@ -390,8 +389,6 @@ pipe_dtor(struct pipe *dpipe)
 		funsetown(&peer->pipe_sigio);
 		pipeclose(peer);
 	}
-	if (ino != 0 && ino != (ino_t)-1)
-		free_unr(pipeino_unr, ino);
 }
 
 /*
@@ -639,7 +636,7 @@ pipe_create(struct pipe *pipe, int backing)
 			(void)pipespace_new(pipe, PIPE_SIZE);
 	}
 
-	pipe->pipe_ino = -1;
+	pipe->pipe_ino = alloc_unr64(&pipeino_unr);
 }
 
 /* ARGSUSED */
@@ -1461,7 +1458,6 @@ pipe_stat(struct file *fp, struct stat *ub, struct ucr
     struct thread *td)
 {
 	struct pipe *pipe;
-	int new_unr;
 #ifdef MAC
 	int error;
 #endif
@@ -1482,23 +1478,6 @@ pipe_stat(struct file *fp, struct stat *ub, struct ucr
 		return (vnops.fo_stat(fp, ub, active_cred, td));
 	}
 
-	/*
-	 * Lazily allocate an inode number for the pipe.  Most pipe
-	 * users do not call fstat(2) on the pipe, which means that
-	 * postponing the inode allocation until it is must be
-	 * returned to userland is useful.  If alloc_unr failed,
-	 * assign st_ino zero instead of returning an error.
-	 * Special pipe_ino values:
-	 *  -1 - not yet initialized;
-	 *  0  - alloc_unr failed, return 0 as st_ino forever.
-	 */
-	if (pipe->pipe_ino == (ino_t)-1) {
-		new_unr = alloc_unr(pipeino_unr);
-		if (new_unr != -1)
-			pipe->pipe_ino = new_unr;
-		else
-			pipe->pipe_ino = 0;
-	}
 	PIPE_UNLOCK(pipe);
 
 	bzero(ub, sizeof(*ub));

Modified: stable/12/sys/kern/uipc_shm.c
==============================================================================
--- stable/12/sys/kern/uipc_shm.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/kern/uipc_shm.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -113,7 +113,7 @@ static LIST_HEAD(, shm_mapping) *shm_dictionary;
 static struct sx shm_dict_lock;
 static struct mtx shm_timestamp_lock;
 static u_long shm_hash;
-static struct unrhdr *shm_ino_unr;
+static struct unrhdr64 shm_ino_unr;
 static dev_t shm_dev_ino;
 
 #define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
@@ -531,7 +531,6 @@ struct shmfd *
 shm_alloc(struct ucred *ucred, mode_t mode)
 {
 	struct shmfd *shmfd;
-	int ino;
 
 	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
 	shmfd->shm_size = 0;
@@ -549,11 +548,7 @@ shm_alloc(struct ucred *ucred, mode_t mode)
 	vfs_timestamp(&shmfd->shm_birthtime);
 	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
 	    shmfd->shm_birthtime;
-	ino = alloc_unr(shm_ino_unr);
-	if (ino == -1)
-		shmfd->shm_ino = 0;
-	else
-		shmfd->shm_ino = ino;
+	shmfd->shm_ino = alloc_unr64(&shm_ino_unr);
 	refcount_init(&shmfd->shm_refs, 1);
 	mtx_init(&shmfd->shm_mtx, "shmrl", NULL, MTX_DEF);
 	rangelock_init(&shmfd->shm_rl);
@@ -584,8 +579,6 @@ shm_drop(struct shmfd *shmfd)
 		rangelock_destroy(&shmfd->shm_rl);
 		mtx_destroy(&shmfd->shm_mtx);
 		vm_object_deallocate(shmfd->shm_object);
-		if (shmfd->shm_ino != 0)
-			free_unr(shm_ino_unr, shmfd->shm_ino);
 		free(shmfd, M_SHMFD);
 	}
 }
@@ -624,8 +617,7 @@ shm_init(void *arg)
 	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
 	sx_init(&shm_dict_lock, "shm dictionary");
 	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
-	shm_ino_unr = new_unrhdr(1, INT32_MAX, NULL);
-	KASSERT(shm_ino_unr != NULL, ("shm fake inodes not initialized"));
+	new_unrhdr64(&shm_ino_unr, 1);
 	shm_dev_ino = devfs_alloc_cdp_inode();
 	KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized"));
 }

Modified: stable/12/sys/kern/uipc_usrreq.c
==============================================================================
--- stable/12/sys/kern/uipc_usrreq.c	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/kern/uipc_usrreq.c	Wed Dec 19 22:42:06 2018	(r342250)
@@ -530,6 +530,7 @@ uipc_attach(struct socket *so, int proto, struct threa
 		UNP_LINK_WLOCK();
 
 	unp->unp_gencnt = ++unp_gencnt;
+	unp->unp_ino = ++unp_ino;
 	unp_count++;
 	switch (so->so_type) {
 	case SOCK_STREAM:
@@ -1302,12 +1303,8 @@ uipc_sense(struct socket *so, struct stat *sb)
 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
 
 	sb->st_blksize = so->so_snd.sb_hiwat;
-	UNP_PCB_LOCK(unp);
 	sb->st_dev = NODEV;
-	if (unp->unp_ino == 0)
-		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
 	sb->st_ino = unp->unp_ino;
-	UNP_PCB_UNLOCK(unp);
 	return (0);
 }
 

Modified: stable/12/sys/sys/systm.h
==============================================================================
--- stable/12/sys/sys/systm.h	Wed Dec 19 22:38:06 2018	(r342249)
+++ stable/12/sys/sys/systm.h	Wed Dec 19 22:42:06 2018	(r342250)
@@ -524,6 +524,32 @@ int alloc_unr_specific(struct unrhdr *uh, u_int item);
 int alloc_unrl(struct unrhdr *uh);
 void free_unr(struct unrhdr *uh, u_int item);
 
+#ifndef __LP64__
+#define UNR64_LOCKED
+#endif
+
+struct unrhdr64 {
+        uint64_t	counter;
+};
+
+static __inline void
+new_unrhdr64(struct unrhdr64 *unr64, uint64_t low)
+{
+
+	unr64->counter = low;
+}
+
+#ifdef UNR64_LOCKED
+uint64_t alloc_unr64(struct unrhdr64 *);
+#else
+static __inline uint64_t
+alloc_unr64(struct unrhdr64 *unr64)
+{
+
+	return (atomic_fetchadd_64(&unr64->counter, 1));
+}
+#endif
+
 void	intr_prof_stack_use(struct thread *td, struct trapframe *frame);
 
 void counted_warning(unsigned *counter, const char *msg);



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