Skip to content

Commit ab602f7

Browse files
Jack Millertorvalds
authored andcommitted
shm: make exit_shm work proportional to task activity
This is small set of patches our team has had kicking around for a few versions internally that fixes tasks getting hung on shm_exit when there are many threads hammering it at once. Anton wrote a simple test to cause the issue: http://ozlabs.org/~anton/junkcode/bust_shm_exit.c Before applying this patchset, this test code will cause either hanging tracebacks or pthread out of memory errors. After this patchset, it will still produce output like: root@somehost:~# ./bust_shm_exit 1024 160 ... INFO: rcu_sched detected stalls on CPUs/tasks: {} (detected by 116, t=2111 jiffies, g=241, c=240, q=7113) INFO: Stall ended before state dump start ... But the task will continue to run along happily, so we consider this an improvement over hanging, even if it's a bit noisy. This patch (of 3): exit_shm obtains the ipc_ns shm rwsem for write and holds it while it walks every shared memory segment in the namespace. Thus the amount of work is related to the number of shm segments in the namespace not the number of segments that might need to be cleaned. In addition, this occurs after the task has been notified the thread has exited, so the number of tasks waiting for the ns shm rwsem can grow without bound until memory is exausted. Add a list to the task struct of all shmids allocated by this task. Init the list head in copy_process. Use the ns->rwsem for locking. Add segments after id is added, remove before removing from id. On unshare of NEW_IPCNS orphan any ids as if the task had exited, similar to handling of semaphore undo. I chose a define for the init sequence since its a simple list init, otherwise it would require a function call to avoid include loops between the semaphore code and the task struct. Converting the list_del to list_del_init for the unshare cases would remove the exit followed by init, but I left it blow up if not inited. Signed-off-by: Milton Miller <miltonm@bga.com> Signed-off-by: Jack Miller <millerjo@us.ibm.com> Cc: Davidlohr Bueso <davidlohr@hp.com> Cc: Manfred Spraul <manfred@colorfullife.com> Cc: Anton Blanchard <anton@samba.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9687fd9 commit ab602f7

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct sched_param {
3333

3434
#include <linux/smp.h>
3535
#include <linux/sem.h>
36+
#include <linux/shm.h>
3637
#include <linux/signal.h>
3738
#include <linux/compiler.h>
3839
#include <linux/completion.h>
@@ -1385,6 +1386,7 @@ struct task_struct {
13851386
#ifdef CONFIG_SYSVIPC
13861387
/* ipc stuff */
13871388
struct sysv_sem sysvsem;
1389+
struct sysv_shm sysvshm;
13881390
#endif
13891391
#ifdef CONFIG_DETECT_HUNG_TASK
13901392
/* hung task detection */

‎include/linux/shm.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _LINUX_SHM_H_
22
#define _LINUX_SHM_H_
33

4+
#include <linux/list.h>
45
#include <asm/page.h>
56
#include <uapi/linux/shm.h>
67
#include <asm/shmparam.h>
@@ -20,6 +21,7 @@ struct shmid_kernel /* private to the kernel */
2021

2122
/* The task created the shm object. NULL if the task is dead. */
2223
struct task_struct *shm_creator;
24+
struct list_head shm_clist; /* list by creator */
2325
};
2426

2527
/* shm_mode upper byte flags */
@@ -44,11 +46,20 @@ struct shmid_kernel /* private to the kernel */
4446
#define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
4547

4648
#ifdef CONFIG_SYSVIPC
49+
struct sysv_shm {
50+
struct list_head shm_clist;
51+
};
52+
4753
long do_shmat(int shmid, char __user *shmaddr, int shmflg, unsigned long *addr,
4854
unsigned long shmlba);
4955
extern int is_file_shm_hugepages(struct file *file);
50-
extern void exit_shm(struct task_struct *task);
56+
void exit_shm(struct task_struct *task);
57+
#define shm_init_task(task) INIT_LIST_HEAD(&(task)->sysvshm.shm_clist)
5158
#else
59+
struct sysv_shm {
60+
/* empty */
61+
};
62+
5263
static inline long do_shmat(int shmid, char __user *shmaddr,
5364
int shmflg, unsigned long *addr,
5465
unsigned long shmlba)
@@ -62,6 +73,9 @@ static inline int is_file_shm_hugepages(struct file *file)
6273
static inline void exit_shm(struct task_struct *task)
6374
{
6475
}
76+
static inline void shm_init_task(struct task_struct *task)
77+
{
78+
}
6579
#endif
6680

6781
#endif /* _LINUX_SHM_H_ */

‎ipc/shm.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ static void shm_rcu_free(struct rcu_head *head)
178178

179179
static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
180180
{
181+
list_del(&s->shm_clist);
181182
ipc_rmid(&shm_ids(ns), &s->shm_perm);
182183
}
183184

@@ -268,14 +269,10 @@ static void shm_close(struct vm_area_struct *vma)
268269
}
269270

