iov_iter: optimize page_copy_sane()
authorEric Dumazet <edumazet@google.com>
Tue, 26 Feb 2019 18:42:39 +0000 (10:42 -0800)
committerAl Viro <viro@zeniv.linux.org.uk>
Tue, 26 Feb 2019 19:05:20 +0000 (14:05 -0500)
Avoid cache line miss dereferencing struct page if we can.

page_copy_sane() mostly deals with order-0 pages.

Extra cache line miss is visible on TCP recvmsg() calls dealing
with GRO packets (typically 45 page frags are attached to one skb).

Bringing the 45 struct pages into cpu cache while copying the data
is not free, since the freeing of the skb (and associated
page frags put_page()) can happen after cache lines have been evicted.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
lib/iov_iter.c

index be4bd627caf060cd89aa41ac88208946da568035..ea36dc355da131b4a45b71d8be6f1bc69a53e637 100644 (file)
@@ -861,8 +861,21 @@ EXPORT_SYMBOL(_copy_from_iter_full_nocache);
 
 static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
 {
-       struct page *head = compound_head(page);
-       size_t v = n + offset + page_address(page) - page_address(head);
+       struct page *head;
+       size_t v = n + offset;
+
+       /*
+        * The general case needs to access the page order in order
+        * to compute the page size.
+        * However, we mostly deal with order-0 pages and thus can
+        * avoid a possible cache line miss for requests that fit all
+        * page orders.
+        */
+       if (n <= v && v <= PAGE_SIZE)
+               return true;
+
+       head = compound_head(page);
+       v += (page - head) << PAGE_SHIFT;
 
        if (likely(n <= v && v <= (PAGE_SIZE << compound_order(head))))
                return true;