Skip to content

Commit e35a6cf

Browse files
Matthew Wilcox (Oracle)KAGA-KOKO
Matthew Wilcox (Oracle)
authored andcommitted
futex: Use a folio instead of a page
The futex code already handles compound pages correctly, but using a folio tells the compiler that there is already a reference to the head page and it doesn't need to call compound_head() again. Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Davidlohr Bueso <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 85be6d8 commit e35a6cf

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

kernel/futex/core.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
222222
{
223223
unsigned long address = (unsigned long)uaddr;
224224
struct mm_struct *mm = current->mm;
225-
struct page *page, *tail;
225+
struct page *page;
226+
struct folio *folio;
226227
struct address_space *mapping;
227228
int err, ro = 0;
228229

@@ -273,54 +274,52 @@ int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
273274
err = 0;
274275

275276
/*
276-
* The treatment of mapping from this point on is critical. The page
277-
* lock protects many things but in this context the page lock
277+
* The treatment of mapping from this point on is critical. The folio
278+
* lock protects many things but in this context the folio lock
278279
* stabilizes mapping, prevents inode freeing in the shared
279280
* file-backed region case and guards against movement to swap cache.
280281
*
281-
* Strictly speaking the page lock is not needed in all cases being
282-
* considered here and page lock forces unnecessarily serialization
282+
* Strictly speaking the folio lock is not needed in all cases being
283+
* considered here and folio lock forces unnecessarily serialization.
283284
* From this point on, mapping will be re-verified if necessary and
284-
* page lock will be acquired only if it is unavoidable
285+
* folio lock will be acquired only if it is unavoidable
285286
*
286-
* Mapping checks require the head page for any compound page so the
287-
* head page and mapping is looked up now. For anonymous pages, it
288-
* does not matter if the page splits in the future as the key is
289-
* based on the address. For filesystem-backed pages, the tail is
290-
* required as the index of the page determines the key. For
291-
* base pages, there is no tail page and tail == page.
287+
* Mapping checks require the folio so it is looked up now. For
288+
* anonymous pages, it does not matter if the folio is split
289+
* in the future as the key is based on the address. For
290+
* filesystem-backed pages, the precise page is required as the
291+
* index of the page determines the key.
292292
*/
293-
tail = page;
294-
page = compound_head(page);
295-
mapping = READ_ONCE(page->mapping);
293+
folio = page_folio(page);
294+
mapping = READ_ONCE(folio->mapping);
296295

297296
/*
298-
* If page->mapping is NULL, then it cannot be a PageAnon
297+
* If folio->mapping is NULL, then it cannot be an anonymous
299298
* page; but it might be the ZERO_PAGE or in the gate area or
300299
* in a special mapping (all cases which we are happy to fail);
301300
* or it may have been a good file page when get_user_pages_fast
302301
* found it, but truncated or holepunched or subjected to
303-
* invalidate_complete_page2 before we got the page lock (also
302+
* invalidate_complete_page2 before we got the folio lock (also
304303
* cases which we are happy to fail). And we hold a reference,
305304
* so refcount care in invalidate_inode_page's remove_mapping
306305
* prevents drop_caches from setting mapping to NULL beneath us.
307306
*
308307
* The case we do have to guard against is when memory pressure made
309308
* shmem_writepage move it from filecache to swapcache beneath us:
310-
* an unlikely race, but we do need to retry for page->mapping.
309+
* an unlikely race, but we do need to retry for folio->mapping.
311310
*/
312311
if (unlikely(!mapping)) {
313312
int shmem_swizzled;
314313

315314
/*
316-
* Page lock is required to identify which special case above
317-
* applies. If this is really a shmem page then the page lock
315+
* Folio lock is required to identify which special case above
316+
* applies. If this is really a shmem page then the folio lock
318317
* will prevent unexpected transitions.
319318
*/
320-
lock_page(page);
321-
shmem_swizzled = PageSwapCache(page) || page->mapping;
322-
unlock_page(page);
323-
put_page(page);
319+
folio_lock(folio);
320+
shmem_swizzled = folio_test_swapcache(folio) || folio->mapping;
321+
folio_unlock(folio);
322+
folio_put(folio);
324323

325324
if (shmem_swizzled)
326325
goto again;
@@ -331,14 +330,14 @@ int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
331330
/*
332331
* Private mappings are handled in a simple way.
333332
*
334-
* If the futex key is stored on an anonymous page, then the associated
333+
* If the futex key is stored in anonymous memory, then the associated
335334
* object is the mm which is implicitly pinned by the calling process.
336335
*
337336
* NOTE: When userspace waits on a MAP_SHARED mapping, even if
338337
* it's a read-only handle, it's expected that futexes attach to
339338
* the object not the particular process.
340339
*/
341-
if (PageAnon(page)) {
340+
if (folio_test_anon(folio)) {
342341
/*
343342
* A RO anonymous page will never change and thus doesn't make
344343
* sense for futex operations.
@@ -357,40 +356,40 @@ int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
357356

358357
/*
359358
* The associated futex object in this case is the inode and
360-
* the page->mapping must be traversed. Ordinarily this should
361-
* be stabilised under page lock but it's not strictly
359+
* the folio->mapping must be traversed. Ordinarily this should
360+
* be stabilised under folio lock but it's not strictly
362361
* necessary in this case as we just want to pin the inode, not
363-
* update the radix tree or anything like that.
362+
* update i_pages or anything like that.
364363
*
365364
* The RCU read lock is taken as the inode is finally freed
366365
* under RCU. If the mapping still matches expectations then the
367366
* mapping->host can be safely accessed as being a valid inode.
368367
*/
369368
rcu_read_lock();
370369

371-
if (READ_ONCE(page->mapping) != mapping) {
370+
if (READ_ONCE(folio->mapping) != mapping) {
372371
rcu_read_unlock();
373-
put_page(page);
372+
folio_put(folio);
374373

375374
goto again;
376375
}
377376

378377
inode = READ_ONCE(mapping->host);
379378
if (!inode) {
380379
rcu_read_unlock();
381-
put_page(page);
380+
folio_put(folio);
382381

383382
goto again;
384383
}
385384

386385
key->both.offset |= FUT_OFF_INODE; /* inode-based key */
387386
key->shared.i_seq = get_inode_sequence_number(inode);
388-
key->shared.pgoff = page_to_pgoff(tail);
387+
key->shared.pgoff = folio->index + folio_page_idx(folio, page);
389388
rcu_read_unlock();
390389
}
391390

392391
out:
393-
put_page(page);
392+
folio_put(folio);
394393
return err;
395394
}
396395

0 commit comments

Comments
 (0)