Skip to contentSkip to conversation

Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

PATCH New Tristan Ye 3 participants 6 messages Nov 19, 2010 – Dec 2, 2010 OCFS2

lore
Share & tools
lore

Tristan Ye

author
#0 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Former logic of ocfs2_file_aio_write() was a bit stricky to unlock the rw_lock and i_alloc_sem, by using some private bits in struct 'iocb' to communite with ocfs2_dio_end_io(), it did work before we introduce the patch of supporting 'coherency=full,buffered' option, since rw_lock and i_alloc_sem were never acquired both at the same time, no mattar we doing buffered or direct IO or not.

This patch tries to teach ocfs2_dio_end_io fully understand the bahavior of all writes, including buffered/concurrency-allowed-odirect/none-concurrency-odirect writes, to have all lock/sem primitives getting correctly released.

---
 fs/ocfs2/aops.c |    9 +++++++--
 fs/ocfs2/aops.h |    6 ++++++
 fs/ocfs2/file.c |   16 ++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
fs/ocfs2/aops.c +7 -2
index f1e962c..fd0713c 100644
@@ -568,7 +568,7 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
 			     bool is_async)
 {
 	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
-	int level;
+	int level, coherency;
 
 	/* this io's submitter should not have unlocked this before we could */
 	BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
@@ -576,7 +576,12 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
 	ocfs2_iocb_clear_rw_locked(iocb);
 
 	level = ocfs2_iocb_rw_locked_level(iocb);
-	if (!level)
+	/*
+	 * 'coherency=full' O_DIRECT writes needs this extra bit
+	 * to correctly up_read the i_alloc_sem.
+	 */
+	coherency = ocfs2_iocb_coherency(iocb);
+	if ((!level) || coherency)
 		up_read(&inode->i_alloc_sem);
 	ocfs2_rw_unlock(inode, level);
 
fs/ocfs2/aops.h +6 -0
index 76bfdfd..213cec6 100644
@@ -72,4 +72,10 @@ static inline void ocfs2_iocb_set_rw_locked(struct kiocb *iocb, int level)
 	clear_bit(0, (unsigned long *)&iocb->private)
 #define ocfs2_iocb_rw_locked_level(iocb) \
 	test_bit(1, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_set_coherency(iocb) \
+	set_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_clear_coherency(iocb) \
+	clear_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_coherency(iocb) \
+	test_bit(2, (unsigned long *)&iocb->private)
 #endif /* OCFS2_FILE_H */
fs/ocfs2/file.c +16 -0
index 77b4c04..df070a3 100644
@@ -2277,8 +2277,24 @@ relock:
 		}
 
 		ocfs2_inode_unlock(inode, 1);
+
+		/*
+		 * Due to the fault of 'full_coherency' O_DIRECT
+		 * write needs to acqure both i_alloc_sem and rw_lock.
+		 * We do another trick here to have coherency bit
+		 * stored in iocb to communicate with ocfs2_dio_end_io
+		 * for properly unlocking i_alloc_sem.
+		 */
+		ocfs2_iocb_set_coherency(iocb);
 	}
 
