From patchwork Fri Sep 17 16:47:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12502779 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7CBFC433EF for ; Fri, 17 Sep 2021 17:03:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5C19B60F70 for ; Fri, 17 Sep 2021 17:03:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5C19B60F70 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 001DE6B0073; Fri, 17 Sep 2021 13:03:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECE0D940008; Fri, 17 Sep 2021 13:03:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6F49940007; Fri, 17 Sep 2021 13:03:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0140.hostedemail.com [216.40.44.140]) by kanga.kvack.org (Postfix) with ESMTP id C56276B0073 for ; Fri, 17 Sep 2021 13:03:48 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 7AC013A8D2 for ; Fri, 17 Sep 2021 17:03:48 +0000 (UTC) X-FDA: 78597687336.27.B2AB132 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf16.hostedemail.com (Postfix) with ESMTP id E6B48F408C5C for ; Fri, 17 Sep 2021 16:48:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631897282; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=grUUg8VAspODuuKM93nZK+6Ve54OZS2pE6wnY0B0Xh4=; b=Jw9XuKahRM6i12XuAhKZTwronY7JciyooHFiG7Revb3ZS7PTEdrYr7h/tM/kWGA0vb+mXl 23Ui6+rQLl0eIY2vv+ZveUc9U8gQRi9H5JWQwsolmy4itkN8csmM0K6yNb7xk2eEeF8BL1 kq2OSr/Ci1Q1dABbPh/yKt2kCsXT/5s= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-153-fmfZcC4GPIqSAsw_0zgAkg-1; Fri, 17 Sep 2021 12:48:01 -0400 X-MC-Unique: fmfZcC4GPIqSAsw_0zgAkg-1 Received: by mail-qt1-f198.google.com with SMTP id d20-20020ac81194000000b002a53ffbd04dso52437282qtj.12 for ; Fri, 17 Sep 2021 09:48:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=grUUg8VAspODuuKM93nZK+6Ve54OZS2pE6wnY0B0Xh4=; b=KhDDIshoCq69gfAYYpoCTFhzNrRMYUu6mxf8ktdewfoNKqVF+W5MMddkEHGpG35hT4 waSQO87Bg9KXHLv8XbrMyY+6qbOXGmGKcJGYKXHxF5m3F9OSaAgB5/pFlFtmXuBTP1Z6 PRVxM8Ht96u2Oh7LypVpXd6xXy1HV8AKmbgingN1huFtGRtYsRdVA5hW8tC2C0h80Mqi uiEqb7C6yPh3YLCfOotjYIAKbOwu0CGDVLBSPhLSK4hRkbFidVmZdUwn9xEEQ3ctG0b/ nHGhx/XO5ClOtapZyaJyOr/O1QGD/EksEKsdh4BiNt53s5dKjGB/WIRsleTJwthoBHG8 z6pQ== X-Gm-Message-State: AOAM5328S9xM6s1q/512lrWCIyVeHZ6I8/WL9Wu6/9FenIG22bRgGDe0 eG6bEYTIrfQo5KxC8Eia2/UUkl3svYUyyKta00L1vYnQjYq8CvqsOVNk1kTqWceUdk6lVmxz5dD UZt+M9C7rElYr+4GF7KePnXH4O4PD4iI1KenWIzztHmlTXBoN2pWgcHN3jzGQ X-Received: by 2002:a37:a883:: with SMTP id r125mr11301946qke.408.1631897280502; Fri, 17 Sep 2021 09:48:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqphWd+Tlov8LMwfi4vrThplMRTxmltW9/KJp77jBhawRfzds+2s8yAR92mY6VZ+JYJoRaqg== X-Received: by 2002:a37:a883:: with SMTP id r125mr11301917qke.408.1631897280218; Fri, 17 Sep 2021 09:48:00 -0700 (PDT) Received: from t490s.phub.net.cable.rogers.com ([2607:fea8:56a2:9100::d35a]) by smtp.gmail.com with ESMTPSA id t26sm4898057qkm.0.2021.09.17.09.47.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Sep 2021 09:47:59 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Vlastimil Babka , Andrew Morton , Hugh Dickins , Andrea Arcangeli , peterx@redhat.com, Matthew Wilcox Subject: [PATCH 1/3] mm/smaps: Fix shmem pte hole swap calculation Date: Fri, 17 Sep 2021 12:47:54 -0400 Message-Id: <20210917164756.8586-2-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210917164756.8586-1-peterx@redhat.com> References: <20210917164756.8586-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: weq7m13mnwbmd1w48kx5g87e4u5oui9a Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Jw9XuKah; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf16.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E6B48F408C5C X-HE-Tag: 1631897282-614127 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 shmem swap calculation on the privately writable mappings are using wrong parameters as spotted by Vlastimil. Fix them. That's introduced in commit 48131e03ca4e, when rework shmem_swap_usage to shmem_partial_swap_usage. Test program: ================== void main(void) { char *buffer, *p; int i, fd; fd = memfd_create("test", 0); assert(fd > 0); /* isize==2M*3, fill in pages, swap them out */ ftruncate(fd, SIZE_2M * 3); buffer = mmap(NULL, SIZE_2M * 3, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); assert(buffer); for (i = 0, p = buffer; i < SIZE_2M * 3 / 4096; i++) { *p = 1; p += 4096; } madvise(buffer, SIZE_2M * 3, MADV_PAGEOUT); munmap(buffer, SIZE_2M * 3); /* * Remap with private+writtable mappings on partial of the inode (<= 2M*3), * while the size must also be >= 2M*2 to make sure there's a none pmd so * smaps_pte_hole will be triggered. */ buffer = mmap(NULL, SIZE_2M * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); printf("pid=%d, buffer=%p\n", getpid(), buffer); /* Check /proc/$PID/smap_rollup, should see 4MB swap */ sleep(1000000); } ================== Before the patch, smaps_rollup shows <4MB swap and the number will be random depending on the alignment of the buffer of mmap() allocated. After this patch, it'll show 4MB. Fixes: 48131e03ca4e ("mm, proc: reduce cost of /proc/pid/smaps for unpopulated shmem mappings") Reported-by: Vlastimil Babka Signed-off-by: Peter Xu Reviewed-by: Vlastimil Babka --- fs/proc/task_mmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index cf25be3e0321..2197f669e17b 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -478,9 +478,11 @@ static int smaps_pte_hole(unsigned long addr, unsigned long end, __always_unused int depth, struct mm_walk *walk) { struct mem_size_stats *mss = walk->private; + struct vm_area_struct *vma = walk->vma; - mss->swap += shmem_partial_swap_usage( - walk->vma->vm_file->f_mapping, addr, end); + mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping, + linear_page_index(vma, addr), + linear_page_index(vma, end)); return 0; }