Skip to contentSkip to conversation

xfs: reduce lock traffic on incore sb lock

PATCH In review Dave Chinner 2 participants 5 messages Sep 29, 2010 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: reduce lock traffic on incore sb lock

From: Dave Chinner <[email protected]>

Under heavy parallel unlink workloads, the incore superblock lock is heavily trafficed in xfs_mod_incore_sb_batch(). This is despite the fact that the counters being modified are typically the counters that are per-cpu and do not require the lock. IOWs, we are locking and unlocking the superblock lock needlessly, and the result is that it is third most heavily contended lock in the system under these workloads.

Fix this by only locking the superblock lock when we are modifying a counter protected by it. This completely removes the m_sb_lock from lock_stat traces during create/remove workloads.

---
 fs/xfs/xfs_mount.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)
fs/xfs/xfs_mount.c +24 -9
index 396d324..adc4ab9 100644
@@ -1883,21 +1883,23 @@ xfs_mod_incore_sb(
  * Either all of the specified deltas will be applied or none of
  * them will.  If any modified field dips below 0, then all modifications
  * will be backed out and EINVAL will be returned.
+ *
+ * The @m_sb_lock is be taken and dropped on demand according to the type of
+ * counter being modified to minimise lock traffic as this can be a very hot
+ * lock.
  */
 int
 xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
 {
 	int		status=0;
 	xfs_mod_sb_t	*msbp;
+	int		locked = 0;
 
 	/*
 	 * Loop through the array of mod structures and apply each
 	 * individually.  If any fail, then back out all those
-	 * which have already been applied.  Do all of this within
-	 * the scope of the m_sb_lock so that all of the changes will
-	 * be atomic.
+	 * which have already been applied.
 	 */
-	spin_lock(&mp->m_sb_lock);
 	msbp = &msb[0];
 	for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
 		/*
@@ -1911,16 +1913,22 @@ xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
 		case XFS_SBS_IFREE:
 		case XFS_SBS_FDBLOCKS:
 			if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
-				spin_unlock(&mp->m_sb_lock);
+				if (locked) {
+					locked = 0;
+					spin_unlock(&mp->m_sb_lock);
+				}
 				status = xfs_icsb_modify_counters(mp,
 							msbp->msb_field,
 							msbp->msb_delta, rsvd);
-				spin_lock(&mp->m_sb_lock);
 				break;
 			}
 			/* FALLTHROUGH */
 #endif
 		default:
+			if (!locked) {
+				spin_lock(&mp->m_sb_lock);
+				locked = 1;
+			}
 			status = xfs_mod_incore_sb_unlocked(mp,
 						msbp->msb_field,
 						msbp->msb_delta, rsvd);
@@ -1949,17 +1957,23 @@ xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
 			case XFS_SBS_IFREE:
 			case XFS_SBS_FDBLOCKS:
 				if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
-					spin_unlock(&mp->m_sb_lock);
+					if (locked) {
+						locked = 0;
+						spin_unlock(&mp->m_sb_lock);
+					}
 					status = xfs_icsb_modify_counters(mp,
 							msbp->msb_field,
 							-(msbp->msb_delta),
 							rsvd);
-					spin_lock(&mp->m_sb_lock);
 					break;
 				}
 				/* FALLTHROUGH */
 #endif
 			default:
+				if (!locked) {
+					spin_lock(&mp->m_sb_lock);
+					locked = 1;
+				}
 				status = xfs_mod_incore_sb_unlocked(mp,
 							msbp->msb_field,
 							-(msbp->msb_delta),
@@ -1970,7 +1984,8 @@ xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
 			msbp--;
 		}
 	}
-	spin_unlock(&mp->m_sb_lock);
+	if (locked)
+		spin_unlock(&mp->m_sb_lock);
 	return status;
 }
 
Signature · 6 lines
1.7.1

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 10 lines · Dave Chinner

From: Dave Chinner <[email protected]>

Under heavy parallel unlink workloads, the incore superblock lock is heavily trafficed in xfs_mod_incore_sb_batch(). This is despite the fact that the counters being modified are typically the counters that are per-cpu and do not require the lock. IOWs, we are locking and unlocking the superblock lock needlessly, and the result is that it is third most heavily contended lock in the system under these workloads.

Quoted 3 lines