+	/*
+	 * Concurrent-allowed odirect writes was able to up_read i_alloc_sem
+	 * correctly, we therefore don't need this extra and tricky bit.
+	 */
+	if (direct_io && !full_coherency)
+		ocfs2_iocb_clear_coherency(iocb);
+
 	can_do_direct = direct_io;
 	ret = ocfs2_prepare_inode_for_write(file, ppos,
 					    iocb->ki_left, appending,
Signature · 1 line
1.5.5
#1 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Hi Tristan,
Just add joel to the cc in case he has a different option.

Quoted 5 lines · Tristan Ye

Former logic of ocfs2_file_aio_write() was a bit stricky to unlock the rw_lock and i_alloc_sem, by using some private bits in struct 'iocb' to communite with ocfs2_dio_end_io(), it did work before we introduce the patch of supporting 'coherency=full,buffered' option, since rw_lock and i_alloc_sem were never acquired both at the same time, no mattar we doing buffered or direct IO or not.

These 2 locks can be acquired at the same time.
So if we go with direct_io, we do have i_alloc_sem and rw_lock locked simultaneously. why do you get this?

I have gone through your patch and the bug. It sees to me that the real cause for the bug is that you have EX rw_lock because of full_coherency while locking i_alloc_sem. So finally in ocfs2_dio_end_io, only rw_lock is freed and i_alloc_sem is left, right? If yes, please update the above commit log for it.

I don't like your solution either. full_coherency is only used in direct write and ocfs2_dio_end_io is used for both direct read/write. So why add the complexity of coherency to ocfs2_dio_end_io? Also you long comment in ocfs2_file_aio_write does indicate that it is really hard for the code reader to learn why we need to set this flag.

My suggestion is: why not use another flag to indicate the state of i_alloc_sem instead of full_coherency? So in place we down_read the i_alloc_sem, set the flag accordingly, and in ocfs2_dio_end_io, just check this flag instead of !rw_locked_level to up_read it. It should be more straightforward. Agree?

Joel, any comments?

Regards,
Tao

Quoted 83 lines

This patch tries to teach ocfs2_dio_end_io fully understand the bahavior of all writes, including buffered/concurrency-allowed-odirect/none-concurrency-odirect writes, to have all lock/sem primitives getting correctly released.

---
  fs/ocfs2/aops.c |    9 +++++++--
  fs/ocfs2/aops.h |    6 ++++++
  fs/ocfs2/file.c |   16 ++++++++++++++++
  3 files changed, 29 insertions(+), 2 deletions(-)
fs/ocfs2/aops.c +7 -2
index f1e962c..fd0713c 100644
@@ -568,7 +568,7 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
  			     bool is_async)
  {
  	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
-	int level;
+	int level, coherency;
 
  	/* this io's submitter should not have unlocked this before we could */
  	BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
@@ -576,7 +576,12 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
  	ocfs2_iocb_clear_rw_locked(iocb);
 
  	level = ocfs2_iocb_rw_locked_level(iocb);
-	if (!level)
+	/*
+	 * 'coherency=full' O_DIRECT writes needs this extra bit
+	 * to correctly up_read the i_alloc_sem.
+	 */
+	coherency = ocfs2_iocb_coherency(iocb);
+	if ((!level) || coherency)
  		up_read(&inode->i_alloc_sem);
  	ocfs2_rw_unlock(inode, level);
 
fs/ocfs2/aops.h +6 -0
index 76bfdfd..213cec6 100644
@@ -72,4 +72,10 @@ static inline void ocfs2_iocb_set_rw_locked(struct kiocb *iocb, int level)
  	clear_bit(0, (unsigned long *)&iocb->private)
  #define ocfs2_iocb_rw_locked_level(iocb) \
  	test_bit(1, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_set_coherency(iocb) \
+	set_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_clear_coherency(iocb) \
+	clear_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_coherency(iocb) \
+	test_bit(2, (unsigned long *)&iocb->private)
  #endif /* OCFS2_FILE_H */
fs/ocfs2/file.c +16 -0
index 77b4c04..df070a3 100644
@@ -2277,8 +2277,24 @@ relock:
  		}
 
  		ocfs2_inode_unlock(inode, 1);
+
+		/*
+		 * Due to the fault of 'full_coherency' O_DIRECT
+		 * write needs to acqure both i_alloc_sem and rw_lock.
+		 * We do another trick here to have coherency bit
+		 * stored in iocb to communicate with ocfs2_dio_end_io
+		 * for properly unlocking i_alloc_sem.
+		 */
+		ocfs2_iocb_set_coherency(iocb);
  	}
 
+	/*
+	 * Concurrent-allowed odirect writes was able to up_read i_alloc_sem
+	 * correctly, we therefore don't need this extra and tricky bit.
+	 */
+	if (direct_io&&  !full_coherency)
+		ocfs2_iocb_clear_coherency(iocb);
+
  	can_do_direct = direct_io;
  	ret = ocfs2_prepare_inode_for_write(file, ppos,
  					    iocb->ki_left, appending,
author
#2 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Quoted 14 lines · Tao Ma

Hi Tristan,
Just add joel to the cc in case he has a different option.

Quoted 10 lines · Tristan Ye

Former logic of ocfs2_file_aio_write() was a bit stricky to unlock the rw_lock
and i_alloc_sem, by using some private bits in struct 'iocb' to communite with
ocfs2_dio_end_io(), it did work before we introduce the patch of supporting
'coherency=full,buffered' option, since rw_lock and i_alloc_sem were never
acquired both at the same time, no mattar we doing buffered or direct IO or not.

Quoted 3 lines

These 2 locks can be acquired at the same time.
So if we go with direct_io, we do have i_alloc_sem and rw_lock locked simultaneously. why do you get this?

For coherency_full direct_io, we have these 2 locks, while for coherency_buffered direct_io, we only acquire i_alloc_sem.

Quoted 6 lines

I have gone through your patch and the bug. It sees to me that the real cause for the bug is that you have EX rw_lock because of full_coherency while locking i_alloc_sem. So finally in ocfs2_dio_end_io, only rw_lock is freed and i_alloc_sem is left, right? If yes, please update the above commit log for it.

Didn't quite get you here, there is no lock was blocking i_alloc_sem, instead, i_alloc_sem was not up_read() correctly and explicitly somewhere.

Quoted 7 lines

I don't like your solution either. full_coherency is only used in direct write and ocfs2_dio_end_io is used for both direct read/write. So why add the complexity of coherency to ocfs2_dio_end_io? Also you long comment in ocfs2_file_aio_write does indicate that it is really hard for the code reader to learn why we need to set this flag.

The complexity is just introduce by the nature that 'coherency_full' and 'coherency_buffered' direct_io writes is gonna have different locks, as you known, we only have one mode for direct_io writes before.

Quoted 6 lines

My suggestion is: why not use another flag to indicate the state of i_alloc_sem instead of full_coherency? So in place we down_read the i_alloc_sem, set the flag accordingly, and in ocfs2_dio_end_io, just check this flag instead of !rw_locked_level to up_read it. It should be more straightforward. Agree?

Yep, I do agree that this fix looks tricky a bit, while the all existed ocfs2_dio_end_io() things were already tricky there;)

Using 'ocfs2_iocb_set_sem_locked' or 'ocfs2_iocb_set_coherency' didn't simplify the logic quite a bit, however, I still appreciated your suggestion to follow existing convention, such as ocfs2_iocb_set_rw_locked' things, they just look more similar and in a series;-)

Tristan

Quoted 95 lines

Joel, any comments?

Regards,
Tao

Quoted 88 lines

This patch tries to teach ocfs2_dio_end_io fully understand the bahavior of
all writes, including buffered/concurrency-allowed-odirect/none-concurrency-odirect writes, to have all lock/sem primitives getting correctly released.

---
  fs/ocfs2/aops.c |    9 +++++++--
  fs/ocfs2/aops.h |    6 ++++++
  fs/ocfs2/file.c |   16 ++++++++++++++++
  3 files changed, 29 insertions(+), 2 deletions(-)
