When allocation of a folio fails (e.g., -ENOMEM), ocfs2_write_begin_nolock() stores an ERR_PTR in wc->w_folios[i]. ocfs2_unlock_and_free_folios() later walks this array but only skips NULL entries, thus passing the error pointer to folio_unlock(), which eventually dereferences it in const_folio_flags and panics with BUG: unable to handle kernel paging request in const_folio_flags. This patch fixes this issue by adding IS_ERR_OR_NULL() to filter both NULL and error entries before any folio operations. Fixes: 7e119cff9d0a ("ocfs2: convert w_pages to w_folios") Reported-by: Zhiyu Zhang <zhiyuzhang999@xxxxxxxxx> Closes: https://lore.kernel.org/linux-fsdevel/CALf2hKtnFskBvmZeigK_=mqq9Vd4TWT+YOXcwfNNt1ydOY=0Yg@xxxxxxxxxxxxxx/T/#u Tested-by: Zhiyu Zhang <zhiyuzhang999@xxxxxxxxx> Signed-off-by: Zhiyu Zhang <zhiyuzhang999@xxxxxxxxx> --- fs/ocfs2/aops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 40b6bce12951..9e500c5fee38 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -760,7 +760,7 @@ void ocfs2_unlock_and_free_folios(struct folio **folios, int num_folios) int i; for(i = 0; i < num_folios; i++) { - if (!folios[i]) + if (IS_ERR_OR_NULL(folios[i])) continue; folio_unlock(folios[i]); folio_mark_accessed(folios[i]); -- 2.34.1