Skip to contentSkip to conversation

gfs2: Move iomap_get before taking sd_quota_mutex

PATCH New Bob Peterson 2 participants 3 messages Feb 11, 2022 – May 17, 2022 GFS2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gfs2: Move iomap_get before taking sd_quota_mutex

Before this patch quota function bh_get called gfs2_iomap_get after it had locked the sd_quota_mutex. That's a problem because that holds the i_rw_mutex, and that lock order is different from other code that locks i_rw_mutex first, then the sd_quota_mutex:

punch_hole
sweep_bh_for_rgrps

      down_write(&ip->i_rw_mutex)
      ...
      __gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta);
   gfs2_quota_change
      do_qc
         lock(&sdp->sd_quota_mutex);

This patch changes the order of events to this:

1. If qd->qd_bh_count is zero (we're likely but not guaranteed to be the first qd holder) it calls gfs2_iomap_get to determine which block should be read.
2. Lock the sd_quota_mutex
3. If qd->qd_bh_count is still zero (now it's guaranteed) it reads in the necessary block.
4. If qd_bh_count is no longer zero (because it wasn't holding the mutex) the iomap_get was wasted time, so return.

The case in which it wastes time (step 4) should be very rare and only occur when sd_quota_mutex is contended. But this avoids the ABBA deadlock.

---
 fs/gfs2/quota.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
fs/gfs2/quota.c +27 -12
index 91bc3affe460..fe98b2a6c0b6 100644
@@ -368,9 +368,23 @@ static int bh_get(struct gfs2_quota_data *qd)
 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
 	unsigned int block, offset;
 	struct buffer_head *bh;
-	struct iomap iomap = { };
+	struct iomap iomap = { .addr = IOMAP_NULL_ADDR, };
 	int error;
 
+	block = qd->qd_slot / sdp->sd_qc_per_block;
+	offset = qd->qd_slot % sdp->sd_qc_per_block;
+
+retry:
+	if (!qd->qd_bh_count) {
+		error = gfs2_iomap_get(sdp->sd_qc_inode,
+				 (loff_t)block << sdp->sd_qc_inode->i_blkbits,
+				       sdp->sd_sb.sb_bsize, &iomap);
+		if (error)
+			return error;
+		if (iomap.addr == IOMAP_NULL_ADDR)
+			return -ENOENT;
+	}
+
 	mutex_lock(&sdp->sd_quota_mutex);
 
 	if (qd->qd_bh_count++) {
@@ -378,17 +392,18 @@ static int bh_get(struct gfs2_quota_data *qd)
 		return 0;
 	}
 
-	block = qd->qd_slot / sdp->sd_qc_per_block;
-	offset = qd->qd_slot % sdp->sd_qc_per_block;
-
-	error = gfs2_iomap_get(sdp->sd_qc_inode,
-			       (loff_t)block << sdp->sd_qc_inode->i_blkbits,
-			       sdp->sd_sb.sb_bsize, &iomap);
-	if (error)
-		goto fail;
-	if (iomap.addr == IOMAP_NULL_ADDR) {
-		error = -ENOENT;
-		goto fail;
+	/*
+	 * Make sure we called iomap_get. If qd_bh_count was non-zero at
+	 * the start of the function, but was changed to 0 (by someone else's
+	 * decrement) and we discover it after we acquired the mutex, we will
+	 * have skipped the call to gfs2_iomap_get() and therefore don't know
+	 * the block number we need to read. In that case we need to start
+	 * over to get the block number.
+	 */
+	if (unlikely(iomap.addr == IOMAP_NULL_ADDR)) {
+		qd->qd_bh_count--;
+		mutex_unlock(&sdp->sd_quota_mutex);
+		goto retry;
 	}
 
 	error = gfs2_meta_read(ip->i_gl, iomap.addr >> sdp->sd_qc_inode->i_blkbits,
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 *

Hi Bob,

Quoted 4 lines · Bob Peterson

Before this patch quota function bh_get called gfs2_iomap_get after it had locked the sd_quota_mutex. That's a problem because that holds the i_rw_mutex, and that lock order is different from other code that locks i_rw_mutex first, then the sd_quota_mutex:

I see the problem, but the patch is pretty awful. Can you resend based on the following suggestion?

Thanks,
Andreas

---
 fs/gfs2/quota.c | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)
fs/gfs2/quota.c +18 -21
index 59d727a4ae2c..58fe5377d589 100644
@@ -370,14 +370,12 @@ static int bh_get(struct gfs2_quota_data *qd)
 	unsigned int block, offset;
 	struct buffer_head *bh;
 	struct iomap iomap = { };
-	int error;
+	int error = 0;
 
 	mutex_lock(&sdp->sd_quota_mutex);
-
-	if (qd->qd_bh_count++) {
-		mutex_unlock(&sdp->sd_quota_mutex);
-		return 0;
-	}
+	if (qd->qd_bh_count)
+		goto inc_out;
+	mutex_unlock(&sdp->sd_quota_mutex);
 
 	block = qd->qd_slot / sdp->sd_qc_per_block;
 	offset = qd->qd_slot % sdp->sd_qc_per_block;
@@ -386,32 +384,31 @@ static int bh_get(struct gfs2_quota_data *qd)
 			       (loff_t)block << inode->i_blkbits,
 			       i_blocksize(inode), &iomap);
 	if (error)
-		goto fail;
-	error = -ENOENT;
+		return error;
 	if (iomap.type != IOMAP_MAPPED)
-		goto fail;
+		return -ENOENT;
 
 	error = gfs2_meta_read(ip->i_gl, iomap.addr >> inode->i_blkbits,
 			       DIO_WAIT, 0, &bh);
 	if (error)
-		goto fail;
-	error = -EIO;
-	if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
-		goto fail_brelse;
+		return error;
+	if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
+		brelse(bh);
+		return -EIO;
+	}
 
+	mutex_lock(&sdp->sd_quota_mutex);
+	if (qd->qd_bh_count) {
+		brelse(bh);
+		goto inc_out;
+	}
 	qd->qd_bh = bh;
 	qd->qd_bh_qc = (struct gfs2_quota_change *)
 		(bh->b_data + sizeof(struct gfs2_meta_header) +
 		 offset * sizeof(struct gfs2_quota_change));
 
-	mutex_unlock(&sdp->sd_quota_mutex);
-
-	return 0;
-
-fail_brelse:
-	brelse(bh);
-fail:
-	qd->qd_bh_count--;
+inc_out:
+	qd->qd_bh_count++;
 	mutex_unlock(&sdp->sd_quota_mutex);
 	return error;
 }
Signature · 1 line
2.35.1
#2 lore raw

PATCH 1/1 gfs2: Move iomap_get before taking sd_quota_mutex

In reply to Bob Peterson

Hi Bob,

Quoted 4 lines · Bob Peterson

Before this patch quota function bh_get called gfs2_iomap_get after it had locked the sd_quota_mutex. That's a problem because that holds the i_rw_mutex, and that lock order is different from other code that locks i_rw_mutex first, then the sd_quota_mutex:

I see the problem, but the patch is pretty awful. Can you resend based on the following suggestion?

Thanks,
Andreas

---
 fs/gfs2/quota.c | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)
