Re: [PATCH] mm: add replace_page_cache_page() function

From: Miklos Szeredi
Date: Thu Dec 16 2010 - 07:00:23 EST


On Thu, 16 Dec 2010, Minchan Kim wrote:
> On Thu, Dec 16, 2010 at 12:49 AM, Miklos Szeredi <miklos@xxxxxxxxxx> wrote:
> > From: Miklos Szeredi <mszeredi@xxxxxxx>
> >
> > This function basically does:
> >
> > Â Â remove_from_page_cache(old);
> > Â Â page_cache_release(old);
> > Â Â add_to_page_cache_locked(new);
> >
> > Except it does this atomically, so there's no possibility for the
> > "add" to fail because of a race.
> >
> > This is used by fuse to move pages into the page cache.
>
> Please write down why fuse need this new atomic function in description.

Okay.

> >
> > Signed-off-by: Miklos Szeredi <mszeredi@xxxxxxx>
> > ---
> > Âfs/fuse/dev.c      |  10 ++++------
> > Âinclude/linux/pagemap.h | Â Â1 +
> > Âmm/filemap.c      Â|  41 +++++++++++++++++++++++++++++++++++++++++
> > Â3 files changed, 46 insertions(+), 6 deletions(-)
> >
> > Index: linux-2.6/mm/filemap.c
> > ===================================================================
> > --- linux-2.6.orig/mm/filemap.c 2010-12-15 16:39:55.000000000 +0100
> > +++ linux-2.6/mm/filemap.c   Â2010-12-15 16:41:24.000000000 +0100
> > @@ -389,6 +389,47 @@ int filemap_write_and_wait_range(struct
> > Â}
> > ÂEXPORT_SYMBOL(filemap_write_and_wait_range);
> >
>
> This function is exported.
> Please, add function description

Right, will do.

> > +int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
> > +{
> > + Â Â Â int error;
> > +
> > + Â Â Â VM_BUG_ON(!PageLocked(old));
> > + Â Â Â VM_BUG_ON(!PageLocked(new));
> > + Â Â Â VM_BUG_ON(new->mapping);
> > +
> > + Â Â Â error = mem_cgroup_cache_charge(new, current->mm,
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â gfp_mask & GFP_RECLAIM_MASK);
> > + Â Â Â if (error)
> > + Â Â Â Â Â Â Â goto out;
> > +
> > + Â Â Â error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
> > + Â Â Â if (error == 0) {
> > + Â Â Â Â Â Â Â struct address_space *mapping = old->mapping;
> > + Â Â Â Â Â Â Â pgoff_t offset = old->index;
> > +
> > + Â Â Â Â Â Â Â page_cache_get(new);
> > + Â Â Â Â Â Â Â new->mapping = mapping;
> > + Â Â Â Â Â Â Â new->index = offset;
> > +
> > + Â Â Â Â Â Â Â spin_lock_irq(&mapping->tree_lock);
> > + Â Â Â Â Â Â Â __remove_from_page_cache(old);
> > + Â Â Â Â Â Â Â error = radix_tree_insert(&mapping->page_tree, offset, new);
> > + Â Â Â Â Â Â Â BUG_ON(error);
> > + Â Â Â Â Â Â Â mapping->nrpages++;
> > + Â Â Â Â Â Â Â __inc_zone_page_state(new, NR_FILE_PAGES);
> > + Â Â Â Â Â Â Â if (PageSwapBacked(new))
> > + Â Â Â Â Â Â Â Â Â Â Â __inc_zone_page_state(new, NR_SHMEM);
> > + Â Â Â Â Â Â Â spin_unlock_irq(&mapping->tree_lock);
> > + Â Â Â Â Â Â Â radix_tree_preload_end();
> > + Â Â Â Â Â Â Â mem_cgroup_uncharge_cache_page(old);
> > + Â Â Â Â Â Â Â page_cache_release(old);
>
> Why do you release reference of old?

That's the page cache reference we release. Just like we acquire the
page cache reference for "new" above.

I suspect it's historic that page_cache_release() doesn't drop the
page cache ref.

Thanks for the review.

Miklos

> > + Â Â Â } else
> > + Â Â Â Â Â Â Â mem_cgroup_uncharge_cache_page(new);
> > +out:
> > + Â Â Â return error;
> > +}
> > +EXPORT_SYMBOL_GPL(replace_page_cache_page);
> > +
> > Â/**
> > Â* add_to_page_cache_locked - add a locked page to the pagecache
> > Â* @page: Â Â Âpage to add
> > Index: linux-2.6/include/linux/pagemap.h
> > ===================================================================
> > --- linux-2.6.orig/include/linux/pagemap.h   Â2010-12-15 16:39:39.000000000 +0100
> > +++ linux-2.6/include/linux/pagemap.h  2010-12-15 16:41:24.000000000 +0100
> > @@ -457,6 +457,7 @@ int add_to_page_cache_lru(struct page *p
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âpgoff_t index, gfp_t gfp_mask);
> > Âextern void remove_from_page_cache(struct page *page);
> > Âextern void __remove_from_page_cache(struct page *page);
> > +int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask);
> >
> > Â/*
> > Â* Like add_to_page_cache_locked, but used to add newly allocated pages:
> > Index: linux-2.6/fs/fuse/dev.c
> > ===================================================================
> > --- linux-2.6.orig/fs/fuse/dev.c    Â2010-12-15 16:39:39.000000000 +0100
> > +++ linux-2.6/fs/fuse/dev.c   2010-12-15 16:41:24.000000000 +0100
> > @@ -729,14 +729,12 @@ static int fuse_try_move_page(struct fus
> > Â Â Â Âif (WARN_ON(PageMlocked(oldpage)))
> > Â Â Â Â Â Â Â Âgoto out_fallback_unlock;
> >
> > - Â Â Â remove_from_page_cache(oldpage);
> > - Â Â Â page_cache_release(oldpage);
> > -
> > - Â Â Â err = add_to_page_cache_locked(newpage, mapping, index, GFP_KERNEL);
> > + Â Â Â err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
> > Â Â Â Âif (err) {
> > - Â Â Â Â Â Â Â printk(KERN_WARNING "fuse_try_move_page: failed to add page");
> > - Â Â Â Â Â Â Â goto out_fallback_unlock;
> > + Â Â Â Â Â Â Â unlock_page(newpage);
> > + Â Â Â Â Â Â Â return err;
> > Â Â Â Â}
> > +
> > Â Â Â Âpage_cache_get(newpage);
> >
> > Â Â Â Âif (!(buf->flags & PIPE_BUF_FLAG_LRU))
> >
> > --
> > To unsubscribe, send a message with 'unsubscribe linux-mm' in
> > the body to majordomo@xxxxxxxxxx ÂFor more info on Linux MM,
> > see: http://www.linux-mm.org/ .
> > Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
> > Don't email: <a href=mailto:"dont@xxxxxxxxx";> email@xxxxxxxxx </a>
> >
>
>
>
> --
> Kind regards,
> Minchan Kim
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/