Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19829323rwd; Wed, 28 Jun 2023 15:10:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7POyWWSHn67QkEo4zGGoWWC5267ddj9EPBlXakRceeYCb1WImMNtK4mEpoQIOg/XQWjACo X-Received: by 2002:a17:90a:343:b0:261:110e:30c1 with SMTP id 3-20020a17090a034300b00261110e30c1mr3414206pjf.4.1687990200564; Wed, 28 Jun 2023 15:10:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687990200; cv=none; d=google.com; s=arc-20160816; b=bAbKrmnxxZvJ/UjhyMhpjwRdYw3V8I8XLkpWl+jZ2h8Q1l0kl75kC0yiL6Ruj5vogd qj+DV5NUfCCmX5FXFka5yrUL+qmGeMurXLlMRZaD3FaqPINGCUs/+rF9lyvCl01C/2rQ k23gl7WA+VoTtlBZ3NGvxAmnE53JALV2o0wJrXMN83nK6bR8IuPEmPbzdmGmWbrgMTAl Ntfnq/3MteekModEX6Ri+BzvbyXiO7AUk0dQaA8z4lekVC77CKLg1693cwMTybK9plAl tetA432T3mEIsLbX9iZcI5XTd19KVnd0o56kgY4EFSDMMjZtRFqhe5idYWia61GkI5TG /RtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LErGcSwJladPWljtQDBZEDG1gs3DbaZNQ9dZknt9+SI=; fh=YRm1SqlPzCC/sbB3u8ugRd2MBYnp3vC9/g67nUn+DD4=; b=CVKuQw+5SHAD92K9ZlwBhd3TnGS3r2wCsM5htc1kojj08THVbnedSNZD2Nidjjx0x6 xxuGyxVWKir1lNVcbbqbgO5MlVEkzmANMWBagd3E1S+00orUuj73FA3UgufoI9CWCX+x BZ40QA6rZNCkXRwwxXLygCXEX/gOEjdWTqVO1KnMHWCyr6GA5nkRnsmb7Bzx7/uEkiAg 5yKhMLrP1aULnng1cFsWc4/ypudyHAIVTd2epD8invO7jmnIZgWgz6nkAwOVZr+6/53V N7BNGPIsy+TO2hbYQD/8LUxnkdW4/LQv7XCE0wUwAOtAy2kR8z47ZtwYQHABaX/0/aTw ckHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bE1pmSAG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 18-20020a17090a035200b00262ff90f0ddsi5321344pjf.8.2023.06.28.15.09.47; Wed, 28 Jun 2023 15:10:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bE1pmSAG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232274AbjF1Vzc (ORCPT + 99 others); Wed, 28 Jun 2023 17:55:32 -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-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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.41.0