270271
/* Called with ns->shm_ids(ns).rwsem locked */
271-
static int shm_try_destroy_current(int id, void *p, void *data)
272+
static void shm_mark_orphan(struct shmid_kernel *shp, struct ipc_namespace *ns)
272273
{
273-
struct ipc_namespace *ns = data;
274-
struct kern_ipc_perm *ipcp = p;
275-
struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
276-
277-
if (shp->shm_creator != current)
278-
return 0;
274+
if (WARN_ON(shp->shm_creator != current)) /* Remove me when it works */
275+
return;
279276

280277
/*
281278
* Mark it as orphaned to destroy the segment when
@@ -289,13 +286,12 @@ static int shm_try_destroy_current(int id, void *p, void *data)
289286
* is not set, it shouldn't be deleted here.
290287
*/
291288
if (!ns->shm_rmid_forced)
292-
return 0;
289+
return;
293290

294291
if (shm_may_destroy(ns, shp)) {
295292
shm_lock_by_ptr(shp);
296293
shm_destroy(ns, shp);
297294
}
298-
return 0;
299295
}
300296

301297
/* Called with ns->shm_ids(ns).rwsem locked */
@@ -333,14 +329,17 @@ void shm_destroy_orphaned(struct ipc_namespace *ns)
333329
void exit_shm(struct task_struct *task)
334330
{
335331
struct ipc_namespace *ns = task->nsproxy->ipc_ns;
332+
struct shmid_kernel *shp, *n;
336333

337334
if (shm_ids(ns).in_use == 0)
338335
return;
339336

340337
/* Destroy all already created segments, but not mapped yet */
341338
down_write(&shm_ids(ns).rwsem);
342-
if (shm_ids(ns).in_use)
343-
idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
339+
list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist)
340+
shm_mark_orphan(shp, ns);
341+
/* remove the list head from any segments still attached */
342+
list_del(&task->sysvshm.shm_clist);
344343
up_write(&shm_ids(ns).rwsem);
345344
}
346345

@@ -561,6 +560,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
561560
shp->shm_nattch = 0;
562561
shp->shm_file = file;
563562
shp->shm_creator = current;
563+
list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
564564

565565
/*
566566
* shmid gets reported as "inode#" in /proc/pid/maps.

‎kernel/fork.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
13621362
if (retval)
13631363
goto bad_fork_cleanup_policy;
13641364
/* copy all the process information */
1365+
shm_init_task(p);
13651366
retval = copy_semundo(clone_flags, p);
13661367
if (retval)
13671368
goto bad_fork_cleanup_audit;
@@ -1913,6 +1914,11 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
19131914
*/
19141915
exit_sem(current);
19151916
}
1917+
if (unshare_flags & CLONE_NEWIPC) {
1918+
/* Orphan segments in old ns (see sem above). */
1919+
exit_shm(current);
1920+
shm_init_task(current);
1921+
}
19161922

19171923
if (new_nsproxy)
19181924
switch_task_namespaces(current, new_nsproxy);

0 commit comments

Comments
 (0)