From nobody Sun Feb 8 12:36:29 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED1F4EB64D7 for ; Wed, 28 Jun 2023 21:55:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232295AbjF1Vze (ORCPT ); Wed, 28 Jun 2023 17:55:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232364AbjF1Vyv (ORCPT ); Wed, 28 Jun 2023 17:54:51 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FD7F211D for ; Wed, 28 Jun 2023 14:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687989203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LErGcSwJladPWljtQDBZEDG1gs3DbaZNQ9dZknt9+SI=; b=bE1pmSAGRaHObDruWiFGWoU2a0fcU2xPfyl3oncN6oCiWwKj5aNjSaLYs8vu/QCXmkoDMU XuAbRgG1Lr38UfrRf6rgNWcBGL1AbhwSAH1unYIn/y0/mbsORqW2YK+7ZDVb4E4qN8MGlg zMRLGlppt+4IJVTOR4q7ncFFNSV0VEM= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-644-gJ9xuyYVOUOJFMofO_LFbA-1; Wed, 28 Jun 2023 17:53:19 -0400 X-MC-Unique: gJ9xuyYVOUOJFMofO_LFbA-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-635e2618aaeso42496d6.0 for ; Wed, 28 Jun 2023 14:53:19 -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=cDV/hM+b43d+MbPRxVRdMMTBye2AcJ05aMtildRSxipnD1rl+9KaJ5FsFhFvcCdSY6 GzDYSzZ4t+RXBmpfyWLY9rdZfVz6khzhJLR1Ev37W70ypkU4/NKs1ZXJ3d7WQ6XAJ2PX M0wzC8gdZcyXz8t5uQHmZnbRZOkUcDkwhBlsnYHkJt8JliBae8zEGtMGgWELSB0ihEeb y2zIT3DPCV/+dUrftaamlS1OSOMfDZHQL/qta/H5gSUD+scs2ETWU28dn+n3vb4hwFpd aGM8XBddVWHuCNb142mlBhTWcOVL0gKxPk91xzvAvD/sd5IP88J5esECzn57l6ZTDxJp NAyQ== X-Gm-Message-State: AC+VfDwaJGSOGeCiqpuykHnkNht0NjWisg7doKMM8GBv2P9xuS1bZztL JYBbdiuL9gY1Vd/Wal7KrSAXPLgpqHJZ9qM/fYeJI+8dEbSP5zryfQxMrqLI1B/BQmnZApJ/WZF ZSGapHYUpetYVWYixoHgt9jak X-Received: by 2002:a05:6214:501d:b0:621:65de:f60c with SMTP id jo29-20020a056214501d00b0062165def60cmr6650320qvb.3.1687989199421; 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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] =3D page; - flush_anon_page(vma, page, start); - flush_dcache_page(page); - ctx.page_mask =3D 0; - } - page_increm =3D 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); if (page_increm > nr_pages) page_increm =3D 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 =3D 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 =3D -EFAULT; + goto out; + } + } + + for (j =3D 0; j < page_increm; j++) { + subpage =3D nth_page(page, j); + pages[i + j] =3D subpage; + flush_anon_page(vma, subpage, start + j * PAGE_SIZE); + flush_dcache_page(subpage); + } + } + i +=3D page_increm; start +=3D page_increm * PAGE_SIZE; nr_pages -=3D page_increm; --=20 2.41.0