From mboxrd@z Thu Jan 1 00:00:00 1970 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 CEEE9EB64D8 for ; Tue, 20 Jun 2023 16:24:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E1D48D0002; Tue, 20 Jun 2023 12:24:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 492418D0001; Tue, 20 Jun 2023 12:24:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 332088D0002; Tue, 20 Jun 2023 12:24:03 -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 208908D0001 for ; Tue, 20 Jun 2023 12:24:03 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E9D5B1C843A for ; Tue, 20 Jun 2023 16:24:02 +0000 (UTC) X-FDA: 80923647924.06.F009051 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf16.hostedemail.com (Postfix) with ESMTP id D26F4180009 for ; Tue, 20 Jun 2023 16:24:00 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gW6Xn7X0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687278241; a=rsa-sha256; cv=none; b=wZ0Zb6TsHAzzPvrgVku8tou2miMeKY9mJWyDvqiukaB2rtoTP+uEzR0gXfktv6AHJjumWL OG7dZ9yMuEkBipaSnC0KXuVSFo2mG15GiB4uI+kjwaxW1FqlnG2aydDLlH+Y0M/PSSPfX+ xAcIFZ+Dk9r8f1gwepoVk6i6YrT8UJA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gW6Xn7X0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1687278241; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tkfPb81x8lQ4BDOiIbrDPIMFlQfa70cNVIYaWHHcOPc=; b=qKKaIIO2ntNqInAzR/MafG9HqAhSrDjyC+VWK0+d2suJ6HzRpc2G/q/oCxyBvkadamu1nH +bGOtNewf9fllGcmZ7vBkFjreTXfouTUph2IWEjhogZZdu/JPxRzsOo9vNNeMUqg+T8yF/ JRZbKBGDCH+pOYh9ycPhqmAjMgHTpMY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687278240; 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: in-reply-to:in-reply-to:references:references; bh=tkfPb81x8lQ4BDOiIbrDPIMFlQfa70cNVIYaWHHcOPc=; b=gW6Xn7X0g5vOens64ttEpgRSYoHC9b6Bdh5P+uuXVw0JYUn/zqgMk67x5lRjTQDeyMSTXg QKGWq4iX8BnupkmdLucB5ILKzyRLeWqWXZOTicMS21WotWIn01TH97qS4N+xX6hW/WONnq pTZj71cXXgPMcnioXGnmikjvJWC3aL8= 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-94-XxhfItZpP0GHka_4Ku02uw-1; Tue, 20 Jun 2023 12:23:58 -0400 X-MC-Unique: XxhfItZpP0GHka_4Ku02uw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-62dd79f63e0so7638636d6.0 for ; Tue, 20 Jun 2023 09:23:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687278237; x=1689870237; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=tkfPb81x8lQ4BDOiIbrDPIMFlQfa70cNVIYaWHHcOPc=; b=NhZPMLKu+RFo0dqy5oq6BFGhstOBLy6KBptryWzQkTKLS+/TYl9Y8he8aeh17UKwTK 3zs5vQ+JXAL8gMJm9ftZ7bEJKSKvLO6ZTnvIo5bYX+LqQ4hj42iG34435YZMCjuA9thE aHWY1Io0p2oipUm/5WZtN4X7fkavETQU9wZBD5ixULiH5ZsF2bSwS34PTQ8hOdTRBElT gzBEcJskUbdowtrGph0W1qNgrFR00jDjWB384uhiHA0vRe73/B8Dl3LDodTPHqv+epxj jrK/HzzpKYHeGSJB5IY1KI6xnjRlvDWFFfn6Cu3LygLG3ZkSCLLJ+I1xaDiTYexJQ8bH rzdg== X-Gm-Message-State: AC+VfDyXePqd+Pn5Ci//C+hfN7+9B/0sxcO46ARDBZ2cQenaTiYXRK6y Bgr8XOQTpp0A3zJEi3HFcKQ0HFC1McRMR+or89Iv2h43MRhRaa/ivqLsEeyeDGfHLISG5IcG42G UoLGqOqxNB1Q= X-Received: by 2002:a05:6214:411c:b0:62d:f62b:907 with SMTP id kc28-20020a056214411c00b0062df62b0907mr16609604qvb.0.1687278237412; Tue, 20 Jun 2023 09:23:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ562mP7vivYVjBC2Ee6xsjKs0n5SEqb0Mareafy6oOvsh/uQz9MO5aQRwdB7fCi+rCy/Ai9eQ== X-Received: by 2002:a05:6214:411c:b0:62d:f62b:907 with SMTP id kc28-20020a056214411c00b0062df62b0907mr16609588qvb.0.1687278237142; Tue, 20 Jun 2023 09:23:57 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id t2-20020a0cde02000000b0062dd9254429sm1358895qvk.142.2023.06.20.09.23.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:23:56 -0700 (PDT) Date: Tue, 20 Jun 2023 12:23:55 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrea Arcangeli , Mike Rapoport , Matthew Wilcox , Vlastimil Babka , John Hubbard , "Kirill A . Shutemov" , James Houghton , Andrew Morton , Lorenzo Stoakes , Hugh Dickins , Mike Kravetz , Jason Gunthorpe Subject: Re: [PATCH v2 5/8] mm/gup: Accelerate thp gup even for "pages != NULL" Message-ID: References: <20230619231044.112894-1-peterx@redhat.com> <20230619231044.112894-6-peterx@redhat.com> <02a057a3-3d9e-4013-8762-25ceb1beec86@redhat.com> MIME-Version: 1.0 In-Reply-To: <02a057a3-3d9e-4013-8762-25ceb1beec86@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D26F4180009 X-Stat-Signature: 8o4me79zee196ep1qhaqnw7omojcy9og X-HE-Tag: 1687278240-738875 X-HE-Meta: U2FsdGVkX19TvVAXOYtPC7qRFOTDdcBhiUzQX4DSseuS7BVjHWY3x264g4sU+YjP6iY+BtEgJrnHk657zxphk/tTeRuC+wGObtBXo7s8xahZDaPcA4Jg+iPOv5LOFcT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9WrWKHBAUzckLSvzWSpt9tnyWXTMHLUzaHQFRF3wvmnxJzamfF6dutowcVDgCNc2ArBccHtNLEgR3XI9ObocfygSlvyHfqoDmZAvA2ZE//tWF6cpk3Ih/24+QDxI4+4z4S+BNGJJQN+4GZHQCxlQNlDmwn4QI7HESH6FwXr8ukg1gPsw21viEP1aGsznPeq5ITo8gMs6L9EwWI9OT8UB/YHSk2PiNmz0g== 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: On Tue, Jun 20, 2023 at 05:43:35PM +0200, David Hildenbrand wrote: > On 20.06.23 01:10, Peter Xu wrote: > > 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. > > In the past we had these sub-page refcounts for THP. My best guess (and I > didn't check if that was still the case in 2013) would be that it was > simpler regarding refcount handling to to do it one-subpage at a time. > > But I might be just wrong. > > > > > 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%) > > > > 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 4a00d609033e..b50272012e49 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; > > Doe checkpatch like pages[i+j]? I'd have used spaces around the +. Can do. > > > + 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; > > > So, we did the first try_grab_folio() while our page was PMD-mapped udner > the PT lock and we had sufficient permissions (e.g., mapped writable, no > unsharing required). With FOLL_PIN, we incremented the pincount. > > > I was wondering if something could have happened ever since we unlocked the > PT table lock and possibly PTE-mapped the THP. ... but as it's already > pinned, it cannot get shared during fork() [will stay exclusive]. > > So we can just take additional pins on that folio. > > > LGTM, although I do like the GUP-fast way of recording+ref'ing it at a > central place (see gup_huge_pmd() with record_subpages() and friends), not > after the effects. My read on this is follow_page_mask() is also used in follow page, which does not need page*. No strong opinion here. Maybe we leave this as a follow up even if it can be justified? This patch is probably still the smallest (and still clean) change to speed this whole thing up over either thp or hugetlb. -- Peter Xu