Skip to content

Commit 8413ac9

Browse files
Nick Piggintorvalds
authored andcommitted
mm: page lock use lock bitops
trylock_page, unlock_page open and close a critical section. Hence, we can use the lock bitops to get the desired memory ordering. Also, mark trylock as likely to succeed (and remove the annotation from callers). Signed-off-by: Nick Piggin <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent a978d6f commit 8413ac9

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

include/linux/pagemap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static inline void __clear_page_locked(struct page *page)
311311

312312
static inline int trylock_page(struct page *page)
313313
{
314-
return !test_and_set_bit(PG_locked, &page->flags);
314+
return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
315315
}
316316

317317
/*

mm/filemap.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,17 +573,14 @@ EXPORT_SYMBOL(wait_on_page_bit);
573573
* mechananism between PageLocked pages and PageWriteback pages is shared.
574574
* But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
575575
*
576-
* The first mb is necessary to safely close the critical section opened by the
577-
* test_and_set_bit() to lock the page; the second mb is necessary to enforce
578-
* ordering between the clear_bit and the read of the waitqueue (to avoid SMP
579-
* races with a parallel wait_on_page_locked()).
576+
* The mb is necessary to enforce ordering between the clear_bit and the read
577+
* of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()).
580578
*/
581579
void unlock_page(struct page *page)
582580
{
583-
smp_mb__before_clear_bit();
584-
if (!test_and_clear_bit(PG_locked, &page->flags))
585-
BUG();
586-
smp_mb__after_clear_bit();
581+
VM_BUG_ON(!PageLocked(page));
582+
clear_bit_unlock(PG_locked, &page->flags);
583+
smp_mb__after_clear_bit();
587584
wake_up_page(page, PG_locked);
588585
}
589586
EXPORT_SYMBOL(unlock_page);

mm/swapfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ void free_swap_and_cache(swp_entry_t entry)
422422
if (p) {
423423
if (swap_entry_free(p, swp_offset(entry)) == 1) {
424424
page = find_get_page(&swapper_space, entry.val);
425-
if (page && unlikely(!trylock_page(page))) {
425+
if (page && !trylock_page(page)) {
426426
page_cache_release(page);
427427
page = NULL;
428428
}

0 commit comments

Comments
 (0)