diff options
author | Al Viro <viro@zeniv.linux.org.uk> | 2009-03-30 07:20:30 -0400 |
---|---|---|
committer | Al Viro <viro@zeniv.linux.org.uk> | 2009-03-31 23:00:26 -0400 |
commit | 498052bba55ecaff58db6a1436b0e25bfd75a7ff (patch) | |
tree | bd3644ac60737e3733995a203acebd70cfd1b21b /kernel/fork.c | |
parent | 3e93cd671813e204c258f1e6c797959920cf7772 (diff) |
New locking/refcounting for fs_struct
* all changes of current->fs are done under task_lock and write_lock of
old fs->lock
* refcount is not atomic anymore (same protection)
* its decrements are done when removing reference from current; at the
same time we decide whether to free it.
* put_fs_struct() is gone
* new field - ->in_exec. Set by check_unsafe_exec() if we are trying to do
execve() and only subthreads share fs_struct. Cleared when finishing exec
(success and failure alike). Makes CLONE_FS fail with -EAGAIN if set.
* check_unsafe_exec() may fail with -EAGAIN if another execve() from subthread
is in progress.
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'kernel/fork.c')
-rw-r--r-- | kernel/fork.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/kernel/fork.c b/kernel/fork.c index 05c02dc586b1..51f138a131de 100644 --- a/kernel/fork.c +++ b/kernel/fork.c | |||
@@ -683,11 +683,19 @@ fail_nomem: | |||
683 | 683 | ||
684 | static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) | 684 | static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) |
685 | { | 685 | { |
686 | struct fs_struct *fs = current->fs; | ||
686 | if (clone_flags & CLONE_FS) { | 687 | if (clone_flags & CLONE_FS) { |
687 | atomic_inc(¤t->fs->count); | 688 | /* tsk->fs is already what we want */ |
689 | write_lock(&fs->lock); | ||
690 | if (fs->in_exec) { | ||
691 | write_unlock(&fs->lock); | ||
692 | return -EAGAIN; | ||
693 | } | ||
694 | fs->users++; | ||
695 | write_unlock(&fs->lock); | ||
688 | return 0; | 696 | return 0; |
689 | } | 697 | } |
690 | tsk->fs = copy_fs_struct(current->fs); | 698 | tsk->fs = copy_fs_struct(fs); |
691 | if (!tsk->fs) | 699 | if (!tsk->fs) |
692 | return -ENOMEM; | 700 | return -ENOMEM; |
693 | return 0; | 701 | return 0; |
@@ -1518,12 +1526,16 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) | |||
1518 | { | 1526 | { |
1519 | struct fs_struct *fs = current->fs; | 1527 | struct fs_struct *fs = current->fs; |
1520 | 1528 | ||
1521 | if ((unshare_flags & CLONE_FS) && | 1529 | if (!(unshare_flags & CLONE_FS) || !fs) |
1522 | (fs && atomic_read(&fs->count) > 1)) { | 1530 | return 0; |
1523 | *new_fsp = copy_fs_struct(current->fs); | 1531 | |
1524 | if (!*new_fsp) | 1532 | /* don't need lock here; in the worst case we'll do useless copy */ |
1525 | return -ENOMEM; | 1533 | if (fs->users == 1) |
1526 | } | 1534 | return 0; |
1535 | |||
1536 | *new_fsp = copy_fs_struct(fs); | ||
1537 | if (!*new_fsp) | ||
1538 | return -ENOMEM; | ||
1527 | 1539 | ||
1528 | return 0; | 1540 | return 0; |
1529 | } | 1541 | } |
@@ -1639,8 +1651,13 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) | |||
1639 | 1651 | ||
1640 | if (new_fs) { | 1652 | if (new_fs) { |
1641 | fs = current->fs; | 1653 | fs = current->fs; |
1654 | write_lock(&fs->lock); | ||
1642 | current->fs = new_fs; | 1655 | current->fs = new_fs; |
1643 | new_fs = fs; | 1656 | if (--fs->users) |
1657 | new_fs = NULL; | ||
1658 | else | ||
1659 | new_fs = fs; | ||
1660 | write_unlock(&fs->lock); | ||
1644 | } | 1661 | } |
1645 | 1662 | ||
1646 | if (new_mm) { | 1663 | if (new_mm) { |
@@ -1679,7 +1696,7 @@ bad_unshare_cleanup_sigh: | |||
1679 | 1696 | ||
1680 | bad_unshare_cleanup_fs: | 1697 | bad_unshare_cleanup_fs: |
1681 | if (new_fs) | 1698 | if (new_fs) |
1682 | put_fs_struct(new_fs); | 1699 | free_fs_struct(new_fs); |
1683 | 1700 | ||
1684 | bad_unshare_cleanup_thread: | 1701 | bad_unshare_cleanup_thread: |
1685 | bad_unshare_out: | 1702 | bad_unshare_out: |