Re: [PATCH] VFS: make file->f_pos access atomic on 32bit arch

Previous thread: Re: motherboard recommendations? by Jacek Poplawski on Tuesday, September 16, 2008 - 7:39 am. (2 messages)

Next thread: Re: [PATCH] VFS: make file->f_pos access atomic on 32bit arch by Bodo Eggert on Tuesday, September 16, 2008 - 8:32 am. (1 message)
From: Michael Trimarchi
Date: Tuesday, September 16, 2008 - 7:57 am

Hi,

Cc: Hisashi Hifumi <hifumi.hisashi@oss.ntt.co.jp>;
akpm@linux-foundation.org; linux-kernel@vger.kernel.org;

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 
--

From: Nick Piggin
Date: Tuesday, September 16, 2008 - 6:24 pm

I understand and agree.
--

From: Hisashi Hifumi
Date: Thursday, September 18, 2008 - 2:59 am

I modified my patch by introducing file_pos_update().

Comments?

Signed-off-by: Hisashi Hifumi <hifumi.hisashi@oss.ntt.co.jp>

diff -Nrup linux-2.6.27-rc6.org/fs/block_dev.c linux-2.6.27-rc6.fpos/fs/block_dev.c
--- linux-2.6.27-rc6.org/fs/block_dev.c	2008-09-10 10:30:08.000000000 +0900
+++ linux-2.6.27-rc6.fpos/fs/block_dev.c	2008-09-18 18:24:42.000000000 +0900
@@ -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);
diff -Nrup linux-2.6.27-rc6.org/fs/file_table.c linux-2.6.27-rc6.fpos/fs/file_table.c
--- linux-2.6.27-rc6.org/fs/file_table.c	2008-09-10 10:30:08.000000000 +0900
+++ linux-2.6.27-rc6.fpos/fs/file_table.c	2008-09-16 14:18:29.000000000 +0900
@@ -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;
diff -Nrup linux-2.6.27-rc6.org/fs/locks.c linux-2.6.27-rc6.fpos/fs/locks.c
--- linux-2.6.27-rc6.org/fs/locks.c	2008-09-10 10:30:08.000000000 +0900
+++ linux-2.6.27-rc6.fpos/fs/locks.c	2008-09-16 18:35:14.000000000 +0900
@@ -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);
diff -Nrup ...
From: Hisashi Hifumi
Date: Thursday, September 18, 2008 - 12:08 am

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.

--

Previous thread: Re: motherboard recommendations? by Jacek Poplawski on Tuesday, September 16, 2008 - 7:39 am. (2 messages)

Next thread: Re: [PATCH] VFS: make file->f_pos access atomic on 32bit arch by Bodo Eggert on Tuesday, September 16, 2008 - 8:32 am. (1 message)