filestreams syzbot fix v2
Hi all,
this series fixes the recently reported crash exposed by syzbot in the filestreams code.
Changes since v1:
- reorder the patches
- remove a now redundant tracepoint argument
- drop the reviewed-bys because the context changed a lot
PATCH 1/2 xfs: fix finding a last resort AG in xfs_filestream_pick_ag
When the main loop in xfs_filestream_pick_ag fails to find a suitable AG it tries to just pick the online AG. But the loop for that uses args->pag as loop iterator while the later code expects pag to be set. Fix this by reusing the max_pag case for this last resort, and also add a check for impossible case of no AG just to make sure that the uninitialized pag doesn't even escape in theory.
--- fs/xfs/xfs_filestream.c | 23 ++++++++++++----------- fs/xfs/xfs_trace.h | 15 +++++---------- 2 files changed, 17 insertions(+), 21 deletions(-)
fs/xfs/xfs_filestream.c +12 -11
@@ -64,7 +64,7 @@ xfs_filestream_pick_ag(struct xfs_perag *pag;struct xfs_perag *max_pag =NULL ;xfs_extlen_t minlen = *longest; -xfs_extlen_t free =0 , minfree, maxfree =0 ; +xfs_extlen_t minfree, maxfree =0 ;xfs_agnumber_t agno;bool first_pass = true;int err;
@@ -107,7 +107,6 @@ xfs_filestream_pick_ag( !(flags & XFS_PICK_USERDATA) || (flags & XFS_PICK_LOWSPACE))) {/* Break out, retaining the reference on the AG. */ - free = pag->pagf_freeblks;break ; } }
@@ -150,23 +149,25 @@ xfs_filestream_pick_ag(* grab. */ if (!max_pag) { -for_each_perag_wrap (args->mp,0 , start_agno, args->pag) +for_each_perag_wrap (args->mp,0 , start_agno, pag) { + max_pag = pag;break ; -atomic_inc (&args->pag->pagf_fstrms); - *longest =0 ; - }else { - pag = max_pag; - free = maxfree; -atomic_inc (&pag->pagf_fstrms); + } + +/* Bail if there are no AGs at all to select from. */ +if (!max_pag) +return -ENOSPC ; } + + pag = max_pag; +atomic_inc (&pag->pagf_fstrms); }else if (max_pag) {xfs_perag_rele (max_pag); } -trace_xfs_filestream_pick (pag, pino, free); +trace_xfs_filestream_pick (pag, pino); args->pag = pag;return 0 ; - } static struct xfs_inode *
fs/xfs/xfs_trace.h +5 -10
@@ -691,8 +691,8 @@ DEFINE_FILESTREAM_EVENT(xfs_filestream_lookup);DEFINE_FILESTREAM_EVENT (xfs_filestream_scan); TRACE_EVENT(xfs_filestream_pick, - TP_PROTO(struct xfs_perag *pag, xfs_ino_t ino, xfs_extlen_t free), - TP_ARGS(pag, ino, free), + TP_PROTO(struct xfs_perag *pag, xfs_ino_t ino), + TP_ARGS(pag, ino), TP_STRUCT__entry( __field(dev_t, dev) __field(xfs_ino_t, ino)
@@ -703,14 +703,9 @@ TRACE_EVENT(xfs_filestream_pick,TP_fast_assign ( __entry->dev = pag->pag_mount->m_super->s_dev; __entry->ino = ino; -if (pag) { - __entry->agno = pag->pag_agno; - __entry->streams =atomic_read (&pag->pagf_fstrms); - }else { - __entry->agno =NULLAGNUMBER ; - __entry->streams =0 ; - } - __entry->free = free; + __entry->agno = pag->pag_agno; + __entry->streams =atomic_read (&pag->pagf_fstrms); + __entry->free = pag->pagf_freeblks; ), TP_printk("dev %d:%d ino 0x%llx agno 0x%x streams %d free %d", MAJOR(__entry->dev), MINOR(__entry->dev),
Signature · 1 line
2.45.2
Re: [PATCH 1/2] xfs: fix finding a last resort AG in xfs_filestream_pick_ag
Quoted 10 lines · Christoph Hellwig
When the main loop in
xfs_filestream_pick_agfails to find a suitable AG it tries to just pick the online AG. But the loop for that usesargs->pagas loop iterator while the later code expects pag to be set. Fix this by reusing themax_pagcase for this last resort, and also add a check for impossible case of no AG just to make sure that the uninitialized pag doesn't even escape in theory.Reported-by [email protected]Signed-off-by Christoph Hellwig <[email protected]>Tested-by [email protected]
Perhaps?
--D
Quoted 98 lines
--- fs/xfs/xfs_filestream.c | 23 ++++++++++++----------- fs/xfs/xfs_trace.h | 15 +++++---------- 2 files changed, 17 insertions(+), 21 deletions(-)fs/xfs/xfs_filestream.c +12
-11@@ -64,7 +64,7 @@ xfs_filestream_pick_ag(struct xfs_perag *pag;struct xfs_perag *max_pag =NULL ;xfs_extlen_t minlen = *longest; -xfs_extlen_t free =0 , minfree, maxfree =0 ; +xfs_extlen_t minfree, maxfree =0 ;xfs_agnumber_t agno;bool first_pass = true;int err;@@ -107,7 +107,6 @@ xfs_filestream_pick_ag( !(flags & XFS_PICK_USERDATA) || (flags & XFS_PICK_LOWSPACE))) {/* Break out, retaining the reference on the AG. */ - free = pag->pagf_freeblks;break ; } }@@ -150,23 +149,25 @@ xfs_filestream_pick_ag(* grab. */ if (!max_pag) { -for_each_perag_wrap (args->mp,0 , start_agno, args->pag) +for_each_perag_wrap (args->mp,0 , start_agno, pag) { + max_pag = pag;break ; -atomic_inc (&args->pag->pagf_fstrms); - *longest =0 ; - }else { - pag = max_pag; - free = maxfree; -atomic_inc (&pag->pagf_fstrms); + } + +/* Bail if there are no AGs at all to select from. */ +if (!max_pag) +return -ENOSPC ; } + + pag = max_pag; +atomic_inc (&pag->pagf_fstrms); }else if (max_pag) {xfs_perag_rele (max_pag); } -trace_xfs_filestream_pick (pag, pino, free); +trace_xfs_filestream_pick (pag, pino); args->pag = pag;return 0 ; - } static struct xfs_inode *fs/xfs/xfs_trace.h +5
-10@@ -691,8 +691,8 @@ DEFINE_FILESTREAM_EVENT(xfs_filestream_lookup);DEFINE_FILESTREAM_EVENT (xfs_filestream_scan); TRACE_EVENT(xfs_filestream_pick, - TP_PROTO(struct xfs_perag *pag, xfs_ino_t ino, xfs_extlen_t free), - TP_ARGS(pag, ino, free), + TP_PROTO(struct xfs_perag *pag, xfs_ino_t ino), + TP_ARGS(pag, ino), TP_STRUCT__entry( __field(dev_t, dev) __field(xfs_ino_t, ino)@@ -703,14 +703,9 @@ TRACE_EVENT(xfs_filestream_pick,TP_fast_assign ( __entry->dev = pag->pag_mount->m_super->s_dev; __entry->ino = ino; -if (pag) { - __entry->agno = pag->pag_agno; - __entry->streams =atomic_read (&pag->pagf_fstrms); - }else { - __entry->agno =NULLAGNUMBER ; - __entry->streams =0 ; - } - __entry->free = free; + __entry->agno = pag->pag_agno; + __entry->streams =atomic_read (&pag->pagf_fstrms); + __entry->free = pag->pagf_freeblks; ), TP_printk("dev %d:%d ino 0x%llx agno 0x%x streams %d free %d", MAJOR(__entry->dev), MINOR(__entry->dev),Signature · 1 line
2.45.2
Re: [PATCH 1/2] xfs: fix finding a last resort AG in xfs_filestream_pick_ag
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
When the main loop in
xfs_filestream_pick_agfails to find a suitable AG it tries to just pick the online AG. But the loop for that usesargs->pagas loop iterator while the later code expects pag to be set. Fix this by reusing themax_pagcase for this last resort, and also add a check for impossible case of no AG just to make sure that the uninitialized pag doesn't even escape in theory.[...]
Applied to for-next, thanks!
[1/2] xfs: fix finding a last resort AG in xfs_filestream_pick_ag
commit: dc60992ce76fbc2f71c2674f435ff6bde2108028
[2/2] xfs: streamline xfs_filestream_pick_ag
commit: 81a1e1c32ef474c20ccb9f730afe1ac25b1c62a4Best regards,
Signature · 1 line
Carlos Maiolino <[email protected]>
PATCH 2/2 xfs: streamline xfs_filestream_pick_ag
In reply to Christoph HellwigDirectly return the error from xfs_bmap_longest_free_extent instead of breaking from the loop and handling it there, and use a done label to directly jump to the exist when we found a suitable perag structure to reduce the indentation level and pag/max_pag check complexity in the tail of the function.
--- fs/xfs/xfs_filestream.c | 96 ++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 50 deletions(-)
fs/xfs/xfs_filestream.c +46 -50
@@ -67,22 +67,28 @@ xfs_filestream_pick_ag(xfs_extlen_t minfree, maxfree =0 ;xfs_agnumber_t agno;bool first_pass = true; -int err;/* 2% of an AG's blocks must be free for it to be chosen. */ minfree = mp->m_sb.sb_agblocks /50 ; restart:for_each_perag_wrap (mp, start_agno, agno, pag) { +int err; +trace_xfs_filestream_scan (pag, pino); + *longest =0 ; err =xfs_bmap_longest_free_extent (pag,NULL , longest);if (err) { -if (err != -EAGAIN ) -break ; -/* Couldn't lock the AGF, skip this AG. */ - err =0 ; -continue ; +if (err == -EAGAIN ) { +/* Couldn't lock the AGF, skip this AG. */ + err =0 ; +continue ; + } +xfs_perag_rele (pag); +if (max_pag) +xfs_perag_rele (max_pag); +return err; }/* Keep track of the AG with the most free blocks. */
@@ -107,7 +113,9 @@ xfs_filestream_pick_ag( !(flags & XFS_PICK_USERDATA) || (flags & XFS_PICK_LOWSPACE))) {/* Break out, retaining the reference on the AG. */ -break ; +if (max_pag) +xfs_perag_rele (max_pag); + goto done; } }
@@ -115,56 +123,44 @@ xfs_filestream_pick_ag(atomic_dec (&pag->pagf_fstrms); } -if (err) { -xfs_perag_rele (pag); -if (max_pag) -xfs_perag_rele (max_pag); -return err; +/* +* Allow a second pass to give xfs_bmap_longest_free_extent() another +* attempt at locking AGFs that it might have skipped over before we +* fail. +*/ +if (first_pass) { + first_pass = false; + goto restart; } -if (!pag) { -/* -* Allow a second pass to give xfs_bmap_longest_free_extent() -* another attempt at locking AGFs that it might have skipped -* over before we fail. -*/ -if (first_pass) { - first_pass = false; - goto restart; - } - -/* -* We must be low on data space, so run a final lowspace -* optimised selection pass if we haven't already. -*/ -if (!(flags &XFS_PICK_LOWSPACE )) { - flags |=XFS_PICK_LOWSPACE ; - goto restart; - } - -/* -* No unassociated AGs are available, so select the AG with the -* most free space, regardless of whether it's already in use by -* another filestream. It none suit, just use whatever AG we can -* grab. -*/ -if (!max_pag) { -for_each_perag_wrap (args->mp,0 , start_agno, pag) { - max_pag = pag; -break ; - } +/* +* We must be low on data space, so run a final lowspace optimised +* selection pass if we haven't already. +*/ +if (!(flags &XFS_PICK_LOWSPACE )) { + flags |=XFS_PICK_LOWSPACE ; + goto restart; + } -/* Bail if there are no AGs at all to select from. */ -if (!max_pag) -return -ENOSPC ; +/* +* No unassociated AGs are available, so select the AG with the most +* free space, regardless of whether it's already in use by another +* filestream. It none suit, just use whatever AG we can grab. +*/ +if (!max_pag) { +for_each_perag_wrap (args->mp,0 , start_agno, pag) { + max_pag = pag; +break ; } - pag = max_pag; -atomic_inc (&pag->pagf_fstrms); - }else if (max_pag) { -xfs_perag_rele (max_pag); +/* Bail if there are no AGs at all to select from. */ +if (!max_pag) +return -ENOSPC ; } + pag = max_pag; +atomic_inc (&pag->pagf_fstrms); +done:trace_xfs_filestream_pick (pag, pino); args->pag = pag;return 0 ;
Signature · 1 line
2.45.2
Re: [PATCH 2/2] xfs: streamline xfs_filestream_pick_ag
Quoted 7 lines · Christoph Hellwig
Directly return the error from
xfs_bmap_longest_free_extentinstead of breaking from the loop and handling it there, and use a done label to directly jump to the exist when we found a suitable perag structure to reduce the indentation level and pag/max_pag check complexity in the tail of the function.Signed-off-by Christoph Hellwig <[email protected]>
Looks fine to me,
--D
Quoted 146 lines
--- fs/xfs/xfs_filestream.c | 96 ++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 50 deletions(-)fs/xfs/xfs_filestream.c +46
-50@@ -67,22 +67,28 @@ xfs_filestream_pick_ag(xfs_extlen_t minfree, maxfree =0 ;xfs_agnumber_t agno;bool first_pass = true; -int err;/* 2% of an AG's blocks must be free for it to be chosen. */ minfree = mp->m_sb.sb_agblocks /50 ; restart:for_each_perag_wrap (mp, start_agno, agno, pag) { +int err; +trace_xfs_filestream_scan (pag, pino); + *longest =0 ; err =xfs_bmap_longest_free_extent (pag,NULL , longest);if (err) { -if (err != -EAGAIN ) -break ; -/* Couldn't lock the AGF, skip this AG. */ - err =0 ; -continue ; +if (err == -EAGAIN ) { +/* Couldn't lock the AGF, skip this AG. */ + err =0 ; +continue ; + } +xfs_perag_rele (pag); +if (max_pag) +xfs_perag_rele (max_pag); +return err; }/* Keep track of the AG with the most free blocks. */ @@ -107,7 +113,9 @@ xfs_filestream_pick_ag( !(flags & XFS_PICK_USERDATA) || (flags & XFS_PICK_LOWSPACE))) {/* Break out, retaining the reference on the AG. */ -break ; +if (max_pag) +xfs_perag_rele (max_pag); + goto done; } }@@ -115,56 +123,44 @@ xfs_filestream_pick_ag(atomic_dec (&pag->pagf_fstrms); } -if (err) { -xfs_perag_rele (pag); -if (max_pag) -xfs_perag_rele (max_pag); -return err; +/* +* Allow a second pass to give xfs_bmap_longest_free_extent() another +* attempt at locking AGFs that it might have skipped over before we +* fail. +*/ +if (first_pass) { + first_pass = false; + goto restart; } -if (!pag) { -/* -* Allow a second pass to give xfs_bmap_longest_free_extent() -* another attempt at locking AGFs that it might have skipped -* over before we fail. -*/ -if (first_pass) { - first_pass = false; - goto restart; - } - -/* -* We must be low on data space, so run a final lowspace -* optimised selection pass if we haven't already. -*/ -if (!(flags &XFS_PICK_LOWSPACE )) { - flags |=XFS_PICK_LOWSPACE ; - goto restart; - } - -/* -* No unassociated AGs are available, so select the AG with the -* most free space, regardless of whether it's already in use by -* another filestream. It none suit, just use whatever AG we can -* grab. -*/ -if (!max_pag) { -for_each_perag_wrap (args->mp,0 , start_agno, pag) { - max_pag = pag; -break ; - } +/* +* We must be low on data space, so run a final lowspace optimised +* selection pass if we haven't already. +*/ +if (!(flags &XFS_PICK_LOWSPACE )) { + flags |=XFS_PICK_LOWSPACE ; + goto restart; + } -/* Bail if there are no AGs at all to select from. */ -if (!max_pag) -return -ENOSPC ; +/* +* No unassociated AGs are available, so select the AG with the most +* free space, regardless of whether it's already in use by another +* filestream. It none suit, just use whatever AG we can grab. +*/ +if (!max_pag) { +for_each_perag_wrap (args->mp,0 , start_agno, pag) { + max_pag = pag; +break ; } - pag = max_pag; -atomic_inc (&pag->pagf_fstrms); - }else if (max_pag) { -xfs_perag_rele (max_pag); +/* Bail if there are no AGs at all to select from. */ +if (!max_pag) +return -ENOSPC ; } + pag = max_pag; +atomic_inc (&pag->pagf_fstrms); +done:trace_xfs_filestream_pick (pag, pino); args->pag = pag;return 0 ;Signature · 1 line
2.45.2