fs/gfs2/quota.c +18 -21
index 59d727a4ae2c..58fe5377d589 100644
@@ -370,14 +370,12 @@ static int bh_get(struct gfs2_quota_data *qd)
 	unsigned int block, offset;
 	struct buffer_head *bh;
 	struct iomap iomap = { };
-	int error;
+	int error = 0;
 
 	mutex_lock(&sdp->sd_quota_mutex);
-
-	if (qd->qd_bh_count++) {
-		mutex_unlock(&sdp->sd_quota_mutex);
-		return 0;
-	}
+	if (qd->qd_bh_count)
+		goto inc_out;
+	mutex_unlock(&sdp->sd_quota_mutex);
 
 	block = qd->qd_slot / sdp->sd_qc_per_block;
 	offset = qd->qd_slot % sdp->sd_qc_per_block;
@@ -386,32 +384,31 @@ static int bh_get(struct gfs2_quota_data *qd)
 			       (loff_t)block << inode->i_blkbits,
 			       i_blocksize(inode), &iomap);
 	if (error)
-		goto fail;
-	error = -ENOENT;
+		return error;
 	if (iomap.type != IOMAP_MAPPED)
-		goto fail;
+		return -ENOENT;
 
 	error = gfs2_meta_read(ip->i_gl, iomap.addr >> inode->i_blkbits,
 			       DIO_WAIT, 0, &bh);
 	if (error)
-		goto fail;
-	error = -EIO;
-	if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
-		goto fail_brelse;
+		return error;
+	if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
+		brelse(bh);
+		return -EIO;
+	}
 
+	mutex_lock(&sdp->sd_quota_mutex);
+	if (qd->qd_bh_count) {
+		brelse(bh);
+		goto inc_out;
+	}
 	qd->qd_bh = bh;
 	qd->qd_bh_qc = (struct gfs2_quota_change *)
 		(bh->b_data + sizeof(struct gfs2_meta_header) +
 		 offset * sizeof(struct gfs2_quota_change));
 
-	mutex_unlock(&sdp->sd_quota_mutex);
-
-	return 0;
-
-fail_brelse:
-	brelse(bh);
-fail:
-	qd->qd_bh_count--;
+inc_out:
+	qd->qd_bh_count++;
 	mutex_unlock(&sdp->sd_quota_mutex);
 	return error;
 }
Signature · 1 line
2.35.1