Skip to contentSkip to conversation

ipc: Convert ipcs_idr to XArray

PATCH In review Matthew Wilcox 3 participants 7 messages Mar 26, 2020 – Apr 21, 2020

Subsystems (3)
lore
Share & tools
lore

Matthew Wilcox

author maintainer
#0 lore raw

PATCH 1/1 ipc: Convert ipcs_idr to XArray

From: "Matthew Wilcox (Oracle)" <[email protected]>

The XArray has better loops than the IDR has, removing the need to open-code them. We also don't need to call idr_destroy() any more. Allocating the ID is a little tricky due to needing to get 'seq' correct. Open-code a variant of __xa_alloc() which lets us set the ID and the seq before depositing the pointer in the array.

---
 include/linux/ipc_namespace.h |  10 +-
 include/linux/xarray.h        |   1 +
 ipc/ipc_sysctl.c              |  14 +--
 ipc/msg.c                     |   1 -
 ipc/namespace.c               |  13 +--
 ipc/sem.c                     |   1 -
 ipc/shm.c                     |  60 +++++------
 ipc/util.c                    | 181 +++++++++++++++++-----------------
 ipc/util.h                    |   4 +-
 lib/xarray.c                  |   2 +-
 10 files changed, 135 insertions(+), 152 deletions(-)
include/linux/ipc_namespace.h +5 -5
index c309f43bde45..bdc39cc4d1dc 100644
@@ -3,13 +3,13 @@
 #define __IPC_NAMESPACE_H__
 
 #include <linux/err.h>
-#include <linux/idr.h>
-#include <linux/rwsem.h>
 #include <linux/notifier.h>
 #include <linux/nsproxy.h>
 #include <linux/ns_common.h>
 #include <linux/refcount.h>
 #include <linux/rhashtable-types.h>
+#include <linux/rwsem.h>
+#include <linux/xarray.h>
 
 struct user_namespace;
 
@@ -17,11 +17,11 @@ struct ipc_ids {
 	int in_use;
 	unsigned short seq;
 	struct rw_semaphore rwsem;
-	struct idr ipcs_idr;
+	struct xarray ipcs;
 	int max_idx;
-	int last_idx;	/* For wrap around detection */
+	int next_idx;
 #ifdef CONFIG_CHECKPOINT_RESTORE
-	int next_id;
+	int restore_id;
 #endif
 	struct rhashtable key_ht;
 };
include/linux/xarray.h +1 -0
index 14c893433139..d79b8e3aa08d 100644
@@ -1501,6 +1501,7 @@ void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
 void xas_init_marks(const struct xa_state *);
 
 bool xas_nomem(struct xa_state *, gfp_t);
+bool __xas_nomem(struct xa_state *, gfp_t);
 void xas_pause(struct xa_state *);
 
 void xas_create_range(struct xa_state *);
ipc/ipc_sysctl.c +7 -7
index affd66537e87..a2df5d0e27f6 100644
@@ -115,7 +115,7 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
 
 int ipc_mni = IPCMNI;
 int ipc_mni_shift = IPCMNI_SHIFT;
