page lock ordering and OCFS2
Subsystems (3)
RFC 1/1 page lock ordering and OCFS2
In recent weeks we've been reworking the locking in OCFS2 to simplify things and make it behave more like a "local" Linux file system. We've run into an ordering inversion between a page's PG_locked and OCFS2's DLM locks that protect page cache contents. I'm including a patch at the end of this mail that I think is a clean way to give the file system a chance to get the ordering right, but we're open to any and all suggestions. We want to do the cleanest thing.
OCFS2 maintains page cache coherence between nodes by requiring that a node hold a valid lock while there are active pages in the page cache. The page cache is invalidated before a node releases a lock so that another node can acquire it. While this invalidation is happening new locks can not be acquired on that node. This is equivalent to a DLM processing thread acquiring PG_locked during truncation while holding a DLM lock. Normal file system user tasks come to the a_ops with PG_locked acquired by their callers before they have a chance to get DLM locks.
We talked a little about modifying the invalidation path to avoid waiting for pages that are held by an OCFS2 path that is waiting for a DLM lock. It seems awfully hard to get right without some very nasty code. The truncation on lock removal has to write back dirty pages so that other nodes can read it -- it can't simply skip these pages if they happened to be locked.
So we aimed right for the lock ordering inversion problem with the appended
patch. It gives file systems a callback that is tried before page locks
are acquired that are going to be passed in to the file system's a_ops
methods. This patch is incomplete, but it shows the sort of work that is
done. Don't be distracted by crappy naming, it was picked out of a hat and can trivially be changed.
So, what do people think about this? Is it reasonable to patch the core to help OCFS2 with this ordering inversion?
- z
---
include/linux/fs.h +8 -0
@@ -325,6 +325,14 @@ loff_t offset, unsigned long nr_segs);struct page * (*get_xip_page)(struct address_space *,sector_t ,int ); + +/* +* These give a file system a chance to get a lock before a page +* lock is acquired and then handed to their a_ops functions. It +* lets the file system order an internal lock above the page lock. +*/ +int (*lock_pgcache)(struct address_space *,int write); +void (*unlock_pgcache)(struct address_space *,int write); };struct backing_dev_info ;
include/linux/pagemap.h +15 -0
@@ -169,6 +169,21 @@__lock_page (page); } +static inline int lock_pgcache (struct address_space *mapping,int write) +{ +might_sleep (); +if (mapping->a_ops->lock_pgcache) +return mapping->a_ops->lock_pgcache (mapping, write); +else +return 0 ; +} + +static inline void unlock_pgcache (struct address_space *mapping,int write) +{ +if (mapping->a_ops->unlock_pgcache) + mapping->a_ops->unlock_pgcache (mapping, write); +} +/* * This is exported only for wait_on_page_locked/wait_on_page_writeback. * Never use this directly!
mm/filemap.c +31 -1
@@ -833,12 +833,17 @@ goto out; page_not_up_to_date: + error =lock_pgcache (mapping,0 ); +if (error) + goto readpage_error; +/* Get exclusive access to the page ... */ lock_page (page);/* Did it get unhashed before we got the lock? */ if (!page->mapping) {unlock_page (page); +unlock_pgcache (mapping,0 );page_cache_release (page);continue ; }
@@ -846,13 +851,14 @@/* Did somebody else fill it already? */ if (PageUptodate (page)) {unlock_page (page); +unlock_pgcache (mapping,0 ); goto page_ok; } readpage:/* Start the actual read. The read will unlock the page. */ error = mapping->a_ops->readpage (filp, page); - +unlock_pgcache (mapping,0 );if (unlikely (error)) goto readpage_error;
@@ -912,21 +918,35 @@* Ok, it wasn't cached, so we need to create a new * page.. */ + error =lock_pgcache (mapping,0 ); +if (error) { + desc->error = error; + goto out; + } +if (!cached_page) { cached_page =page_cache_alloc_cold (mapping);if (!cached_page) { +unlock_pgcache (mapping,0 ); desc->error = -ENOMEM ; goto out; } } + error =add_to_page_cache_lru (cached_page, mapping, index,GFP_KERNEL );if (error) { +unlock_pgcache (mapping,0 ); +if (mapping->a_ops->lock_pgcache) { +page_cache_release (cached_page); + cached_page =NULL ; + }if (error == -EEXIST ) goto find_page; desc->error = error; goto out; } + page = cached_page; cached_page =NULL ; goto readpage;
@@ -1911,6 +1931,7 @@const struct iovec *cur_iov = iov;/* current iovec */ size_t iov_base =0 ;/* offset in the current iovec */ char __user *buf; +int pgcache_locked =0 ;pagevec_init (&lru_pvec,0 );
@@ -1947,6 +1968,11 @@ maxlen = bytes;fault_in_pages_readable (buf, maxlen); + status =lock_pgcache (mapping,1 ); +if (status) +break ; + pgcache_locked =1 ; + page =__grab_cache_page (mapping,index,&cached_page,&lru_pvec);if (!page) { status = -ENOMEM ;
@@ -1998,6 +2024,8 @@if (status >=0 ) status = -EFAULT ;unlock_page (page); +unlock_pgcache (mapping,1 ); + pgcache_locked =0 ;mark_page_accessed (page);page_cache_release (page);if (status <0 )
@@ -2009,6 +2037,8 @@if (cached_page)page_cache_release (cached_page); +if (pgcache_locked) +unlock_pgcache (mapping,1 );/* * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
mm/readahead.c +5 -0
@@ -270,6 +270,10 @@ end_index = ((isize -1 ) >>PAGE_CACHE_SHIFT ); + ret =lock_pgcache (mapping,0 ); +if (ret) + goto out; +/* * Preallocate as many pages as we will need. */
@@ -303,6 +307,7 @@if (ret)read_pages (mapping, filp, &page_pool, ret);BUG_ON (!list_empty (&page_pool)); +unlock_pgcache (mapping,0 ); out:return ret; }