From patchwork Mon May 15 17:26:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanchu Xie X-Patchwork-Id: 13241871 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 172C1C7EE23 for ; Mon, 15 May 2023 17:26:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8B44900003; Mon, 15 May 2023 13:26:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3C10900002; Mon, 15 May 2023 13:26:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 903D6900003; Mon, 15 May 2023 13:26:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7F8E2900002 for ; Mon, 15 May 2023 13:26:27 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 231BB1203B7 for ; Mon, 15 May 2023 17:26:27 +0000 (UTC) X-FDA: 80793168414.14.CDE12BF Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 754C040005 for ; Mon, 15 May 2023 17:26:25 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=lnrHEXI2; spf=pass (imf04.hostedemail.com: domain of 3QGtiZAcKCMkD9p2rw9v33v0t.r310x29C-11zAprz.36v@flex--yuanchu.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3QGtiZAcKCMkD9p2rw9v33v0t.r310x29C-11zAprz.36v@flex--yuanchu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684171585; 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:in-reply-to: references:dkim-signature; bh=y97BQ7sR2LBYQN45EWEO4FJ6RSEUfFrbQxSXtC1eEEg=; b=oioZ9lWFPooSZT+lsjlu/q4Hk9KQrT7KiNao2J4UeEKkF+HtQFFBPiBY4nNxLKeCIiWW5U taoh37/sFWCgk0U+WiSdoOtH8bO6No5KScsaIF0vHxRL/xnJ4I0hg5VdGrY3VgkA/WyYm4 F/BIOqcezj6r8IFK+nB1mIfx2w3+aYQ= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=lnrHEXI2; spf=pass (imf04.hostedemail.com: domain of 3QGtiZAcKCMkD9p2rw9v33v0t.r310x29C-11zAprz.36v@flex--yuanchu.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3QGtiZAcKCMkD9p2rw9v33v0t.r310x29C-11zAprz.36v@flex--yuanchu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684171585; a=rsa-sha256; cv=none; b=EVo7wDI6oxq0iw8jg8a/Yl2vA23vNyKAOk/bSUk0RY0FzaaeZG5zJyQuEAtq4JKQ1ZcJDM Hi7PQSUIe+DT3LCsoip3w+CnhHHUGh2nAbKVXzM8zyVHP8UsbeBZHFxYmDKStsnpBQRIsm UcDTxPhTMvsKd4NExbAWUzxtDSgcbxo= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-b9a7d92d0f7so23827342276.1 for ; Mon, 15 May 2023 10:26:25 -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=D6Hr05GcMdi+wpK3SW9Iqc6RdCuAu58OEMLjciTc/qqG2kBK63CA9Utp5UK48JKgON aJ6FxnU+3N7xwE2HtONI6sf02pns1alMXDDrmPA9o320Ix8iKvs9PFBBd2a+hOXTT0B/ 2zNoOlNVkgA8bIRQKramY9qZWmvvnncyF2H0QRkcrDrzDE0txkzWuP+9HyYvhotJVehg EIhk+jBRM3zX+D0om8GkcDiAJORl/JE2LJiOnYhm7AADVE9nCL4iFU9te2qBZwX7TRZc 675PcSAkWgCvdZciZDNwKXvFqcvJuVXIGBqUwcv5d7PxrKgGcuadIsstmbvXa5mKQFO8 kyqg== X-Gm-Message-State: AC+VfDy1FmNBOUNLiZBX0tIVCPeXPjUevGA0p4TlKdVOlbPFQ+lw5Y6u 1+ZNVrRFJbY+OkdGS6Y6miLSvn23GnHF X-Google-Smtp-Source: ACHHUZ5TeR03DncbFn2MDJZP9KzUWVPcJVpfEqM7CUznGNTSHd5/NHOxIZ8GKK9NiQzFZ/fPDVHY/iZDWiai 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 X-Stat-Signature: j1aysw1he3xtdcd3h5bjntnztqddrp8m X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 754C040005 X-Rspam-User: X-HE-Tag: 1684171585-509872 X-HE-Meta: U2FsdGVkX19UaqBZGXP/FPdt/CnC8iCTHK1UIy1GRCTyVbrHyxX+shPV5642Egu9YK7OxgzXHz5ON7sqRzOkxsl9CKgyS1HSxbRqeJ9ly0EyaSXpI3zpqHWKj/6UREjeSKYeJPrYYWWX7GTtlm8o3E6F1CBkU7cR8xQ3HPM3bfWxfJSVeVJa8/EcpjNMLlZPQA+uDg7/4mIHxjeGTlxc5HjnhY90ClFNPioaGzI3X6PS1I0wjLuOFFt7ocpQM7j4a0X2WQMYdKPyMndYgetsQtx2o3deTMAevv73C0XWsOogM1hZRUAQGMzgkImGnJ08lLpocMTUqg0q3fJC2gkLOnqkirHnsZLLikCfT5/AFplal/5H0+P9mL0/CozPFQoBaQGz74SDQLa1g0Uw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tiC9u6Oblho/boonTI8KeUVi0phDpo8XiNmWONYp2SwWPFeTPPBpvd25DRmL3BrJLh9wLauADFbOcY8sLvuyt9oS2iI3XaMUJH1W6F7RdsZ6/gnXW3dtoA7cSzySek5idiuMIUUkUOJKjFE4ftBQ/88= 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 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 Acked-by: Peter Xu Acked-by: Mike Rapoport (IBM) Reviewed-by: Yang Shi Acked-by: David Rientjes --- 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;