Skip to contentSkip to conversation

VFS: make file->f_pos access atomic on 32bit arch

PATCH Michael Trimarchi 3 participants 4 messages Sep 16, 2008 – Sep 18, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

----- Messaggio originale -----

Quoted 3 lines

Da: Nick Piggin <[email protected]>
A: Michael Trimarchi <[email protected]>

Cc: Hisashi Hifumi <[email protected]>; [email protected]; [email protected]; [email protected]

Quoted 3 lines

Inviato: Martedì 16 settembre 2008, 16:11:21
Oggetto: Re: [PATCH] VFS: make file->f_pos access atomic on 32bit arch

...

Quoted 7 lines
Quoted 3 lines

i was thinking about introducing a file_pos_update() not implemented using
file_pos_read()/file_pos_write() and taking the seq_lock or disabling preemption only one time

AFAIK, the only way to do an atomic 64 bit store on 32-bit x86 is "cmpxchg8b" (with lock prefix on SMP). That would be far slower I'm sure.

Maybe it is clear with an example:

+/*
+ * file_pos_read/write is atomic by using sequence lock on 32bit arch.
+ */
+static inline loff_t file_pos_read(struct file *file)
+{
+#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
+ loff_t pos;
+ unsigned int seq;
+
+ do {
+ seq = read_seqbegin(&file->f_pos_seqlock);
+ pos = file->f_pos;
+ } while (read_seqretry(&file->f_pos_seqlock, seq));
+ return pos;
+#elif BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT)
+ loff_t pos;
+
+ preempt_disable();
+ pos = file->f_pos;
+ preempt_enable();
+ return pos;
+#else
+ return file->f_pos;
+#endif
+}
+
+static inline void file_pos_write(struct file *file, loff_t pos) +{
+#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
+ write_seqlock(&file->f_pos_seqlock);
+ file->f_pos = pos;
+ write_sequnlock(&file->f_pos_seqlock);
+#elif BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT)
+ preempt_disable();
+ file->f_pos = pos;
+ preempt_enable();
+#else
+ file->f_pos = pos;
+#endif
+}
+
if (offset != file_pos_read(file))

                 file_pos_write(file, offset);

Compile with:
BITS_PER_LONG=32
CONFIG_PREEMPT

The code does:

file_pos_read(...)
preempt_disable
....
preempt_enable

file_pos_write(...)
preempt_disable
....
preempt_enable

with the file_pos_update() the code does:

preempt_disable
...
...
preempt_enable

Regards Michael

__________________________________________________
Do You Yahoo!?
Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi
http://mail.yahoo.it
--
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

#1 lore raw
Quoted 20 lines · Michael Trimarchi

Hi,

----- Messaggio originale -----

Quoted 2 lines

Da: Nick Piggin <[email protected]>
A: Michael Trimarchi <[email protected]>

Cc: Hisashi Hifumi <[email protected]>; [email protected]; [email protected]; [email protected]

Quoted 2 lines

Inviato: Martedì 16 settembre 2008, 16:11:21
Oggetto: Re: [PATCH] VFS: make file->f_pos access atomic on 32bit arch

...

Quoted 4 lines
Quoted 3 lines

i was thinking about introducing a file_pos_update() not implemented
using file_pos_read()/file_pos_write() and taking the seq_lock or disabling preemption only one time

Quoted 5 lines
Quoted 3 lines

AFAIK, the only way to do an atomic 64 bit store on 32-bit x86 is "cmpxchg8b" (with lock prefix on SMP). That would be far slower I'm sure.

Maybe it is clear with an example:

I understand and agree.
--
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

#2 lore raw
Quoted 11 lines · Nick Piggin
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

i was thinking about introducing a file_pos_update() not implemented
using file_pos_read()/file_pos_write() and taking the seq_lock or disabling preemption only one time

AFAIK, the only way to do an atomic 64 bit store on 32-bit x86 is "cmpxchg8b" (with lock prefix on SMP). That would be far slower I'm sure.

Maybe it is clear with an example:

I understand and agree.

I modified my patch by introducing file_pos_update().

Comments?

fs/block_dev.c +2 -4
diff -Nrup linux-2.6.27-rc6.org/fs/block_dev.c linux-2.6.27-rc6.fpos/fs/block_dev.c
@@ -225,13 +225,11 @@ static loff_t block_llseek(struct file *
 			offset += size;
 			break;
 		case 1:
-			offset += file->f_pos;
+			offset += file_pos_read(file);
 	}
 	retval = -EINVAL;
 	if (offset >= 0 && offset <= size) {
-		if (offset != file->f_pos) {
-			file->f_pos = offset;
-		}
+		file_pos_update(file, offset);
 		retval = offset;
 	}
 	mutex_unlock(&bd_inode->i_mutex);
fs/file_table.c +1 -0
diff -Nrup linux-2.6.27-rc6.org/fs/file_table.c linux-2.6.27-rc6.fpos/fs/file_table.c
@@ -121,6 +121,7 @@ struct file *get_empty_filp(void)
 	tsk = current;
 	INIT_LIST_HEAD(&f->f_u.fu_list);
 	atomic_long_set(&f->f_count, 1);
