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 92D80EB64DC for ; Tue, 20 Jun 2023 15:43:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25A028D0002; Tue, 20 Jun 2023 11:43:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 209B28D0001; Tue, 20 Jun 2023 11:43:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 084048D0002; Tue, 20 Jun 2023 11:43:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EA0518D0001 for ; Tue, 20 Jun 2023 11:43:47 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BECC7A0811 for ; Tue, 20 Jun 2023 15:43:47 +0000 (UTC) X-FDA: 80923546494.10.9371C85 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 693C28000E for ; Tue, 20 Jun 2023 15:43:44 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IRi5cizn; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687275824; 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=7/6q4gTkvc30OYeDjeAdNcm26xZpxhhkMzm5gksN37U=; b=LL9i31uAfBPhnsE+0pbfCOyKews/gJNsUJZYZTfoOr8Ae1IwIMFsDttwIigAj7XTi4cGVO MFR5ESpBleE5pKcHqKclwK7cXKcDt+5V/Tl6wmxy3JCEKsLHYqhQh/clNkzgg+ORhYLZLu 54gWBjEBiPeu/pJun2QJCk+zPChSGQs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IRi5cizn; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687275824; a=rsa-sha256; cv=none; b=JmYvUqwqaRWMw+PJmhM/o4HlrqzQsfwgA5x2++g3cSUkA4DCt1DVxnPDjOzLpTha9QqPiF WRmjOyXDMaSDe0Z4yQtKKCUl/chdROtJnjdRs2Snbk/6lg8wbUcN0krmWgyy4mG9wwryEU NicDJNHu/l/NsZ/NDRTbueIl1EuykDs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687275823; 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=7/6q4gTkvc30OYeDjeAdNcm26xZpxhhkMzm5gksN37U=; b=IRi5cizngGAzF+T4CoYRmwpJeT/F37w5986wzbnjZRc8OAVLBx0/kp8n0U3/FuvHQu6teK qCHWB2149BbQ68h3bpr/ytAIt1G1q0Nlfg3CsE2H3Tk82Pv42wDAJES++i+oSgxfKu5Gjn +tjrO/iqRspnw0O6M1GvyJL8y/nx18k= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-PP0RzZjtPD6tBv9deCSYoQ-1; Tue, 20 Jun 2023 11:43:40 -0400 X-MC-Unique: PP0RzZjtPD6tBv9deCSYoQ-1 Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-2b44370b6a1so37907491fa.3 for ; Tue, 20 Jun 2023 08:43:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687275817; x=1689867817; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7/6q4gTkvc30OYeDjeAdNcm26xZpxhhkMzm5gksN37U=; b=cNJQB30x6p8voLCep6RO0N3GEoTxtFbbLl1KNwlqx0ZcWhsxkOzzt3rdGtRdYo/IDK ProzzitXdZUzjuhO3Q/XQP3O1/172CxhNvgYPyBDZSWR9qdHiefwRNNCHoT4EAY1UzJ/ /2mCqS3Dvk150Kx4RKdZX+hZ2K7MXjk4HAW/6eQi+Yct+eeDBH8HbHMQSK0ONwcB2pZa u6mRIcxEA1uh0tRPUJgHPYcVKw7DwGv27G8MXmqzQzH7TQbQi5uF5FtgTZYX5LKkLpSZ SrSN3ECs/M0v8ZVBcnu72+IaMWJpo8A7DvqsNWjPQUutnE9GsOZ6z+WLDjQ1SKQggKbo 57Uw== X-Gm-Message-State: AC+VfDyfSJH0yAo3rM/f1sp1s8/yLmhXCEsk8IH7k7J6RVxPZ1tQoP08 h0RY8Fgu99yqzOVT6LyXWP+q+Ol9ADLgQuiDA/O4/UJXG9tpPnwZrvRyRXWF/n3nhxc7dAtb13V HsmpeTB4xWu8= X-Received: by 2002:a19:ca01:0:b0:4f8:56cd:da8c with SMTP id a1-20020a19ca01000000b004f856cdda8cmr6673078lfg.34.1687275817180; Tue, 20 Jun 2023 08:43:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6w7hOiteiIn8xI/CmrWnZtgYHpyBwP1uBoTYnp+qxJ1Ld6Y72h5hSEc+v2kSqdr2tsSNZZ8w== X-Received: by 2002:a19:ca01:0:b0:4f8:56cd:da8c with SMTP id a1-20020a19ca01000000b004f856cdda8cmr6673052lfg.34.1687275816805; Tue, 20 Jun 2023 08:43:36 -0700 (PDT) Received: from [192.168.3.108] (p4ff23bc9.dip0.t-ipconnect.de. [79.242.59.201]) by smtp.gmail.com with ESMTPSA id m7-20020a7bca47000000b003f90b9b2c31sm7728390wml.28.2023.06.20.08.43.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Jun 2023 08:43:36 -0700 (PDT) Message-ID: <02a057a3-3d9e-4013-8762-25ceb1beec86@redhat.com> Date: Tue, 20 Jun 2023 17:43:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 To: Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: 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 References: <20230619231044.112894-1-peterx@redhat.com> <20230619231044.112894-6-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 5/8] mm/gup: Accelerate thp gup even for "pages != NULL" In-Reply-To: <20230619231044.112894-6-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 693C28000E X-Rspam-User: X-Stat-Signature: 5hiox8n5rhnktzoyk13q9cp7siaii1e7 X-Rspamd-Server: rspam01 X-HE-Tag: 1687275824-9446 X-HE-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C66Cj3Wc7lyfVlMdg5YbTz8BjEqzfXCq7eDxv04STZGXpyNOhvY1nETw00lHZdTYzFOaji/5KBFbTURHA2DOIhwYFU5yiIBgJU5GqiN67nprZEcmtkxTAh6pOaVYU5FOcmba7V0DiILq+jjdFTiYyeuSIFy4DQLdccXqmc/zCTUH95rws2R8IeO25P6bBDsalSURSqb5IoSdscdHsYX1S11t7zO0MWLtotIcmdv8dMTjICkUPBdV9wJ4b5DooC/wd9JwGljR3KG4gQt3PHm1jUIJt19yZ3eiPnfWtGCVk0ZBjngyB7AuzYnYur5L046QwB99Lw4fDdbIKMU2DS9cKIhPj8oCPYYnUq2ErelkV+Bs3by0VG5q6mJ3sCzySxDD9nUEWpdL/52g== 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 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 +. > + 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. -- Cheers, David / dhildenb