Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp3368642rwp; Sat, 15 Jul 2023 00:57:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlFOUg3q/gBmVT1T5th6HrXXrPpcu2yKqwp79FkHpOyhmGo6kQ/83glcDLJBtL3lsuE5SjfL X-Received: by 2002:a05:6402:40d1:b0:521:71ef:996f with SMTP id z17-20020a05640240d100b0052171ef996fmr1565574edb.1.1689407831717; Sat, 15 Jul 2023 00:57:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689407831; cv=none; d=google.com; s=arc-20160816; b=S85IrZhhuk2aBFwNT+89VFolvHYkWyMgZbsGN5DmHL9mezZjUHUaUY5eYR8kIo4FbZ KkTM8YExbL/F1lPwqquLCQpG2eLcrMUkFJTv13tPcA/PJ+weLTBa1h1LIPIF/X5e9gX2 zWoNvkVREQCOkMTapHuHfKaIOP/zs3xIcCs0+mmhgGuQy4S/ncS/P3uJR7PtnZArc1fV 6iG6V/dM/uUrJeG1TneECPrCUIpiqwB2DKc4UASCWzoHcZgCsiPI/pmX5ZFpSLT84FXY 2pswZtoxKeVnubAPfQ0iygXpf8uBovij4wJeKQ9W8zXSwcli0y/WU4hg0yh6eR93mP7K 2VmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=WfySVCw+dpSTsc2B4oZPue0u3zK7+gWWCOiPp0wEcv4=; fh=BprBv6rOoAonkI42LFZZCe0KRTic3hX9pq7p946UTFA=; b=tZ3fDIXiHL/6qdT3yiGRu8/Tr+brGhlBOLg/+uqOrOXNtco2y/nGTUebLl0icU7ncI TUdMC6F5+y2M7ABgjB3qAH7Q7xvAeeZ7Iw1qcDYIl24lEvExWvnbng49x6Y0vkuL1xFt tokkAhTOnmh8x04h5fe7SQj3fxOEOohPURSSd8khcD9Sn/XEWjG7vYlduCM4+Y+OqAdY wYf7lQr+vQ/GaanU+gs1fkLtA1FXzAjtsoe5tyRDfRed1qJzso0kMd0oiM678j0WPvC3 k/fgqq7VM4qR6fZPltGP1u8pjPUtUv5uQzpQ/fRIRPIqhgaeexuFbMC7lOt66NwRDiIu WSpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=FjpzhPEP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r18-20020aa7da12000000b0051ff2b7561fsi4050892eds.140.2023.07.15.00.56.47; Sat, 15 Jul 2023 00:57:11 -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=@google.com header.s=20221208 header.b=FjpzhPEP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229891AbjGOGHS (ORCPT + 99 others); Sat, 15 Jul 2023 02:07:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjGOGHR (ORCPT ); Sat, 15 Jul 2023 02:07:17 -0400 Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B237D30DE for ; Fri, 14 Jul 2023 23:07:15 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id d75a77b69052e-403b622101bso103121cf.1 for ; Fri, 14 Jul 2023 23:07:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689401235; x=1691993235; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=WfySVCw+dpSTsc2B4oZPue0u3zK7+gWWCOiPp0wEcv4=; b=FjpzhPEP83eaD5w6Z6mPAwSFgtQ/CSnpow4tvOH87Li3FnGeF8BbVB7frhWkwJHzCa vehXELES21VPBnrYq/800TmG7L6GNqAtfplXqWCJnGvzuqRBYnyWoOsUM1ED5OWMnJbR n8kWVM66vx6J9HIk9c98Cnf79Ls02VE5TcRUh7UwU6tzIfWUfTQ8GuLuyqmW/rMaXruP ONdPTNsFs5qyhE8vsmV8TRXuOqKP/aEFtbbfwcoGlI8hzM1LgjPITxIgw6SDX+5IofEd i2TUxPkw6izO03Y+4LYq9fUFWDlrSzjnbquk1lEbfRDlfxLMfXzqBV92nBynl8kJ78BS pfaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689401235; x=1691993235; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WfySVCw+dpSTsc2B4oZPue0u3zK7+gWWCOiPp0wEcv4=; b=IxTV2LkX/pzF+ZJe/Jx7RJ0Fb3w/a8t8mhFBd8pvd7N3y9qmwb6yiP/yTa64VApvWc HBAbom3hnw43ULnLH7qvATRuyJvBFzno4ODcAysmrveI2u2FDrbV1uZ/mc0mGnnTKafc gTcujxru0QOoaerri4vVUezfsnRtuUOpu/qnchIrTGKlHh/3si0ElEzQgwofraHJuvrN ESrC2SZaZegeeWUq66HBCvrDLY6u/29pZILY9V28s/LrAw/1Tw2TV10FBSYEpCmq43tb 35cNoLuFFSYPAHUbehTdkk8o07WokMmH4y+huiLxFg2MdO0B57H/+CVHeQ/nuHKV+gAh P/tA== X-Gm-Message-State: ABy/qLZPkYPsbDpm0lbA+fwfeGsCZnbjhbnWw/9iOaZm7bxmRj4/+ZJQ jr9QORySxQIEXcS0KtpVCl4zmqfMgNCc5FTS7yXRZA== X-Received: by 2002:ac8:5814:0:b0:3ef:404a:b291 with SMTP id g20-20020ac85814000000b003ef404ab291mr991086qtg.7.1689401234707; Fri, 14 Jul 2023 23:07:14 -0700 (PDT) MIME-Version: 1.0 References: <20230712060144.3006358-1-fengwei.yin@intel.com> <20230712060144.3006358-4-fengwei.yin@intel.com> In-Reply-To: From: Yu Zhao Date: Sat, 15 Jul 2023 00:06:38 -0600 Message-ID: Subject: Re: [RFC PATCH v2 3/3] mm: mlock: update mlock_pte_range to handle large folio To: Yin Fengwei Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, willy@infradead.org, david@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, Jul 12, 2023 at 12:31=E2=80=AFAM Yu Zhao wrote: > > On Wed, Jul 12, 2023 at 12:02=E2=80=AFAM Yin Fengwei wrote: > > > > Current kernel only lock base size folio during mlock syscall. > > Add large folio support with following rules: > > - Only mlock large folio when it's in VM_LOCKED VMA range > > > > - If there is cow folio, mlock the cow folio as cow folio > > is also in VM_LOCKED VMA range. > > > > - munlock will apply to the large folio which is in VMA range > > or cross the VMA boundary. > > > > The last rule is used to handle the case that the large folio is > > mlocked, later the VMA is split in the middle of large folio > > and this large folio become cross VMA boundary. > > > > Signed-off-by: Yin Fengwei > > --- > > mm/mlock.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++--- > > 1 file changed, 99 insertions(+), 5 deletions(-) > > > > diff --git a/mm/mlock.c b/mm/mlock.c > > index 0a0c996c5c214..f49e079066870 100644 > > --- a/mm/mlock.c > > +++ b/mm/mlock.c > > @@ -305,6 +305,95 @@ void munlock_folio(struct folio *folio) > > local_unlock(&mlock_fbatch.lock); > > } > > > > +static inline bool should_mlock_folio(struct folio *folio, > > + struct vm_area_struct *vma) > > +{ > > + if (vma->vm_flags & VM_LOCKED) > > + return (!folio_test_large(folio) || > > + folio_within_vma(folio, vma)); > > + > > + /* > > + * For unlock, allow munlock large folio which is partially > > + * mapped to VMA. As it's possible that large folio is > > + * mlocked and VMA is split later. > > + * > > + * During memory pressure, such kind of large folio can > > + * be split. And the pages are not in VM_LOCKed VMA > > + * can be reclaimed. > > + */ > > + > > + return true; > > Looks good, or just > > should_mlock_folio() // or whatever name you see fit, can_mlock_folio()? > { > return !(vma->vm_flags & VM_LOCKED) || folio_within_vma(); > } > > > +} > > + > > +static inline unsigned int get_folio_mlock_step(struct folio *folio, > > + pte_t pte, unsigned long addr, unsigned long en= d) > > +{ > > + unsigned int nr; > > + > > + nr =3D folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte); > > + return min_t(unsigned int, nr, (end - addr) >> PAGE_SHIFT); > > +} > > + > > +void mlock_folio_range(struct folio *folio, struct vm_area_struct *vma= , > > + pte_t *pte, unsigned long addr, unsigned int nr) > > +{ > > + struct folio *cow_folio; > > + unsigned int step =3D 1; > > + > > + mlock_folio(folio); > > + if (nr =3D=3D 1) > > + return; > > + > > + for (; nr > 0; pte +=3D step, addr +=3D (step << PAGE_SHIFT), n= r -=3D step) { > > + pte_t ptent; > > + > > + step =3D 1; > > + ptent =3D ptep_get(pte); > > + > > + if (!pte_present(ptent)) > > + continue; > > + > > + cow_folio =3D vm_normal_folio(vma, addr, ptent); > > + if (!cow_folio || cow_folio =3D=3D folio) { > > + continue; > > + } > > + > > + mlock_folio(cow_folio); > > + step =3D get_folio_mlock_step(folio, ptent, > > + addr, addr + (nr << PAGE_SHIFT)); > > + } > > +} > > + > > +void munlock_folio_range(struct folio *folio, struct vm_area_struct *v= ma, > > + pte_t *pte, unsigned long addr, unsigned int nr) > > +{ > > + struct folio *cow_folio; > > + unsigned int step =3D 1; > > + > > + munlock_folio(folio); > > + if (nr =3D=3D 1) > > + return; > > + > > + for (; nr > 0; pte +=3D step, addr +=3D (step << PAGE_SHIFT), n= r -=3D step) { > > + pte_t ptent; > > + > > + step =3D 1; > > + ptent =3D ptep_get(pte); > > + > > + if (!pte_present(ptent)) > > + continue; > > + > > + cow_folio =3D vm_normal_folio(vma, addr, ptent); > > + if (!cow_folio || cow_folio =3D=3D folio) { > > + continue; > > + } > > + > > + munlock_folio(cow_folio); > > + step =3D get_folio_mlock_step(folio, ptent, > > + addr, addr + (nr << PAGE_SHIFT)); > > + } > > +} > > I'll finish the above later. There is a problem here that I didn't have the time to elaborate: we can't mlock() a folio that is within the range but not fully mapped because this folio can be on the deferred split queue. When the split happens, those unmapped folios (not mapped by this vma but are mapped into other vmas) will be stranded on the unevictable lru. For that matter, we can't mlock any large folios that are being shared, unless you want to overengineer it by checking whether all sharing vmas are also mlocked -- mlock is cleared during fork. So the condition for mlocking large anon folios is 1) within range 2) fully mapped 3) not shared (mapcount is 1). The final patch should look like something like this: - if (folio_test_large(folio)) + if (folio_pfn(folio) !=3D pte_pfn(ptent)) + continue; + if (!the_aforementioned_condition()) There is another corner case I forgot to mention: for example, what if a folio spans two (the only two) adjacent mlocked vmas? No need to worry about this since it's not worth optimizing.