fs/ocfs2/aops.c +1 -1
index f1e962c..fd0713c 100644
@@ -568,7 +568,7 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
                   bool is_async)
  {
      struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
-    int level;
+    int level, coherency;
 
      /* this io's submitter should not have unlocked this before we 

could */

      BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
fs/ocfs2/aops.c +6 -1
@@ -576,7 +576,12 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
      ocfs2_iocb_clear_rw_locked(iocb);
 
      level = ocfs2_iocb_rw_locked_level(iocb);
-    if (!level)
+    /*
+     * 'coherency=full' O_DIRECT writes needs this extra bit
+     * to correctly up_read the i_alloc_sem.
+     */
+    coherency = ocfs2_iocb_coherency(iocb);
+    if ((!level) || coherency)
          up_read(&inode->i_alloc_sem);
      ocfs2_rw_unlock(inode, level);
 
fs/ocfs2/aops.h +6 -0
index 76bfdfd..213cec6 100644
@@ -72,4 +72,10 @@ static inline void ocfs2_iocb_set_rw_locked(struct 
 kiocb *iocb, int level)
      clear_bit(0, (unsigned long *)&iocb->private)
  #define ocfs2_iocb_rw_locked_level(iocb) \
      test_bit(1, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_set_coherency(iocb) \
+    set_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_clear_coherency(iocb) \
+    clear_bit(2, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_coherency(iocb) \
+    test_bit(2, (unsigned long *)&iocb->private)
  #endif /* OCFS2_FILE_H */
fs/ocfs2/file.c +11 -0
index 77b4c04..df070a3 100644
@@ -2277,8 +2277,24 @@ relock:
          }
 
          ocfs2_inode_unlock(inode, 1);
+
+        /*
+         * Due to the fault of 'full_coherency' O_DIRECT
+         * write needs to acqure both i_alloc_sem and rw_lock.
+         * We do another trick here to have coherency bit
+         * stored in iocb to communicate with ocfs2_dio_end_io
+         * for properly unlocking i_alloc_sem.
+         */
+        ocfs2_iocb_set_coherency(iocb);
      }
 
+    /*
+     * Concurrent-allowed odirect writes was able to up_read 

i_alloc_sem

fs/ocfs2/file.c +5 -0
+     * correctly, we therefore don't need this extra and tricky bit.
+     */
+    if (direct_io&&  !full_coherency)
+        ocfs2_iocb_clear_coherency(iocb);
+
      can_do_direct = direct_io;
      ret = ocfs2_prepare_inode_for_write(file, ppos,
                          iocb->ki_left, appending,
#3 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Hi Tristan,

Quoted 15 lines · Tristan Ye
Quoted 14 lines · Tao Ma

Hi Tristan,
Just add joel to the cc in case he has a different option.

Quoted 10 lines · Tristan Ye

Former logic of ocfs2_file_aio_write() was a bit stricky to unlock the rw_lock
and i_alloc_sem, by using some private bits in struct 'iocb' to communite with
ocfs2_dio_end_io(), it did work before we introduce the patch of supporting
'coherency=full,buffered' option, since rw_lock and i_alloc_sem were never
acquired both at the same time, no mattar we doing buffered or direct IO or not.

Quoted 6 lines
Quoted 3 lines

These 2 locks can be acquired at the same time.
So if we go with direct_io, we do have i_alloc_sem and rw_lock locked simultaneously. why do you get this?

For coherency_full direct_io, we have these 2 locks, while for coherency_buffered direct_io, we only acquire i_alloc_sem.

How do you get this? We always get rw_lock?

Quoted 10 lines
Quoted 7 lines

I have gone through your patch and the bug. It sees to me that the real cause for the bug is that you have EX rw_lock because of full_coherency while locking i_alloc_sem. So finally in ocfs2_dio_end_io, only rw_lock is freed and i_alloc_sem is left, right? If yes, please update the above commit log for it.

Didn't quite get you here, there is no lock was blocking i_alloc_sem, instead, i_alloc_sem was not up_read() correctly and explicitly somewhere.

yes, it isn't freed because of the rw_level, right? In your case, rw_level is 1, so in ocfs2_dio_end_io, i_alloc_sem isn't freed, right?

Quoted 14 lines
Quoted 8 lines

I don't like your solution either. full_coherency is only used in direct write and ocfs2_dio_end_io is used for both direct read/write. So why add the complexity of coherency to ocfs2_dio_end_io? Also you long comment in ocfs2_file_aio_write does indicate that it is really hard for the code reader to learn why we need to set this flag.

The complexity is just introduce by the nature that 'coherency_full' and 'coherency_buffered' direct_io writes is gonna have different locks, as you known, we only have one mode for direct_io writes before.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

My suggestion is: why not use another flag to indicate the state of i_alloc_sem instead of full_coherency? So in place we down_read the i_alloc_sem, set the flag accordingly, and in ocfs2_dio_end_io, just check this flag instead of !rw_locked_level to up_read it. It should be more straightforward. Agree?

Yep, I do agree that this fix looks tricky a bit, while the all existed ocfs2_dio_end_io() things were already tricky there;)

No, ocfs2_dio_end_io isn't tricky. So in general, you lock something in ocfs2_file_aio_{read,write}, and then they are freed in ocfs2_dio_end_io. That's it. The only thing that you may think as tricky is that we use a rw_level to indicate whether we need to up_read i_alloc_sem. It worked in the old case, but doesn't work now.

Quoted 6 lines

Using 'ocfs2_iocb_set_sem_locked' or 'ocfs2_iocb_set_coherency' didn't simplify the logic quite a bit, however, I still appreciated your suggestion to follow existing convention, such as ocfs2_iocb_set_rw_locked' things, they just look more similar and in a series;-)

yes, it looks more natural and easy. So when you lock i_alloc_sem, just call ocfs2_iocb_set_sem_locked, and when you lock rw_lock, just set the ocfs2_iocb_set_rw_locked. That's it. You don't neet to think about some stuff like coherency or not.

Regards,
Tao

author
#4 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Quoted 18 lines · Tao Ma

Hi Tristan,

Quoted 15 lines · Tristan Ye
Quoted 14 lines · Tao Ma

Hi Tristan,
Just add joel to the cc in case he has a different option.

Quoted 10 lines · Tristan Ye

Former logic of ocfs2_file_aio_write() was a bit stricky to unlock the rw_lock
and i_alloc_sem, by using some private bits in struct 'iocb' to communite with
ocfs2_dio_end_io(), it did work before we introduce the patch of supporting
'coherency=full,buffered' option, since rw_lock and i_alloc_sem were never
acquired both at the same time, no mattar we doing buffered or direct IO or not.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

These 2 locks can be acquired at the same time.
So if we go with direct_io, we do have i_alloc_sem and rw_lock locked simultaneously. why do you get this?

For coherency_full direct_io, we have these 2 locks, while for coherency_buffered direct_io, we only acquire i_alloc_sem.

How do you get this? We always get rw_lock?

Yep, I may misunderstand you a bit;), I mean in 'coherency_buffered' case, we get PR rw_lock,
and with 'coherency_full' mode, it acquires EX rw_lock, so that's kind of 'we're not always getting
EX rw_lock things', anyway, it's not a big deal. we're basically reaching a consensus here;-)

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

I have gone through your patch and the bug. It sees to me that the real cause for the bug is that you have EX rw_lock because of full_coherency while locking i_alloc_sem. So finally in ocfs2_dio_end_io, only rw_lock is freed and i_alloc_sem is left, right? If yes, please update the above commit log for it.

Quoted 5 lines
Quoted 3 lines

Didn't quite get you here, there is no lock was blocking i_alloc_sem, instead, i_alloc_sem was not up_read() correctly and explicitly somewhere.

yes, it isn't freed because of the rw_level, right? In your case, rw_level is 1, so in ocfs2_dio_end_io, i_alloc_sem isn't freed, right?

Exactly.

Quoted 33 lines
Quoted 14 lines
Quoted 8 lines

I don't like your solution either. full_coherency is only used in direct write and ocfs2_dio_end_io is used for both direct read/write. So why add the complexity of coherency to ocfs2_dio_end_io? Also you long comment in ocfs2_file_aio_write does indicate that it is really hard for the code reader to learn why we need to set this flag.

The complexity is just introduce by the nature that 'coherency_full' and 'coherency_buffered' direct_io writes is gonna have different locks, as you known, we only have one mode for direct_io writes before.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

My suggestion is: why not use another flag to indicate the state of i_alloc_sem instead of full_coherency? So in place we down_read the i_alloc_sem, set the flag accordingly, and in ocfs2_dio_end_io, just check this flag instead of !rw_locked_level to up_read it. It should be more straightforward. Agree?

Yep, I do agree that this fix looks tricky a bit, while the all existed ocfs2_dio_end_io() things were already tricky there;)

No, ocfs2_dio_end_io isn't tricky. So in general, you lock something in ocfs2_file_aio_{read,write}, and then they are freed in ocfs2_dio_end_io. That's it. The only thing that you may think as tricky is that we use a rw_level to indicate whether we need to up_read i_alloc_sem. It worked in the old case, but doesn't work now.

Quoted 6 lines

Using 'ocfs2_iocb_set_sem_locked' or 'ocfs2_iocb_set_coherency' didn't simplify the logic quite a bit, however, I still appreciated your suggestion to follow existing convention, such as ocfs2_iocb_set_rw_locked' things, they just look more similar and in a series;-)

Quoted 4 lines

yes, it looks more natural and easy. So when you lock i_alloc_sem, just call ocfs2_iocb_set_sem_locked, and when you lock rw_lock, just set the ocfs2_iocb_set_rw_locked. That's it. You don't neet to think about some stuff like coherency or not.

Reasonable.

Quoted 3 lines

Regards,
Tao

Joel,

How do you think about it?

#5 lore raw

PATCH 1/1 Ocfs2: Teach 'coherency=full' O_DIRECT writes to correctly up_read i_alloc_sem.

Quoted 10 lines · Tristan Ye
Quoted 4 lines

yes, it looks more natural and easy. So when you lock i_alloc_sem, just call ocfs2_iocb_set_sem_locked, and when you lock rw_lock, just set the ocfs2_iocb_set_rw_locked. That's it. You don't neet to think about some stuff like coherency or not.

Reasonable.

Joel,

How do you think about it?

I agree with Tao. ocfs2_dio_end_io() doesn't care why the
locks are locked; it just needs to know which ones to release.

Joel

Signature · 9 lines
Life's Little Instruction Book #20

	"Be forgiving of yourself and others."

Joel Becker
Senior Development Manager
Oracle
E-mail: joel.becker at oracle.com
Phone: (650) 506-8127