Re: [PATCH v5] mm/gup: disallow GUP writing to file-backed mappings by default

From: Jan Kara
Date: Tue May 02 2023 - 04:00:25 EST


On Sat 29-04-23 02:43:32, Kirill A . Shutemov wrote:
> I think I found relevant snippet of code that solves similar issue.
> get_futex_key() uses RCU to stabilize page->mapping after GUP_fast:
>
>
> /*
> * The associated futex object in this case is the inode and
> * the page->mapping must be traversed. Ordinarily this should
> * be stabilised under page lock but it's not strictly
> * necessary in this case as we just want to pin the inode, not
> * update the radix tree or anything like that.
> *
> * The RCU read lock is taken as the inode is finally freed
> * under RCU. If the mapping still matches expectations then the
> * mapping->host can be safely accessed as being a valid inode.
> */
> rcu_read_lock();
>
> if (READ_ONCE(page->mapping) != mapping) {
> rcu_read_unlock();
> put_page(page);
>
> goto again;
> }
>
> inode = READ_ONCE(mapping->host);
> if (!inode) {
> rcu_read_unlock();
> put_page(page);
>
> goto again;
> }
>
> I think something similar can be used inside GUP_fast too.

Yeah, inodes (and thus struct address_space) is RCU protected these days so
grabbing RCU lock in gup_fast() will get you enough protection for checking
aops if you are careful (like the futex code is).

Honza

--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR