Skip to content

Commit

Permalink
shmem: fix smaps BUG sleeping while atomic
Browse files Browse the repository at this point in the history
commit e5548f8 upstream.

smaps_pte_hole_lookup() is calling shmem_partial_swap_usage() with page
table lock held: but shmem_partial_swap_usage() does cond_resched_rcu() if
need_resched(): "BUG: sleeping function called from invalid context".

Since shmem_partial_swap_usage() is designed to count across a range, but
smaps_pte_hole_lookup() only calls it for a single page slot, just break
out of the loop on the last or only page, before checking need_resched().

Link: https://lkml.kernel.org/r/6fe3b3ec-abdf-332f-5c23-6a3b3a3b11a9@google.com
Fixes: 2301003 ("mm/smaps: simplify shmem handling of pte holes")
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
Cc: <stable@vger.kernel.org>	[5.16+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Hugh Dickins authored and Greg Kroah-Hartman committed Aug 30, 2023
1 parent 535cdce commit 6218f96
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,14 +806,16 @@ unsigned long shmem_partial_swap_usage(struct address_space *mapping,
XA_STATE(xas, &mapping->i_pages, start);
struct page *page;
unsigned long swapped = 0;
unsigned long max = end - 1;

rcu_read_lock();
xas_for_each(&xas, page, end - 1) {
xas_for_each(&xas, page, max) {
if (xas_retry(&xas, page))
continue;
if (xa_is_value(page))
swapped++;

if (xas.xa_index == max)
break;
if (need_resched()) {
xas_pause(&xas);
cond_resched_rcu();
Expand Down

0 comments on commit 6218f96

Please sign in to comment.