public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] devcgroup: relax white-list protection down to RCU
@ 2008-06-27 16:57 Pavel Emelyanov
  2008-06-27 20:29 ` Serge E. Hallyn
  0 siblings, 1 reply; 3+ messages in thread
From: Pavel Emelyanov @ 2008-06-27 16:57 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Serge Hallyn

Currently this list is protected with a simple spinlock, even
for reading from one. This is OK, but can be better.

Actually I want it to be better very much, since after replacing
the OpenVZ device permissions engine with the cgroup-based one
I noticed, that we set 12 default device permissions for each newly
created container (for /dev/null, full, terminals, ect devices),
and people sometimes have up to 20 perms more, so traversing the
~30-40 elements list under a spinlock doesn't seem very good.

Here's the liter RCU protection for white-list.

Signed-off-by: Pavel Emelyanov <xemul@openvz.org>

---

diff --git a/security/device_cgroup.c b/security/device_cgroup.c
index 4ea5836..9d940c3 100644
--- a/security/device_cgroup.c
+++ b/security/device_cgroup.c
@@ -41,6 +41,7 @@ struct dev_whitelist_item {
 	short type;
 	short access;
 	struct list_head list;
+	struct rcu_head rcu;
 };
 
 struct dev_cgroup {
@@ -110,11 +111,19 @@ static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
 
 	memcpy(whcopy, wh, sizeof(*whcopy));
 	spin_lock(&dev_cgroup->lock);
-	list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
+	list_add_tail_rcu(&whcopy->list, &dev_cgroup->whitelist);
 	spin_unlock(&dev_cgroup->lock);
 	return 0;
 }
 
+static void whitelist_item_free(struct rcu_head *rcu)
+{
+	struct dev_whitelist_item *item;
+
+	item = container_of(rcu, struct dev_whitelist_item, rcu);
+	kfree(item);
+}
+
 /*
  * called under cgroup_lock()
  * since the list is visible to other tasks, we need the spinlock also
@@ -138,8 +147,8 @@ static void dev_whitelist_rm(struct dev_cgroup *dev_cgroup,
 remove:
 		walk->access &= ~wh->access;
 		if (!walk->access) {
-			list_del(&walk->list);
-			kfree(walk);
+			list_del_rcu(&walk->list);
+			call_rcu(&walk->rcu, whitelist_item_free);
 		}
 	}
 	spin_unlock(&dev_cgroup->lock);
@@ -246,15 +255,15 @@ static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
 	struct dev_whitelist_item *wh;
 	char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
 
-	spin_lock(&devcgroup->lock);
-	list_for_each_entry(wh, &devcgroup->whitelist, list) {
+	rcu_read_lock();
+	list_for_each_entry_rcu(wh, &devcgroup->whitelist, list) {
 		set_access(acc, wh->access);
 		set_majmin(maj, wh->major);
 		set_majmin(min, wh->minor);
 		seq_printf(m, "%c %s:%s %s\n", type_to_char(wh->type),
 			   maj, min, acc);
 	}
-	spin_unlock(&devcgroup->lock);
+	rcu_read_unlock();
 
 	return 0;
 }
@@ -516,8 +525,8 @@ int devcgroup_inode_permission(struct inode *inode, int mask)
 	if (!dev_cgroup)
 		return 0;
 
-	spin_lock(&dev_cgroup->lock);
-	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
+	rcu_read_lock();
+	list_for_each_entry_rcu(wh, &dev_cgroup->whitelist, list) {
 		if (wh->type & DEV_ALL)
 			goto acc_check;
 		if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
@@ -533,10 +542,10 @@ acc_check:
 			continue;
 		if ((mask & MAY_READ) && !(wh->access & ACC_READ))
 			continue;
-		spin_unlock(&dev_cgroup->lock);
+		rcu_read_unlock();
 		return 0;
 	}
-	spin_unlock(&dev_cgroup->lock);
+	rcu_read_unlock();
 
 	return -EPERM;
 }
@@ -552,7 +561,7 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
 	if (!dev_cgroup)
 		return 0;
 
-	spin_lock(&dev_cgroup->lock);
+	rcu_read_lock();
 	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
 		if (wh->type & DEV_ALL)
 			goto acc_check;
@@ -567,9 +576,9 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
 acc_check:
 		if (!(wh->access & ACC_MKNOD))
 			continue;
-		spin_unlock(&dev_cgroup->lock);
+		rcu_read_unlock();
 		return 0;
 	}
-	spin_unlock(&dev_cgroup->lock);
+	rcu_read_unlock();
 	return -EPERM;
 }

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

* Re: [PATCH] devcgroup: relax white-list protection down to RCU
  2008-06-27 16:57 [PATCH] devcgroup: relax white-list protection down to RCU Pavel Emelyanov
@ 2008-06-27 20:29 ` Serge E. Hallyn
  0 siblings, 0 replies; 3+ messages in thread
From: Serge E. Hallyn @ 2008-06-27 20:29 UTC (permalink / raw)
  To: Pavel Emelyanov; +Cc: Linux Kernel Mailing List, Serge Hallyn

