Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1002920rwd; Tue, 16 May 2023 10:20:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ65oedOUB3IKpuv6k0G85QWpBFLbzcwVLmFAFNl2ufjz5o1XYGA3IXUh2hiI3loqxfzc1K1 X-Received: by 2002:a05:6a00:21c9:b0:645:1081:98ec with SMTP id t9-20020a056a0021c900b00645108198ecmr43727444pfj.13.1684257651212; Tue, 16 May 2023 10:20:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684257651; cv=none; d=google.com; s=arc-20160816; b=SmGL0RNg8UQjT9x3q2cZZT/AhpojMoYPozC5ESN527TrNpgaHmKnBHRwPi2PqNVN7K +SNkRcWsqMy9iloYF8ffWT3xhe0SG6ofUW/C3dXtiz9HxjmBEyiah76REdgiSUI+oD9y 7YCxfepVBnePQUJetSuHZTjf3XzOiJr0UpnylVwn+RUuBjh/B5c+kjIvCTV6lszgsTR3 Op/GXSC4+AjxEuPXFO2dRBCp6rTKAdkY1j6aspgyjIwad3avgNIe6rf7IrT2YpGW0pcl 1hNfV0u4rWLIHOBKvdbwXn3J6tMOLOADRo35CvxmgF4miw97LNfycwNu2kL4ygIx+qm2 hc9A== 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=MgE20X4RBaYfDUVw67ihMRG3U700BcM/UKdWm0Z+sO8=; b=KxvFTWubqxDYlgU4oCyAfe6epDfqDbWwPDnBb6Hif2HLRD32w34zfmbEITGXSRmg42 XqeScUIPJbtLM3oyV6tk+wrohkfTIzlf/8Q0oWuGkmuzeFl8+ZFLIJewRNK7N/euMQP1 W8v4RGJ3YfUcVlyLhc6/zLI82oPKdnpfbkiYxIe6THYmo2Kw5Zz+R/bPKx1sLoK230AU +lCQhAgR+5BHcoXcNkhg0DPfqHyy3ePld/zrKPB4TGMdriJM2o1EHfWQs+2GSbAkMa+a AyZN0/9TJT9q3BT+TqG7yILCSyksR7+UAc/f3picgih4bByAlZvM/Yz/eafMzFs1JBdB NUng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ecQFqE9V; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d77-20020a621d50000000b006437ddddd2asi19105950pfd.362.2023.05.16.10.20.39; Tue, 16 May 2023 10:20:51 -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=@gmail.com header.s=20221208 header.b=ecQFqE9V; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229717AbjEPRKj (ORCPT + 99 others); Tue, 16 May 2023 13:10:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230517AbjEPRKX (ORCPT ); Tue, 16 May 2023 13:10:23 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE5C45259; Tue, 16 May 2023 10:10:21 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-64ac461af60so5602214b3a.3; Tue, 16 May 2023 10:10:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684257021; x=1686849021; 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=MgE20X4RBaYfDUVw67ihMRG3U700BcM/UKdWm0Z+sO8=; b=ecQFqE9VbqRMJX11Rj3FBmAIHKeEnDkPiaWEuMso0O8JPcgYtjXKOqBhXH/bnv6pAG k9Q+kUiBb/10+BsOy+5JcGb6B4ptKyWDiPAvwfunBh2fV6MLS1vPUQgB1QPC2lI5/u+1 c03iI5SoEueu+msenwplrghr3QlE48XMQOHAZri4jI5z6Hwtd8mJOnwRBIz2Qx7hb03J C9sdwPorooomqVyadmlCgf99tlr4U2C2WAW5pg2QrHqGcIMvQZjkNhrUX0cskI4IoCbJ yVmWwTPcHbxtftrq3ifdT80TPzlnHG28FJ6qp/Z5v44blNikVTDU1hYeKjjXlf57QLXL WoQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684257021; x=1686849021; 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=MgE20X4RBaYfDUVw67ihMRG3U700BcM/UKdWm0Z+sO8=; b=WnxUktnAv14dtMJv4qnZbs19scAp3DXt0POCgfjNPt3wI+2V2fUcCs7PbAeT2CDJhc mKRF7Sm2s4d/0bnSM/gFkjKiWTw5O/uJSKufkKhcKIlhqY8P+/tI6qYK4T+z+/Yk3IEo 6rSv/ZIk0Zxzfqympb1dlTzdV0wHzG4OVBNAcBZ//2clk+deGxy9b4byhf2BS9n4z/oe O/IJs1HVgfft39KTVAj07fukrru1ToelLBt7etr49tVYBscDU446eHV7xAVgIg1gV+me 1ZtWC2AsqoyjIfpN94bD9J1gqb7haSZbgMHg3tE8reO6cA8SfNb1KkazYTNuzX+jJtcl PqUA== X-Gm-Message-State: AC+VfDy4eG6Zg38agW7s2Qjwm+fEW0A4bdZyReAebZlxYzUZdRkETDiA 0Vc033PtSWh4IgLDNmrpgHabQ5LnkgyQIdhIloM= X-Received: by 2002:a05:6a00:10ce:b0:63d:24e4:f9c with SMTP id d14-20020a056a0010ce00b0063d24e40f9cmr47792889pfu.17.1684257021063; Tue, 16 May 2023 10:10:21 -0700 (PDT) MIME-Version: 1.0 References: <20230515172608.3558391-1-yuanchu@google.com> In-Reply-To: <20230515172608.3558391-1-yuanchu@google.com> From: Yang Shi Date: Tue, 16 May 2023 10:10:09 -0700 Message-ID: Subject: Re: [PATCH] mm: pagemap: restrict pagewalk to the requested range To: Yuanchu Xie Cc: Andrew Morton , "Liam R . Howlett" , "Zach O'Keefe" , Peter Xu , "Kirill A . Shutemov" , Matthew Wilcox , Pasha Tatashin , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, 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 Mon, May 15, 2023 at 10:26=E2=80=AFAM Yuanchu Xie w= rote: > > The pagewalk in pagemap_read reads one PTE past the end of the requested > range, and stops when the buffer runs out of space. While it produces > the right result, the extra read is unnecessary and less performant. > > I timed the following command before and after this patch: > dd count=3D100000 if=3D/proc/self/pagemap of=3D/dev/null > The results are consistently within 0.001s across 5 runs. > > Before: > 100000+0 records in > 100000+0 records out > 51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s > > real 0m0.078s > user 0m0.012s > sys 0m0.065s > > After: > 100000+0 records in > 100000+0 records out > 51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s > > real 0m0.050s > user 0m0.011s > sys 0m0.039s > > Signed-off-by: Yuanchu Xie Reviewed-by: Yang Shi > --- > fs/proc/task_mmu.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index 420510f6a545..6259dd432eeb 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -1689,23 +1689,23 @@ static ssize_t pagemap_read(struct file *file, ch= ar __user *buf, > /* watch out for wraparound */ > start_vaddr =3D end_vaddr; > if (svpfn <=3D (ULONG_MAX >> PAGE_SHIFT)) { > + unsigned long end; > + > ret =3D mmap_read_lock_killable(mm); > if (ret) > goto out_free; > start_vaddr =3D untagged_addr_remote(mm, svpfn << PAGE_SH= IFT); > mmap_read_unlock(mm); > + > + end =3D start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_S= HIFT); > + if (end >=3D start_vaddr && end < mm->task_size) > + end_vaddr =3D end; > } > > /* Ensure the address is inside the task */ > if (start_vaddr > mm->task_size) > start_vaddr =3D end_vaddr; > > - /* > - * The odds are that this will stop walking way > - * before end_vaddr, because the length of the > - * user buffer is tracked in "pm", and the walk > - * will stop when we hit the end of the buffer. > - */ > ret =3D 0; > while (count && (start_vaddr < end_vaddr)) { > int len; > -- > 2.40.1.606.ga4b1b128d6-goog >