Skip to contentSkip to conversation

Introduce and use get_task_mnt_ns()

PATCH In review Alexey Dobriyan 2 participants 2 messages Jan 17, 2007 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Introduce and use get_task_mnt_ns()

Apply after "[PATCH] Fix NULL ->nsproxy dereference in /proc/*/mounts".

Similar to get_task_mm(): get a reference to task's mnt namespace if any. Suggested by Pavel Emelianov.

---

 fs/proc/base.c          |   15 ++-------------
 include/linux/nsproxy.h |    1 +
 kernel/nsproxy.c        |   14 ++++++++++++++
 3 files changed, 17 insertions(+), 13 deletions(-)
fs/proc/base.c +2 -13
@@ -370,13 +370,7 @@ static int mounts_open(struct inode *ino
 	int ret = -EINVAL;
 
 	if (task) {
-		task_lock(task);
-		if (task->nsproxy) {
-			ns = task->nsproxy->mnt_ns;
-			if (ns)
-				get_mnt_ns(ns);
-		}
-		task_unlock(task);
+		ns = get_task_mnt_ns(task);
 		put_task_struct(task);
 	}
 
@@ -443,12 +437,7 @@ static int mountstats_open(struct inode 
 		struct task_struct *task = get_proc_task(inode);
 
 		if (task) {
-			task_lock(task);
-			if (task->nsproxy)
-				mnt_ns = task->nsproxy->mnt_ns;
-			if (mnt_ns)
-				get_mnt_ns(mnt_ns);
-			task_unlock(task);
+			mnt_ns = get_task_mnt_ns(task);
 			put_task_struct(task);
 		}
 
include/linux/nsproxy.h +1 -0
@@ -35,6 +35,7 @@ struct nsproxy *dup_namespaces(struct ns
 int copy_namespaces(int flags, struct task_struct *tsk);
 void get_task_namespaces(struct task_struct *tsk);
 void free_nsproxy(struct nsproxy *ns);
+struct mnt_namespace * get_task_mnt_ns(struct task_struct *tsk);
 
 static inline void put_nsproxy(struct nsproxy *ns)
 {
kernel/nsproxy.c +14 -0
@@ -147,3 +147,17 @@ void free_nsproxy(struct nsproxy *ns)
 		put_pid_ns(ns->pid_ns);
 	kfree(ns);
 }
+
+struct mnt_namespace * get_task_mnt_ns(struct task_struct *tsk)
+{
+	struct mnt_namespace *mnt_ns = NULL;
+
+	task_lock(tsk);
+	if (tsk->nsproxy)
+		mnt_ns = tsk->nsproxy->mnt_ns;
+	if (mnt_ns)
+		get_mnt_ns(mnt_ns);
+	task_unlock(tsk);
+
+	return mnt_ns;
+}
Acked-by Serge Hallyn on v1 1/1
#1 lore raw
Quoted 6 lines · Alexey Dobriyan

Apply after "[PATCH] Fix NULL ->nsproxy dereference in /proc/*/mounts".

Similar to get_task_mm(): get a reference to task's mnt namespace if any. Suggested by Pavel Emelianov.

Yeah, that's nicer, thanks.

Quoted 74 lines

---

 fs/proc/base.c          |   15 ++-------------
 include/linux/nsproxy.h |    1 +
 kernel/nsproxy.c        |   14 ++++++++++++++
 3 files changed, 17 insertions(+), 13 deletions(-)
fs/proc/base.c +2 -13
@@ -370,13 +370,7 @@ static int mounts_open(struct inode *ino
 	int ret = -EINVAL;
 
 	if (task) {
-		task_lock(task);
-		if (task->nsproxy) {
-			ns = task->nsproxy->mnt_ns;
-			if (ns)
-				get_mnt_ns(ns);
-		}
-		task_unlock(task);
+		ns = get_task_mnt_ns(task);
 		put_task_struct(task);
 	}
 
@@ -443,12 +437,7 @@ static int mountstats_open(struct inode 
 		struct task_struct *task = get_proc_task(inode);
 
 		if (task) {
-			task_lock(task);
-			if (task->nsproxy)
-				mnt_ns = task->nsproxy->mnt_ns;
-			if (mnt_ns)
-				get_mnt_ns(mnt_ns);
-			task_unlock(task);
+			mnt_ns = get_task_mnt_ns(task);
 			put_task_struct(task);
 		}
 
include/linux/nsproxy.h +1 -0
@@ -35,6 +35,7 @@ struct nsproxy *dup_namespaces(struct ns
 int copy_namespaces(int flags, struct task_struct *tsk);
 void get_task_namespaces(struct task_struct *tsk);
 void free_nsproxy(struct nsproxy *ns);
+struct mnt_namespace * get_task_mnt_ns(struct task_struct *tsk);
 
 static inline void put_nsproxy(struct nsproxy *ns)
 {
kernel/nsproxy.c +14 -1
@@ -147,3 +147,17 @@ void free_nsproxy(struct nsproxy *ns)
 		put_pid_ns(ns->pid_ns);
 	kfree(ns);
 }
+
+struct mnt_namespace * get_task_mnt_ns(struct task_struct *tsk)
+{
+	struct mnt_namespace *mnt_ns = NULL;
+
+	task_lock(tsk);
+	if (tsk->nsproxy)
+		mnt_ns = tsk->nsproxy->mnt_ns;
+	if (mnt_ns)
+		get_mnt_ns(mnt_ns);
+	task_unlock(tsk);
+
+	return mnt_ns;
+}
 
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/