Skip to contentSkip to conversation

sysvipc: make get_maxid O(1) again

PATCH New [email protected] 1 participant 1 message Nov 17, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 86/94 sysvipc: make get_maxid O(1) again

From: Davidlohr Bueso <[email protected]>
Subject: sysvipc: make get_maxid O(1) again

For a custom microbenchmark on a 3.30GHz Xeon SandyBridge, which calls IPC_STAT over and over, it was calculated that, on avg the cost of ipc_get_maxid() for increasing amounts of keys was:

10 keys: ~900 cycles
100 keys: ~15000 cycles
1000 keys: ~150000 cycles
10000 keys: ~2100000 cycles

This is unsurprising as maxid is currently O(n).

By having the max_id available in O(1) we save all those cycles for each semctl(_STAT) command, the idr_find can be expensive -- which some real (customer) workloads actually poll on. Note that this used to be the case, until 7ca7e564e04 ("ipc: store ipcs into IDRs"). The cost is the extra idr_find when doing RMIDs, but we simply go backwards, and should not take too many iterations to find the new value.

---

 include/linux/ipc_namespace.h |    1 
 ipc/util.c                    |   43 +++++++++-----------------------
 ipc/util.h                    |   21 +++++++++++++--
 3 files changed, 32 insertions(+), 33 deletions(-)
include/linux/ipc_namespace.h renamed from include/linux/ipc_namespace.h~sysvipc-make-get_maxid-o1-again +1 -0
diff -puN include/linux/ipc_namespace.h~sysvipc-make-get_maxid-o1-again include/linux/ipc_namespace.h
@@ -19,6 +19,7 @@ struct ipc_ids {
 	bool tables_initialized;
 	struct rw_semaphore rwsem;
 	struct idr ipcs_idr;
+	int max_id;
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	int next_id;
 #endif
ipc/util.c renamed from ipc/util.c~sysvipc-make-get_maxid-o1-again +13 -30

67 lines not on this page · raw message

ipc/util.h renamed from ipc/util.h~sysvipc-make-get_maxid-o1-again +18 -3
diff -puN ipc/util.h~sysvipc-make-get_maxid-o1-again ipc/util.h
@@ -13,6 +13,7 @@
 
 #include <linux/unistd.h>
 #include <linux/err.h>
+#include <linux/ipc_namespace.h>
 
 #define SEQ_MULTIPLIER	(IPCMNI)
 
@@ -99,9 +100,6 @@ void __init ipc_init_proc_interface(cons
 /* must be called with ids->rwsem acquired for writing */
 int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
 
-/* must be called with ids->rwsem acquired for reading */
-int ipc_get_maxid(struct ipc_ids *);
-
 /* must be called with both locks acquired. */
 void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
 
@@ -111,6 +109,23 @@ void ipc_set_key_private(struct ipc_ids
 /* must be called with ipcp locked */
 int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
 
+/**
+ * ipc_get_maxid - get the last assigned id
+ * @ids: ipc identifier set
+ *
+ * Called with ipc_ids.rwsem held for reading.
+ */
+static inline int ipc_get_maxid(struct ipc_ids *ids)
+{
+	if (ids->in_use == 0)
+		return -1;
+
+	if (ids->in_use == IPCMNI)
+		return IPCMNI - 1;
+
+	return ids->max_id;
+}
+
 /*
  * For allocation that need to be freed by RCU.
  * Objects are reference counted, they start with reference count 1.

_