-int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
+int ipc_min_cycle = XA_CHUNK_SIZE;
 
 static struct ctl_table ipc_kern_table[] = {
 	{
@@ -196,8 +196,8 @@ static struct ctl_table ipc_kern_table[] = {
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	{
 		.procname	= "sem_next_id",
-		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
@@ -205,8 +205,8 @@ static struct ctl_table ipc_kern_table[] = {
 	},
 	{
 		.procname	= "msg_next_id",
-		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
@@ -214,8 +214,8 @@ static struct ctl_table ipc_kern_table[] = {
 	},
 	{
 		.procname	= "shm_next_id",
-		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
ipc/msg.c +0 -1
index caca67368cb5..0c4fdc2f5c08 100644
@@ -1308,7 +1308,6 @@ void msg_init_ns(struct ipc_namespace *ns)
 void msg_exit_ns(struct ipc_namespace *ns)
 {
 	free_ipcs(ns, &msg_ids(ns), freeque);
-	idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
 	rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht);
 }
 #endif
ipc/namespace.c +4 -9
index b3ca1476ca51..e8567c3d32e9 100644
@@ -96,22 +96,17 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
 	       void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
 {
 	struct kern_ipc_perm *perm;
-	int next_id;
-	int total, in_use;
+	unsigned long index;
 
 	down_write(&ids->rwsem);
 
-	in_use = ids->in_use;
-
-	for (total = 0, next_id = 0; total < in_use; next_id++) {
-		perm = idr_find(&ids->ipcs_idr, next_id);
-		if (perm == NULL)
-			continue;
+	xa_for_each(&ids->ipcs, index, perm) {
 		rcu_read_lock();
 		ipc_lock_object(perm);
 		free(ns, perm);
-		total++;
 	}
+	BUG_ON(!xa_empty(&ids->ipcs));
+
 	up_write(&ids->rwsem);
 }
 
ipc/sem.c +0 -1
index 3687b71151b3..8d6550ac035a 100644
@@ -258,7 +258,6 @@ void sem_init_ns(struct ipc_namespace *ns)
 void sem_exit_ns(struct ipc_namespace *ns)
 {
 	free_ipcs(ns, &sem_ids(ns), freeary);
-	idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
 	rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
 }
 #endif
ipc/shm.c +23 -37

90 lines not on this page · raw message

ipc/util.c +92 -89

280 lines not on this page · raw message

ipc/util.h +2 -2
index 5766c61aed0e..04d49db4cefa 100644
@@ -27,7 +27,7 @@
  */
 #define IPCMNI_SHIFT		15
 #define IPCMNI_EXTEND_SHIFT	24
-#define IPCMNI_EXTEND_MIN_CYCLE	(RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE)
+#define IPCMNI_EXTEND_MIN_CYCLE	(XA_CHUNK_SIZE * XA_CHUNK_SIZE)
 #define IPCMNI			(1 << IPCMNI_SHIFT)
 #define IPCMNI_EXTEND		(1 << IPCMNI_EXTEND_SHIFT)
 
@@ -42,7 +42,7 @@ extern int ipc_min_cycle;
 #else /* CONFIG_SYSVIPC_SYSCTL */
 
 #define ipc_mni			IPCMNI
-#define ipc_min_cycle		((int)RADIX_TREE_MAP_SIZE)
+#define ipc_min_cycle		((int)XA_CHUNK_SIZE)
 #define ipcmni_seq_shift()	IPCMNI_SHIFT
 #define IPCMNI_IDX_MASK		((1 << IPCMNI_SHIFT) - 1)
 #endif /* CONFIG_SYSVIPC_SYSCTL */
lib/xarray.c +1 -1
index e9e641d3c0c3..8488fb4eff2e 100644
@@ -319,7 +319,7 @@ EXPORT_SYMBOL_GPL(xas_nomem);
  *
  * Return: true if memory was needed, and was successfully allocated.
  */
-static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
+bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
 	__must_hold(xas->xa->xa_lock)
 {
 	unsigned int lock_type = xa_lock_type(xas->xa);

base-commit: 5149100c3aebe5e640d6ff68e0b5e5a7eb8638e0

Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 7 lines · On Thu, 26 Mar 2020 08:14:18 -0700 Matthew Wilcox <[email protected]> wrote:

From: "Matthew Wilcox (Oracle)" <[email protected]>

The XArray has better loops than the IDR has, removing the need to open-code them. We also don't need to call idr_destroy() any more. Allocating the ID is a little tricky due to needing to get 'seq' correct. Open-code a variant of __xa_alloc() which lets us set the ID and the seq before depositing the pointer in the array.

hm, this goes rather deep. Manfred & Davidlohr, are you able to run this through some testing?

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 107

Quoted 10 lines

...

ipc/util.c +1 -0
@@ -104,12 +104,20 @@ static const struct rhashtable_params ipc_kht_params = {
 	.automatic_shrinking	= true,
 };
 
+#ifdef CONFIG_CHECKPOINT_RESTORE

The code grew a few additional CONFIG_CHECKPOINT_RESTORE ifdefs. What's going on here? Why is CRIU special in ipc/?

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 113

Quoted 6 lines
ipc/util.c +6 -0
+#define set_restore_id(ids, x)	ids->restore_id = x
+#define get_restore_id(ids)	ids->restore_id
+#else
+#define set_restore_id(ids, x)	do { } while (0)
+#define get_restore_id(ids)	(-1)
+#endif

Well these are ugly. Can't all this be done in C?

Quoted 3 lines

...

author maintainer
#2 lore raw
Quoted 10 lines · Andrew Morton

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 107

Quoted 7 lines
ipc/util.c +1 -0
@@ -104,12 +104,20 @@ static const struct rhashtable_params ipc_kht_params = {
 	.automatic_shrinking	= true,
 };
 
+#ifdef CONFIG_CHECKPOINT_RESTORE

The code grew a few additional CONFIG_CHECKPOINT_RESTORE ifdefs. What's going on here? Why is CRIU special in ipc/?

"grew a few"? I added (this) one and deleted two others. From in the middle of functions, like we usually prefer.

I mean, this is why we need something like this:

@@ -17,11 +17,11 @@ struct ipc_ids {
...
#ifdef CONFIG_CHECKPOINT_RESTORE
- int next_id;
+ int restore_id;
#endif

Quoted 8 lines

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 113

Quoted 6 lines
ipc/util.c +6 -0
+#define set_restore_id(ids, x)	ids->restore_id = x
+#define get_restore_id(ids)	ids->restore_id
+#else
+#define set_restore_id(ids, x)	do { } while (0)
+#define get_restore_id(ids)	(-1)
+#endif

Well these are ugly. Can't all this be done in C?

Would you rather see it done as:

static inline void set_restore_id(struct ipc_ids *ids, int id) {

#ifdef CONFIG_CHECKPOINT_RESTORE
	ids->restore_id = id;

#endif
}

static inline int get_restore_id(struct ipc_ids *ids) {

#ifdef CONFIG_CHECKPOINT_RESTORE
	return ids->restore_id;
#else
	return -1;

#endif
}

maintainer
#3 lore raw
Quoted 7 lines · On Sat, 18 Apr 2020 14:34:51 -0700 Matthew Wilcox <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 6 lines
ipc/util.c +0 -0
@@ -104,12 +104,20 @@ static const struct rhashtable_params ipc_kht_params = {
 	.automatic_shrinking	= true,
 };
 
Quoted 8 lines
Quoted 4 lines

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 107

Quoted 1 line
ipc/util.c +1 -0
+#ifdef CONFIG_CHECKPOINT_RESTORE

The code grew a few additional CONFIG_CHECKPOINT_RESTORE ifdefs. What's going on here? Why is CRIU special in ipc/?

"grew a few"? I added (this) one and deleted two others. From in the middle of functions, like we usually prefer.

Oh.

Quoted 26 lines
include/linux/ipc_namespace.h +1 -1
@@ -17,11 +17,11 @@ struct ipc_ids {
...
 #ifdef CONFIG_CHECKPOINT_RESTORE
-       int next_id;
+       int restore_id;
 #endif
Quoted 9 lines

ipc/util.c static const struct rhashtable_params ipc_kht_params = { · line 113

Quoted 7 lines
ipc/util.c +6 -0
+#define set_restore_id(ids, x)	ids->restore_id = x
+#define get_restore_id(ids)	ids->restore_id
+#else
+#define set_restore_id(ids, x)	do { } while (0)
+#define get_restore_id(ids)	(-1)
+#endif

Well these are ugly. Can't all this be done in C?

Would you rather see it done as:

static inline void set_restore_id(struct ipc_ids *ids, int id) {

#ifdef CONFIG_CHECKPOINT_RESTORE
	ids->restore_id = id;

#endif
}

Quoted 8 lines

static inline int get_restore_id(struct ipc_ids *ids) {

#ifdef CONFIG_CHECKPOINT_RESTORE
	return ids->restore_id;
#else
	return -1;

#endif
}

Looks nicer. Has type checking regardless of Kconfig. Doesn't have lval-and-rval in one case, neither in the other. Doesn't risk unused-var warnings dependent on Kconfig. Etc.

Could also do

#ifdef CONFIG_CHECKPOINT_RESTORE
static inline void set_restore_id(struct ipc_ids *ids, int id)

{
	ids->restore_id = id;
}

static inline int get_restore_id(struct ipc_ids *ids)

{
	return ids->restore_id;
	return -1;
}

#else
...

#4 lore raw
In reply to Matthew Wilcox

Hi Matthew,

Quoted 31 lines · Matthew Wilcox

From: "Matthew Wilcox (Oracle)" <[email protected]>

The XArray has better loops than the IDR has, removing the need to open-code them. We also don't need to call idr_destroy() any more. Allocating the ID is a little tricky due to needing to get 'seq' correct. Open-code a variant of __xa_alloc() which lets us set the ID and the seq before depositing the pointer in the array.

---

ipc/util.c +6 -14
-		max_idx = max(ids->in_use*3/2, ipc_min_cycle);
-		max_idx = min(max_idx, ipc_mni);
-
-		/* allocate the idx, with a NULL struct kern_ipc_perm */
-		idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
-					GFP_NOWAIT);
-
-		if (idx >= 0) {
-			/*
-			 * idx got allocated successfully.
-			 * Now calculate the sequence number and set the
-			 * pointer for real.
-			 */
-			if (idx <= ids->last_idx) {
+		min_idx = ids->next_idx;
+		new->seq = ids->seq;
+
+		/* Modified version of __xa_alloc */
+		do {
+			xas.xa_index = min_idx;

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 236

Quoted 10 lines
ipc/util.c +6 -0
+			xas_find_marked(&xas, max_idx, XA_FREE_MARK);
+			if (xas.xa_node == XAS_RESTART && min_idx > 0) {
  				ids->seq++;
  				if (ids->seq >= ipcid_seq_max())
  					ids->seq = 0;
+				new->seq = ids->seq;
+				xas.xa_index = 0;
+				min_idx = 0;
+				xas_find_marked(&xas, max_idx, XA_FREE_MARK);
  			}

Is is nessary to have that many details of xarray in ipc/util?

This function is not performance critical.

The core requirement is that ipc_obtain_object_check() must scale.

Would it be possible to use something like

    xa_alloc(,entry=NULL,)

    new->seq = ...

    xa_store(,entry=new,);

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 249

Quoted 7 lines
ipc/util.c +0 -7
-			ids->last_idx = idx;
-
-			new->seq = ids->seq;
-			/* no need for smp_wmb(), this is done
-			 * inside idr_replace, as part of
-			 * rcu_assign_pointer
-			 */

Could you leave the memory barrier comments in the code?

The rcu_assign_pointer() is the first hands-off from semget() or msgget().

Before the rcu_assign_pointer, e.g. semop() calls would return -EINVAL;

After the rcu_assign_pointer, semwhatever() must work - and e.g. the permission checks are lockless.

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 241

Quoted 7 lines
ipc/util.c +5 -2
-			idr_replace(&ids->ipcs_idr, new, idx);
-		}
+			if (xas.xa_node == XAS_RESTART)
+				xas_set_err(&xas, -ENOSPC);
+			else
+				new->id = (new->seq << ipcmni_seq_shift()) +
+					xas.xa_index;

Setting new->id should remain at the end, outside any locking:

The variable has no special protection, access is only allowed after proper locking, thus no need to have the initialization in the middle.

What is crucial is that the final value of new->seq is visible to all cpus before a storing the pointer.

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 245

Quoted 4 lines
ipc/util.c +4 -0
+			xas_store(&xas, new);
+			xas_clear_mark(&xas, XA_FREE_MARK);
+		} while (__xas_nomem(&xas, GFP_KERNEL));
+

Just for my curiosity:

If the xas is in an invalid state, then xas_store() will not store anything. Thus the loop will not store "new" multiple times, it will be stored only once.

@@ -472,7 +487,7 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)

ipc/util.c void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) · line 493

Quoted 8 lines

idx--;
if (idx == -1)
break;

ipc/util.c +1 -1
-		} while (!idr_find(&ids->ipcs_idr, idx));
+		} while (!xa_load(&ids->ipcs, idx));
  		ids->max_idx = idx;
  	}
  }

Is there an xa_find_last() function?

It is outside of any hot path, I have a patch that does a binary search with idr_get_next().

If there is no xa_find_last(), then I would rebase that patch.

--

    Manfred

author maintainer
#5 lore raw
Quoted 37 lines · Manfred Spraul
Quoted 20 lines
ipc/util.c +6 -14
-		max_idx = max(ids->in_use*3/2, ipc_min_cycle);
-		max_idx = min(max_idx, ipc_mni);
-
-		/* allocate the idx, with a NULL struct kern_ipc_perm */
-		idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
-					GFP_NOWAIT);
-
-		if (idx >= 0) {
-			/*
-			 * idx got allocated successfully.
-			 * Now calculate the sequence number and set the
-			 * pointer for real.
-			 */
-			if (idx <= ids->last_idx) {
+		min_idx = ids->next_idx;
+		new->seq = ids->seq;
+
+		/* Modified version of __xa_alloc */
+		do {
+			xas.xa_index = min_idx;

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 236

Quoted 10 lines
ipc/util.c +6 -0
+			xas_find_marked(&xas, max_idx, XA_FREE_MARK);
+			if (xas.xa_node == XAS_RESTART && min_idx > 0) {
  				ids->seq++;
  				if (ids->seq >= ipcid_seq_max())
  					ids->seq = 0;
+				new->seq = ids->seq;
+				xas.xa_index = 0;
+				min_idx = 0;
+				xas_find_marked(&xas, max_idx, XA_FREE_MARK);
  			}

Is is nessary to have that many details of xarray in ipc/util?

This function is not performance critical.

The core requirement is that ipc_obtain_object_check() must scale.

Quoted 7 lines

Would it be possible to use something like

    xa_alloc(,entry=NULL,)

    new->seq = ...

    xa_store(,entry=new,);

Yes, that would absolutely be possible, and some users do exactly that. I thought that creating a new message queue / semaphore set / shared memory segment would be performance sensitive.

Quoted 10 lines

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 249

Quoted 7 lines
ipc/util.c +0 -7
-			ids->last_idx = idx;
-
-			new->seq = ids->seq;
-			/* no need for smp_wmb(), this is done
-			 * inside idr_replace, as part of
-			 * rcu_assign_pointer
-			 */

Could you leave the memory barrier comments in the code?

Quoted 6 lines

The rcu_assign_pointer() is the first hands-off from semget() or msgget().

Before the rcu_assign_pointer, e.g. semop() calls would return -EINVAL;

After the rcu_assign_pointer, semwhatever() must work - and e.g. the permission checks are lockless.

How about simply:

			/* xa_store contains a memory barrier */
Quoted 10 lines

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 241

Quoted 7 lines
ipc/util.c +5 -2
-			idr_replace(&ids->ipcs_idr, new, idx);
-		}
+			if (xas.xa_node == XAS_RESTART)
+				xas_set_err(&xas, -ENOSPC);
+			else
+				new->id = (new->seq << ipcmni_seq_shift()) +
+					xas.xa_index;

Setting new->id should remain at the end, outside any locking:

Quoted 5 lines

The variable has no special protection, access is only allowed after proper locking, thus no need to have the initialization in the middle.

What is crucial is that the final value of new->seq is visible to all cpus before a storing the pointer.

The IPC locking is weird. Most users spin_lock the xarray/idr/radix tree for modifications, and on the read-side use RCU to protect the lookup and a refcount on the object looked up in it (after which, RCU is unlocked). IPC seems to hold the RCU lock much longer, and it has a per-object spinlock rather than refcount. And it has an rwsem. It feels like it could be much simpler, but I haven't had time to dig into it and figure out why it's so different from all the other users. Maybe it's just older code.

Quoted 10 lines

ipc/util.c static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) · line 245

Quoted 4 lines
ipc/util.c +4 -0
+			xas_store(&xas, new);
+			xas_clear_mark(&xas, XA_FREE_MARK);
+		} while (__xas_nomem(&xas, GFP_KERNEL));
+

Just for my curiosity:

If the xas is in an invalid state, then xas_store() will not store anything. Thus the loop will not store "new" multiple times, it will be stored only once.

Correct, although we're going to delete this loop entirely.

Quoted 11 lines
(quoted diff) +0 -0
@@ -472,7 +487,7 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm
 *ipcp)
 >   			idx--;
 >   			if (idx == -1)
 >   				break;
 > -		} while (!idr_find(&ids->ipcs_idr, idx));
 > +		} while (!xa_load(&ids->ipcs, idx));
 >   		ids->max_idx = idx;
