eCryptfs: Privileged kthread for lower file opens
PATCH 1/1 eCryptfs: Privileged kthread for lower file opens
eCryptfs would really like to have read-write access to all files in the lower filesystem. Right now, the persistent lower file may be opened read-only if the attempt to open it read-write fails. One way to keep from having to do that is to have a privileged kthread that can open the lower persistent file on behalf of the user opening the eCryptfs file; this patch implements this functionality.
This patch will properly allow a less-privileged user to open the eCryptfs file, followed by a more-privileged user opening the eCryptfs file, with the first user only being able to read and the second user being able to both read and write. eCryptfs currently does this wrong; it will wind up calling vfs_write() on a file that was opened read-only. This is fixed in this patch.
--- fs/ecryptfs/Makefile | 2 +- fs/ecryptfs/ecryptfs_kernel.h | 31 ++++++ fs/ecryptfs/file.c | 7 ++ fs/ecryptfs/kthread.c | 211 +++++++++++++++++++++++++++++++++++++++++ fs/ecryptfs/main.c | 42 ++++---- 5 files changed, 271 insertions(+), 22 deletions(-) create mode 100644 fs/ecryptfs/kthread.c
fs/ecryptfs/Makefile +1 -1
@@ -4,4 +4,4 @@ obj-$(CONFIG_ECRYPT_FS) += ecryptfs.o -ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o miscdev.o debug.o +ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o miscdev.o kthread.o debug.o
fs/ecryptfs/ecryptfs_kernel.h +31 -0
@@ -559,6 +559,32 @@ extern struct kmem_cache *ecryptfs_key_record_cache;extern struct kmem_cache *ecryptfs_key_sig_cache;extern struct kmem_cache *ecryptfs_global_auth_tok_cache;extern struct kmem_cache *ecryptfs_key_tfm_cache; +extern struct kmem_cache *ecryptfs_open_req_cache; + +extern struct task_struct *ecryptfs_kthread; + +struct ecryptfs_open_req { +#define ECRYPTFS_REQ_PROCESSED 0x00000001 +#define ECRYPTFS_REQ_DROPPED 0x00000002 +#define ECRYPTFS_REQ_ZOMBIE 0x00000004 +u32 flags; +struct file **lower_file; +struct dentry *lower_dentry; +struct vfsmount *lower_mnt; +struct task_struct *requesting_task; +struct mutex mux; +struct list_head kthread_ctl_list; +}; + +struct ecryptfs_kthread_ctl { +#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001 +u32 flags; +struct mutex mux; +struct list_head req_list; +wait_queue_head_t wait; +}; + +extern struct ecryptfs_kthread_ctl ecryptfs_kthread_ctl; int ecryptfs_interpose(struct dentry *hidden_dentry, struct dentry *this_dentry, struct super_block *sb,
@@ -692,5 +718,10 @@ void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);int ecryptfs_spawn_daemon (struct ecryptfs_daemon **daemon,uid_t euid,struct user_namespace *user_ns,struct pid *pid); +int ecryptfs_init_kthread (void ); +void ecryptfs_destroy_kthread (void ); +int ecryptfs_privileged_open (struct file **lower_file, +struct dentry *lower_dentry, +struct vfsmount *lower_mnt);#endif /* #ifndef ECRYPTFS_KERNEL_H */
fs/ecryptfs/file.c +7 -0
@@ -191,6 +191,13 @@ static int ecryptfs_open(struct inode *inode, struct file *file) | ECRYPTFS_ENCRYPTED); }mutex_unlock (&crypt_stat->cs_mutex); +if ((ecryptfs_inode_to_private (inode)->lower_file->f_flags &O_RDONLY ) + && !(file->f_flags &O_RDONLY )) { + rc = -EPERM ; +printk (KERN_WARNING "%s: Lower persistent file is RO; eCryptfs " +"file must thus be opened RO\n" , __func__); + goto out; + }ecryptfs_set_file_lower ( file,ecryptfs_inode_to_private (inode)->lower_file);if (S_ISDIR (ecryptfs_dentry->d_inode->i_mode)) {
fs/ecryptfs/kthread.c new file +211 -0
211 lines not on this page · raw message
fs/ecryptfs/main.c +21 -21
76 lines not on this page · raw message
Signature · 1 line
1.5.3.6
Michael Halcrow wrote:
[snip]
Hi,
2 comments on the patch.
Quoted 71 lines
fs/ecryptfs/kthread.c new file +65
-0@@ -0,0 +1,211 @@ +/** +* eCryptfs: Linux filesystem encryption layer +* +* Copyright (C) 2008 International Business Machines Corp. +* Author(s): Michael A. Halcrow <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the +* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +* 02111-1307, USA. +*/ + +#include <linux/kthread.h> +#include <linux/freezer.h> +#include <linux/wait.h> +#include <linux/mount.h> +#include "ecryptfs_kernel.h" + +struct task_struct *ecryptfs_kthread; +struct ecryptfs_kthread_ctl ecryptfs_kthread_ctl; + +struct kmem_cache *ecryptfs_open_req_cache; + +/** +* ecryptfs_threadfn +* @ignored: ignored +* +* The eCryptfs kernel thread that has the responsibility of getting +* the lower persistent file with RW permissions. +* +* Returns zero on success; non-zero otherwise +*/ +static int ecryptfs_threadfn (void *ignored) +{ +set_freezable (); +while (1 ) { +struct ecryptfs_open_req *req; + +wait_event_freezable ( + ecryptfs_kthread_ctl.wait, + !list_empty (&ecryptfs_kthread_ctl.req_list)); +mutex_lock (&ecryptfs_kthread_ctl.mux); +if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { +mutex_unlock (&ecryptfs_kthread_ctl.mux); + goto out; + } +while (!list_empty (&ecryptfs_kthread_ctl.req_list)) { + req =list_first_entry (&ecryptfs_kthread_ctl.req_list, + struct ecryptfs_open_req, + kthread_ctl_list); +BUG_ON (!req); +mutex_lock (&req->mux); +list_del (&req->kthread_ctl_list); +if (req->flags &ECRYPTFS_REQ_ZOMBIE ) +mutex_unlock (&req->mux);
fs/ecryptfs/kthread.c line 75
Quoted 10 lines
fs/ecryptfs/kthread.c new file +10
-0+else { +dget (req->lower_dentry); +mntget (req->lower_mnt); + (*req->lower_file) =dentry_open ( + req->lower_dentry, req->lower_mnt, + (O_RDWR |O_LARGEFILE )); + req->flags |=ECRYPTFS_REQ_PROCESSED ; +wake_up_process (req->requesting_task); +mutex_unlock (&req->mux); + }
Why not:
if (!(req->flags & ECRYPTFS_REQ_ZOMBIE)) dget(req->lower_dentry); mntget(req->lower_mnt); (*req->lower_file) = dentry_open( req->lower_dentry, req->lower_mnt, (O_RDWR | O_LARGEFILE)); req->flags |= ECRYPTFS_REQ_PROCESSED; wake_up_process(req->requesting_task); } mutex_unlock(&req->mux);
Quoted 78 lines
fs/ecryptfs/kthread.c new file +78
-0+ } +mutex_unlock (&ecryptfs_kthread_ctl.mux); + } +out: +do_exit (0 ); +} + +int ecryptfs_init_kthread (void ) +{ +int rc =0 ; + +mutex_init (&ecryptfs_kthread_ctl.mux); +init_waitqueue_head (&ecryptfs_kthread_ctl.wait); +INIT_LIST_HEAD (&ecryptfs_kthread_ctl.req_list); + ecryptfs_kthread =kthread_create (&ecryptfs_threadfn,NULL , +"ecryptfs-kthread" ); +if (IS_ERR (ecryptfs_kthread)) { + rc =PTR_ERR (ecryptfs_kthread); +printk (KERN_ERR "%s: Failed to create kernel thread; rc = [%d]" +"\n" , __func__, rc); + } +wake_up_process (ecryptfs_kthread); +return rc; +} + +void ecryptfs_destroy_kthread (void ) +{ +struct ecryptfs_open_req tmp_req; +struct ecryptfs_open_req *req; + +mutex_lock (&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |=ECRYPTFS_KTHREAD_ZOMBIE ; +list_for_each_entry (req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { +mutex_lock (&req->mux); + req->flags |=ECRYPTFS_REQ_ZOMBIE ; +wake_up_process (req->requesting_task); +mutex_unlock (&req->mux); + } +memset (&tmp_req,0 ,sizeof (tmp_req)); + tmp_req.flags =ECRYPTFS_REQ_ZOMBIE ; +list_add_tail (&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); +mutex_unlock (&ecryptfs_kthread_ctl.mux); +wake_up (&ecryptfs_kthread_ctl.wait); +} + +/** +* ecryptfs_privileged_open +* @lower_file: Result of dentry_open by root on lower dentry +* @lower_dentry: Lower dentry for file to open +* @lower_mnt: Lower vfsmount for file to open +* +* This function gets a r/w file opened againt the lower dentry. +* +* Returns zero on success; non-zero otherwise +*/ +int ecryptfs_privileged_open (struct file **lower_file, +struct dentry *lower_dentry, +struct vfsmount *lower_mnt) +{ +struct ecryptfs_open_req *req; +int rc =0 ; + +/* Corresponding dput() and mntput() are done when the +* persistent file is fput() when the eCryptfs inode is +* destroyed. */ +dget (lower_dentry); +mntget (lower_mnt); + (*lower_file) =dentry_open (lower_dentry, lower_mnt, + (O_RDWR |O_LARGEFILE )); +if (!IS_ERR (*lower_file)) + goto out; + req =kmem_cache_alloc (ecryptfs_open_req_cache,GFP_KERNEL ); +if (!req) { + rc = -ENOMEM ; + goto out; + }
fs/ecryptfs/kthread.c line 163
Quoted 10 lines
fs/ecryptfs/kthread.c new file +10
-0+mutex_init (&req->mux); + req->lower_file = lower_file; + req->lower_dentry = lower_dentry; + req->lower_mnt = lower_mnt; + req->requesting_task = current; + req->flags =0 ; +mutex_lock (&ecryptfs_kthread_ctl.mux); +mutex_lock (&req->mux); +if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { + rc = -EIO ;
Isn't a mutex_unlock(&req->mux) missing here?
Quoted 48 lines
fs/ecryptfs/kthread.c new file +48
-0+mutex_unlock (&ecryptfs_kthread_ctl.mux); +printk (KERN_ERR "%s: We are in the middle of shutting down; " +"aborting privileged request to open lower file\n" , + __func__); + goto out_free; + } +list_add_tail (&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); +mutex_unlock (&req->mux); +mutex_unlock (&ecryptfs_kthread_ctl.mux); +wake_up (&ecryptfs_kthread_ctl.wait); +schedule: +set_current_state (TASK_INTERRUPTIBLE ); +schedule (); +mutex_lock (&req->mux); +if (req->flags ==0 ) { +mutex_unlock (&req->mux); + goto schedule; + } +set_current_state (TASK_RUNNING ); +if (req->flags &ECRYPTFS_REQ_DROPPED + || req->flags &ECRYPTFS_REQ_ZOMBIE ) { + rc = -EIO ; +printk (KERN_WARNING "%s: Privileged open request dropped\n" , + __func__); + goto out_unlock; + } +if (IS_ERR (*req->lower_file)) { + rc =PTR_ERR (*req->lower_file); +dget (lower_dentry); +mntget (lower_mnt); + (*lower_file) =dentry_open (lower_dentry, lower_mnt, + (O_RDONLY |O_LARGEFILE )); +if (IS_ERR (*lower_file)) { + rc =PTR_ERR (*req->lower_file); + (*lower_file) =NULL ; +printk (KERN_WARNING "%s: Error attempting privileged " +"open of lower file with either RW or RO " +"perms; rc = [%d]. Giving up.\n" , + __func__, rc); + } + } +out_unlock: +mutex_unlock (&req->mux); +out_free: +kmem_cache_free (ecryptfs_open_req_cache, req); +out: +return rc; +}
Quoted 10 lines · Andrea Righi
Quoted 9 lines · Michael Halcrow
fs/ecryptfs/kthread.c new file +9
-0+ req =kmem_cache_alloc (ecryptfs_open_req_cache,GFP_KERNEL ); +if (!req) { + rc = -ENOMEM ; + goto out; + } +mutex_init (&req->mux); + req->lower_file = lower_file; + req->lower_dentry = lower_dentry; + req->lower_mnt = lower_mnt;
Quoted 8 lines
fs/ecryptfs/kthread.c line 163
Quoted 6 lines
fs/ecryptfs/kthread.c new file +6
-0+ req->requesting_task = current; + req->flags =0 ; +mutex_lock (&ecryptfs_kthread_ctl.mux); +mutex_lock (&req->mux); +if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { + rc = -EIO ;Isn't a
mutex_unlock(&req->mux)missing here?
Nobody else actually has a reference to req at this point, so the unlock is not really necessary. It might not be a bad idea to jump to out_unlock anyway just to avoid future bugs.
Quoted 48 lines
Quoted 48 lines
fs/ecryptfs/kthread.c new file +48
-0+mutex_unlock (&ecryptfs_kthread_ctl.mux); +printk (KERN_ERR "%s: We are in the middle of shutting down; " +"aborting privileged request to open lower file\n" , + __func__); + goto out_free; + } +list_add_tail (&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); +mutex_unlock (&req->mux); +mutex_unlock (&ecryptfs_kthread_ctl.mux); +wake_up (&ecryptfs_kthread_ctl.wait); +schedule: +set_current_state (TASK_INTERRUPTIBLE ); +schedule (); +mutex_lock (&req->mux); +if (req->flags ==0 ) { +mutex_unlock (&req->mux); + goto schedule; + } +set_current_state (TASK_RUNNING ); +if (req->flags &ECRYPTFS_REQ_DROPPED + || req->flags &ECRYPTFS_REQ_ZOMBIE ) { + rc = -EIO ; +printk (KERN_WARNING "%s: Privileged open request dropped\n" , + __func__); + goto out_unlock; + } +if (IS_ERR (*req->lower_file)) { + rc =PTR_ERR (*req->lower_file); +dget (lower_dentry); +mntget (lower_mnt); + (*lower_file) =dentry_open (lower_dentry, lower_mnt, + (O_RDONLY |O_LARGEFILE )); +if (IS_ERR (*lower_file)) { + rc =PTR_ERR (*req->lower_file); + (*lower_file) =NULL ; +printk (KERN_WARNING "%s: Error attempting privileged " +"open of lower file with either RW or RO " +"perms; rc = [%d]. Giving up.\n" , + __func__, rc); + } + } +out_unlock: +mutex_unlock (&req->mux); +out_free: +kmem_cache_free (ecryptfs_open_req_cache, req); +out: +return rc; +}
PATCH 1/1 eCryptfs: Remove useless lock
Quoted 24 lines · Michael Halcrow
Quoted 10 lines · Andrea Righi
Quoted 9 lines · Michael Halcrow
(quoted diff) +9
-0+ req = kmem_cache_alloc(ecryptfs_open_req_cache, GFP_KERNEL); + if (!req) { + rc = -ENOMEM; + goto out; + } + mutex_init(&req->mux); + req->lower_file = lower_file; + req->lower_dentry = lower_dentry; + req->lower_mnt = lower_mnt;Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ req->requesting_task = current; + req->flags = 0; + mutex_lock(&ecryptfs_kthread_ctl.mux); + mutex_lock(&req->mux); + if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) { + rc = -EIO;Isn't a
mutex_unlock(&req->mux)missing here?Nobody else actually has a reference to req at this point, so the unlock is not really necessary. It might not be a bad idea to jump to
out_unlockanyway just to avoid future bugs.
Quoted 9 lines
Quoted 9 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+ mutex_unlock(&ecryptfs_kthread_ctl.mux); + printk(KERN_ERR "%s: We are in the middle of shutting down; " + "aborting privileged request to open lower file\n", + __func__); + goto out_free; + } + list_add_tail(&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&req->mux); + mutex_unlock(&ecryptfs_kthread_ctl.mux);
Nobody can get a reference to req so long as &ecryptfs_kthread_ctl.mux is held. Remove lock of req->mux for newly created req.
--- fs/ecryptfs/kthread.c | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
fs/ecryptfs/kthread.c +0 -2
@@ -158,7 +158,6 @@ int ecryptfs_privileged_open(struct file **lower_file, req->requesting_task = current; req->flags =0 ;mutex_lock (&ecryptfs_kthread_ctl.mux); -mutex_lock (&req->mux);if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { rc = -EIO ;mutex_unlock (&ecryptfs_kthread_ctl.mux);
@@ -168,7 +167,6 @@ int ecryptfs_privileged_open(struct file **lower_file, goto out_free; }list_add_tail (&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); -mutex_unlock (&req->mux);mutex_unlock (&ecryptfs_kthread_ctl.mux);wake_up (&ecryptfs_kthread_ctl.wait); schedule:
Signature · 1 line
1.5.3.6
Quoted 7 lines · On Tue, 20 May 2008 16:46:10 -0500 Michael Halcrow <[email protected]> wrote:
eCryptfs would really like to have read-write access to all files in the lower filesystem. Right now, the persistent lower file may be opened read-only if the attempt to open it read-write fails. One way to keep from having to do that is to have a privileged kthread that can open the lower persistent file on behalf of the user opening the eCryptfs file; this patch implements this functionality.
Quoted 7 lines
This patch will properly allow a less-privileged user to open the eCryptfs file, followed by a more-privileged user opening the eCryptfs file, with the first user only being able to read and the second user being able to both read and write. eCryptfs currently does this wrong; it will wind up calling
vfs_write()on a file that was opened read-only. This is fixed in this patch.
hm, interesting. A bit scary though.
Quoted 19 lines
fs/ecryptfs/main.c | 42 ++++---- 5 files changed, 271 insertions(+), 22 deletions(-) create mode 100644 fs/ecryptfs/kthread.cfs/ecryptfs/Makefile +1
-1@@ -4,4 +4,4 @@ obj-$(CONFIG_ECRYPT_FS) += ecryptfs.o -ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o miscdev.o debug.o +ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o miscdev.o kthread.o debug.ofs/ecryptfs/ecryptfs_kernel.h +0
-0
fs/ecryptfs/ecryptfs_kernel.h extern struct kmem_cache *ecryptfs_key_record_cache; · line 564
Quoted 6 lines
fs/ecryptfs/ecryptfs_kernel.h +3
-0@@ -559,6 +559,32 @@ extern struct kmem_cache *ecryptfs_key_record_cache;extern struct kmem_cache *ecryptfs_key_sig_cache;extern struct kmem_cache *ecryptfs_global_auth_tok_cache;extern struct kmem_cache *ecryptfs_key_tfm_cache; +extern struct kmem_cache *ecryptfs_open_req_cache; + +extern struct task_struct *ecryptfs_kthread;
This can be made static to fs/ecryptfs/kthread.c.
Quoted 12 lines
fs/ecryptfs/ecryptfs_kernel.h +12
-0+struct ecryptfs_open_req { +#define ECRYPTFS_REQ_PROCESSED 0x00000001 +#define ECRYPTFS_REQ_DROPPED 0x00000002 +#define ECRYPTFS_REQ_ZOMBIE 0x00000004 +u32 flags; +struct file **lower_file; +struct dentry *lower_dentry; +struct vfsmount *lower_mnt; +struct task_struct *requesting_task; +struct mutex mux; +struct list_head kthread_ctl_list; +};
fs/ecryptfs/ecryptfs_kernel.h extern struct kmem_cache *ecryptfs_key_record_cache;
Quoted 10 lines
fs/ecryptfs/ecryptfs_kernel.h +8
-0+struct ecryptfs_kthread_ctl { +#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001 +u32 flags; +struct mutex mux; +struct list_head req_list; +wait_queue_head_t wait; +}; +extern struct ecryptfs_kthread_ctl ecryptfs_kthread_ctl;
I think this guy can be made static too. As a result of which, entire data structure definitions could possibly be pushed down into kthread.c?
Quoted 18 lines
fs/ecryptfs/ecryptfs_kernel.h +5
-0int ecryptfs_interpose(struct dentry *hidden_dentry, struct dentry *this_dentry, struct super_block *sb,@@ -692,5 +718,10 @@ void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);int ecryptfs_spawn_daemon (struct ecryptfs_daemon **daemon,uid_t euid,struct user_namespace *user_ns,struct pid *pid); +int ecryptfs_init_kthread (void ); +void ecryptfs_destroy_kthread (void ); +int ecryptfs_privileged_open (struct file **lower_file, +struct dentry *lower_dentry, +struct vfsmount *lower_mnt);#endif /* #ifndef ECRYPTFS_KERNEL_H */ fs/ecryptfs/file.c +0
-0
fs/ecryptfs/file.c static int ecryptfs_open(struct inode *inode, struct file *file) · line 198
Quoted 8 lines
fs/ecryptfs/file.c +5
-0@@ -191,6 +191,13 @@ static int ecryptfs_open(struct inode *inode, struct file *file) | ECRYPTFS_ENCRYPTED); }mutex_unlock (&crypt_stat->cs_mutex); +if ((ecryptfs_inode_to_private (inode)->lower_file->f_flags &O_RDONLY ) + && !(file->f_flags &O_RDONLY )) { + rc = -EPERM ; +printk (KERN_WARNING "%s: Lower persistent file is RO; eCryptfs " +"file must thus be opened RO\n" , __func__);
"hence" ;)
Quoted 45 lines
fs/ecryptfs/file.c +39
-0+ goto out; + }ecryptfs_set_file_lower ( file,ecryptfs_inode_to_private (inode)->lower_file);if (S_ISDIR (ecryptfs_dentry->d_inode->i_mode)) { +/** +* ecryptfs_threadfn +* @ignored: ignored +* +* The eCryptfs kernel thread that has the responsibility of getting +* the lower persistent file with RW permissions. +* +* Returns zero on success; non-zero otherwise +*/ +static int ecryptfs_threadfn (void *ignored) +{ +set_freezable (); +while (1 ) { +struct ecryptfs_open_req *req; + +wait_event_freezable ( + ecryptfs_kthread_ctl.wait, + !list_empty (&ecryptfs_kthread_ctl.req_list)); +mutex_lock (&ecryptfs_kthread_ctl.mux); +if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { +mutex_unlock (&ecryptfs_kthread_ctl.mux); + goto out; + } +while (!list_empty (&ecryptfs_kthread_ctl.req_list)) { + req =list_first_entry (&ecryptfs_kthread_ctl.req_list, + struct ecryptfs_open_req, + kthread_ctl_list); +BUG_ON (!req); +mutex_lock (&req->mux); +list_del (&req->kthread_ctl_list); +if (req->flags &ECRYPTFS_REQ_ZOMBIE ) +mutex_unlock (&req->mux); +else { +dget (req->lower_dentry); +mntget (req->lower_mnt); + (*req->lower_file) = dentry_open( + req->lower_dentry, req->lower_mnt,
fs/ecryptfs/file.c static int ecryptfs_open(struct inode *inode, struct file *file)
Quoted 10 lines
fs/ecryptfs/file.c +10
-0+ (O_RDWR | O_LARGEFILE)); + req->flags |=ECRYPTFS_REQ_PROCESSED ; +wake_up_process (req->requesting_task); +mutex_unlock (&req->mux); + } + } +mutex_unlock (&ecryptfs_kthread_ctl.mux); + } +out: +do_exit (0 );
A plain old `return 0;' will suffice here, and is more typical. I'm moderately surprised that do_exit() is exported to modules, actually.
Quoted 9 lines
fs/ecryptfs/file.c +9
-0+} + +int ecryptfs_init_kthread (void ) +{ +int rc =0 ; + +mutex_init (&ecryptfs_kthread_ctl.mux); +init_waitqueue_head (&ecryptfs_kthread_ctl.wait); +INIT_LIST_HEAD (&ecryptfs_kthread_ctl.req_list);
fs/ecryptfs/file.c
Quoted 10 lines
fs/ecryptfs/file.c +10
-0+ ecryptfs_kthread =kthread_create (&ecryptfs_threadfn,NULL , +"ecryptfs-kthread" ); +if (IS_ERR (ecryptfs_kthread)) { + rc =PTR_ERR (ecryptfs_kthread); +printk (KERN_ERR "%s: Failed to create kernel thread; rc = [%d]" +"\n" , __func__, rc); + } +wake_up_process (ecryptfs_kthread); +return rc; +}
kthread_run() does the kthread_create() and the wake_up_process() for you.
Quoted 11 lines
fs/ecryptfs/file.c +11
-0+void ecryptfs_destroy_kthread (void ) +{ +struct ecryptfs_open_req tmp_req; +struct ecryptfs_open_req *req; + +mutex_lock (&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |=ECRYPTFS_KTHREAD_ZOMBIE ; +list_for_each_entry (req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { +mutex_lock (&req->mux); + req->flags |=ECRYPTFS_REQ_ZOMBIE ;
fs/ecryptfs/file.c
Quoted 10 lines
fs/ecryptfs/file.c +10
-0+wake_up_process (req->requesting_task); +mutex_unlock (&req->mux); + } +memset (&tmp_req,0 ,sizeof (tmp_req)); + tmp_req.flags =ECRYPTFS_REQ_ZOMBIE ; +list_add_tail (&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); +mutex_unlock (&ecryptfs_kthread_ctl.mux); +wake_up (&ecryptfs_kthread_ctl.wait); +}
eh? We attach a local variable to a global list and then return? That won't last very long.
Quoted 20 lines
fs/ecryptfs/file.c +20
-0+/** +* ecryptfs_privileged_open +* @lower_file: Result of dentry_open by root on lower dentry +* @lower_dentry: Lower dentry for file to open +* @lower_mnt: Lower vfsmount for file to open +* +* This function gets a r/w file opened againt the lower dentry. +* +* Returns zero on success; non-zero otherwise +*/ +int ecryptfs_privileged_open (struct file **lower_file, +struct dentry *lower_dentry, +struct vfsmount *lower_mnt) +{ +struct ecryptfs_open_req *req; +int rc =0 ; + +/* Corresponding dput() and mntput() are done when the +* persistent file is fput() when the eCryptfs inode is +* destroyed. */
fs/ecryptfs/file.c
Quoted 10 lines
fs/ecryptfs/file.c +10
-0+dget (lower_dentry); +mntget (lower_mnt); + (*lower_file) =dentry_open (lower_dentry, lower_mnt, + (O_RDWR |O_LARGEFILE )); +if (!IS_ERR (*lower_file)) + goto out; + req =kmem_cache_alloc (ecryptfs_open_req_cache,GFP_KERNEL ); +if (!req) { + rc = -ENOMEM ; + goto out;
Did we just leak the dentry_open() result?
Quoted 6 lines
fs/ecryptfs/file.c +6
-0+ } +mutex_init (&req->mux); + req->lower_file = lower_file; + req->lower_dentry = lower_dentry; + req->lower_mnt = lower_mnt; + req->requesting_task = current;
fs/ecryptfs/file.c
Quoted 10 lines
fs/ecryptfs/file.c +10
-0+ req->flags =0 ; +mutex_lock (&ecryptfs_kthread_ctl.mux); +mutex_lock (&req->mux); +if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) { + rc = -EIO ; +mutex_unlock (&ecryptfs_kthread_ctl.mux); +printk (KERN_ERR "%s: We are in the middle of shutting down; " +"aborting privileged request to open lower file\n" , + __func__); + goto out_free;
ditto.
fs/ecryptfs/file.c
Quoted 8 lines
fs/ecryptfs/file.c +8
-0+ } +list_add_tail (&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); +mutex_unlock (&req->mux); +mutex_unlock (&ecryptfs_kthread_ctl.mux); +wake_up (&ecryptfs_kthread_ctl.wait); +schedule: +set_current_state (TASK_INTERRUPTIBLE ); +schedule ();
This looks racy to me. I assume we're waiting for ecryptfs_kthread to wake us up. But that thread could have sent us its wakeup _before_ we did the set_current_state+schedule. In which case we lost the wakeup and we'll get stuck.
fs/ecryptfs/file.c
Quoted 6 lines
fs/ecryptfs/file.c +6
-0+mutex_lock (&req->mux); +if (req->flags ==0 ) { +mutex_unlock (&req->mux); + goto schedule; + } +set_current_state (TASK_RUNNING );
This is unneeded. schedule() always returns in state TASK_RUNNING.
Quoted 29 lines
fs/ecryptfs/file.c +29
-0+if (req->flags &ECRYPTFS_REQ_DROPPED + || req->flags &ECRYPTFS_REQ_ZOMBIE ) { + rc = -EIO ; +printk (KERN_WARNING "%s: Privileged open request dropped\n" , + __func__); + goto out_unlock; + } +if (IS_ERR (*req->lower_file)) { + rc =PTR_ERR (*req->lower_file); +dget (lower_dentry); +mntget (lower_mnt); + (*lower_file) =dentry_open (lower_dentry, lower_mnt, + (O_RDONLY |O_LARGEFILE )); +if (IS_ERR (*lower_file)) { + rc =PTR_ERR (*req->lower_file); + (*lower_file) =NULL ; +printk (KERN_WARNING "%s: Error attempting privileged " +"open of lower file with either RW or RO " +"perms; rc = [%d]. Giving up.\n" , + __func__, rc); + } + } +out_unlock: +mutex_unlock (&req->mux); +out_free: +kmem_cache_free (ecryptfs_open_req_cache, req); +out: +return rc; +}
PATCH 1/1 eCryptfs: Clean up kthread synchronization
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Tue, 20 May 2008 16:46:10 -0500 Michael Halcrow <[email protected]> wrote:
+extern struct
task_struct*ecryptfs_kthread;This can be made static to
fs/ecryptfs/kthread.c.
Done.
Quoted 5 lines
Quoted 1 line
+extern struct
ecryptfs_kthread_ctlecryptfs_kthread_ctl;I think this guy can be made static too. As a result of which, entire data structure definitions could possibly be pushed down into
kthread.c?
struct ecryptfs_kthread_ctl can be moved there.
Quoted 1 line
Quoted 1 line
+static int
ecryptfs_threadfn(void *ignored)
...
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+out: + do_exit(0);A plain old `return 0;' will suffice here, and is more typical. I'm moderately surprised that
do_exit()is exported to modules, actually.
Done.
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5
-0+ ecryptfs_kthread = kthread_create(&ecryptfs_threadfn, NULL, + "ecryptfs-kthread"); + if (IS_ERR(ecryptfs_kthread)) { + rc = PTR_ERR(ecryptfs_kthread); + printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ "\n", __func__, rc); + } + wake_up_process(ecryptfs_kthread); + return rc; +}
kthread_run()does thekthread_create()and thewake_up_process()for you.
Okay.
Quoted 16 lines
Quoted 16 lines
(quoted diff) +16
-0+void ecryptfs_destroy_kthread(void) +{ + struct ecryptfs_open_req tmp_req; + struct ecryptfs_open_req *req; + + mutex_lock(&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; + list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { + mutex_lock(&req->mux); + req->flags |= ECRYPTFS_REQ_ZOMBIE; + wake_up_process(req->requesting_task); + mutex_unlock(&req->mux); + } + memset(&tmp_req, 0, sizeof(tmp_req)); + tmp_req.flags = ECRYPTFS_REQ_ZOMBIE;
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ list_add_tail(&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&ecryptfs_kthread_ctl.mux); + wake_up(&ecryptfs_kthread_ctl.wait); +}eh? We attach a local variable to a global list and then return? That won't last very long.
Adding this dummy entry to the list is just my own way of getting the kthread to wake up and shut down. This actually works, albeit a little ugly. The list and its contents get dropped on the floor at this point because (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE). The only consumer of this list (the kthread) checks for this flag immediately after getting the mux, and if it is there, it just exits. The only producer on this list (ecryptfs_privileged_open()) checks for this flag immediately after getting the mux and bows out if it is set. In other words, once this flag is set, the list and its contents become untouchable by anything other than ecryptfs_destroy_kthread().
Quoted 24 lines
Quoted 24 lines
(quoted diff) +24
-0+/** + * ecryptfs_privileged_open + * @lower_file: Result of dentry_open by root on lower dentry + * @lower_dentry: Lower dentry for file to open + * @lower_mnt: Lower vfsmount for file to open + * + * This function gets a r/w file opened againt the lower dentry. + * + * Returns zero on success; non-zero otherwise + */ +int ecryptfs_privileged_open(struct file **lower_file, + struct dentry *lower_dentry, + struct vfsmount *lower_mnt) +{ + struct ecryptfs_open_req *req; + int rc = 0; + + /* Corresponding dput() and mntput() are done when the + * persistent file is fput() when the eCryptfs inode is + * destroyed. */ + dget(lower_dentry); + mntget(lower_mnt); + (*lower_file) = dentry_open(lower_dentry, lower_mnt, + (O_RDWR | O_LARGEFILE));
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ if (!IS_ERR(*lower_file)) + goto out; + req = kmem_cache_alloc(ecryptfs_open_req_cache, GFP_KERNEL); + if (!req) { + rc = -ENOMEM; + goto out;Did we just leak the
dentry_open()result?
Nope; IS_ERR(*lower_file) is true.
Quoted 10 lines
Quoted 10 lines
(quoted diff) +10
-0+ } + mutex_init(&req->mux); + req->lower_file = lower_file; + req->lower_dentry = lower_dentry; + req->lower_mnt = lower_mnt; + req->requesting_task = current; + req->flags = 0; + mutex_lock(&ecryptfs_kthread_ctl.mux); + mutex_lock(&req->mux); + if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ rc = -EIO; + mutex_unlock(&ecryptfs_kthread_ctl.mux); + printk(KERN_ERR "%s: We are in the middle of shutting down; " + "aborting privileged request to open lower file\n", + __func__); + goto out_free;ditto.
IS_ERR(*lower_file) is true here too.
Quoted 14 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ } + list_add_tail(&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&req->mux); + mutex_unlock(&ecryptfs_kthread_ctl.mux); + wake_up(&ecryptfs_kthread_ctl.wait); +schedule: + set_current_state(TASK_INTERRUPTIBLE); + schedule();This looks racy to me. I assume we're waiting for
ecryptfs_kthreadto wake us up. But that thread could have sent us its wakeup _before_ we did the set_current_state+schedule. In which case we lost the wakeup and we'll get stuck.
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ mutex_lock(&req->mux); + if (req->flags == 0) { + mutex_unlock(&req->mux); + goto schedule; + } + set_current_state(TASK_RUNNING);This is unneeded.
schedule()always returns in stateTASK_RUNNING.
How about just another wait queue then?
---
Replace the schedule/wake_up_process pair with a wait queue. Just return from the kthread rather than call do_exit(). Use kthread_run() rather than kthread_create(). Move struct ecryptfs_kthread_ctl definition into kthread.c. Remove ecryptfs_kthread reference from ecryptfs_kernel.h.
--- fs/ecryptfs/ecryptfs_kernel.h | 14 +------------ fs/ecryptfs/file.c | 2 +- fs/ecryptfs/kthread.c | 44 +++++++++++++++++++--------------------- fs/ecryptfs/main.c | 2 +- 4 files changed, 24 insertions(+), 38 deletions(-)
fs/ecryptfs/ecryptfs_kernel.h +1 -13
@@ -561,8 +561,6 @@ extern struct kmem_cache *ecryptfs_global_auth_tok_cache;extern struct kmem_cache *ecryptfs_key_tfm_cache;extern struct kmem_cache *ecryptfs_open_req_cache; -extern struct task_struct *ecryptfs_kthread; -struct ecryptfs_open_req {#define ECRYPTFS_REQ_PROCESSED 0x00000001#define ECRYPTFS_REQ_DROPPED 0x00000002
@@ -571,21 +569,11 @@ struct ecryptfs_open_req {struct file **lower_file;struct dentry *lower_dentry;struct vfsmount *lower_mnt; -struct task_struct *requesting_task; +wait_queue_head_t wait;struct mutex mux;struct list_head kthread_ctl_list; }; -struct ecryptfs_kthread_ctl { -#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001 -u32 flags; -struct mutex mux; -struct list_head req_list; -wait_queue_head_t wait; -}; - -extern struct ecryptfs_kthread_ctl ecryptfs_kthread_ctl; -int ecryptfs_interpose (struct dentry *hidden_dentry,struct dentry *this_dentry,struct super_block *sb,int flag);
fs/ecryptfs/file.c +1 -1
@@ -195,7 +195,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file) && !(file->f_flags & O_RDONLY)) { rc = -EPERM ;printk (KERN_WARNING "%s: Lower persistent file is RO; eCryptfs " -"file must thus be opened RO\n" , __func__); +"file must hence be opened RO\n" , __func__); goto out; } ecryptfs_set_file_lower(
fs/ecryptfs/kthread.c +21 -23
105 lines not on this page · raw message
fs/ecryptfs/main.c +1 -1
@@ -789,7 +789,7 @@ static int __init ecryptfs_init(void) rc =ecryptfs_init_kthread ();if (rc) {printk (KERN_ERR "%s: kthread initialization failed; " -"rc = [%d]\n" , __func__); +"rc = [%d]\n" , __func__, rc); goto out_do_sysfs_unregistration; } rc =ecryptfs_init_messaging (ecryptfs_transport);
Signature · 1 line
1.5.3.6
Re: [PATCH] eCryptfs: Clean up kthread synchronization
Quoted 25 lines · On Thu, 22 May 2008 14:31:55 -0500 Michael Halcrow <[email protected]> wrote:
Quoted 16 lines
Quoted 16 lines
(quoted diff) +16
-0+void ecryptfs_destroy_kthread(void) +{ + struct ecryptfs_open_req tmp_req; + struct ecryptfs_open_req *req; + + mutex_lock(&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; + list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { + mutex_lock(&req->mux); + req->flags |= ECRYPTFS_REQ_ZOMBIE; + wake_up_process(req->requesting_task); + mutex_unlock(&req->mux); + } + memset(&tmp_req, 0, sizeof(tmp_req)); + tmp_req.flags = ECRYPTFS_REQ_ZOMBIE;Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ list_add_tail(&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&ecryptfs_kthread_ctl.mux); + wake_up(&ecryptfs_kthread_ctl.wait); +}eh? We attach a local variable to a global list and then return? That won't last very long.
Quoted 11 lines
Adding this dummy entry to the list is just my own way of getting the kthread to wake up and shut down. This actually works, albeit a little ugly. The list and its contents get dropped on the floor at this point because (ecryptfs_kthread_ctl.flags &
ECRYPTFS_KTHREAD_ZOMBIE). The only consumer of this list (the kthread) checks for this flag immediately after getting the mux, and if it is there, it just exits. The only producer on this list (ecryptfs_privileged_open()) checks for this flag immediately after getting the mux and bows out if it is set. In other words, once this flag is set, the list and its contents become untouchable by anything other thanecryptfs_destroy_kthread().
Unconvinced.
As soon as ecryptfs_destroy_kthread() returns, tmp_req is destroyed. But it remains on ecryptfs_kthread_ctl.req_list.
Quoted 6 lines
memset(&tmp_req, 0, sizeof(tmp_req)); tmp_req.flags = ECRYPTFS_REQ_ZOMBIE; list_add_tail(&tmp_req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); mutex_unlock(&ecryptfs_kthread_ctl.mux); wake_up(&ecryptfs_kthread_ctl.wait);
-> it's dead.
Quoted 1 line
}
Re: [PATCH] eCryptfs: Clean up kthread synchronization
Quoted 40 lines · Andrew Morton
Quoted 25 lines · On Thu, 22 May 2008 14:31:55 -0500 Michael Halcrow <[email protected]> wrote:
Quoted 16 lines
Quoted 16 lines
(quoted diff) +16
-0+void ecryptfs_destroy_kthread(void) +{ + struct ecryptfs_open_req tmp_req; + struct ecryptfs_open_req *req; + + mutex_lock(&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; + list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { + mutex_lock(&req->mux); + req->flags |= ECRYPTFS_REQ_ZOMBIE; + wake_up_process(req->requesting_task); + mutex_unlock(&req->mux); + } + memset(&tmp_req, 0, sizeof(tmp_req)); + tmp_req.flags = ECRYPTFS_REQ_ZOMBIE;Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ list_add_tail(&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&ecryptfs_kthread_ctl.mux); + wake_up(&ecryptfs_kthread_ctl.wait); +}eh? We attach a local variable to a global list and then return? That won't last very long.
Quoted 11 lines
Adding this dummy entry to the list is just my own way of getting the kthread to wake up and shut down. This actually works, albeit a little ugly. The list and its contents get dropped on the floor at this point because (ecryptfs_kthread_ctl.flags &
ECRYPTFS_KTHREAD_ZOMBIE). The only consumer of this list (the kthread) checks for this flag immediately after getting the mux, and if it is there, it just exits. The only producer on this list (ecryptfs_privileged_open()) checks for this flag immediately after getting the mux and bows out if it is set. In other words, once this flag is set, the list and its contents become untouchable by anything other thanecryptfs_destroy_kthread().
Quoted 4 lines
Unconvinced.
As soon as
ecryptfs_destroy_kthread()returns,tmp_reqis destroyed. But it remains on ecryptfs_kthread_ctl.req_list.
I intend for ecryptfs_kthread_ctl.req_list to be irrelevant once ecryptfs_destroy_kthread() grabs the mux and sets (ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE); nobody will ever do anything with that list any more. The state of the list -- including the dangling list pointer -- simply does not matter any more.
Quoted 8 lines
Quoted 6 lines
memset(&tmp_req, 0, sizeof(tmp_req)); tmp_req.flags = ECRYPTFS_REQ_ZOMBIE; list_add_tail(&tmp_req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); mutex_unlock(&ecryptfs_kthread_ctl.mux); wake_up(&ecryptfs_kthread_ctl.wait);-> it's dead.
This is what gets woken up:
---
wait_event_freezable(
ecryptfs_kthread_ctl.wait,
!list_empty(&ecryptfs_kthread_ctl.req_list));
mutex_lock(&ecryptfs_kthread_ctl.mux);
if (ecryptfs_kthread_ctl.flags &
ECRYPTFS_KTHREAD_ZOMBIE) {
mutex_unlock(&ecryptfs_kthread_ctl.mux);
goto out;
}---
So the flag causes the kthread to just quit, ignoring the list altogether.
PATCH 1/1 eCryptfs: Use kthread_should_stop() instead of the non-empty list hack
Quoted 12 lines · Michael Halcrow
This is what gets woken up:
---
wait_event_freezable( ecryptfs_kthread_ctl.wait, !list_empty(&ecryptfs_kthread_ctl.req_list)); mutex_lock(&ecryptfs_kthread_ctl.mux); if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) { mutex_unlock(&ecryptfs_kthread_ctl.mux); goto out; }
Quoted 4 lines
---
So the flag causes the kthread to just quit, ignoring the list altogether.
The code for shutting down the kthread is ugly. This patch uses the kthread_stop() call to shut down the kthread instead.
--- fs/ecryptfs/kthread.c | 10 +++------- 1 files changed, 3 insertions(+), 7 deletions(-)
fs/ecryptfs/kthread.c +3 -7
@@ -55,7 +55,8 @@ static int ecryptfs_threadfn(void *ignored)wait_event_freezable ( ecryptfs_kthread_ctl.wait, - !list_empty (&ecryptfs_kthread_ctl.req_list)); + (!list_empty (&ecryptfs_kthread_ctl.req_list) + ||kthread_should_stop ()));mutex_lock (&ecryptfs_kthread_ctl.mux);if (ecryptfs_kthread_ctl.flags &ECRYPTFS_KTHREAD_ZOMBIE ) {mutex_unlock (&ecryptfs_kthread_ctl.mux);
@@ -103,7 +104,6 @@ int ecryptfs_init_kthread(void)void ecryptfs_destroy_kthread (void ) { -struct ecryptfs_open_req tmp_req;struct ecryptfs_open_req *req;mutex_lock (&ecryptfs_kthread_ctl.mux);
@@ -115,12 +115,8 @@ void ecryptfs_destroy_kthread(void)wake_up (&req->wait);mutex_unlock (&req->mux); } -memset (&tmp_req,0 ,sizeof (tmp_req)); - tmp_req.flags =ECRYPTFS_REQ_ZOMBIE ; -/* Both the list and dummy entry are entirely defunct at this point */ -list_add_tail (&tmp_req.kthread_ctl_list, - &ecryptfs_kthread_ctl.req_list);mutex_unlock (&ecryptfs_kthread_ctl.mux); +kthread_stop (ecryptfs_kthread);wake_up (&ecryptfs_kthread_ctl.wait); }
Signature · 1 line
1.5.3.6
Re: [PATCH] eCryptfs: Clean up kthread synchronization
In reply to Michael HalcrowQuoted 46 lines · On Thu, 22 May 2008 16:16:25 -0500 Michael Halcrow <[email protected]> wrote:
Quoted 40 lines · Andrew Morton
Quoted 25 lines · On Thu, 22 May 2008 14:31:55 -0500 Michael Halcrow <[email protected]> wrote:
Quoted 16 lines
Quoted 16 lines
(quoted diff) +16
-0+void ecryptfs_destroy_kthread(void) +{ + struct ecryptfs_open_req tmp_req; + struct ecryptfs_open_req *req; + + mutex_lock(&ecryptfs_kthread_ctl.mux); + ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; + list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list, + kthread_ctl_list) { + mutex_lock(&req->mux); + req->flags |= ECRYPTFS_REQ_ZOMBIE; + wake_up_process(req->requesting_task); + mutex_unlock(&req->mux); + } + memset(&tmp_req, 0, sizeof(tmp_req)); + tmp_req.flags = ECRYPTFS_REQ_ZOMBIE;Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ list_add_tail(&tmp_req.kthread_ctl_list, + &ecryptfs_kthread_ctl.req_list); + mutex_unlock(&ecryptfs_kthread_ctl.mux); + wake_up(&ecryptfs_kthread_ctl.wait); +}eh? We attach a local variable to a global list and then return? That won't last very long.
Quoted 11 lines
Adding this dummy entry to the list is just my own way of getting the kthread to wake up and shut down. This actually works, albeit a little ugly. The list and its contents get dropped on the floor at this point because (ecryptfs_kthread_ctl.flags &
ECRYPTFS_KTHREAD_ZOMBIE). The only consumer of this list (the kthread) checks for this flag immediately after getting the mux, and if it is there, it just exits. The only producer on this list (ecryptfs_privileged_open()) checks for this flag immediately after getting the mux and bows out if it is set. In other words, once this flag is set, the list and its contents become untouchable by anything other thanecryptfs_destroy_kthread().Quoted 4 lines
Unconvinced.
As soon as
ecryptfs_destroy_kthread()returns,tmp_reqis destroyed. But it remains on ecryptfs_kthread_ctl.req_list.
Quoted 6 lines
I intend for ecryptfs_kthread_ctl.req_list to be irrelevant once
ecryptfs_destroy_kthread()grabs the mux and sets (ecryptfs_kthread_ctl.flags |=ECRYPTFS_KTHREAD_ZOMBIE); nobody will ever do anything with that list any more. The state of the list -- including the dangling list pointer -- simply does not matter any more.
OK.
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
memset(&tmp_req, 0, sizeof(tmp_req)); tmp_req.flags = ECRYPTFS_REQ_ZOMBIE; list_add_tail(&tmp_req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); mutex_unlock(&ecryptfs_kthread_ctl.mux); wake_up(&ecryptfs_kthread_ctl.wait);-> it's dead.
So the above horridly-wrong code is not needed at all, and tmp_req can be removed.