Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2366822rwd; Mon, 15 May 2023 10:37:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4HMOeLQoREv0sphJztatYXfjdLNXmU3qEBHtMq6HcvxtdYmwiFO5ol4Qn5RpicPaFGPhJp X-Received: by 2002:a17:902:db02:b0:1ad:e633:ee96 with SMTP id m2-20020a170902db0200b001ade633ee96mr14522032plx.55.1684172226072; Mon, 15 May 2023 10:37:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684172226; cv=none; d=google.com; s=arc-20160816; b=EWGjweOD0kLjh0CmPFRHq5vYDwNFKouF1rqIO+EMj/+uaMhG2R1Z5UbljQ35hL8NMt iAIfHJSNaX0KY9y7Du1o9t8ukYqxEcxQ5DZKPJDa8AZ/IA2yROQGrQ5hobBbtbHETNAd vJ8PTPwkAJf7jtgFwTJbRLzXjpTURXoumhUG9cDOFoNdxqlK62BQWasKeIN0ebKDMnZi WzBrt00aJVoKthfVIt12WqxnYQvZB6+BO9IphaJyOpceeZYH0cxq926kxzCGcEJ5gY4B rMASUAZ0sidbhGYb8k2A+4qiQ93+IuaIMXnn+uCnwH0OZD8dowRNLkxrVt8ANIUJC4rd Spmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=y97BQ7sR2LBYQN45EWEO4FJ6RSEUfFrbQxSXtC1eEEg=; b=ozpED6f2dznIzGziWqzX7CcNHk4wQGBWsTuRFdF6BjCQmTCfRRGJkwNEn+W8LOE4z/ qmnUr9I9of+2nDPM0CmqF4CAywz5M4QLqZi88aOnTBXTLr1S8O3n2TKOuaFuLMjqE7Sq QqltJ7MCpCQyKsGf+Eo3tNbiLHPnwp2yiNrAIZ3Vb4NF89DY2y2CNFUuKfQhTL8IbOvc GfIrFZ8CMTYQmU+xneTCNPEVZZoPGorh6gTisUJ0hdx4CbmBDqo/MR8mDKmjjYMXNI5t nPMyYjyLytxCwW+ZlBlwAY6PQs2YDkHUMvYk/ndxYdVYSd0HBGHWW586+DOosutv4qGy xnXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=lnrHEXI2; 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 i11-20020a170902c94b00b001a637615a89si17606309pla.561.2023.05.15.10.36.54; Mon, 15 May 2023 10:37: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=@google.com header.s=20221208 header.b=lnrHEXI2; 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 S244131AbjEOR2v (ORCPT + 99 others); Mon, 15 May 2023 13:28:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244288AbjEOR1l (ORCPT ); Mon, 15 May 2023 13:27:41 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A492214342 for ; Mon, 15 May 2023 10:26:26 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba7831dfe95so4179676276.2 for ; Mon, 15 May 2023 10:26:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684171584; x=1686763584; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=y97BQ7sR2LBYQN45EWEO4FJ6RSEUfFrbQxSXtC1eEEg=; b=lnrHEXI214x0JOHTaG19NvuKmqNZq8vfQFKdpU6IFL5CrGuJIGS18GJfVi7BwsHwLu z/19S9vAZW30pNF09qix5IlZ31ZEXIfoGPrblHX6Enf6KFUoXTTZnWap+ULYaVY8vhtU OkCdhS24qIkQeup6yuRZj7uh7CWNtThe5UGcraCNGfBBC5Hww348MxDxRagpAxcDVTgA AZ7h+zuAXJzQAH2BNFM6Zv0fa/yUNkqKLh+ZXEOhgjyLaqTiXJhjvNH8l0R1RPrqwNd7 0RKoPgChapU4TerGr+ESr+HQzkCOntbXEi2Aj+4c6i7PG4f7i/oYDqbdgA2BE8qDfgLN /7wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684171584; x=1686763584; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=y97BQ7sR2LBYQN45EWEO4FJ6RSEUfFrbQxSXtC1eEEg=; b=CdBiaWx1LxX3ezy+P4iV4RdOeOG7o8BYzmmSVmxcCey7i+JFbiqfwvPOSBffudXmG/ F6BSiiGGVmGZF2e+FY+mI2qHvLISEkE24OJN386WcpXNGf8kWn/69m2Jv599hopJe9yh CTqmS3sQWkdIsICdxHoidpbaRIVlg68jqdIuKqvGKEXXTfmsNgQR16muNWUtCMkpD0R9 n6vFrv3oE+9vddW/J47MZTuiJYxcf93BvWP7DDNxEpfARklKJUuiubm7iDehOaH9y0f/ D1tH9C37zFsPQ66N47lovLFd4PFSRhmznIdGUn3oaaoyfYCgUmjJ+1wLWRVSGASwJOAk Cpcw== X-Gm-Message-State: AC+VfDyS6QIXUKfBZCMOWnjP5lDttwvp+ZVbDY5N9wezD8q+Nt26wICl 9Mc5U9H1OvO/niyoHIamZYoMvJB+Rh/q X-Received: from yuanchu.bej.corp.google.com ([2401:fa00:44:10:166c:6ee8:fb91:4744]) (user=yuanchu job=sendgmr) by 2002:a5b:750:0:b0:ba7:75a8:e37d with SMTP id s16-20020a5b0750000000b00ba775a8e37dmr3199880ybq.4.1684171584365; Mon, 15 May 2023 10:26:24 -0700 (PDT) Date: Tue, 16 May 2023 01:26:08 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Message-ID: <20230515172608.3558391-1-yuanchu@google.com> Subject: [PATCH] mm: pagemap: restrict pagewalk to the requested range From: Yuanchu Xie To: Andrew Morton Cc: "Liam R . Howlett" , Yang Shi , "Zach O'Keefe" , Peter Xu , "Kirill A . Shutemov" , Matthew Wilcox , Pasha Tatashin , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 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=100000 if=/proc/self/pagemap of=/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 --- 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, char __user *buf, /* watch out for wraparound */ start_vaddr = end_vaddr; if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) { + unsigned long end; + ret = mmap_read_lock_killable(mm); if (ret) goto out_free; start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT); mmap_read_unlock(mm); + + end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); + if (end >= start_vaddr && end < mm->task_size) + end_vaddr = end; } /* Ensure the address is inside the task */ if (start_vaddr > mm->task_size) start_vaddr = 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 = 0; while (count && (start_vaddr < end_vaddr)) { int len; -- 2.40.1.606.ga4b1b128d6-goog