linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Yama: add RCU to drop read locking
@ 2012-11-14  3:58 Kees Cook
  2012-11-14 15:10 ` Serge Hallyn
  2012-11-14 22:12 ` John Johansen
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2012-11-14  3:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: James Morris, Kees Cook, John Johansen, Serge E. Hallyn,
	Eric Paris, linux-security-module

Stop using spinlocks in the read path. Add RCU list to handle the readers.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 security/yama/yama_lsm.c |   43 ++++++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index b4c2984..17da6ca 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -30,6 +30,7 @@ struct ptrace_relation {
 	struct task_struct *tracer;
 	struct task_struct *tracee;
 	struct list_head node;
+	struct rcu_head rcu;
 };
 
 static LIST_HEAD(ptracer_relations);
@@ -48,32 +49,29 @@ static DEFINE_SPINLOCK(ptracer_relations_lock);
 static int yama_ptracer_add(struct task_struct *tracer,
 			    struct task_struct *tracee)
 {
-	int rc = 0;
-	struct ptrace_relation *added;
-	struct ptrace_relation *entry, *relation = NULL;
+	struct ptrace_relation *relation, *added;
 
 	added = kmalloc(sizeof(*added), GFP_KERNEL);
 	if (!added)
 		return -ENOMEM;
 
+	added->tracee = tracee;
+	added->tracer = tracer;
+
 	spin_lock_bh(&ptracer_relations_lock);
-	list_for_each_entry(entry, &ptracer_relations, node)
-		if (entry->tracee == tracee) {
-			relation = entry;
-			break;
+	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
+		if (relation->tracee == tracee) {
+			list_replace_rcu(&relation->node, &added->node);
+			kfree_rcu(relation, rcu);
+			goto out;
 		}
-	if (!relation) {
-		relation = added;
-		relation->tracee = tracee;
-		list_add(&relation->node, &ptracer_relations);
 	}
-	relation->tracer = tracer;
 
-	spin_unlock_bh(&ptracer_relations_lock);
-	if (added != relation)
-		kfree(added);
+	list_add_rcu(&added->node, &ptracer_relations);
 
-	return rc;
+out:
+	spin_unlock_bh(&ptracer_relations_lock);
+	return 0;
 }
 
 /**
@@ -84,15 +82,16 @@ static int yama_ptracer_add(struct task_struct *tracer,
 static void yama_ptracer_del(struct task_struct *tracer,
 			     struct task_struct *tracee)
 {
-	struct ptrace_relation *relation, *safe;
+	struct ptrace_relation *relation;
 
 	spin_lock_bh(&ptracer_relations_lock);
-	list_for_each_entry_safe(relation, safe, &ptracer_relations, node)
+	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
 		if (relation->tracee == tracee ||
 		    (tracer && relation->tracer == tracer)) {
-			list_del(&relation->node);
-			kfree(relation);
+			list_del_rcu(&relation->node);
+			kfree_rcu(relation, rcu);
 		}
+	}
 	spin_unlock_bh(&ptracer_relations_lock);
 }
 
@@ -217,11 +216,10 @@ static int ptracer_exception_found(struct task_struct *tracer,
 	struct task_struct *parent = NULL;
 	bool found = false;
 
-	spin_lock_bh(&ptracer_relations_lock);
 	rcu_read_lock();
 	if (!thread_group_leader(tracee))
 		tracee = rcu_dereference(tracee->group_leader);
-	list_for_each_entry(relation, &ptracer_relations, node)
+	list_for_each_entry_rcu(relation, &ptracer_relations, node)
 		if (relation->tracee == tracee) {
 			parent = relation->tracer;
 			found = true;
@@ -231,7 +229,6 @@ static int ptracer_exception_found(struct task_struct *tracer,
 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
 		rc = 1;
 	rcu_read_unlock();
-	spin_unlock_bh(&ptracer_relations_lock);
 
 	return rc;
 }
-- 
1.7.9.5

-- 
Kees Cook
Chrome OS Security

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Yama: add RCU to drop read locking
  2012-11-14  3:58 [PATCH] Yama: add RCU to drop read locking Kees Cook
@ 2012-11-14 15:10 ` Serge Hallyn
  2012-11-14 22:12 ` John Johansen
  1 sibling, 0 replies; 3+ messages in thread
From: Serge Hallyn @ 2012-11-14 15:10 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, James Morris, John Johansen, Eric Paris,
	linux-security-module

Quoting Kees Cook (keescook@chromium.org):
> Stop using spinlocks in the read path. Add RCU list to handle the readers.

Looks good to me.  BTW, kfree_rcu is neat :)

Reviewed-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>

> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  security/yama/yama_lsm.c |   43 ++++++++++++++++++++-----------------------
>  1 file changed, 20 insertions(+), 23 deletions(-)
> 
> diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> index b4c2984..17da6ca 100644
> --- a/security/yama/yama_lsm.c
> +++ b/security/yama/yama_lsm.c
> @@ -30,6 +30,7 @@ struct ptrace_relation {
>  	struct task_struct *tracer;
>  	struct task_struct *tracee;
>  	struct list_head node;
> +	struct rcu_head rcu;
>  };
>  
>  static LIST_HEAD(ptracer_relations);
> @@ -48,32 +49,29 @@ static DEFINE_SPINLOCK(ptracer_relations_lock);
>  static int yama_ptracer_add(struct task_struct *tracer,
>  			    struct task_struct *tracee)
>  {
> -	int rc = 0;
> -	struct ptrace_relation *added;
> -	struct ptrace_relation *entry, *relation = NULL;
> +	struct ptrace_relation *relation, *added;
>  
>  	added = kmalloc(sizeof(*added), GFP_KERNEL);
>  	if (!added)
>  		return -ENOMEM;
>  
> +	added->tracee = tracee;
> +	added->tracer = tracer;
> +
>  	spin_lock_bh(&ptracer_relations_lock);
> -	list_for_each_entry(entry, &ptracer_relations, node)
> -		if (entry->tracee == tracee) {
> -			relation = entry;
> -			break;
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
> +		if (relation->tracee == tracee) {
> +			list_replace_rcu(&relation->node, &added->node);
> +			kfree_rcu(relation, rcu);
> +			goto out;
>  		}
> -	if (!relation) {
> -		relation = added;
> -		relation->tracee = tracee;
> -		list_add(&relation->node, &ptracer_relations);
>  	}
> -	relation->tracer = tracer;
>  
> -	spin_unlock_bh(&ptracer_relations_lock);
> -	if (added != relation)
> -		kfree(added);
> +	list_add_rcu(&added->node, &ptracer_relations);
>  
> -	return rc;
> +out:
> +	spin_unlock_bh(&ptracer_relations_lock);
> +	return 0;
>  }
>  
>  /**
> @@ -84,15 +82,16 @@ static int yama_ptracer_add(struct task_struct *tracer,
>  static void yama_ptracer_del(struct task_struct *tracer,
>  			     struct task_struct *tracee)
>  {
> -	struct ptrace_relation *relation, *safe;
> +	struct ptrace_relation *relation;
>  
>  	spin_lock_bh(&ptracer_relations_lock);
> -	list_for_each_entry_safe(relation, safe, &ptracer_relations, node)
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
>  		if (relation->tracee == tracee ||
>  		    (tracer && relation->tracer == tracer)) {
> -			list_del(&relation->node);
> -			kfree(relation);
> +			list_del_rcu(&relation->node);
> +			kfree_rcu(relation, rcu);
>  		}
> +	}
>  	spin_unlock_bh(&ptracer_relations_lock);
>  }
>  
> @@ -217,11 +216,10 @@ static int ptracer_exception_found(struct task_struct *tracer,
>  	struct task_struct *parent = NULL;
>  	bool found = false;
>  
> -	spin_lock_bh(&ptracer_relations_lock);
>  	rcu_read_lock();
>  	if (!thread_group_leader(tracee))
>  		tracee = rcu_dereference(tracee->group_leader);
> -	list_for_each_entry(relation, &ptracer_relations, node)
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node)
>  		if (relation->tracee == tracee) {
>  			parent = relation->tracer;
>  			found = true;
> @@ -231,7 +229,6 @@ static int ptracer_exception_found(struct task_struct *tracer,
>  	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
>  		rc = 1;
>  	rcu_read_unlock();
> -	spin_unlock_bh(&ptracer_relations_lock);
>  
>  	return rc;
>  }
> -- 
> 1.7.9.5
> 
> -- 
> Kees Cook
> Chrome OS Security

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Yama: add RCU to drop read locking
  2012-11-14  3:58 [PATCH] Yama: add RCU to drop read locking Kees Cook
  2012-11-14 15:10 ` Serge Hallyn
@ 2012-11-14 22:12 ` John Johansen
  1 sibling, 0 replies; 3+ messages in thread
From: John Johansen @ 2012-11-14 22:12 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, James Morris, Serge E. Hallyn, Eric Paris,
	linux-security-module

On 11/13/2012 07:58 PM, Kees Cook wrote:
> Stop using spinlocks in the read path. Add RCU list to handle the readers.
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>

Looks good to me

Acked-by: John Johansen <john.johansen@canonical.com>

> ---
>  security/yama/yama_lsm.c |   43 ++++++++++++++++++++-----------------------
>  1 file changed, 20 insertions(+), 23 deletions(-)
> 
> diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> index b4c2984..17da6ca 100644
> --- a/security/yama/yama_lsm.c
> +++ b/security/yama/yama_lsm.c
> @@ -30,6 +30,7 @@ struct ptrace_relation {
>  	struct task_struct *tracer;
>  	struct task_struct *tracee;
>  	struct list_head node;
> +	struct rcu_head rcu;
>  };
>  
>  static LIST_HEAD(ptracer_relations);
> @@ -48,32 +49,29 @@ static DEFINE_SPINLOCK(ptracer_relations_lock);
>  static int yama_ptracer_add(struct task_struct *tracer,
>  			    struct task_struct *tracee)
>  {
> -	int rc = 0;
> -	struct ptrace_relation *added;
> -	struct ptrace_relation *entry, *relation = NULL;
> +	struct ptrace_relation *relation, *added;
>  
>  	added = kmalloc(sizeof(*added), GFP_KERNEL);
>  	if (!added)
>  		return -ENOMEM;
>  
> +	added->tracee = tracee;
> +	added->tracer = tracer;
> +
>  	spin_lock_bh(&ptracer_relations_lock);
> -	list_for_each_entry(entry, &ptracer_relations, node)
> -		if (entry->tracee == tracee) {
> -			relation = entry;
> -			break;
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
> +		if (relation->tracee == tracee) {
> +			list_replace_rcu(&relation->node, &added->node);
> +			kfree_rcu(relation, rcu);
> +			goto out;
>  		}
> -	if (!relation) {
> -		relation = added;
> -		relation->tracee = tracee;
> -		list_add(&relation->node, &ptracer_relations);
>  	}
> -	relation->tracer = tracer;
>  
> -	spin_unlock_bh(&ptracer_relations_lock);
> -	if (added != relation)
> -		kfree(added);
> +	list_add_rcu(&added->node, &ptracer_relations);
>  
> -	return rc;
> +out:
> +	spin_unlock_bh(&ptracer_relations_lock);
> +	return 0;
>  }
>  
>  /**
> @@ -84,15 +82,16 @@ static int yama_ptracer_add(struct task_struct *tracer,
>  static void yama_ptracer_del(struct task_struct *tracer,
>  			     struct task_struct *tracee)
>  {
> -	struct ptrace_relation *relation, *safe;
> +	struct ptrace_relation *relation;
>  
>  	spin_lock_bh(&ptracer_relations_lock);
> -	list_for_each_entry_safe(relation, safe, &ptracer_relations, node)
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
>  		if (relation->tracee == tracee ||
>  		    (tracer && relation->tracer == tracer)) {
> -			list_del(&relation->node);
> -			kfree(relation);
> +			list_del_rcu(&relation->node);
> +			kfree_rcu(relation, rcu);
>  		}
> +	}
>  	spin_unlock_bh(&ptracer_relations_lock);
>  }
>  
> @@ -217,11 +216,10 @@ static int ptracer_exception_found(struct task_struct *tracer,
>  	struct task_struct *parent = NULL;
>  	bool found = false;
>  
> -	spin_lock_bh(&ptracer_relations_lock);
>  	rcu_read_lock();
>  	if (!thread_group_leader(tracee))
>  		tracee = rcu_dereference(tracee->group_leader);
> -	list_for_each_entry(relation, &ptracer_relations, node)
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node)
>  		if (relation->tracee == tracee) {
>  			parent = relation->tracer;
>  			found = true;
> @@ -231,7 +229,6 @@ static int ptracer_exception_found(struct task_struct *tracer,
>  	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
>  		rc = 1;
>  	rcu_read_unlock();
> -	spin_unlock_bh(&ptracer_relations_lock);
>  
>  	return rc;
>  }
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-11-14 22:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-14  3:58 [PATCH] Yama: add RCU to drop read locking Kees Cook
2012-11-14 15:10 ` Serge Hallyn
2012-11-14 22:12 ` John Johansen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).