mm/shmem: Fix data-race in shmem_fault
Subsystems (2)
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
@@ -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
Baolin Wang
reviewerQuoted 24 lines · yahia
From: yahia ahmed <[email protected]>
shmem_faultaccessesinode->i_privatewithout holding a lock, whileshmem_fallocateis modifying it while holding a lock, thus a data-race is created.Fix this by using
READ_ONCE, which provides an atomic lockless read ofinode->i_privatewhich prevents compiler optimizations such as caching registers. which is safe because subsequent call toshmem_falloc_waitrevalidates the lock.Reported-by [email protected]Signed-off-by yahia ahmed <[email protected]>--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/shmem.c +0
-0
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.
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
@@ -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
Baolin Wang
reviewerQuoted 7 lines · yahia
From: yahia ahmed <[email protected]>
Hi,
Thanks for your review, you are absolutely correct that i missed
WRITE_ONCEis required forREAD_ONCEto work.
Please write a useful commit message as you did in V1.
Quoted 15 lines
Reported-by [email protected]Signed-off-by yahia ahmed <[email protected]>---
v2:
- AddWRITE_ONCEtoshmem_fallocate()--- mm/shmem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/shmem.c +0
-0
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
-1ret =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.
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
@@ -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
Baolin Wang
reviewerQuoted 31 lines · yahia
From: yahia ahmed <[email protected]>
shmem_faultandshmem_writeoutaccessinode->i_privatewithout holding a lock, whileshmem_fallocateis modifying it while holding a lock, thus a data-race is created.Fix this by using
READ_ONCEandWRITE_ONCE, which provides an atomic, lockless read ofinode->i_privatewhich prevents compiler optimizations such as caching in registers and add writing toinode->i_privatewithWRITE_ONCEto prevent the compiler from writing in registers.Reported-by [email protected]Signed-off-by yahia ahmed <[email protected]>---
v3:
- Fix lockless read inshmem_writeout
- ApplyWRITE_ONCEandREAD_ONCEacross all
locked instances ofinode->i_private
v2:
- AddWRITE_ONCEtoshmem_fallocate()--- mm/shmem.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)mm/shmem.c +0
-0
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
-1if (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.
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
@@ -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
Quoted 10 lines · yahia
shmem_faultandshmem_writeoutaccessinode->i_privatewithout holding a lock, whileshmem_fallocateis modifying it while holding a lock, thus a data-race is created.Fix this by using
READ_ONCEandWRITE_ONCE, which provides an atomic, lockless read and write ofinode->i_privatewhich prevents compiler optimizations such as caching in registers and add writing toinode->i_privatewithWRITE_ONCEto prevent the compiler from writing in registers.
Quoted 4 lines
Reported-by [email protected]Signed-off-by yahia ahmed <[email protected]>---
LGTM. Thanks.