Quoted 2 lines
  	}
  }
Quoted 6 lines

Is there an xa_find_last() function?

It is outside of any hot path, I have a patch that does a binary search with idr_get_next().

If there is no xa_find_last(), then I would rebase that patch.

There is not currently an xa_find_last(). It shouldn't be too hard to add; start at the top of the tree and walk backwards in each node until finding a non-NULL entry. Of course, it'll need documentation and test cases.

author maintainer
#6 lore raw
Quoted 5 lines · Matthew Wilcox
Quoted 4 lines · Manfred Spraul

Would it be possible to use something like

    xa_alloc(,entry=NULL,)

Quoted 7 lines
Quoted 3 lines

    new->seq = ...

    xa_store(,entry=new,);

Yes, that would absolutely be possible, and some users do exactly that. I thought that creating a new message queue / semaphore set / shared memory segment would be performance sensitive.

Updated:

From 386556c2b1544463c2a05e0f48da2187960ec544 Mon Sep 17 00:00:00 2001
From: "Matthew Wilcox (Oracle)" <[email protected]>
Date: Sun, 4 Aug 2019 07:20:30 -0400
Subject: [PATCH] ipc: Convert ipcs_idr to XArray

The XArray has better loops than the IDR has, removing the need to open-code them. We also don't need to call idr_destroy() any more. Allocating the ID is a little tricky due to needing to get 'seq' correct. Open-code a variant of __xa_alloc() which lets us set the ID and the seq before depositing the pointer in the array.

---
 include/linux/ipc_namespace.h |  10 +-
 ipc/ipc_sysctl.c              |  14 +--
 ipc/msg.c                     |   1 -
 ipc/namespace.c               |  13 +--
 ipc/sem.c                     |   1 -
 ipc/shm.c                     |  60 +++++-------
 ipc/util.c                    | 171 ++++++++++++++++------------------
 ipc/util.h                    |   4 +-
 8 files changed, 122 insertions(+), 152 deletions(-)
include/linux/ipc_namespace.h +5 -5
index c309f43bde45..bdc39cc4d1dc 100644
@@ -3,13 +3,13 @@
 #define __IPC_NAMESPACE_H__
 
 #include <linux/err.h>
-#include <linux/idr.h>
-#include <linux/rwsem.h>
 #include <linux/notifier.h>
 #include <linux/nsproxy.h>
 #include <linux/ns_common.h>
 #include <linux/refcount.h>
 #include <linux/rhashtable-types.h>
+#include <linux/rwsem.h>
+#include <linux/xarray.h>
 
 struct user_namespace;
 
@@ -17,11 +17,11 @@ struct ipc_ids {
 	int in_use;
 	unsigned short seq;
 	struct rw_semaphore rwsem;
-	struct idr ipcs_idr;
+	struct xarray ipcs;
 	int max_idx;
-	int last_idx;	/* For wrap around detection */
+	int next_idx;
 #ifdef CONFIG_CHECKPOINT_RESTORE
-	int next_id;
+	int restore_id;
 #endif
 	struct rhashtable key_ht;
 };
