From patchwork Wed Jun 28 21:53:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13296392 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D458EB64DC for ; Wed, 28 Jun 2023 21:53:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F27318D0007; Wed, 28 Jun 2023 17:53:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E621A8D0001; Wed, 28 Jun 2023 17:53:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDC128D0007; Wed, 28 Jun 2023 17:53:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BB39D8D0001 for ; Wed, 28 Jun 2023 17:53:24 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 75A8B80354 for ; Wed, 28 Jun 2023 21:53:24 +0000 (UTC) X-FDA: 80953508328.04.9B27A63 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 60E024000F for ; Wed, 28 Jun 2023 21:53:22 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Gc5+DCG9; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf11.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687989202; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LErGcSwJladPWljtQDBZEDG1gs3DbaZNQ9dZknt9+SI=; b=w2cZr3FyzlYqF98noECfezA9CYWeKS6E8VMnQnxxJWhUQQIbEN64gdJJfi19UANKH3u5hM ueR6/nRcEMqhvyDzptXZktvLqVExhNzVyfM7ZkSNNLQUZ1RbJzRW19Fm9ucj5u4TGich/h lLyAr6yyGFl8W6qzkiJ4HkuUoRB0Dqg= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Gc5+DCG9; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf11.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687989202; a=rsa-sha256; cv=none; b=wLgiyrbgJgC4MJ40CbppWAVMUbjZr/dAUfIe9MArcBFafufjj3VqEu0KuGXtAkvtArGBBS 1wbSqx1l21TzC4Vy+PcJphyayPNDGOWLExIPA7vhgvqg5cF0gDW+6BbuGFD72NYtgHjrgi 8/UBJiyC2eG0xY5lJh7goVWVi8kdWLw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687989201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LErGcSwJladPWljtQDBZEDG1gs3DbaZNQ9dZknt9+SI=; b=Gc5+DCG9htzZwbnRi3edq97hbW3GjAjfp296NpOYML+b6jzNe9qFSUdvnHE53UCEpUw7// OdEVpH0wiSC+P2nl/ajOzXusOmHA8r00nTvP57mNxtM6GfVSvuHEDbMFRueyMucJJBstwO 7xYBHSfJBNDXWGr0n4Q/eHrl9n9bNuI= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-UF5u1aa-MS6MWIx3cqPAfw-1; Wed, 28 Jun 2023 17:53:20 -0400 X-MC-Unique: UF5u1aa-MS6MWIx3cqPAfw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-635e2618aaeso42536d6.0 for ; Wed, 28 Jun 2023 14:53:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687989199; x=1690581199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LErGcSwJladPWljtQDBZEDG1gs3DbaZNQ9dZknt9+SI=; b=TIeLifFjp4JvvCFek+htg99GO4dsgh51FChlmXL2v55OY3e+w212a4VK0YpuiRjv/9 N5diKmVwnxX4V/ls5EvyarzQCEHYabEfTSz93KIg84lOYFQvIFbuPu2oVKkWMDO5zeXv Ie4AX9fSiAdjfXg2BmYnFhnDhtarxrlSuUgYc0YSpFVWCUP8V6uxdBMkq0Nxsp4AQ5m4 V2i+7IdkjkPqO4cO69yki25OtUzAGbVE7U6vc5uy65/A+lLhw5S8odL52a6n3gs+Y4hO 4G3O8nxUIjF1vZcFb0q26kd3AGkyErMRXzUyVBMx1qABurNLaOwfEiIP3xe11IW196TR qhFw== X-Gm-Message-State: AC+VfDzkNX5Au5RHgGwDKp8LkUg4NTEX/sZjDI0e98ohvcjS0pPWxDMf j/xNHUMwH/V4F0qpUA9+ysVp5zZcFuD1ReNpWqBNitZ6sSB7/GTP10Ox6kHA9P1/kDLA2DK0O0f +E7AnAei5uDpcDIUZYUf+iSL7DjhjHbJX1ovUjTKzvZGtVKYYQyogo6do7wOQnAvg2tva X-Received: by 2002:a05:6214:501d:b0:621:65de:f60c with SMTP id jo29-20020a056214501d00b0062165def60cmr6650332qvb.3.1687989199502; Wed, 28 Jun 2023 14:53:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4aH92Xekg+ZXFKVELiAnLIK82VapCJUdD+ZE2LzSIKvWv5j/RuoE6PplAHYjC4on1+uql7Bw== X-Received: by 2002:a05:6214:501d:b0:621:65de:f60c with SMTP id jo29-20020a056214501d00b0062165def60cmr6650303qvb.3.1687989199149; Wed, 28 Jun 2023 14:53:19 -0700 (PDT) Received: from x1n.. (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id p3-20020a0cfac3000000b00631fea4d5bcsm6277797qvo.95.2023.06.28.14.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 14:53:18 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , "Kirill A . Shutemov" , Andrew Morton , Andrea Arcangeli , Mike Rapoport , John Hubbard , Matthew Wilcox , Mike Kravetz , Vlastimil Babka , Yang Shi , James Houghton , Jason Gunthorpe , Lorenzo Stoakes , Hugh Dickins , peterx@redhat.com Subject: [PATCH v4 5/8] mm/gup: Accelerate thp gup even for "pages != NULL" Date: Wed, 28 Jun 2023 17:53:07 -0400 Message-ID: <20230628215310.73782-6-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230628215310.73782-1-peterx@redhat.com> References: <20230628215310.73782-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 60E024000F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 19estdr99xjds5by6jm7uf958zc9k37z X-HE-Tag: 1687989202-295002 X-HE-Meta: U2FsdGVkX19m5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5Te69nVV7ZLhn6jg/btWbSvAGz7jPJqPvIcxbqBjPrEuomlrFFbBpIWTI+axh9soLZqoD7ZvHGITDsj6Kg1IvhiMqbP9e7pxSREO4S7PguQXdDH3sovPsvS5LBL8bzdUcBhSVKSeD8U2s52enGuzwEljhy0ZQ1RP5AeAl+qkDMzVUGGXClFGq6MXWBdhS5kw/I5ZfX578SYsKLbjKQmSWLzGW5BPPz7vkzppuqmX0AzWodNmOFpD5Tw5B327Rl8wPr9hagg1IGs1ed7Gnz3v4UgZT8CpTIxw/0NXgJjJUT8QI+y2RQgmOK4cHn7YC0fqqOmuEe/AdFT12iRBUd1m1frGg3x0rZnap9b8Nvj5Ou+Cf0LQCY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The acceleration of THP was done with ctx.page_mask, however it'll be ignored if **pages is non-NULL. The old optimization was introduced in 2013 in 240aadeedc4a ("mm: accelerate mm_populate() treatment of THP pages"). It didn't explain why we can't optimize the **pages non-NULL case. It's possible that at that time the major goal was for mm_populate() which should be enough back then. Optimize thp for all cases, by properly looping over each subpage, doing cache flushes, and boost refcounts / pincounts where needed in one go. This can be verified using gup_test below: # chrt -f 1 ./gup_test -m 512 -t -L -n 1024 -r 10 Before: 13992.50 ( +-8.75%) After: 378.50 (+-69.62%) Reviewed-by: Lorenzo Stoakes Signed-off-by: Peter Xu --- mm/gup.c | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 5af1be81390b..0e2b0ff1143a 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1199,16 +1199,53 @@ static long __get_user_pages(struct mm_struct *mm, goto out; } next_page: - if (pages) { - pages[i] = page; - flush_anon_page(vma, page, start); - flush_dcache_page(page); - ctx.page_mask = 0; - } - page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); if (page_increm > nr_pages) page_increm = nr_pages; + + if (pages) { + struct page *subpage; + unsigned int j; + + /* + * This must be a large folio (and doesn't need to + * be the whole folio; it can be part of it), do + * the refcount work for all the subpages too. + * + * NOTE: here the page may not be the head page + * e.g. when start addr is not thp-size aligned. + * try_grab_folio() should have taken care of tail + * pages. + */ + if (page_increm > 1) { + struct folio *folio; + + /* + * Since we already hold refcount on the + * large folio, this should never fail. + */ + folio = try_grab_folio(page, page_increm - 1, + foll_flags); + if (WARN_ON_ONCE(!folio)) { + /* + * Release the 1st page ref if the + * folio is problematic, fail hard. + */ + gup_put_folio(page_folio(page), 1, + foll_flags); + ret = -EFAULT; + goto out; + } + } + + for (j = 0; j < page_increm; j++) { + subpage = nth_page(page, j); + pages[i + j] = subpage; + flush_anon_page(vma, subpage, start + j * PAGE_SIZE); + flush_dcache_page(subpage); + } + } + i += page_increm; start += page_increm * PAGE_SIZE; nr_pages -= page_increm;