Skip to contentSkip to conversation

fat: serialize primary and mirror updates

PATCH In review Matthias Goergens 2 participants 4 messages Oct 31 – Nov 2 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fat: serialize primary and mirror updates

__fat_alloc_clusters() drops fat_lock before syncing and mirroring modified buffer heads. Another allocation can change the primary FAT while an earlier allocation is copying it to the mirror.

fat_ent_write() also updates and mirrors without fat_lock. Take the lock in both paths, preserving the existing post-unlock error cleanup.

The test detector reproduced a primary-FAT write during a mirror copy. Three exact-current trials completed with no FAT conflicts and matching mirrors.

---
 fs/fat/fatent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/fat/fatent.c +5 -2
index 2d10a815a579..5f2779c7f947 100644
@@ -419,6 +419,7 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
 	int err;
 
+	lock_fat(MSDOS_SB(sb));
 	ops->ent_put(fatent, new);
 	if (wait) {
 		err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
@@ -429,7 +430,7 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 			 * would not have value to write again on I/O
 			 * error.
 			 */
-			return err;
+			goto out_unlock;
 		}
 	}
 
@@ -440,6 +441,8 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 		if (wait)
 			fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
 	}
+out_unlock:
+	unlock_fat(MSDOS_SB(sb));
 	return err;
 }
 
@@ -582,7 +585,6 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluste
 	err = -ENOSPC;
 
 out:
-	unlock_fat(sbi);
 	mark_fsinfo_dirty(sb);
 	fatent_brelse(&fatent);
 	if (!err) {
@@ -591,6 +593,7 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluste
 		if (!err)
 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
 	}
+	unlock_fat(sbi);
 	for (i = 0; i < nr_bhs; i++)
 		brelse(bhs[i]);
 

base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3

Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 10 lines · Matthias Goergens

__fat_alloc_clusters() drops fat_lock before syncing and mirroring modified buffer heads. Another allocation can change the primary FAT while an earlier allocation is copying it to the mirror.

fat_ent_write() also updates and mirrors without fat_lock. Take the lock in both paths, preserving the existing post-unlock error cleanup.

The test detector reproduced a primary-FAT write during a mirror copy. Three exact-current trials completed with no FAT conflicts and matching mirrors.

What is the real issue?

Quoted 53 lines
---
 fs/fat/fatent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/fat/fatent.c +5 -2
index 2d10a815a579..5f2779c7f947 100644
@@ -419,6 +419,7 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
 	int err;
 
+	lock_fat(MSDOS_SB(sb));
 	ops->ent_put(fatent, new);
 	if (wait) {
 		err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
@@ -429,7 +430,7 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 			 * would not have value to write again on I/O
 			 * error.
 			 */
-			return err;
+			goto out_unlock;
 		}
 	}
 
@@ -440,6 +441,8 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 		if (wait)
 			fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
 	}
+out_unlock:
+	unlock_fat(MSDOS_SB(sb));
 	return err;
 }
 
@@ -582,7 +585,6 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluste
 	err = -ENOSPC;
 
 out:
-	unlock_fat(sbi);
 	mark_fsinfo_dirty(sb);
 	fatent_brelse(&fatent);
 	if (!err) {
@@ -591,6 +593,7 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluste
 		if (!err)
 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
 	}
+	unlock_fat(sbi);
 	for (i = 0; i < nr_bhs; i++)
 		brelse(bhs[i]);
 

base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#2 lore raw
Quoted 1 line · OGAWA Hirofumi

What is the real issue?

Only the data race syzbot reports, "KCSAN: data-race in fat16_ent_put / fat_mirror_bhs": fat_mirror_bhs() copies a FAT block to the backup FAT while another task updates an entry in that block. I have not found any effect on disk. fat_mirror_bhs() holds the backup buffer's lock across the copy, and every writer mirrors the block again after its own update, so a copy that caught an entry mid-update is replaced by that writer's copy. The commit message should have said so, and the patch also lacked my Signed-off-by: it went out before it was ready, sorry.

That leaves two ways to deal with the report. Would you prefer marking the copy as an intended race, data_race() around the memcpy() with a comment on why it is harmless? Or the locking in this patch, which holds fat_lock across fat_sync_bhs() for synchronous inodes and so makes other allocations wait for that I/O?

Matthias

maintainer
#3 lore raw
Quoted 13 lines · Matthias Goergens
Quoted 1 line · OGAWA Hirofumi

What is the real issue?

Only the data race syzbot reports, "KCSAN: data-race in fat16_ent_put / fat_mirror_bhs": fat_mirror_bhs() copies a FAT block to the backup FAT while another task updates an entry in that block. I have not found any effect on disk. fat_mirror_bhs() holds the backup buffer's lock across the copy, and every writer mirrors the block again after its own update, so a copy that caught an entry mid-update is replaced by that writer's copy. The commit message should have said so, and the patch also lacked my Signed-off-by: it went out before it was ready, sorry.

Quoted 5 lines

That leaves two ways to deal with the report. Would you prefer marking the copy as an intended race, data_race() around the memcpy() with a comment on why it is harmless? Or the locking in this patch, which holds fat_lock across fat_sync_bhs() for synchronous inodes and so makes other allocations wait for that I/O?

I think former. And Actually I explained a few times for similar issues in past, I didn't add the comment though.

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>