From patchwork Fri Jun 23 14:29:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13290737 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 0FCD4C0015E for ; Fri, 23 Jun 2023 14:29:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2BD48D0008; Fri, 23 Jun 2023 10:29:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 93FDB8D0001; Fri, 23 Jun 2023 10:29:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CD3E8D0008; Fri, 23 Jun 2023 10:29:52 -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 3C55B8D0001 for ; Fri, 23 Jun 2023 10:29:52 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0E1E8409A5 for ; Fri, 23 Jun 2023 14:29:52 +0000 (UTC) X-FDA: 80934246624.02.78E2FF9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 9152EA001F for ; Fri, 23 Jun 2023 14:29:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dsoLDDaG; spf=pass (imf25.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@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=1687530589; 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=ZmCvEzGFNb0TFjW2MTydxEZnQUnXGwjoOel6yGTr3Bo=; b=pq7GHVKcC3AqOgDKJRj9U0uaIFd4k1BCUz+gB0fZupL4HlbH7qtBtIOBLYrHiOERsdv5ei VcNQg4j2fSHgc+iN13lDtbDpS0S5GEEy04cb0mFEbMdZexQU2WdRTiWnymAT4uubmh5NU1 ELF3HlfMqkXFO+0BYsC+FyO0zQkaljs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687530589; a=rsa-sha256; cv=none; b=kehkO35auq9p+Yno0Ih7pxxDgBsWLxWGMNI/wGesoFuhcmM9G1anUuqYxln972Egz8Io3V c2pgaFQERFNLQh6Kre/zPD8N/mvjQ0K+1XACi+wCjDPEUcsXFpVjuip0xCafv0txPVlOSj SM2tRTyeUIgtkrqrnA3E8yDq6yJNB58= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dsoLDDaG; spf=pass (imf25.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687530588; 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=ZmCvEzGFNb0TFjW2MTydxEZnQUnXGwjoOel6yGTr3Bo=; b=dsoLDDaGhwGMVpYiAVRuUvN+lCzxw878Ko3VE7deO1SDo69qCo//F5iWOhdT99mjJ/o1g+ 62M4R9fmQwQ55fRmgb91Elv1klwrGjoxqNICh/e44Q0S2GpUeFv6yMeX4m1v1d3Zg5lMaG UtljpoL0fRH/ZrtCKO36JWQtuT/2vF0= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-567-RjX9wvPHODGS26OFN69kAg-1; Fri, 23 Jun 2023 10:29:47 -0400 X-MC-Unique: RjX9wvPHODGS26OFN69kAg-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-62fea7a5de9so1325286d6.0 for ; Fri, 23 Jun 2023 07:29:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687530586; x=1690122586; 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=ZmCvEzGFNb0TFjW2MTydxEZnQUnXGwjoOel6yGTr3Bo=; b=jyi85oroTxWF0tbaEVRBb5UhVnxP8vyBR4yblVM7/qg7z/fY5XE5GytXvIqwMtKcTo +dJndyDhkA9v2FEZCm96eMd19KEhf0f2cUzIhvSloY/3W4BcYPn/YJJZ2OGa4ikQ6ql0 R+6DgMX7zuf6Ierj7YNxJA8FTE8DO3VhorRd8LFpKTH5UdtjFpl/3lI7nGBNAgt+aY9E N6PGRexN6D1/baRQR5Xm3HsdHN1H1RDfbIyHxIgYca4OwKdcrpQtL5cyLK49xi9CDwW3 ljzdOtrfH6QLmZuwb40xlFX3h9ynGiYUkR6MuSLZOd3TC5iYTvzDYfAFAOqbdV5w/3Y+ LDWg== X-Gm-Message-State: AC+VfDwx5KsX+4NxgcgmI4sS687cQmjZDUI4bfL115noLaY6F+TqDkJo iuB/RADm/isbkH23c6V35P3nqudhj6hdJTBpsjwmupLWOTkQymLj7ZkzWNFQsbJcx9HA+hxE/Z2 eKDwSDvaQNnVQsxy4/k9RRx+I41A4Q4ygx6SNC/jfdg9zuuUr6E+tNj+PrvDwncS6JgRV X-Received: by 2002:a05:6214:300f:b0:625:86ed:8ab4 with SMTP id ke15-20020a056214300f00b0062586ed8ab4mr4610266qvb.3.1687530586680; Fri, 23 Jun 2023 07:29:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6oG/I/D+2ou09cQcTlfX+z4F1YPxjXAbS6ijyg5lFwMxdYPO8Sr/TxyEq0M2ORmBAvTkxKmg== X-Received: by 2002:a05:6214:300f:b0:625:86ed:8ab4 with SMTP id ke15-20020a056214300f00b0062586ed8ab4mr4610231qvb.3.1687530586290; Fri, 23 Jun 2023 07:29:46 -0700 (PDT) Received: from x1n.. (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id b9-20020a0cc989000000b0062821057ac7sm5104827qvk.39.2023.06.23.07.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:29:46 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Lorenzo Stoakes , John Hubbard , Andrew Morton , Mike Rapoport , David Hildenbrand , peterx@redhat.com, Yang Shi , Andrea Arcangeli , Vlastimil Babka , "Kirill A . Shutemov" , James Houghton , Matthew Wilcox , Mike Kravetz , Hugh Dickins , Jason Gunthorpe Subject: [PATCH v3 5/8] mm/gup: Accelerate thp gup even for "pages != NULL" Date: Fri, 23 Jun 2023 10:29:33 -0400 Message-Id: <20230623142936.268456-6-peterx@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230623142936.268456-1-peterx@redhat.com> References: <20230623142936.268456-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 9152EA001F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 736se3i9s84ipfbo4rwwum95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uvf9M2gAr1wo2uqUsEMvmUUtBwxSeUZNhDv2yLQN4iAETkXo8+PD09cJCbbGnjY3oGgZ2BT+rvkyFtDfWXEKsFnFvLKJ3EodhKTtkZ+had9ESjg0AcnRkDDcqXfA5+61SttuWhE4iXO5l0bqDLzfNtTnn8d5IAc/T/AHvfq73BW5HeV/3Hi6/C+2Gtv1eS9v/GKz50mD6e4AISKxRyYYymB1SgABWrDwNdQa7HPy76JqOejah746mArfdtURhx0laKzxGbc+noWJG/4HQrxp3SVxO00HO/HzXJ8FQ0HGEtjAw3KmAvoj5u/mY7w5NEsRKD3kj9GvZNijwARd/rYYiEX3AgbT/oqIfFRet8d58S/BXahwHhPxFuluMaf5nrFkZjhiM9REp0Q= 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: 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 4a00d609033e..22e32cff9ac7 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;