Skip to content

Commit 73e10de

Browse files
committed
mm: Add set/end/wait functions for PG_private_2
Add three functions to manipulate PG_private_2: (*) set_page_private_2() - Set the flag and take an appropriate reference on the flagged page. (*) end_page_private_2() - Clear the flag, drop the reference and wake up any waiters, somewhat analogously with end_page_writeback(). (*) wait_on_page_private_2() - Wait for the flag to be cleared. Wrappers will need to be placed in the netfs lib header in the patch that adds that. [This implements a suggestion by Linus[1] to not mix the terminology of PG_private_2 and PG_fscache in the mm core function] Changes: v7: - Use compound_head() in all the functions to make them THP safe[6]. v5: - Add set and end functions, calling the end function end rather than unlock[3]. - Keep a ref on the page when PG_private_2 is set[4][5]. v4: - Remove extern from the declaration[2]. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org> Tested-by: Jeff Layton <jlayton@kernel.org> Tested-by: Dave Wysochanski <dwysocha@redhat.com> Tested-By: Marc Dionne <marc.dionne@auristor.com> cc: Alexander Viro <viro@zeniv.linux.org.uk> cc: Christoph Hellwig <hch@lst.de> cc: linux-mm@kvack.org cc: linux-cachefs@redhat.com cc: linux-afs@lists.infradead.org cc: linux-nfs@vger.kernel.org cc: linux-cifs@vger.kernel.org cc: ceph-devel@vger.kernel.org cc: v9fs-developer@lists.sourceforge.net cc: linux-fsdevel@vger.kernel.org Link: https://lore.kernel.org/r/1330473.1612974547@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/CAHk-=wjgA-74ddehziVk=XAEMTKswPu1Yw4uaro1R3ibs27ztw@mail.gmail.com/ [1] Link: https://lore.kernel.org/r/20210216102659.GA27714@lst.de/ [2] Link: https://lore.kernel.org/r/161340387944.1303470.7944159520278177652.stgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/161539528910.286939.1252328699383291173.stgit@warthog.procyon.org.uk # v4 Link: https://lore.kernel.org/r/20210321105309.GG3420@casper.infradead.org [3] Link: https://lore.kernel.org/r/CAHk-=wh+2gbF7XEjYc=HV9w_2uVzVf7vs60BPz0gFA=+pUm3ww@mail.gmail.com/ [4] Link: https://lore.kernel.org/r/CAHk-=wjSGsRj7xwhSMQ6dAQiz53xA39pOG+XA_WeTgwBBu4uqg@mail.gmail.com/ [5] Link: https://lore.kernel.org/r/20210408145057.GN2531743@casper.infradead.org/ [6] Link: https://lore.kernel.org/r/161653788200.2770958.9517755716374927208.stgit@warthog.procyon.org.uk/ # v5 Link: https://lore.kernel.org/r/161789066013.6155.9816857201817288382.stgit@warthog.procyon.org.uk/ # v6
1 parent 7ff5062 commit 73e10de

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

include/linux/pagemap.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,26 @@ void wait_for_stable_page(struct page *page);
688688

689689
void page_endio(struct page *page, bool is_write, int err);
690690

691+
/**
692+
* set_page_private_2 - Set PG_private_2 on a page and take a ref
693+
* @page: The page.
694+
*
695+
* Set the PG_private_2 flag on a page and take the reference needed for the VM
696+
* to handle its lifetime correctly. This sets the flag and takes the
697+
* reference unconditionally, so care must be taken not to set the flag again
698+
* if it's already set.
699+
*/
700+
static inline void set_page_private_2(struct page *page)
701+
{
702+
page = compound_head(page);
703+
get_page(page);
704+
SetPagePrivate2(page);
705+
}
706+
707+
void end_page_private_2(struct page *page);
708+
void wait_on_page_private_2(struct page *page);
709+
int wait_on_page_private_2_killable(struct page *page);
710+
691711
/*
692712
* Add an arbitrary waiter to a page's wait queue
693713
*/

mm/filemap.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,67 @@ void unlock_page(struct page *page)
14321432
}
14331433
EXPORT_SYMBOL(unlock_page);
14341434

1435+
/**
1436+
* end_page_private_2 - Clear PG_private_2 and release any waiters
1437+
* @page: The page
1438+
*
1439+
* Clear the PG_private_2 bit on a page and wake up any sleepers waiting for
1440+
* this. The page ref held for PG_private_2 being set is released.
1441+
*
1442+
* This is, for example, used when a netfs page is being written to a local
1443+
* disk cache, thereby allowing writes to the cache for the same page to be
1444+
* serialised.
1445+
*/
1446+
void end_page_private_2(struct page *page)
1447+
{
1448+
page = compound_head(page);
1449+
VM_BUG_ON_PAGE(!PagePrivate2(page), page);
1450+
clear_bit_unlock(PG_private_2, &page->flags);
1451+
wake_up_page_bit(page, PG_private_2);
1452+
put_page(page);
1453+
}
1454+
EXPORT_SYMBOL(end_page_private_2);
1455+
1456+
/**
1457+
* wait_on_page_private_2 - Wait for PG_private_2 to be cleared on a page
1458+
* @page: The page to wait on
1459+
*
1460+
* Wait for PG_private_2 (aka PG_fscache) to be cleared on a page.
1461+
*/
1462+
void wait_on_page_private_2(struct page *page)
1463+
{
1464+
page = compound_head(page);
1465+
while (PagePrivate2(page))
1466+
wait_on_page_bit(page, PG_private_2);
1467+
}
1468+
EXPORT_SYMBOL(wait_on_page_private_2);
1469+
1470+
/**
1471+
* wait_on_page_private_2_killable - Wait for PG_private_2 to be cleared on a page
1472+
* @page: The page to wait on
1473+
*
1474+
* Wait for PG_private_2 (aka PG_fscache) to be cleared on a page or until a
1475+
* fatal signal is received by the calling task.
1476+
*
1477+
* Return:
1478+
* - 0 if successful.
1479+
* - -EINTR if a fatal signal was encountered.
1480+
*/
1481+
int wait_on_page_private_2_killable(struct page *page)
1482+
{
1483+
int ret = 0;
1484+
1485+
page = compound_head(page);
1486+
while (PagePrivate2(page)) {
1487+
ret = wait_on_page_bit_killable(page, PG_private_2);
1488+
if (ret < 0)
1489+
break;
1490+
}
1491+
1492+
return ret;
1493+
}
1494+
EXPORT_SYMBOL(wait_on_page_private_2_killable);
1495+
14351496
/**
14361497
* end_page_writeback - end writeback against a page
14371498
* @page: the page

0 commit comments

Comments
 (0)