ipc: Allow boot time extension of IPCMNI from 32k to 16M
Subsystems (2)
PATCH RESEND 1/3 ipc: Allow boot time extension of IPCMNI from 32k to 16M
The maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more, especially those that are migrating from Solaris which uses 24 bits for unique identifiers. To satisfy the need of those users, a new boot time kernel option "ipcmni_extend" is added to extend the IPCMNI value to 16M. This is a 512X increase which should be big enough for users out there that need a large number of unique IPC identifier.
The use of this new option will change the pattern of the IPC identifiers returned by functions like shmget(2). An application that depends on such pattern may not work properly. So it should only be used if the users really need more than 32k of unique IPC numbers.
This new option does have the side effect of reducing the maximum number of unique sequence numbers from 64k down to 128. So it is a trade-off.
The computation of a new IPC id is not done in the performance critical path. So a little bit of additional overhead shouldn't have any real performance impact.
--- .../admin-guide/kernel-parameters.txt | 3 ++ ipc/ipc_sysctl.c | 12 ++++- ipc/util.c | 10 ++--- ipc/util.h | 44 +++++++++++++++---- 4 files changed, 54 insertions(+), 15 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -1828,6 +1828,9 @@ ip= [IP_PNP] See Documentation/filesystems/nfs/nfsroot.txt. + ipcmni_extend [KNL] Extend the maximum number of unique System V + IPC identifiers from 32,768 to 16,777,216. + irqaffinity= [SMP] Set the default irq affinity mask The argument is a cpu list, as described above.
ipc/ipc_sysctl.c +11 -1
@@ -120,7 +120,8 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,static int zero;static int one =1 ;static int int_max =INT_MAX ; -static int ipc_mni =IPCMNI ; +int ipc_mni =IPCMNI ; +int ipc_mni_shift =IPCMNI_SHIFT ;static struct ctl_table ipc_kern_table[] = { {
@@ -246,3 +247,12 @@ static int __init ipc_sysctl_init(void) }device_initcall (ipc_sysctl_init); + +static int __init ipc_mni_extend (char *str) +{ + ipc_mni =IPCMNI_EXTEND ; + ipc_mni_shift =IPCMNI_EXTEND_SHIFT ; +pr_info ("IPCMNI extended to %d.\n" , ipc_mni); +return 0 ; +} +early_param ("ipcmni_extend" , ipc_mni_extend);
ipc/util.c +5 -5
@@ -110,7 +110,7 @@ static const struct rhashtable_params ipc_kht_params = {* @ids: ipc identifier set * * Set up the sequence range to use for the ipc identifier range (limited -* below IPCMNI) then initialise the keys hashtable and ids idr. +* below ipc_mni) then initialise the keys hashtable and ids idr. */ void ipc_init_ids (struct ipc_ids *ids) {
@@ -226,7 +226,7 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) 0, GFP_NOWAIT); }if (idx >=0 ) - new->id =SEQ_MULTIPLIER * new->seq + idx; + new->id = (new->seq <<IPCMNI_SEQ_SHIFT ) + idx;return idx; }
@@ -254,8 +254,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)/* 1) Initialize the refcount so that ipc_rcu_putref works */ refcount_set (&new->refcount,1 ); -if (limit >IPCMNI ) - limit =IPCMNI ; +if (limit > ipc_mni) + limit = ipc_mni;if (ids->in_use >= limit)return -ENOSPC ;
@@ -738,7 +738,7 @@ static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,if (total >= ids->in_use)return NULL ; -for (; pos <IPCMNI ; pos++) { +for (; pos < ipc_mni; pos++) { ipc =idr_find (&ids->ipcs_idr, pos);if (ipc !=NULL ) { *new_pos = pos +1 ;
ipc/util.h +35 -9
70 lines not on this page · raw message
Signature · 1 line
2.18.1
PATCH RESEND 2/3 ipc: Conserve sequence numbers in ipcmni_extend mode
From: Manfred Spraul <[email protected]>
Rewrite, based on the patch from Waiman Long:
The mixing in of a sequence number into the IPC IDs is probably to avoid ID reuse in userspace as much as possible. With ipcmni_extend mode, the number of usable sequence numbers is greatly reduced leading to higher chance of ID reuse.
To address this issue, we need to conserve the sequence number space as much as possible. Right now, the sequence number is incremented for every new ID created. In reality, we only need to increment the sequence number when new allocated ID is not greater than the last one allocated. It is in such case that the new ID may collide with an existing one. This is being done irrespective of the ipcmni mode.
In order to avoid any races, the index is first allocated and then the pointer is replaced.
Changes compared to the initial patch:
- Handle failures from idr_alloc().
- Avoid that concurrent operations can see the wrong sequence number.
(This is achieved by using idr_replace()).
- IPCMNI_SEQ_SHIFT is not a constant, thus renamed to
ipcmni_seq_shift(). - IPCMNI_SEQ_MAX is not a constant, thus renamed to ipcmni_seq_max().
Original-patch-from: Waiman Long <[email protected]>
--- include/linux/ipc_namespace.h | 1 + ipc/util.c | 35 ++++++++++++++++++++++++++++++----- ipc/util.h | 8 ++++---- 3 files changed, 35 insertions(+), 9 deletions(-)
include/linux/ipc_namespace.h +1 -0
@@ -19,6 +19,7 @@ struct ipc_ids {struct rw_semaphore rwsem;struct idr ipcs_idr;int max_idx; +int last_idx;/* For wrap around detection */ #ifdef CONFIG_CHECKPOINT_RESTORE int next_id;#endif
ipc/util.c +30 -5
@@ -120,6 +120,7 @@ void ipc_init_ids(struct ipc_ids *ids)rhashtable_init (&ids->key_ht, &ipc_kht_params);idr_init (&ids->ipcs_idr); ids->max_idx =-1 ; + ids->last_idx =-1 ;#ifdef CONFIG_CHECKPOINT_RESTORE ids->next_id =-1 ;#endif
@@ -193,6 +194,10 @@ static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)* * The caller must own kern_ipc_perm.lock.of the new object. * On error, the function returns a (negative) error code. +* +* To conserve sequence number space, especially with extended ipc_mni, +* the sequence number is incremented only when the returned ID is less than +* the last one. */ static inline int ipc_idr_alloc (struct ipc_ids *ids,struct kern_ipc_perm *new) {
@@ -216,17 +221,37 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)*/ if (next_id <0 ) {/* !CHECKPOINT_RESTORE or next_id is unset */ - new->seq = ids->seq++; -if (ids->seq >IPCID_SEQ_MAX ) - ids->seq =0 ; - idx =idr_alloc (&ids->ipcs_idr, new,0 ,0 ,GFP_NOWAIT ); + +/* allocate the idx, with a NULL struct kern_ipc_perm */ + idx =idr_alloc (&ids->ipcs_idr,NULL ,0 ,0 ,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) { + ids->seq++; +if (ids->seq >=ipcid_seq_max ()) + ids->seq =0 ; + } + 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 +*/ +idr_replace (&ids->ipcs_idr, new, idx); + } }else { new->seq =ipcid_to_seqx (next_id); idx =idr_alloc (&ids->ipcs_idr, new,ipcid_to_idx (next_id),0 ,GFP_NOWAIT ); }if (idx >=0 ) - new->id = (new->seq <<IPCMNI_SEQ_SHIFT ) + idx; + new->id = (new->seq <<ipcmni_seq_shift ()) + idx;return idx; }
ipc/util.h +4 -4
@@ -34,13 +34,13 @@extern int ipc_mni;extern int ipc_mni_shift; -#define IPCMNI_SEQ_SHIFT ipc_mni_shift +#define ipcmni_seq_shift () ipc_mni_shift#define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1)#else /* CONFIG_SYSVIPC_SYSCTL */ #define ipc_mni IPCMNI -#define IPCMNI_SEQ_SHIFT IPCMNI_SHIFT +#define ipcmni_seq_shift () IPCMNI_SHIFT#define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1)#endif /* CONFIG_SYSVIPC_SYSCTL */
@@ -123,8 +123,8 @@ struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);#define IPC_SHM_IDS 2#define ipcid_to_idx (id) ((id) & IPCMNI_IDX_MASK) -#define ipcid_to_seqx (id) ((id) >> IPCMNI_SEQ_SHIFT) -#define IPCID_SEQ_MAX (INT_MAX >> IPCMNI_SEQ_SHIFT) +#define ipcid_to_seqx (id) ((id) >> ipcmni_seq_shift()) +#define ipcid_seq_max () (INT_MAX >> ipcmni_seq_shift())/* must be called with ids->rwsem acquired for writing */ int ipc_addid (struct ipc_ids *,struct kern_ipc_perm *,int );
Signature · 1 line
2.18.1
Re: [RESEND PATCH 2/3] ipc: Conserve sequence numbers in ipcmni_extend mode
Quoted 10 lines · On Fri, 29 Mar 2019 16:49:29 -0400 Waiman Long <[email protected]> wrote:
From: Manfred Spraul <[email protected]>
Rewrite, based on the patch from Waiman Long:
...
Suggested-by Matthew Wilcox <[email protected]>Original-patch-from: Waiman Long <[email protected]>
Signed-off-by Manfred Spraul <[email protected]>Acked-by Waiman Long <[email protected]>
I added your Signed-off-by: to this. Because you contributed and because you were on the patch delivery path (either of these require a SOB).
And I removed the Original-patch-from line. The changelog already says that and Original-patch-from isn't a standard changelog tag.
Re: [RESEND PATCH 2/3] ipc: Conserve sequence numbers in ipcmni_extend mode
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Fri, 29 Mar 2019 16:49:29 -0400 Waiman Long <[email protected]> wrote:
From: Manfred Spraul <[email protected]>
Rewrite, based on the patch from Waiman Long:
...
Suggested-by Matthew Wilcox <[email protected]>Original-patch-from: Waiman Long <[email protected]>
Signed-off-by Manfred Spraul <[email protected]>Acked-by Waiman Long <[email protected]>
Quoted 6 lines
I added your Signed-off-by: to this. Because you contributed and because you were on the patch delivery path (either of these require a SOB).
And I removed the Original-patch-from line. The changelog already says that and Original-patch-from isn't a standard changelog tag.
Thanks for including those patches in your tree. Hopefully they can make 5.2.
Cheers,
Longman
PATCH RESEND 3/3 ipc: Do cyclic id allocation for the ipc object.
In reply to Waiman LongFrom: Manfred Spraul <[email protected]>
For ipcmni_extend mode, the sequence number space is only 7 bits. So the chance of id reuse is relatively high compared with the non-extended mode.
To alleviate this id reuse problem, this patch enables cyclic allocation for the index to the radix tree (idx).
The disadvantage is that this can cause a slight slow-down of the fast path, as the radix tree could be higher than necessary.
To limit the radix tree height, I have chosen the following limits:
- 1) The cycling is done over in_use*1.5.
- 2) At least, the cycling is done over
"normal" ipcnmi mode: RADIX_TREE_MAP_SIZE elements "ipcmni_extended": 4096 elements
Result:
- for normal mode:
No change for <= 42 active ipc elements. With more than 42 active ipc elements, a 2nd level would be added to the radix tree.
Without cyclic allocation, a 2nd level would be added only with more than 63 active elements.
- for extended mode:
Cycling creates always at least a 2-level radix tree. With more than 2730 active objects, a 3rd level would be added, instead of > 4095 active objects until the 3rd level is added without cyclic allocation.
For a 2-level radix tree compared to a 1-level radix tree, I have observed < 1% performance impact.
Notes:
1) Normal "x=semget();y=semget();" is unaffected: Then the idx is e.g. a and a+1, regardless if idr_alloc() or idr_alloc_cyclic() is used.
2) The -1% happens in a microbenchmark after this situation:
x=semget();
for(i=0;i<4000;i++) {t=semget();semctl(t,0,IPC_RMID);}
y=semget();
Now perform semget calls on x and y that do not sleep.3) The worst-case reuse cycle time is unfortunately unaffected:
If you have 2^24-1 ipc objects allocated, and get/remove the last possible element in a loop, then the id is reused after 128 get/remove pairs.
Performance check:
A microbenchmark that performes no-op semop() randomly on two IDs, with only these two IDs allocated.
The IDs were set using /proc/sys/kernel/sem_next_id. The test was run 5 times, averages are shown.
1 & 2: Base (6.22 seconds for 10.000.000 semops)
1 & 40: -0.2%
1 & 3348: - 0.8%
1 & 27348: - 1.6%
1 & 15777204: - 3.2%
Or: ~12.6 cpu cycles per additional radix tree level. The cpu is an Intel I3-5010U. ~1300 cpu cycles/syscall is slower than what I remember (spectre impact?).
V2 of the patch:
- use "min" and "max"
- use RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE instead of
(2<<12).
--- ipc/ipc_sysctl.c | 2 ++ ipc/util.c | 7 ++++++- ipc/util.h | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-)
ipc/ipc_sysctl.c +2 -0
@@ -122,6 +122,7 @@ static int one = 1;static int int_max =INT_MAX ;int ipc_mni =IPCMNI ;int ipc_mni_shift =IPCMNI_SHIFT ; +int ipc_min_cycle =RADIX_TREE_MAP_SIZE ;static struct ctl_table ipc_kern_table[] = { {
@@ -252,6 +253,7 @@ static int __init ipc_mni_extend(char *str) { ipc_mni =IPCMNI_EXTEND ; ipc_mni_shift =IPCMNI_EXTEND_SHIFT ; + ipc_min_cycle =IPCMNI_EXTEND_MIN_CYCLE ;pr_info ("IPCMNI extended to %d.\n" , ipc_mni);return 0 ; }
ipc/util.c +6 -1
@@ -221,9 +221,14 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)*/ if (next_id <0 ) {/* !CHECKPOINT_RESTORE or next_id is unset */ +int max_idx; + + 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 (&ids->ipcs_idr,NULL ,0 ,0 ,GFP_NOWAIT ); + idx =idr_alloc_cyclic (&ids->ipcs_idr,NULL ,0 , max_idx, +GFP_NOWAIT );if (idx >=0 ) {/*
ipc/util.h +3 -0
@@ -27,12 +27,14 @@*/ #define IPCMNI_SHIFT 15#define IPCMNI_EXTEND_SHIFT 24 +#define IPCMNI_EXTEND_MIN_CYCLE (RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE)#define IPCMNI (1 << IPCMNI_SHIFT)#define IPCMNI_EXTEND (1 << IPCMNI_EXTEND_SHIFT)#ifdef CONFIG_SYSVIPC_SYSCTL extern int ipc_mni;extern int ipc_mni_shift; +extern int ipc_min_cycle;#define ipcmni_seq_shift () ipc_mni_shift#define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1)
@@ -40,6 +42,7 @@ extern int ipc_mni_shift;#else /* CONFIG_SYSVIPC_SYSCTL */ #define ipc_mni IPCMNI +#define ipc_min_cycle RADIX_TREE_MAP_SIZE#define ipcmni_seq_shift () IPCMNI_SHIFT#define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1)#endif /* CONFIG_SYSVIPC_SYSCTL */
Signature · 1 line
2.18.1