ipc/ipc_sysctl.c +7 -7
index affd66537e87..a2df5d0e27f6 100644
@@ -115,7 +115,7 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
 
 int ipc_mni = IPCMNI;
 int ipc_mni_shift = IPCMNI_SHIFT;
-int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
+int ipc_min_cycle = XA_CHUNK_SIZE;
 
 static struct ctl_table ipc_kern_table[] = {
 	{
@@ -196,8 +196,8 @@ static struct ctl_table ipc_kern_table[] = {
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	{
 		.procname	= "sem_next_id",
-		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
@@ -205,8 +205,8 @@ static struct ctl_table ipc_kern_table[] = {
 	},
 	{
 		.procname	= "msg_next_id",
-		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
@@ -214,8 +214,8 @@ static struct ctl_table ipc_kern_table[] = {
 	},
 	{
 		.procname	= "shm_next_id",
-		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
-		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
+		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].restore_id,
+		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].restore_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
ipc/msg.c +0 -1
index caca67368cb5..0c4fdc2f5c08 100644
@@ -1308,7 +1308,6 @@ void msg_init_ns(struct ipc_namespace *ns)
 void msg_exit_ns(struct ipc_namespace *ns)
 {
 	free_ipcs(ns, &msg_ids(ns), freeque);
-	idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
 	rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht);
 }
 #endif
ipc/namespace.c +4 -9
index b3ca1476ca51..e8567c3d32e9 100644
@@ -96,22 +96,17 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
 	       void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
 {
 	struct kern_ipc_perm *perm;
-	int next_id;
-	int total, in_use;
+	unsigned long index;
 
 	down_write(&ids->rwsem);
 
-	in_use = ids->in_use;
-
-	for (total = 0, next_id = 0; total < in_use; next_id++) {
-		perm = idr_find(&ids->ipcs_idr, next_id);
-		if (perm == NULL)
-			continue;
+	xa_for_each(&ids->ipcs, index, perm) {
 		rcu_read_lock();
 		ipc_lock_object(perm);
 		free(ns, perm);
-		total++;
 	}
+	BUG_ON(!xa_empty(&ids->ipcs));
+
 	up_write(&ids->rwsem);
 }
 
ipc/sem.c +0 -1
index 3687b71151b3..8d6550ac035a 100644
@@ -258,7 +258,6 @@ void sem_init_ns(struct ipc_namespace *ns)
 void sem_exit_ns(struct ipc_namespace *ns)
 {
 	free_ipcs(ns, &sem_ids(ns), freeary);
-	idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
 	rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
 }
 #endif
ipc/shm.c +23 -37

90 lines not on this page · raw message

ipc/util.c +81 -90

269 lines not on this page · raw message

ipc/util.h +2 -2
index 5766c61aed0e..04d49db4cefa 100644
@@ -27,7 +27,7 @@
  */
 #define IPCMNI_SHIFT		15
 #define IPCMNI_EXTEND_SHIFT	24
-#define IPCMNI_EXTEND_MIN_CYCLE	(RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE)
+#define IPCMNI_EXTEND_MIN_CYCLE	(XA_CHUNK_SIZE * XA_CHUNK_SIZE)
 #define IPCMNI			(1 << IPCMNI_SHIFT)
 #define IPCMNI_EXTEND		(1 << IPCMNI_EXTEND_SHIFT)
 
@@ -42,7 +42,7 @@ extern int ipc_min_cycle;
 #else /* CONFIG_SYSVIPC_SYSCTL */
 
 #define ipc_mni			IPCMNI
-#define ipc_min_cycle		((int)RADIX_TREE_MAP_SIZE)
+#define ipc_min_cycle		((int)XA_CHUNK_SIZE)
 #define ipcmni_seq_shift()	IPCMNI_SHIFT
 #define IPCMNI_IDX_MASK		((1 << IPCMNI_SHIFT) - 1)
 #endif /* CONFIG_SYSVIPC_SYSCTL */
Signature · 1 line
2.26.1