From: Ondrej Mosnacek <omosnace@redhat.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org,
linux-security-module@vger.kernel.org, selinux@vger.kernel.org,
rcu@vger.kernel.org, linux-kernel@vger.kernel.org,
Martin Pitt <mpitt@redhat.com>
Subject: [PATCH 1/2] fs: convert simple_xattrs to RCU list
Date: Thu, 1 Sep 2022 17:26:31 +0200 [thread overview]
Message-ID: <20220901152632.970018-2-omosnace@redhat.com> (raw)
In-Reply-To: <20220901152632.970018-1-omosnace@redhat.com>
Use the RCU list mechanism instead of a simple lock to access/modify
simple_xattrs. The performance benefit is probably negligible, but it
will help avoid lock nesting concerns for an upcoming patch.
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
---
fs/xattr.c | 36 ++++++++++++++++++++++--------------
include/linux/xattr.h | 1 +
2 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/fs/xattr.c b/fs/xattr.c
index a1f4998bc6be..fad2344f1168 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -22,6 +22,8 @@
#include <linux/audit.h>
#include <linux/vmalloc.h>
#include <linux/posix_acl_xattr.h>
+#include <linux/rculist.h>
+#include <linux/rcupdate.h>
#include <linux/uaccess.h>
@@ -1030,8 +1032,8 @@ int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
struct simple_xattr *xattr;
int ret = -ENODATA;
- spin_lock(&xattrs->lock);
- list_for_each_entry(xattr, &xattrs->head, list) {
+ rcu_read_lock();
+ list_for_each_entry_rcu(xattr, &xattrs->head, list) {
if (strcmp(name, xattr->name))
continue;
@@ -1044,10 +1046,18 @@ int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
}
break;
}
- spin_unlock(&xattrs->lock);
+ rcu_read_unlock();
return ret;
}
+static void simple_xattr_free_rcu(struct rcu_head *rcu)
+{
+ struct simple_xattr *xattr = container_of(rcu, struct simple_xattr, rcu);
+
+ kfree(xattr->name);
+ kvfree(xattr);
+}
+
/**
* simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
* @xattrs: target simple_xattr list
@@ -1094,11 +1104,11 @@ int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
xattr = new_xattr;
err = -EEXIST;
} else if (new_xattr) {
- list_replace(&xattr->list, &new_xattr->list);
+ list_replace_rcu(&xattr->list, &new_xattr->list);
if (removed_size)
*removed_size = xattr->size;
} else {
- list_del(&xattr->list);
+ list_del_rcu(&xattr->list);
if (removed_size)
*removed_size = xattr->size;
}
@@ -1109,15 +1119,13 @@ int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
xattr = new_xattr;
err = -ENODATA;
} else {
- list_add(&new_xattr->list, &xattrs->head);
+ list_add_rcu(&new_xattr->list, &xattrs->head);
xattr = NULL;
}
out:
spin_unlock(&xattrs->lock);
- if (xattr) {
- kfree(xattr->name);
- kvfree(xattr);
- }
+ if (xattr)
+ call_rcu(&xattr->rcu, simple_xattr_free_rcu);
return err;
}
@@ -1169,8 +1177,8 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
}
#endif
- spin_lock(&xattrs->lock);
- list_for_each_entry(xattr, &xattrs->head, list) {
+ rcu_read_lock();
+ list_for_each_entry_rcu(xattr, &xattrs->head, list) {
/* skip "trusted." attributes for unprivileged callers */
if (!trusted && xattr_is_trusted(xattr->name))
continue;
@@ -1179,7 +1187,7 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
if (err)
break;
}
- spin_unlock(&xattrs->lock);
+ rcu_read_unlock();
return err ? err : size - remaining_size;
}
@@ -1191,6 +1199,6 @@ void simple_xattr_list_add(struct simple_xattrs *xattrs,
struct simple_xattr *new_xattr)
{
spin_lock(&xattrs->lock);
- list_add(&new_xattr->list, &xattrs->head);
+ list_add_rcu(&new_xattr->list, &xattrs->head);
spin_unlock(&xattrs->lock);
}
diff --git a/include/linux/xattr.h b/include/linux/xattr.h
index 979a9d3e5bfb..3236c469aaac 100644
--- a/include/linux/xattr.h
+++ b/include/linux/xattr.h
@@ -86,6 +86,7 @@ struct simple_xattrs {
struct simple_xattr {
struct list_head list;
+ struct rcu_head rcu;
char *name;
size_t size;
char value[];
--
2.37.2
next prev parent reply other threads:[~2022-09-01 15:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-01 15:26 [PATCH 0/2] fs: fix capable() call in simple_xattr_list() Ondrej Mosnacek
2022-09-01 15:26 ` Ondrej Mosnacek [this message]
2022-09-01 15:26 ` [PATCH 2/2] fs: don't call capable() prematurely " Ondrej Mosnacek
2022-09-05 9:08 ` [PATCH 0/2] fs: fix capable() call " Christian Brauner
2022-09-05 10:15 ` Ondrej Mosnacek
2022-09-05 15:30 ` Christian Brauner
2022-11-02 18:24 ` Christian Brauner
2022-11-03 1:59 ` Serge E. Hallyn
2022-11-03 9:04 ` Ondrej Mosnacek
2022-11-03 9:12 ` Christian Brauner
2022-11-03 10:51 ` Ondrej Mosnacek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220901152632.970018-2-omosnace@redhat.com \
--to=omosnace@redhat.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=mpitt@redhat.com \
--cc=rcu@vger.kernel.org \
--cc=selinux@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).