Skip to contentSkip to conversation

mm/shmem: Fix data-race in shmem_fault

PATCH Superseded yahia 2 participants 8 messages Jun 27 – Jul 1

Subsystems (2)
lore
Share & tools
lore

yahia

author
#0 lore raw

PATCH 1/1 mm/shmem: Fix data-race in shmem_fault

From: yahia ahmed <[email protected]>

shmem_fault accesses inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE, which provides an atomic lockless read of inode->i_private which prevents compiler optimizations such as caching registers. which is safe because subsequent call to shmem_falloc_wait revalidates the lock.

---
 mm/shmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/shmem.c +1 -1
index b51f83c970bb..97b3913144ad 100644
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
 	 * Trinity finds that probing a hole which tmpfs is punching can
 	 * prevent the hole-punch from ever completing: noted in i_private.
 	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {
 		ret = shmem_falloc_wait(vmf, inode);
 		if (ret)
 			return ret;
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 24 lines · yahia

From: yahia ahmed <[email protected]>

shmem_fault accesses inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE, which provides an atomic lockless read of inode->i_private which prevents compiler optimizations such as caching registers. which is safe because subsequent call to shmem_falloc_wait revalidates the lock.

---
  mm/shmem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/shmem.c +0 -0
index b51f83c970bb..97b3913144ad 100644

mm/shmem.c static vm_fault_t shmem_fault(struct vm_fault *vmf) · line 2699

Quoted 8 lines
mm/shmem.c +1 -1
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
  	 * Trinity finds that probing a hole which tmpfs is punching can
  	 * prevent the hole-punch from ever completing: noted in i_private.
  	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {
  		ret = shmem_falloc_wait(vmf, inode);
  		if (ret)
  			return ret;

Make sense to me. But we only added READ_ONCE() for the reader, which doesn't seem sufficient. Usually we'd also need WRITE_ONCE() for the writer side.

author
#2 lore raw

PATCH v2 1/1 mm/shmem: Fix data-race in shmem_fault

From: yahia ahmed <[email protected]>

Hi,

Thanks for your review, you are absolutely correct that i missed WRITE_ONCE is required for READ_ONCE to work.

---
v2:
- Add WRITE_ONCE to shmem_fallocate()

---
 mm/shmem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/shmem.c +2 -2
index b51f83c970bb..20ee24ab4806 100644
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
 	 * Trinity finds that probing a hole which tmpfs is punching can
 	 * prevent the hole-punch from ever completing: noted in i_private.
 	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {
 		ret = shmem_falloc_wait(vmf, inode);
 		if (ret)
 			return ret;
@@ -3630,7 +3630,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
 		spin_lock(&inode->i_lock);
-		inode->i_private = &shmem_falloc;
+		WRITE_ONCE(inode->i_private, &shmem_falloc);
 		spin_unlock(&inode->i_lock);
 
 		if ((u64)unmap_end > (u64)unmap_start)
Signature · 1 line
2.54.0
reviewer
#3 lore raw
Quoted 7 lines · yahia

From: yahia ahmed <[email protected]>

Hi,

Thanks for your review, you are absolutely correct that i missed WRITE_ONCE is required for READ_ONCE to work.

Please write a useful commit message as you did in V1.

Quoted 15 lines

---
v2:
- Add WRITE_ONCE to shmem_fallocate()

---
  mm/shmem.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/shmem.c +0 -0
index b51f83c970bb..20ee24ab4806 100644

mm/shmem.c static vm_fault_t shmem_fault(struct vm_fault *vmf) · line 2696

Quoted 5 lines
mm/shmem.c +1 -1
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
  	 * Trinity finds that probing a hole which tmpfs is punching can
  	 * prevent the hole-punch from ever completing: noted in i_private.
  	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {

There is another lockless reader in shmem_writeout().

mm/shmem.c static long shmem_fallocate(struct file *file, int mode, loff_t offset, · line 3634

Quoted 10 lines
mm/shmem.c +1 -1
  		ret = shmem_falloc_wait(vmf, inode);
  		if (ret)
  			return ret;
@@ -3630,7 +3630,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  		shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
  		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  		spin_lock(&inode->i_lock);
-		inode->i_private = &shmem_falloc;
+		WRITE_ONCE(inode->i_private, &shmem_falloc);
  		spin_unlock(&inode->i_lock);

This is still not enough. Please check all the writers.

author
#4 lore raw

PATCH v3 1/1 mm/shmem: Fix data-race in shmem_fault

From: yahia ahmed <[email protected]>

shmem_fault and shmem_writeout access inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE and WRITE_ONCE, which provides an atomic, lockless read of inode->i_private which prevents compiler optimizations such as caching in registers and add writing to inode->i_private with WRITE_ONCE to prevent the compiler from writing in registers.

---
v3:
- Fix lockless read in shmem_writeout
- Apply WRITE_ONCE and READ_ONCE across all
locked instances of inode->i_private
v2:
- Add WRITE_ONCE to shmem_fallocate()

---
 mm/shmem.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
mm/shmem.c +8 -8
index b51f83c970bb..1c39e9dfc524 100644
@@ -1657,10 +1657,10 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 	 * reactivate the folio, and let shmem_fallocate() quit when too many.
 	 */
 	if (!folio_test_uptodate(folio)) {
-		if (inode->i_private) {
+		if (READ_ONCE(inode->i_private)) {
 			struct shmem_falloc *shmem_falloc;
 			spin_lock(&inode->i_lock);
-			shmem_falloc = inode->i_private;
+			shmem_falloc = READ_ONCE(inode->i_private);
 			if (shmem_falloc &&
 			    !shmem_falloc->waitq &&
 			    index >= shmem_falloc->start &&
@@ -2647,7 +2647,7 @@ static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode)
 	vm_fault_t ret = 0;
 
 	spin_lock(&inode->i_lock);
-	shmem_falloc = inode->i_private;
+	shmem_falloc = READ_ONCE(inode->i_private);
 	if (shmem_falloc &&
 	    shmem_falloc->waitq &&
 	    vmf->pgoff >= shmem_falloc->start &&
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
 	 * Trinity finds that probing a hole which tmpfs is punching can
 	 * prevent the hole-punch from ever completing: noted in i_private.
 	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {
 		ret = shmem_falloc_wait(vmf, inode);
 		if (ret)
 			return ret;
@@ -3630,7 +3630,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
 		spin_lock(&inode->i_lock);
-		inode->i_private = &shmem_falloc;
+		WRITE_ONCE(inode->i_private, &shmem_falloc);
 		spin_unlock(&inode->i_lock);
 
 		if ((u64)unmap_end > (u64)unmap_start)
@@ -3640,7 +3640,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		/* No need to unmap again: hole-punching leaves COWed pages */
 
 		spin_lock(&inode->i_lock);
-		inode->i_private = NULL;
+		WRITE_ONCE(inode->i_private, NULL);
 		wake_up_all(&shmem_falloc_waitq);
 		WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
 		spin_unlock(&inode->i_lock);
@@ -3672,7 +3672,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 	shmem_falloc.nr_falloced = 0;
 	shmem_falloc.nr_unswapped = 0;
 	spin_lock(&inode->i_lock);
-	inode->i_private = &shmem_falloc;
+	WRITE_ONCE(inode->i_private, &shmem_falloc);
 	spin_unlock(&inode->i_lock);
 
 	/*
@@ -3747,7 +3747,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		i_size_write(inode, offset + len);
 undone:
 	spin_lock(&inode->i_lock);
-	inode->i_private = NULL;
+	WRITE_ONCE(inode->i_private, NULL);
 	spin_unlock(&inode->i_lock);
 out:
 	if (!error)
Signature · 1 line
2.54.0
reviewer
#5 lore raw
Quoted 31 lines · yahia

From: yahia ahmed <[email protected]>

shmem_fault and shmem_writeout access inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE and WRITE_ONCE, which provides an atomic, lockless read of inode->i_private which prevents compiler optimizations such as caching in registers and add writing to inode->i_private with WRITE_ONCE to prevent the compiler from writing in registers.

---
v3:
- Fix lockless read in shmem_writeout
- Apply WRITE_ONCE and READ_ONCE across all
locked instances of inode->i_private
v2:
- Add WRITE_ONCE to shmem_fallocate()

---
  mm/shmem.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
mm/shmem.c +0 -0
index b51f83c970bb..1c39e9dfc524 100644

mm/shmem.c int shmem_writeout(struct folio *folio, struct swap_iocb **plug, · line 1663

Quoted 9 lines
mm/shmem.c +2 -2
@@ -1657,10 +1657,10 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
  	 * reactivate the folio, and let shmem_fallocate() quit when too many.
  	 */
  	if (!folio_test_uptodate(folio)) {
-		if (inode->i_private) {
+		if (READ_ONCE(inode->i_private)) {
  			struct shmem_falloc *shmem_falloc;
  			spin_lock(&inode->i_lock);
-			shmem_falloc = inode->i_private;
+			shmem_falloc = READ_ONCE(inode->i_private);

The locked read cannot race with the write side, so READ_ONCE() is unnecessary here.

mm/shmem.c static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode) · line 2650

Quoted 9 lines
mm/shmem.c +1 -1
  			if (shmem_falloc &&
  			    !shmem_falloc->waitq &&
  			    index >= shmem_falloc->start &&
@@ -2647,7 +2647,7 @@ static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode)
  	vm_fault_t ret = 0;
 
  	spin_lock(&inode->i_lock);
-	shmem_falloc = inode->i_private;
+	shmem_falloc = READ_ONCE(inode->i_private);

Ditto.

author
#6 lore raw

PATCH v4 1/1 mm/shmem: Fix data-race in shmem_fault

shmem_fault and shmem_writeout access inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE and WRITE_ONCE, which provides an atomic, lockless read and write of inode->i_private which prevents compiler optimizations such as caching in registers and add writing to inode->i_private with WRITE_ONCE to prevent the compiler from writing in registers.

---
v4:
- Remove redundant READ_ONCE
v3:
- Fix lockless read in shmem_writeout
- Apply WRITE_ONCE and READ_ONCE across all
locked instances of inode->i_private
v2:
- Add WRITE_ONCE to shmem_fallocate()

---
 mm/shmem.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
mm/shmem.c +6 -6
index b51f83c970bb..adb8431743e6 100644
@@ -1657,7 +1657,7 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 	 * reactivate the folio, and let shmem_fallocate() quit when too many.
 	 */
 	if (!folio_test_uptodate(folio)) {
-		if (inode->i_private) {
+		if (READ_ONCE(inode->i_private)) {
 			struct shmem_falloc *shmem_falloc;
 			spin_lock(&inode->i_lock);
 			shmem_falloc = inode->i_private;
@@ -2693,7 +2693,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
 	 * Trinity finds that probing a hole which tmpfs is punching can
 	 * prevent the hole-punch from ever completing: noted in i_private.
 	 */
-	if (unlikely(inode->i_private)) {
+	if (unlikely(READ_ONCE(inode->i_private))) {
 		ret = shmem_falloc_wait(vmf, inode);
 		if (ret)
 			return ret;
@@ -3630,7 +3630,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
 		spin_lock(&inode->i_lock);
-		inode->i_private = &shmem_falloc;
+		WRITE_ONCE(inode->i_private, &shmem_falloc);
 		spin_unlock(&inode->i_lock);
 
 		if ((u64)unmap_end > (u64)unmap_start)
@@ -3640,7 +3640,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		/* No need to unmap again: hole-punching leaves COWed pages */
 
 		spin_lock(&inode->i_lock);
-		inode->i_private = NULL;
+		WRITE_ONCE(inode->i_private, NULL);
 		wake_up_all(&shmem_falloc_waitq);
 		WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
 		spin_unlock(&inode->i_lock);
@@ -3672,7 +3672,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 	shmem_falloc.nr_falloced = 0;
 	shmem_falloc.nr_unswapped = 0;
 	spin_lock(&inode->i_lock);
-	inode->i_private = &shmem_falloc;
+	WRITE_ONCE(inode->i_private, &shmem_falloc);
 	spin_unlock(&inode->i_lock);
 
 	/*
@@ -3747,7 +3747,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		i_size_write(inode, offset + len);
 undone:
 	spin_lock(&inode->i_lock);
-	inode->i_private = NULL;
+	WRITE_ONCE(inode->i_private, NULL);
 	spin_unlock(&inode->i_lock);
 out:
 	if (!error)
Signature · 1 line
2.54.0
reviewer Reviewed-by on v4 1/1
#7 lore raw
Quoted 10 lines · yahia

shmem_fault and shmem_writeout access inode->i_private without holding a lock, while shmem_fallocate is modifying it while holding a lock, thus a data-race is created.

Fix this by using READ_ONCE and WRITE_ONCE, which provides an atomic, lockless read and write of inode->i_private which prevents compiler optimizations such as caching in registers and add writing to inode->i_private with WRITE_ONCE to prevent the compiler from writing in registers.

Quoted 4 lines

---

LGTM. Thanks.