Fix this by only locking the superblock lock when we are modifying a counter protected by it. This completely removes the m_sb_lock from lock_stat traces during create/remove workloads.

God spot of the idiocy there, but I really don't like the patch. I've started writing a small patches series solving the issue slightly better by cleaning up this area a bit.

After this we will never use xfs_mod_incore_sb/xfs_mod_incore_sb_batch for the percpu counters but rather make those always go through xfs_icsb_modify_counters. I'll need to quickly finish it up and will send it out soon.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Dave Chinner

From: Dave Chinner <[email protected]>

Under heavy parallel unlink workloads, the incore superblock lock is heavily trafficed in xfs_mod_incore_sb_batch(). This is despite the fact that the counters being modified are typically the counters that are per-cpu and do not require the lock. IOWs, we are locking and unlocking the superblock lock needlessly, and the result is that it is third most heavily contended lock in the system under these workloads.

Quoted 5 lines
Quoted 3 lines

Fix this by only locking the superblock lock when we are modifying a counter protected by it. This completely removes the m_sb_lock from lock_stat traces during create/remove workloads.

God spot of the idiocy there, but I really don't like the patch.

Fair enough - it is a rather quick hack. ;)

Quoted 2 lines

I've started writing a small patches series solving the issue slightly better by cleaning up this area a bit.

Oh, cool. That code is quite a tangle.

Quoted 4 lines

After this we will never use xfs_mod_incore_sb/xfs_mod_incore_sb_batch for the percpu counters but rather make those always go through xfs_icsb_modify_counters. I'll need to quickly finish it up and will send it out soon.

FWIW, I've got a prototype that converts the per-cpu counters to the generic per-cpu counter infrastructure. It chops out almost all the xfs_icsb_* stuff (including xfs_icsb_modify_counters()) and has a diffstat of:

6 files changed, 317 insertions(+), 709 deletions(-)

It needs a significant cleanup of xfs_mod_incore_sb() before/after the conversion which I haven't done yet because I haven't quite got my new percpu_counter_test_and_add_delta() function working correctly yet. I spotted this locking problem when testing the patch...

That said, there's no reason why my percpu counter code needs to run through xfs_mod_incore_sb() at all. If we have a separate path for per-cpu counters then I can rework my code on top of that....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#3 lore raw
Quoted 4 lines · Dave Chinner

FWIW, I've got a prototype that converts the per-cpu counters to the generic per-cpu counter infrastructure. It chops out almost all the xfs_icsb_* stuff (including xfs_icsb_modify_counters()) and has a diffstat of:

Sounds good - I always throught of the balanced per-cpu counters as infrastructure that really shouldn't sit inside XFS.

Quoted 9 lines

It needs a significant cleanup of xfs_mod_incore_sb() before/after the conversion which I haven't done yet because I haven't quite got my new percpu_counter_test_and_add_delta() function working correctly yet. I spotted this locking problem when testing the patch...

That said, there's no reason why my percpu counter code needs to run through xfs_mod_incore_sb() at all. If we have a separate path for per-cpu counters then I can rework my code on top of that....

We'll always need a low-level function to to the actual superblock updates and a high-level one modifying the per-cpu counters. I don't think the exact naming matters too much.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#4 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Dave Chinner

FWIW, I've got a prototype that converts the per-cpu counters to the generic per-cpu counter infrastructure. It chops out almost all the xfs_icsb_* stuff (including xfs_icsb_modify_counters()) and has a diffstat of:

Sounds good - I always throught of the balanced per-cpu counters as infrastructure that really shouldn't sit inside XFS.

The only reason I implemented them like that in the first place was that there was no generic per-cpu counter infrastructure in 2.6.15... ;)

Quoted 6 lines
Quoted 6 lines

It needs a significant cleanup of xfs_mod_incore_sb() before/after the conversion which I haven't done yet because I haven't quite got my new percpu_counter_test_and_add_delta() function working correctly yet. I spotted this locking problem when testing the patch...

Quoted 7 lines
Quoted 3 lines

That said, there's no reason why my percpu counter code needs to run through xfs_mod_incore_sb() at all. If we have a separate path for per-cpu counters then I can rework my code on top of that....

We'll always need a low-level function to to the actual superblock updates and a high-level one modifying the per-cpu counters. I don't think the exact naming matters too much.

Agreed. I think it's probably best to wait for your cleanup patches before reworking the counter implementation completely, though.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs