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 011F8EB64D8 for ; Tue, 20 Jun 2023 18:03:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B2988D0003; Tue, 20 Jun 2023 14:03:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8629C8D0001; Tue, 20 Jun 2023 14:03:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72B218D0003; Tue, 20 Jun 2023 14:03:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 671D58D0001 for ; Tue, 20 Jun 2023 14:03:53 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 01DD61A027A for ; Tue, 20 Jun 2023 18:03:52 +0000 (UTC) X-FDA: 80923899546.04.45FE841 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 23A5120064 for ; Tue, 20 Jun 2023 18:02:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KXwkCbsU; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687284167; a=rsa-sha256; cv=none; b=Qcj5zegftBHZSmeEaqhG4FgTgREA6NLA0BYrKvEbokvrIAQaBs+hwqbQayl4c+ASNlIXdz mKG4a5p73iuA2InBdBv2UNG7SQEosxPwPgD3EUqEFr5EHjeOKyigwgCj2yXkNyvYGl60eE LjeKB8UN+kzEn48HfutLA3AQ3uHPB34= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KXwkCbsU; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687284167; 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=GRm4yc4qMtunLBzXmR/1ejUxdVZnW/vHIX1yfFUZTks=; b=QxvZN3Ol9Oil3+j9FoamuJNGN0nZyIxbRpm+rZ4tYvFNrDxnYrheJhBL33ZtY6rPhGscUT 6yvgaLlg9SDcUsPDMZOKqZK8m0i2QCWQhzgBsi90wsy3yr9EjTTNwP01JaccfsyduunZli S97ioyYllD6QfODV/oIQ5YREU3cW300= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687284166; 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=GRm4yc4qMtunLBzXmR/1ejUxdVZnW/vHIX1yfFUZTks=; b=KXwkCbsUyn/iqfYiUu1wKpnQ3onGPe5e7izPIfhyl9OidM/9Gb+giF5z0ECKyLZxb5xyyi Iq2yILmCTWhQ2Tdc81VHb6e9QAz0NKZCIg6pha3uWSHCN9Iw9XM4my3ss3tsl3nvCRKg1e BM8NuvHAWL3iQ2EiA1Mh83RODp5tbYU= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-453-HL_OOkAtPnu1BUmgfEn5qw-1; Tue, 20 Jun 2023 14:02:45 -0400 X-MC-Unique: HL_OOkAtPnu1BUmgfEn5qw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f9b003507bso11639625e9.0 for ; Tue, 20 Jun 2023 11:02:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687284163; x=1689876163; 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=GRm4yc4qMtunLBzXmR/1ejUxdVZnW/vHIX1yfFUZTks=; b=ZH8y16C4DdQUHYcbHRSqark43IOXfkNmOWV+ACnkKcwNsIh1EpSJnsYVj9iDWEqQqO uwEffU5T+MWUbM0Qguz2OviHYrPVFAe4UpzQ+jP3a39hzcxqttaBQb6g4NL3aREX2i0F tp3CX57QmNA97mARJWXrtjunMFDVXvPQi6/UgVh6Bz2QWxaiJbPyx3Pc6+fmhdSevN0G hwOr0KJEZtIhCMMZO3WzDFCkinGe5BGCSTeJ4rRSZ4p+P2s00hhGvjvhtEcgk5xCj+hj OsR3UtSLuX4hyBa5raOZ+cx5J6d3YG2Vq3JlE7P/MuSe07DOKFUim+W4T52xqxger4s6 tB3Q== X-Gm-Message-State: AC+VfDz87oStPyuz+Y/Txq5UT0SUYJnxQu2JQJLEo+qa1qbx6qXPdi3a ZwV4hVfmtoPHXxl3eTYSZlbPGvdsXZS6ORFyfZ3tOXmkudcmOGejWdVz4SgUVs5frXPmrHTORuX 2ylzeZiiAUZM= X-Received: by 2002:a05:600c:ad4:b0:3f9:b8b8:20df with SMTP id c20-20020a05600c0ad400b003f9b8b820dfmr1677421wmr.33.1687284162968; Tue, 20 Jun 2023 11:02:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ61L1i5+eB0b2iW8qyt23nne+c+R+ficSGQ1kzLD8tgpFdbtBQFblzWO68kvfr8ugu5ddb5eg== X-Received: by 2002:a05:600c:ad4:b0:3f9:b8b8:20df with SMTP id c20-20020a05600c0ad400b003f9b8b820dfmr1677403wmr.33.1687284162551; Tue, 20 Jun 2023 11:02:42 -0700 (PDT) Received: from ?IPV6:2003:cb:c739:d200:8745:c520:8bf6:b587? (p200300cbc739d2008745c5208bf6b587.dip0.t-ipconnect.de. [2003:cb:c739:d200:8745:c520:8bf6:b587]) by smtp.gmail.com with ESMTPSA id h9-20020a5d4fc9000000b0031134bcdacdsm2487036wrw.42.2023.06.20.11.02.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Jun 2023 11:02:42 -0700 (PDT) Message-ID: Date: Tue, 20 Jun 2023 20:02:41 +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 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 References: <20230619231044.112894-1-peterx@redhat.com> <20230619231044.112894-6-peterx@redhat.com> <02a057a3-3d9e-4013-8762-25ceb1beec86@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: 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 23A5120064 X-Stat-Signature: b1bx8mujau61e74h3nhg4s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ZLiE+Qzh5qPwQAg679O6Z39kGLPtf2stYarpUptWCScE0dQZT0uS4wEs43cP/TdNJYbC79Riqo7/iPThaovE/Cr3Mn88rtOg39J+2Mvz8ny9gu4uwfVd/x/Uqbb+1hxJtfzKwxIlgR94vI8R1NiIhgZq6953taJw3ngVC11HzlaPPGenNLTUX4scKH7+dLF1JuJsYUQPG/h3qNpQVkM9GzMQy5oce323Azo9BHVOEudgDvYgOCatZS1/mE0oFrx9sqIulpCBcD6BlHz+o72mLi/z1PoKZfZ4gKW1pXxOjVHA29VM0yoseBJxS73lJdCvUlOdrNCx7AfCzX3YE31GbCrYAmau5L3WZLSQNCg4Rmzl3W388w2ICG92RdELupV5IVr6Ka1YZMg= 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 18:23, Peter Xu wrote: > 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*. Right ... maybe one day we can do that "better". > > 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. Sure, we can leave that as a follow-up. Thinking about why we have the flush_anon_page/flush_dcache_page stuff here and not in GUP-fast ... I suspect that all GUP-fast archs don't need that stuff. I was wondering if there are some possible races with the flush_anon_page() / flush_dcache_page() on a page that might have been unmapped in the meantime (as we dropped the PT lock ...). Some flush_dcache_page() implementations do some IMHO confusing page_mapcount() things (like in arch/arc/mm/cache.c). But maybe the unmap code handles that as well ... and most likely these archs don't support THP. Anyhow, just a note that the flush_anon_page/flush_dcache_page left me confused. -- Cheers, David / dhildenb