Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6811395rwd; Mon, 19 Jun 2023 12:49:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ56LW3N1D3Gxehtc9zEsT/fDEBxd70DIg0MH7TpX6JhEZXY/nkltlQME0bDXZrX7g7RjqKu X-Received: by 2002:a17:903:22c4:b0:1b0:6e1:4b9f with SMTP id y4-20020a17090322c400b001b006e14b9fmr1617921plg.43.1687204146193; Mon, 19 Jun 2023 12:49:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687204146; cv=none; d=google.com; s=arc-20160816; b=asa40jevpKpLoq/Wk6wRbPPCE0ADfZRNmDfRCpakMx13vyeCG0nV/iDuNwnNo7hEDy si2ihJssLxpCgeWb4mgcbmveSqEonQnmMemX+AyqiV4FjK2tDjQ9vyXPUnK6dYfwEL9I xfPrTptDjTD9A8lqv5gN6zUT3NiRknGZk8QHW7owH3tQHH6UY97v2QbT0kv4f7E/dCS4 7JjhI1+RLS2e0WLr6GcqyUhHY0mCKPhUl4XmXgrPuztVSADV6+VOpr8/I4po+HyxI/u9 fg8ekNnjTvXG3LMOJ1SDSbaZ4j09pGLb3HdPYTDa/Ta0YcWkiVnrcbdd2V9f6+QOruIy eDIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tl6PHbc+VyYybGOhiv2rCh7kHt25l23EfmA39lRewBs=; b=MPKh5CiQvkLzeVbkXcA/GB5ReRXN5/8eYMG6BbwZt3GlLdcGqKASI+enCBrVZFVjLf d8V1PFZbT58wp8Lk9wYh0uiDAhnVXzm58wzQPwbthGVVaw9ixtJHHz5UswF5+Rfzk0h/ RBh8OcnAbdB+9Yt5z8wUVawHYWPHHcKvoGNYlyPM+FQJ+eM/n4x1/QpQ+yz8nwFc/QE6 fnzeVT5h6Am0DuvKfvaAQjafZMHJUKrIecYtJD2/w6+hpXyZHzy3ZBz4UJCrznoyZuD6 65DT+JgxDkGsoteKr56sd1ZAHNXSyxBZ4I6Fvy+vyd2d1z4lOofeMTDK32txDmZESA32 82NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QX7U9x2b; 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 u12-20020a17090341cc00b001ac6a6f2dd3si383755ple.555.2023.06.19.12.48.52; Mon, 19 Jun 2023 12:49:06 -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=QX7U9x2b; 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 S230038AbjFSTiY (ORCPT + 99 others); Mon, 19 Jun 2023 15:38:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbjFSTiX (ORCPT ); Mon, 19 Jun 2023 15:38:23 -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 1B0D0106 for ; Mon, 19 Jun 2023 12:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687203457; 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=tl6PHbc+VyYybGOhiv2rCh7kHt25l23EfmA39lRewBs=; b=QX7U9x2bpKEp6A38t/CIUmQvut0vGIo/wqY6KKMd3yzC6c4j9NzA8uIgaMexN+Yo17icCw P8KlpMFNnRKwUZ9gILIkZzfge0H1+kRNqDwRUCL8BrWtJn9b1jucdAj6bxwIN9u0UsHb2z 41qJwG/wBKbnh9bWHELXibW7b3EVNk4= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-146-2dqP3yp2N5G_baN2X2EUiA-1; Mon, 19 Jun 2023 15:37:36 -0400 X-MC-Unique: 2dqP3yp2N5G_baN2X2EUiA-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7606a134623so60933685a.1 for ; Mon, 19 Jun 2023 12:37:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687203455; x=1689795455; 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=tl6PHbc+VyYybGOhiv2rCh7kHt25l23EfmA39lRewBs=; b=DrYRUksMIyNRXkmoiO/tNOrs9dl2IOS63TKyejHEf1kHCvaLZH82650ekJszFRXvP2 riHvEOJ404PaApGMzzqrDgKcfQcqt8FRKifsNUBVl9PKlQL/XE3ZDf89pLE1EXO4ERD3 jxZKGxwqbRf/+RDHsoxCXfxX5D5vBUx8a5xKoqLyaB+cIxymATVt1mDOnFbH37G+O5tC z2kFpOObRAQiQ6uk6DZXmCsZ8T/u4DsGb/Ut5F1NvzVWbowbPlSPA+Mg4woRX4EIq5+B HRHnoo6Xd1SwNksXQ3DTMBYnU3L/NaI/1Lkq5QLp0jnsB+iSvHhZ4ebOpl7AEtYXZmXK yQcQ== X-Gm-Message-State: AC+VfDwAaop29cyiDjQV82ID4YJhuq98Nvz9g64TD0LzR8l0zfMdyJ87 dwWY2UF8pwkKPZk5PVBno4LZNaVUCLuqkZHSe6Z+heFofmkRuP19UPVS+3KcoimTB7DS4Lx8vHT f8gK+OnglOO0ByPuYTFpP6oY5nqJ3HxLt X-Received: by 2002:a05:6214:5298:b0:62f:1283:6185 with SMTP id kj24-20020a056214529800b0062f12836185mr13121859qvb.2.1687203455489; Mon, 19 Jun 2023 12:37:35 -0700 (PDT) X-Received: by 2002:a05:6214:5298:b0:62f:1283:6185 with SMTP id kj24-20020a056214529800b0062f12836185mr13121850qvb.2.1687203455193; Mon, 19 Jun 2023 12:37:35 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id f19-20020a0cf3d3000000b005fdbcab3065sm280254qvm.72.2023.06.19.12.37.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 12:37:34 -0700 (PDT) Date: Mon, 19 Jun 2023 15:37:30 -0400 From: Peter Xu To: Lorenzo Stoakes Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Matthew Wilcox , Andrea Arcangeli , John Hubbard , Mike Rapoport , David Hildenbrand , Vlastimil Babka , "Kirill A . Shutemov" , Andrew Morton , Mike Kravetz , James Houghton , Hugh Dickins Subject: Re: [PATCH 6/7] mm/gup: Accelerate thp gup even for "pages != NULL" Message-ID: References: <20230613215346.1022773-1-peterx@redhat.com> <20230613215346.1022773-7-peterx@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: 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 On Sat, Jun 17, 2023 at 09:27:22PM +0100, Lorenzo Stoakes wrote: > On Tue, Jun 13, 2023 at 05:53:45PM -0400, 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. > > > > 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 | 36 +++++++++++++++++++++++++++++------- > > 1 file changed, 29 insertions(+), 7 deletions(-) > > > > diff --git a/mm/gup.c b/mm/gup.c > > index a2d1b3c4b104..cdabc8ea783b 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -1210,16 +1210,38 @@ 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. > > + * Since we already hold refcount on the head page, > > + * it should never fail. > > + * > > + * NOTE: here the page may not be the head page > > + * e.g. when start addr is not thp-size aligned. > > + */ > > + if (page_increm > 1) > > + WARN_ON_ONCE( > > + try_grab_folio(compound_head(page), > > + page_increm - 1, > > + foll_flags) == NULL); > > I'm not sure this should be warning but otherwise ignoring this returning > NULL? This feels like a case that could come up in realtiy, > e.g. folio_ref_try_add_rcu() fails, or !folio_is_longterm_pinnable(). Note that we hold already at least 1 refcount on the folio (also mentioned in the comment above this chunk of code), so both folio_ref_try_add_rcu() and folio_is_longterm_pinnable() should already have been called on the same folio and passed. If it will fail it should have already, afaict. I still don't see how that would trigger if the refcount won't overflow. Here what I can do is still guard this try_grab_folio() and fail the GUP if for any reason it failed. Perhaps then it means I'll also keep that one untouched in hugetlb_follow_page_mask() too. But I suppose keeping the WARN_ON_ONCE() seems still proper. Thanks, -- Peter Xu