Skip to contentSkip to conversation

get_write_access()/deny_write_access() without inode->i_lock

RFC In review Al Viro 5 participants 12 messages Jun 19, 2011 – Jun 20, 2011

Subsystems (2)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

RFC 1/1 get_write_access()/deny_write_access() without inode->i_lock

I'm seriously tempted to throw away i_lock uses in
{get,deny}_write_access(), as in the patch below. The question is, how badly will it suck on various architectures? I'd expect it to be not worse than the current version, but...
BTW, I wonder if we need barriers in {put,allow}_write_access (in
either version).

Related question: would it make sense to turn that into
atomic_inc_unless_negative/atomic_dec_unless_positive? I don't remember any code doing that kind of stuff - no idea if there are any potential users for that.

fs/namei.c +0 -46
index 26bef77..7dffe2e 100644
@@ -341,52 +341,6 @@ ok:
 	return security_inode_exec_permission(inode, flags);
 }
 
-/*
- * get_write_access() gets write permission for a file.
- * put_write_access() releases this write permission.
- * This is used for regular files.
- * We cannot support write (and maybe mmap read-write shared) accesses and
- * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
- * can have the following values:
- * 0: no writers, no VM_DENYWRITE mappings
- * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
- * > 0: (i_writecount) users are writing to the file.
- *
- * Normally we operate on that counter with atomic_{inc,dec} and it's safe
- * except for the cases where we don't hold i_writecount yet. Then we need to
- * use {get,deny}_write_access() - these functions check the sign and refuse
- * to do the change if sign is wrong. Exclusion between them is provided by
- * the inode->i_lock spinlock.
- */
-
-int get_write_access(struct inode * inode)
-{
-	spin_lock(&inode->i_lock);
-	if (atomic_read(&inode->i_writecount) < 0) {
-		spin_unlock(&inode->i_lock);
-		return -ETXTBSY;
-	}
-	atomic_inc(&inode->i_writecount);
-	spin_unlock(&inode->i_lock);
-
-	return 0;
-}
-
-int deny_write_access(struct file * file)
-{
-	struct inode *inode = file->f_path.dentry->d_inode;
-
-	spin_lock(&inode->i_lock);
-	if (atomic_read(&inode->i_writecount) > 0) {
-		spin_unlock(&inode->i_lock);
-		return -ETXTBSY;
-	}
-	atomic_dec(&inode->i_writecount);
-	spin_unlock(&inode->i_lock);
-
-	return 0;
-}
-
 /**
  * path_get - get a reference to a path
  * @path: path to get the reference to
include/linux/fs.h +37 -2
index 7302e44..ab89aa3 100644
@@ -2194,8 +2194,43 @@ static inline bool execute_ok(struct inode *inode)
 	return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
 }
 
-extern int get_write_access(struct inode *);
-extern int deny_write_access(struct file *);
+/*
+ * get_write_access() gets write permission for a file.
+ * put_write_access() releases this write permission.
+ * This is used for regular files.
+ * We cannot support write (and maybe mmap read-write shared) accesses and
+ * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
+ * can have the following values:
+ * 0: no writers, no VM_DENYWRITE mappings
+ * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
+ * > 0: (i_writecount) users are writing to the file.
+ *
+ * Normally we operate on that counter with atomic_{inc,dec} and it's safe
+ * except for the cases where we don't hold i_writecount yet. Then we need to
+ * use {get,deny}_write_access() - these functions check the sign and refuse
+ * to do the change if sign is wrong.
+ */
+static inline int get_write_access(struct inode *inode)
+{
+	int v, v1;
+	for (v = atomic_read(&inode->i_writecount); v >= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))
+			return 0;
+	}
+	return -ETXTBSY;
+}
+static inline int deny_write_access(struct file *file)
+{
+	struct inode *inode = file->f_path.dentry->d_inode;
+	int v, v1;
+	for (v = atomic_read(&inode->i_writecount); v <= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))
+			return 0;
+	}
+	return -ETXTBSY;
+}
 static inline void put_write_access(struct inode * inode)
 {
 	atomic_dec(&inode->i_writecount);
#1 lore raw

include/linux/fs.h static inline bool execute_ok(struct inode *inode) · line 2220

Quoted 5 lines · Al Viro
include/linux/fs.h +5 -0
+	for (v = atomic_read(&inode->i_writecount); v >= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))
+			return 0;
+	}

You don't need to reissue the atomic_read(). atomic_cmpxchg() returns the current value of the memory location. Just set v to v1 before going round the loop again.

David

author maintainer
#2 lore raw
Quoted 11 lines · David Howells

include/linux/fs.h static inline bool execute_ok(struct inode *inode) · line 2220

Quoted 5 lines · Al Viro
include/linux/fs.h +5 -0
+	for (v = atomic_read(&inode->i_writecount); v >= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))
+			return 0;
+	}

You don't need to reissue the atomic_read(). atomic_cmpxchg() returns the current value of the memory location. Just set v to v1 before going round the loop again.

That's precisely what that loop is doing...

#3 lore raw
In reply to David Howells
Quoted 9 lines · David Howells

include/linux/fs.h static inline bool execute_ok(struct inode *inode) · line 2220

Quoted 5 lines
include/linux/fs.h +5 -0
+	for (v = atomic_read(&inode->i_writecount); v >= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))
+			return 0;
+	}

You don't need to reissue the atomic_read(). atomic_cmpxchg() returns the current value of the memory location. Just set v to v1 before going round the loop again.

Ah... Never mind - you did that anyway.

David

#4 lore raw
In reply to Al Viro
Quoted 101 lines · Al Viro

I'm seriously tempted to throw away i_lock uses in
{get,deny}_write_access(), as in the patch below. The question is, how badly will it suck on various architectures? I'd expect it to be not worse than the current version, but...
BTW, I wonder if we need barriers in {put,allow}_write_access (in
either version).

Related question: would it make sense to turn that into
atomic_inc_unless_negative/atomic_dec_unless_positive? I don't remember any code doing that kind of stuff - no idea if there are any potential users for that.

fs/namei.c +0 -46
index 26bef77..7dffe2e 100644
@@ -341,52 +341,6 @@ ok:
 	return security_inode_exec_permission(inode, flags);
 }
 
-/*
- * get_write_access() gets write permission for a file.
- * put_write_access() releases this write permission.
- * This is used for regular files.
- * We cannot support write (and maybe mmap read-write shared) accesses and
- * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
- * can have the following values:
- * 0: no writers, no VM_DENYWRITE mappings
- * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
- * > 0: (i_writecount) users are writing to the file.
- *
- * Normally we operate on that counter with atomic_{inc,dec} and it's safe
- * except for the cases where we don't hold i_writecount yet. Then we need to
- * use {get,deny}_write_access() - these functions check the sign and refuse
- * to do the change if sign is wrong. Exclusion between them is provided by
- * the inode->i_lock spinlock.
- */
-
-int get_write_access(struct inode * inode)
-{
-	spin_lock(&inode->i_lock);
-	if (atomic_read(&inode->i_writecount) < 0) {
-		spin_unlock(&inode->i_lock);
-		return -ETXTBSY;
-	}
-	atomic_inc(&inode->i_writecount);
-	spin_unlock(&inode->i_lock);
-
-	return 0;
-}
-
-int deny_write_access(struct file * file)
-{
-	struct inode *inode = file->f_path.dentry->d_inode;
-
-	spin_lock(&inode->i_lock);
-	if (atomic_read(&inode->i_writecount) > 0) {
-		spin_unlock(&inode->i_lock);
-		return -ETXTBSY;
-	}
-	atomic_dec(&inode->i_writecount);
-	spin_unlock(&inode->i_lock);
-
-	return 0;
-}
-
 /**
  * path_get - get a reference to a path
  * @path: path to get the reference to
include/linux/fs.h +22 -2
index 7302e44..ab89aa3 100644
@@ -2194,8 +2194,43 @@ static inline bool execute_ok(struct inode *inode)
 	return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
 }
 
-extern int get_write_access(struct inode *);
-extern int deny_write_access(struct file *);
+/*
+ * get_write_access() gets write permission for a file.
+ * put_write_access() releases this write permission.
+ * This is used for regular files.
+ * We cannot support write (and maybe mmap read-write shared) accesses and
+ * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
+ * can have the following values:
+ * 0: no writers, no VM_DENYWRITE mappings
+ * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
+ * > 0: (i_writecount) users are writing to the file.
+ *
+ * Normally we operate on that counter with atomic_{inc,dec} and it's safe
+ * except for the cases where we don't hold i_writecount yet. Then we need to
+ * use {get,deny}_write_access() - these functions check the sign and refuse
+ * to do the change if sign is wrong.
+ */
+static inline int get_write_access(struct inode *inode)
+{
+	int v, v1;
+	for (v = atomic_read(&inode->i_writecount); v >= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
+		if (likely(v1 == v))

include/linux/fs.h static inline bool execute_ok(struct inode *inode) · line 2228

Quoted 10 lines
include/linux/fs.h +10 -0
+			return 0;
+	}
+	return -ETXTBSY;
+}
+static inline int deny_write_access(struct file *file)
+{
+	struct inode *inode = file->f_path.dentry->d_inode;
+	int v, v1;
+	for (v = atomic_read(&inode->i_writecount); v <= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
                                                             ^^^^^

Shouldn't i_writecount be decreased here. Looks like cut & paste problem to me. Please ignore if I'm wrong.

Quoted 13 lines
include/linux/fs.h +5 -1
+		if (likely(v1 == v))
+			return 0;
+	}
+	return -ETXTBSY;
+}
 static inline void put_write_access(struct inode * inode)
 {
 	atomic_dec(&inode->i_writecount);
--

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/

Signature · 1 line
Frantisek Hrbata
author maintainer
#5 lore raw
Quoted 9 lines · Frantisek Hrbata

include/linux/fs.h static inline bool execute_ok(struct inode *inode) · line 2228

Quoted 6 lines
include/linux/fs.h +6 -0
+static inline int deny_write_access(struct file *file)
+{
+	struct inode *inode = file->f_path.dentry->d_inode;
+	int v, v1;
+	for (v = atomic_read(&inode->i_writecount); v <= 0; v = v1) {
+		v1 = atomic_cmpxchg(&inode->i_writecount, v, v + 1);
                                                             ^^^^^

Shouldn't i_writecount be decreased here. Looks like cut & paste problem to me.

Yes, it should and yes, it is. Thanks for spotting...

#6 lore raw
In reply to Al Viro
Quoted 4 lines · Al Viro

       I'm seriously tempted to throw away i_lock uses in {get,deny}_write_access(), as in the patch below.  The question is, how badly will it suck on various architectures?  I'd expect it to be not worse than the current version, but...

It might be worse, because doing a read-before-write can turn a single cache operation ("get for write") into multiple cache operations ("get for read" followed by "make exclusive").

We had that exact issue with some other users of the "read + cmpxchg" model.

The way we fixed it before was to simply omit the read, and turn that into a "guess".

In other words, I'd suggest you get rid of the "atomic_read()" entirely, and just assume that the write counter was zero to begin with. Even if that is a wrong assumption (and it probably isn't all that wrong), it can actually be more efficient to essentiall go through the loop twice: the first time yoou use the cmpxchg as just an odd way to do a read. It basically bcomes a read-with-write-intent, and solves the cacheline issue.

At that point, it's likely faster in pretty much all cases except for UP (where the spinlocks just go away, and "cmpxchg" is slower than normal code).

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

author maintainer
#7 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Al Viro

? ? ? ?I'm seriously tempted to throw away i_lock uses in {get,deny}_write_access(), as in the patch below. ?The question is, how badly will it suck on various architectures? ?I'd expect it to be not worse than the current version, but...

It might be worse, because doing a read-before-write can turn a single cache operation ("get for write") into multiple cache operations ("get for read" followed by "make exclusive").

Er... The current mainline does atomic_read() followed by atomic_inc(), so we get the same thing (plus the spin_lock()/spin_unlock()), don't we?

Quoted 5 lines

We had that exact issue with some other users of the "read + cmpxchg" model.

The way we fixed it before was to simply omit the read, and turn that into a "guess".

Quoted 7 lines

In other words, I'd suggest you get rid of the "atomic_read()" entirely, and just assume that the write counter was zero to begin with. Even if that is a wrong assumption (and it probably isn't all that wrong), it can actually be more efficient to essentiall go through the loop twice: the first time yoou use the cmpxchg as just an odd way to do a read. It basically bcomes a read-with-write-intent, and solves the cacheline issue.

For get_write_access() it's probably the right assumption for everything but /dev/tty*; for deny_write_access() it's not - a lot of binaries are run by more than one process...

FWIW, I wonder what will the things look like on ll/sc architectures; maybe it's really better to turn that into atomic_inc_unless_negative() and let the architectures override the default...

#8 lore raw
Quoted 3 lines · Al Viro

Er...  The current mainline does atomic_read() followed by atomic_inc(), so we get the same thing (plus the spin_lock()/spin_unlock()), don't we?

Yes. Unless the spinlock is in the same cacheline. No reason not to fix that, though.

Of course, if the "ETXTBUSY" case is the common case (which I doubt), then not doing the write at all would be the optimal case. But I doubt that case is even worth really worrying about ;)

Quoted 3 lines

For get_write_access() it's probably the right assumption for everything but /dev/tty*; for deny_write_access() it's not - a lot of binaries are run by more than one process...

Note the fact that EVEN IF WE GUESS INCORRECTLY, performance is likely better by guessing rather than reading, unless you know the thing is already in the local CPU cache.

Doing the loop twice instead of once is still *much* faster than an extra cache transaction that goes to the bus (or L3 or whatever).

Quoted 1 line

FWIW, I wonder what will the things look like on ll/sc architectures;

There are no ll/sc architectures worth worrying about, so I don't think that's the primary concern. That said, I don't disagree with creating a "atomic_inc_unless_negative()" helper.

Linus

author maintainer
#9 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 3 lines · Al Viro

Er... ?The current mainline does atomic_read() followed by atomic_inc(), so we get the same thing (plus the spin_lock()/spin_unlock()), don't we?

Yes. Unless the spinlock is in the same cacheline. No reason not to fix that, though.

Of course, if the "ETXTBUSY" case is the common case (which I doubt), then not doing the write at all would be the optimal case. But I doubt that case is even worth really worrying about ;)

It isn't, unless your box is spinning in attempts to do something like opening /bin/sh for write. In which case you've got worse problems ;-)

Quoted 9 lines
Quoted 4 lines

For get_write_access() it's probably the right assumption for everything but /dev/tty*; for deny_write_access() it's not - a lot of binaries are run by more than one process...

Note the fact that EVEN IF WE GUESS INCORRECTLY, performance is likely better by guessing rather than reading, unless you know the thing is already in the local CPU cache.

Quoted 8 lines

Doing the loop twice instead of once is still *much* faster than an extra cache transaction that goes to the bus (or L3 or whatever).

Quoted 1 line

FWIW, I wonder what will the things look like on ll/sc architectures;

There are no ll/sc architectures worth worrying about, so I don't think that's the primary concern. That said, I don't disagree with creating a "atomic_inc_unless_negative()" helper.

OK... Let me see if I got it right:
static inline int atomic_inc_unless_negative(atomic_t *p)

{
	int v, v1;
	for (v = 0; v >= 0; v = v1) {
		v1 = atomic_cmpxchg(p, v, v + 1);
		if (v == v1)
			return 1;
	}
	return 0;
}
with get_write_access(inode) becoming
	return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBUSY;

and similar for atomic_dec_unless_positive()/deny_write_access()?

BTW, atomic_add_unless/atomic_inc_not_zero is done as read/cmpxchg on everything I've checked, including alpha and sparc. I suspect that it's seriously suboptimal there...

author maintainer
#10 lore raw
Quoted 3 lines · Al Viro

BTW, atomic_add_unless/atomic_inc_not_zero is done as read/cmpxchg on everything I've checked, including alpha and sparc. I suspect that it's seriously suboptimal there...

Actually, on sparc it's probably fine. alpha, OTOH, probably ought to switch to something similar to what ppc is doing...

#11 lore raw
In reply to Al Viro
Quoted 2 lines · Al Viro

I'm seriously tempted to throw away i_lock uses in
{get,deny}_write_access(), as in the patch below. The question is, how

Are there any known workload where the spinlock contends badly here? Or what's the motivation for it?

Thanks,

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only