From patchwork Fri Aug 5 16:00:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12937508 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 5B028C00140 for ; Fri, 5 Aug 2022 16:00:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5C3F6B0071; Fri, 5 Aug 2022 12:00:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C0C2C6B0072; Fri, 5 Aug 2022 12:00:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFA8E8E0001; Fri, 5 Aug 2022 12:00:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A28356B0071 for ; Fri, 5 Aug 2022 12:00:09 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6ED38A15F4 for ; Fri, 5 Aug 2022 16:00:09 +0000 (UTC) X-FDA: 79766000538.22.D5F6205 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 097F24012A for ; Fri, 5 Aug 2022 16:00:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659715208; 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; bh=I84jJZh3uMy8OjbvjnM/V7a2QEPXmVsEuIStQ5Ektjw=; b=U9oBtOfDFZDcPLfhFSPmRWkrEyYBV2GzyjYfhVDwpWqvv51vNnEaSiWhQ0QK2wN1uHL24D xKFo89MxBfCecU6uU4EHbgeh2VjRwpT0VJ4yXiV2+R9e5TppW1+nFh88Wxofnl8TlGHXzN XhZGVT1CofBFxMWOTdSp0DdhfcixHw4= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-d01u71K6P-KgnPEY8Jg-_w-1; Fri, 05 Aug 2022 12:00:07 -0400 X-MC-Unique: d01u71K6P-KgnPEY8Jg-_w-1 Received: by mail-qk1-f198.google.com with SMTP id bi37-20020a05620a31a500b006b5ef0afedaso2272023qkb.22 for ; Fri, 05 Aug 2022 09:00:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=I84jJZh3uMy8OjbvjnM/V7a2QEPXmVsEuIStQ5Ektjw=; b=YqXlwEEM12eu3Vp3DZbeKKnY7wiX1SCD8ZVDQcmGtDJXtq/x9O9i9qblCzDuIA1sD/ lqwmTqLv6VuNBwbzocMaJwM7VZ2u4e2oFCqaZ8a8znEeMKvuB9bA9JI7XExlT3XvEU7M AK/L7QS0hZiJNCAqjxI5SpC7FOt+Ej2fQG+39k+RVYqVsEDGUL8TuG71veanITHce3J7 1nsfI9Cd7qT+5wRKuBv1RXx1q2OsZAY/0b8RUQER+H6N1zcLCufBoiPNQfCblHvT/biN POGBLBkXzz7M2rGoYRfYZrnvn3mgtqrh2fBN6csGFf5qjt/09tVc4WNnO0oKnUXuL5Nb qZjg== X-Gm-Message-State: ACgBeo3Q6NI0q1yBb8P48XGfH0UNkHk0HIvEn5fGxCup8XY2YZ872aAe IMb1SshxA+HkL5i254QCavZbUyv8Ln1ztb43AFhRnTYpdwA1wS0c4ENsPPcJGjL8XbLdI/S5cv8 JQZn2Vtiqx5Q= X-Received: by 2002:a05:622a:4c7:b0:31f:dd6:93ca with SMTP id q7-20020a05622a04c700b0031f0dd693camr6296140qtx.526.1659715206351; Fri, 05 Aug 2022 09:00:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR6PHOhKBqkOUNsdNVGM60zSMR0a7lfTCil/iE/ktiqNKF6nIKAk9dOBI8Mxu+yZTUrPUrRYRw== X-Received: by 2002:a05:622a:4c7:b0:31f:dd6:93ca with SMTP id q7-20020a05622a04c700b0031f0dd693camr6296114qtx.526.1659715206125; Fri, 05 Aug 2022 09:00:06 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id r19-20020ac84253000000b0033b9c5c27dfsm2743047qtm.16.2022.08.05.09.00.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 05 Aug 2022 09:00:05 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Yang Shi , David Hildenbrand , peterx@redhat.com, Nadav Amit , Andrew Morton , Konstantin Khlebnikov , Huang Ying , Vlastimil Babka Subject: [PATCH v2] mm/smaps: Don't access young/dirty bit if pte unpresent Date: Fri, 5 Aug 2022 12:00:03 -0400 Message-Id: <20220805160003.58929-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659715208; a=rsa-sha256; cv=none; b=iJemZ9VStCkAGutXapF5Js1RMnvvFdTvCLtCjc/c4Jhq6GBujcWnfaDF37+ExrNxDXPEqL rBF/PuKNLBWc3m/nUZVF57oPMcc8QAGkT6ApiXlwCecey2oBA9+vrqGsLxFOOpyoWH1uQ+ TSmgHhwgMInkMmhFcNqqRuCIgTv/7OY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=U9oBtOfD; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659715208; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=I84jJZh3uMy8OjbvjnM/V7a2QEPXmVsEuIStQ5Ektjw=; b=ZZpkxBgprpBf8VHSsCJmqxS/ak83acXTbZr5e0EV+Eclf6ruos0WEHzxgTMJP+CWE0jVuR 11Y1IvepoXbJHyzmwhOzte2unnV+BJN2whBSHsRWy5ZxgOpr2ZOgL3Z1XDgKDNH+wM7rZk TvXS78zXntLbLwgkUSDdp09l+Z4xLno= X-Rspamd-Server: rspam04 X-Stat-Signature: m6wcjse89ns9pjxhwn9chcoq96ihe1cp Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=U9oBtOfD; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Queue-Id: 097F24012A X-Rspam-User: X-HE-Tag: 1659715207-719353 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: These bits should only be valid when the ptes are present. Introducing two booleans for it and set it to false when !pte_present() for both pte and pmd accountings. The bug is found during code reading and no real world issue reported, but logically such an error can cause incorrect readings for either smaps or smaps_rollup output on quite a few fields. For example, it could cause over-estimate on values like Shared_Dirty, Private_Dirty, Referenced. Or it could also cause under-estimate on values like LazyFree, Shared_Clean, Private_Clean. Cc: Konstantin Khlebnikov Cc: Huang Ying Fixes: b1d4d9e0cbd0 ("proc/smaps: carefully handle migration entries") Fixes: c94b6923fa0a ("/proc/PID/smaps: Add PMD migration entry parsing") Reviewed-by: Vlastimil Babka Reviewed-by: David Hildenbrand Reviewed-by: Yang Shi Signed-off-by: Peter Xu Reviewed-by: "Huang, Ying" --- fs/proc/task_mmu.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 9913f3be9fd2..d56c65f98d00 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -527,10 +527,12 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, struct vm_area_struct *vma = walk->vma; bool locked = !!(vma->vm_flags & VM_LOCKED); struct page *page = NULL; - bool migration = false; + bool migration = false, young = false, dirty = false; if (pte_present(*pte)) { page = vm_normal_page(vma, addr, *pte); + young = pte_young(*pte); + dirty = pte_dirty(*pte); } else if (is_swap_pte(*pte)) { swp_entry_t swpent = pte_to_swp_entry(*pte); @@ -560,8 +562,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, if (!page) return; - smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), - locked, migration); + smaps_account(mss, page, false, young, dirty, locked, migration); } #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -572,11 +573,13 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, struct vm_area_struct *vma = walk->vma; bool locked = !!(vma->vm_flags & VM_LOCKED); struct page *page = NULL; - bool migration = false; + bool migration = false, young = false, dirty = false; if (pmd_present(*pmd)) { /* FOLL_DUMP will return -EFAULT on huge zero page */ page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); + young = pmd_young(*pmd); + dirty = pmd_dirty(*pmd); } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { swp_entry_t entry = pmd_to_swp_entry(*pmd); @@ -596,8 +599,7 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, else mss->file_thp += HPAGE_PMD_SIZE; - smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), - locked, migration); + smaps_account(mss, page, true, young, dirty, locked, migration); } #else static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,