+	f_pos_ordered_init(f);
 	rwlock_init(&f->f_owner.lock);
 	f->f_uid = tsk->fsuid;
 	f->f_gid = tsk->fsgid;
fs/locks.c +2 -2
diff -Nrup linux-2.6.27-rc6.org/fs/locks.c linux-2.6.27-rc6.fpos/fs/locks.c
@@ -317,7 +317,7 @@ static int flock_to_posix_lock(struct fi
 		start = 0;
 		break;
 	case SEEK_CUR:
-		start = filp->f_pos;
+		start = file_pos_read(filp);
 		break;
 	case SEEK_END:
 		start = i_size_read(filp->f_path.dentry->d_inode);
@@ -367,7 +367,7 @@ static int flock64_to_posix_lock(struct 
 		start = 0;
 		break;
 	case SEEK_CUR:
-		start = filp->f_pos;
+		start = file_pos_read(filp);
 		break;
 	case SEEK_END:
 		start = i_size_read(filp->f_path.dentry->d_inode);
fs/read_write.c +4 -18
diff -Nrup linux-2.6.27-rc6.org/fs/read_write.c linux-2.6.27-rc6.fpos/fs/read_write.c
@@ -42,15 +42,13 @@ generic_file_llseek_unlocked(struct file
 			offset += inode->i_size;
 			break;
 		case SEEK_CUR:
-			offset += file->f_pos;
+			offset += file_pos_read(file);
 	}
 	retval = -EINVAL;
 	if (offset>=0 && offset<=inode->i_sb->s_maxbytes) {
 		/* Special lock needed here? */
-		if (offset != file->f_pos) {
-			file->f_pos = offset;
+		if (file_pos_update(file, offset))
 			file->f_version = 0;
-		}
 		retval = offset;
 	}
 	return retval;
@@ -83,14 +81,12 @@ loff_t default_llseek(struct file *file,
 			offset += i_size_read(file->f_path.dentry->d_inode);
 			break;
 		case SEEK_CUR:
-			offset += file->f_pos;
+			offset += file_pos_read(file);
 	}
 	retval = -EINVAL;
 	if (offset >= 0) {
-		if (offset != file->f_pos) {
-			file->f_pos = offset;
+		if (file_pos_update(file, offset))
 			file->f_version = 0;
-		}
 		retval = offset;
 	}
 	unlock_kernel();
@@ -324,16 +320,6 @@ ssize_t vfs_write(struct file *file, con
 
 EXPORT_SYMBOL(vfs_write);
 
-static inline loff_t file_pos_read(struct file *file)
-{
-	return file->f_pos;
-}
-
-static inline void file_pos_write(struct file *file, loff_t pos)
-{
-	file->f_pos = pos;
-}
-
 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
 {
 	struct file *file;
include/linux/fs.h +80 -0

98 lines not on this page · raw message

#3 lore raw
In reply to Michael Trimarchi
Quoted 63 lines

Maybe it is clear with an example:

(quoted diff) +41 -0
+/*
+ * file_pos_read/write is atomic by using sequence lock on 32bit arch.
+ */
+static inline loff_t file_pos_read(struct file *file)
+{
+#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
+	loff_t pos;
+	unsigned int seq;
+
+	do {
+		seq = read_seqbegin(&file->f_pos_seqlock);
+		pos = file->f_pos;
+	} while (read_seqretry(&file->f_pos_seqlock, seq));
+	return pos;
+#elif BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT)
+	loff_t pos;
+
+	preempt_disable();
+	pos = file->f_pos;
+	preempt_enable();
+	return pos;
+#else
+	return file->f_pos;
+#endif
+}
+
+static inline void file_pos_write(struct file *file, loff_t pos)
+{
+#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
+	write_seqlock(&file->f_pos_seqlock);
+	file->f_pos = pos;
+	write_sequnlock(&file->f_pos_seqlock);
+#elif BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT)
+	preempt_disable();
+	file->f_pos = pos;
+	preempt_enable();
+#else
+	file->f_pos = pos;
+#endif
+}
+

if (offset != file_pos_read(file))

                file_pos_write(file, offset);

Compile with:
BITS_PER_LONG=32
CONFIG_PREEMPT

The code does:

file_pos_read(...)
preempt_disable
....
preempt_enable

file_pos_write(...)
preempt_disable
....
preempt_enable

Quoted 9 lines

with the file_pos_update() the code does:

preempt_disable
...
...
preempt_enable

Regards Michael

I think file_pos_update() with BITS_PER_LONG=32 && CONFIG_PREEMPT is easy, this is like this.

static inline void file_pos_update(struct file *file, loff_t pos)

{
	preempt_disable();
	if (pos != file->f_pos)
		file->f_pos = pos;
	preempt_enable();
}

But I think BITS_PER_LONG=32 && CONFIG_SMP version has a problem, this is like this.

static inline void file_pos_update(struct file *file, loff_t pos)

{
	write_seqlock(&file->f_pos_seqlock);
	if (pos != file->f_pos)
		file->f_pos = pos;
	write_sequnlock(&file->f_pos_seqlock);
}

file_pos_update acquires seqlock only once, but write_seqlock is called whether file->f_pos is overwritten or not.