Quoting Pavel Emelyanov (xemul@openvz.org):
> Currently this list is protected with a simple spinlock, even
> for reading from one. This is OK, but can be better.
> 
> Actually I want it to be better very much, since after replacing
> the OpenVZ device permissions engine with the cgroup-based one
> I noticed, that we set 12 default device permissions for each newly
> created container (for /dev/null, full, terminals, ect devices),
> and people sometimes have up to 20 perms more, so traversing the
> ~30-40 elements list under a spinlock doesn't seem very good.
> 
> Here's the liter RCU protection for white-list.
> 
> Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
> 
> ---
> 
> diff --git a/security/device_cgroup.c b/security/device_cgroup.c
> index 4ea5836..9d940c3 100644
> --- a/security/device_cgroup.c
> +++ b/security/device_cgroup.c
> @@ -41,6 +41,7 @@ struct dev_whitelist_item {
>  	short type;
>  	short access;
>  	struct list_head list;
> +	struct rcu_head rcu;
>  };
> 
>  struct dev_cgroup {
> @@ -110,11 +111,19 @@ static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
> 
>  	memcpy(whcopy, wh, sizeof(*whcopy));
>  	spin_lock(&dev_cgroup->lock);
> -	list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
> +	list_add_tail_rcu(&whcopy->list, &dev_cgroup->whitelist);
>  	spin_unlock(&dev_cgroup->lock);
>  	return 0;
>  }
> 
> +static void whitelist_item_free(struct rcu_head *rcu)
> +{
> +	struct dev_whitelist_item *item;
> +
> +	item = container_of(rcu, struct dev_whitelist_item, rcu);
> +	kfree(item);
> +}
> +
>  /*
>   * called under cgroup_lock()
>   * since the list is visible to other tasks, we need the spinlock also
> @@ -138,8 +147,8 @@ static void dev_whitelist_rm(struct dev_cgroup *dev_cgroup,
>  remove:
>  		walk->access &= ~wh->access;
>  		if (!walk->access) {
> -			list_del(&walk->list);
> -			kfree(walk);
> +			list_del_rcu(&walk->list);
> +			call_rcu(&walk->rcu, whitelist_item_free);

The only thing I'd suggest is that a call_rcu() really isn't necessary.
You'd avoid the rcu_head in each dev_whitelist_item if you just did

			synchronize_rcu();
			kfree(walk);

here.  Downside is you're keeping the cgroup_lock() a little longer
then...

But that's just an idea.  Whether you do that or not,

Acked-by: Serge Hallyn <serue@us.ibm.com>

Thanks for doing this.

-serge

>  		}
>  	}
>  	spin_unlock(&dev_cgroup->lock);
> @@ -246,15 +255,15 @@ static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
>  	struct dev_whitelist_item *wh;
>  	char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
> 
> -	spin_lock(&devcgroup->lock);
> -	list_for_each_entry(wh, &devcgroup->whitelist, list) {
> +	rcu_read_lock();
> +	list_for_each_entry_rcu(wh, &devcgroup->whitelist, list) {
>  		set_access(acc, wh->access);
>  		set_majmin(maj, wh->major);
>  		set_majmin(min, wh->minor);
>  		seq_printf(m, "%c %s:%s %s\n", type_to_char(wh->type),
>  			   maj, min, acc);
>  	}
> -	spin_unlock(&devcgroup->lock);
> +	rcu_read_unlock();
> 
>  	return 0;
>  }
> @@ -516,8 +525,8 @@ int devcgroup_inode_permission(struct inode *inode, int mask)
>  	if (!dev_cgroup)
>  		return 0;
> 
> -	spin_lock(&dev_cgroup->lock);
> -	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
> +	rcu_read_lock();
> +	list_for_each_entry_rcu(wh, &dev_cgroup->whitelist, list) {
>  		if (wh->type & DEV_ALL)
>  			goto acc_check;
>  		if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
> @@ -533,10 +542,10 @@ acc_check:
>  			continue;
>  		if ((mask & MAY_READ) && !(wh->access & ACC_READ))
>  			continue;
> -		spin_unlock(&dev_cgroup->lock);
> +		rcu_read_unlock();
>  		return 0;
>  	}
> -	spin_unlock(&dev_cgroup->lock);
> +	rcu_read_unlock();
> 
>  	return -EPERM;
>  }
> @@ -552,7 +561,7 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
>  	if (!dev_cgroup)
>  		return 0;
> 
> -	spin_lock(&dev_cgroup->lock);
> +	rcu_read_lock();
>  	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
>  		if (wh->type & DEV_ALL)
>  			goto acc_check;
> @@ -567,9 +576,9 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
>  acc_check:
>  		if (!(wh->access & ACC_MKNOD))
>  			continue;
> -		spin_unlock(&dev_cgroup->lock);
> +		rcu_read_unlock();
>  		return 0;
>  	}
> -	spin_unlock(&dev_cgroup->lock);
> +	rcu_read_unlock();
>  	return -EPERM;
>  }

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

* [PATCH] devcgroup: relax white-list protection down to RCU
@ 2008-07-11 15:27 Pavel Emelyanov
  0 siblings, 0 replies; 3+ messages in thread
From: Pavel Emelyanov @ 2008-07-11 15:27 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Serge Hallyn, Linux Kernel Mailing List

Currently this list is protected with a simple spinlock, even
for reading from one. This is OK, but can be better.

Actually I want it to be better very much, since after replacing
the OpenVZ device permissions engine with the cgroup-based one
I noticed, that we set 12 default device permissions for each newly
created container (for /dev/null, full, terminals, ect devices),
and people sometimes have up to 20 perms more, so traversing the
~30-40 elements list under a spinlock doesn't seem very good.

Here's the RCU protection for white-list - dev_whitelist_item-s
are added and removed under the devcg->lock, but are looked up in
permissions checking under the rcu_read_lock.

Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>

---

diff --git a/security/device_cgroup.c b/security/device_cgroup.c
index 0bbd705..5d447a4 100644
--- a/security/device_cgroup.c
+++ b/security/device_cgroup.c
@@ -41,6 +41,7 @@ struct dev_whitelist_item {
 	short type;
 	short access;
 	struct list_head list;
+	struct rcu_head rcu;
 };
 
 struct dev_cgroup {
@@ -133,11 +134,19 @@ static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
 	}
 
 	if (whcopy != NULL)
-		list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
+		list_add_tail_rcu(&whcopy->list, &dev_cgroup->whitelist);
 	spin_unlock(&dev_cgroup->lock);
 	return 0;
 }
 
+static void whitelist_item_free(struct rcu_head *rcu)
+{
+	struct dev_whitelist_item *item;
+
+	item = container_of(rcu, struct dev_whitelist_item, rcu);
+	kfree(item);
+}
+
 /*
  * called under cgroup_lock()
  * since the list is visible to other tasks, we need the spinlock also
@@ -161,8 +170,8 @@ static void dev_whitelist_rm(struct dev_cgroup *dev_cgroup,
 remove:
 		walk->access &= ~wh->access;
 		if (!walk->access) {
-			list_del(&walk->list);
-			kfree(walk);
+			list_del_rcu(&walk->list);
+			call_rcu(&walk->rcu, whitelist_item_free);
 		}
 	}
 	spin_unlock(&dev_cgroup->lock);
@@ -269,15 +278,15 @@ static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
 	struct dev_whitelist_item *wh;
 	char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
 
-	spin_lock(&devcgroup->lock);
-	list_for_each_entry(wh, &devcgroup->whitelist, list) {
+	rcu_read_lock();
+	list_for_each_entry_rcu(wh, &devcgroup->whitelist, list) {
 		set_access(acc, wh->access);
 		set_majmin(maj, wh->major);
 		set_majmin(min, wh->minor);
 		seq_printf(m, "%c %s:%s %s\n", type_to_char(wh->type),
 			   maj, min, acc);
 	}
-	spin_unlock(&devcgroup->lock);
+	rcu_read_unlock();
 
 	return 0;
 }
@@ -508,8 +517,8 @@ int devcgroup_inode_permission(struct inode *inode, int mask)
 	if (!dev_cgroup)
 		return 0;
 
-	spin_lock(&dev_cgroup->lock);
-	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
+	rcu_read_lock();
+	list_for_each_entry_rcu(wh, &dev_cgroup->whitelist, list) {
 		if (wh->type & DEV_ALL)
 			goto acc_check;
 		if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
@@ -525,10 +534,10 @@ acc_check:
 			continue;
 		if ((mask & MAY_READ) && !(wh->access & ACC_READ))
 			continue;
-		spin_unlock(&dev_cgroup->lock);
+		rcu_read_unlock();
 		return 0;
 	}
-	spin_unlock(&dev_cgroup->lock);
+	rcu_read_unlock();
 
 	return -EPERM;
 }
@@ -543,7 +552,7 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
 	if (!dev_cgroup)
 		return 0;
 
-	spin_lock(&dev_cgroup->lock);
+	rcu_read_lock();
 	list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
 		if (wh->type & DEV_ALL)
 			goto acc_check;
@@ -558,9 +567,9 @@ int devcgroup_inode_mknod(int mode, dev_t dev)
 acc_check:
 		if (!(wh->access & ACC_MKNOD))
 			continue;
-		spin_unlock(&dev_cgroup->lock);
+		rcu_read_unlock();
 		return 0;
 	}
-	spin_unlock(&dev_cgroup->lock);
+	rcu_read_unlock();
 	return -EPERM;
 }

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

end of thread, other threads:[~2008-07-11 15:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-27 16:57 [PATCH] devcgroup: relax white-list protection down to RCU Pavel Emelyanov
2008-06-27 20:29 ` Serge E. Hallyn
  -- strict thread matches above, loose matches on Subject: below --
2008-07-11 15:27 Pavel Emelyanov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox