Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp998460lqt; Fri, 7 Jun 2024 05:25:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXMHc2nm8NsGdv3IEL+r1/qx1dfh7Z95u0fA+r/UqVG6dqPYG8Z6tRTyhaNGb4Us5sR3j72K4Qx2Zt+JVfSoDgvQz9FFGDhvIUGfvO3vA== X-Google-Smtp-Source: AGHT+IEkJEKuXkpfQz3U278vLwmF6jzMSKHOF+lHXc2copxHYoXcomrKItHfiU5On5b6QNZlr/0n X-Received: by 2002:ac5:c8a1:0:b0:4e9:7df0:dc7d with SMTP id 71dfb90a1353d-4eb562c061amr2428374e0c.15.1717763126788; Fri, 07 Jun 2024 05:25:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717763126; cv=pass; d=google.com; s=arc-20160816; b=XgKxJkOpG7syN2rUL1W85gETsmY8xdZIr2WXyassxnYxqN66AI+m0ne6HR+bRkbmyV 7YaApUynFBWQICfUguZB4U3yQW7KYOQi+kh//u2zR85GJm+LuDdHmkFCYE+/ZKVdWykc b3gDDvFl6ydRjK/Jn3StDfZaNPB85b5YcuY0BPXNtNL2svanaeNBrZFP7+QxNvgDeRys lSSwfTGekmvb4scFv+vPq5jOvh4Vlm21PtC9keUW63amIfWjmRbxSLUR+/605RZDPx9E mn5L5w6P8bQsRBptz4NEO/7sUtvst17U3gBICWm/XXPGio213VA0tzqmcmU6fj++5Vya 1lUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Yx3Qbyh+Nn+bqmSyWmDh1sWt/uN/FzGydIKvFJPogXs=; fh=51jV7WQxntzmbBmGMw9RLl+8SzlZuq36dY7llxxkwJE=; b=PQU2GPIbtWhbbnS8+um5Xlw79lD2UNFjQ/dTxmcc/M35XT3ahbO23yn02IyrLH8khu UUbiTco+Fe7/oUjZhUE1sSEHqyNlxmX/JP/zAQ9tL7IyoVFGixTWOMEIMYTBh2fqEpfN RO5uH3bh6ASeyMCW0lbU68pD8tY327Vr6uMXxAsecM8I4WqYtUzoJ12xEkCtILeBjEEE j/Mpw9LzOnODxqXYhiHzWbVDuYIsYfIwsOG1CLslAcAiO6zTl9Bm+Gow774t7IkLrk3b H+V3eCTUbPzu8vgInIx0bfDo267pQgpW2hZUpvOhlqs3sGG7MQNEnOHjHx0qLWinB88U 2H1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dj+0FRNX; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-206021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206021-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 71dfb90a1353d-4eb4ad343fbsi707900e0c.134.2024.06.07.05.25.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 05:25:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dj+0FRNX; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-206021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206021-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6F8781C226F3 for ; Fri, 7 Jun 2024 12:25:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D1313194A7C; Fri, 7 Jun 2024 12:24:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Dj+0FRNX" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 587001922D2 for ; Fri, 7 Jun 2024 12:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717763053; cv=none; b=VYsCXhU/V0o2USQbyg4UUqdZtYIpAzmY5ZwgO7g5rXKbbd75HWkWseJYdj+7AV/RmbNP3+AbFYMIXSqEbpxtPo3FIBl/W227mFN7oZsnFjfqElzT27cNGXbEp+SWoCroay4de+XaR06jBATyMlQyyKw/ulogV8KdWhOx2f2SkJQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717763053; c=relaxed/simple; bh=tqRveSp+nk5b1gcqOA9DfmBAfkuN9S+BMP+/LLSkI0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ur+q1xaBXWa5WwhqDcqYVfLOzxEPGQVOoFyeT0fjhvaY/KUyI7cUIPq8nP1j17wQ1BDEDjD9609a2yR/MFNhb4qyVIxHdXnSqQ7j2pzJ+ZBDzjesO02etzgrv8plJV9JDuoyCB1tfnKDkNTjQ/OO+EawnMNHLlUa/6q2QFsD/v4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Dj+0FRNX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717763051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yx3Qbyh+Nn+bqmSyWmDh1sWt/uN/FzGydIKvFJPogXs=; b=Dj+0FRNXlLHi3S3VC76r4TWI24L244lViH936HYJxDZFVWMmzA5AJOdL6HPND+C61E6WzP zki6ViTHy2CjCb16Q+n7C306djUclZ2ztlxC6ZkrlYfZRH5TDBDtFSlSvupYE5WUmwKS8R UtGg5CLfVAnkj4GY91TZT1nsvxbCt7o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-FRJ1tLmeM4SPq5VPjh0mRQ-1; Fri, 07 Jun 2024 08:24:08 -0400 X-MC-Unique: FRJ1tLmeM4SPq5VPjh0mRQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A49BC811E79; Fri, 7 Jun 2024 12:24:07 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.109]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0098492BD0; Fri, 7 Jun 2024 12:24:04 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, David Hildenbrand , Andrew Morton , Jonathan Corbet , "Kirill A . Shutemov" Subject: [PATCH v1 3/6] fs/proc/task_mmu: properly detect PM_MMAP_EXCLUSIVE per page of PMD-mapped THPs Date: Fri, 7 Jun 2024 14:23:54 +0200 Message-ID: <20240607122357.115423-4-david@redhat.com> In-Reply-To: <20240607122357.115423-1-david@redhat.com> References: <20240607122357.115423-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 We added PM_MMAP_EXCLUSIVE in 2015 via commit 77bb499bb60f ("pagemap: add mmap-exclusive bit for marking pages mapped only here"), when THPs could not be partially mapped and page_mapcount() returned something that was true for all pages of the THP. In 2016, we added support for partially mapping THPs via commit 53f9263baba6 ("mm: rework mapcount accounting to enable 4k mapping of THPs") but missed to determine PM_MMAP_EXCLUSIVE as well per page. Checking page_mapcount() on the head page does not tell the whole story. We should check each individual page. In a future without per-page mapcounts it will be different, but we'll change that to be consistent with PTE-mapped THPs once we deal with that. Fixes: 53f9263baba6 ("mm: rework mapcount accounting to enable 4k mapping of THPs") Cc: Kirill A. Shutemov Signed-off-by: David Hildenbrand --- fs/proc/task_mmu.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 40e38bc33a9d2..f427176ce2c34 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1474,6 +1474,7 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, ptl = pmd_trans_huge_lock(pmdp, vma); if (ptl) { + unsigned int idx = (addr & ~PMD_MASK) >> PAGE_SHIFT; u64 flags = 0, frame = 0; pmd_t pmd = *pmdp; struct page *page = NULL; @@ -1490,8 +1491,7 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, if (pmd_uffd_wp(pmd)) flags |= PM_UFFD_WP; if (pm->show_pfn) - frame = pmd_pfn(pmd) + - ((addr & ~PMD_MASK) >> PAGE_SHIFT); + frame = pmd_pfn(pmd) + idx; } #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION else if (is_swap_pmd(pmd)) { @@ -1500,11 +1500,9 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, if (pm->show_pfn) { if (is_pfn_swap_entry(entry)) - offset = swp_offset_pfn(entry); + offset = swp_offset_pfn(entry) + idx; else - offset = swp_offset(entry); - offset = offset + - ((addr & ~PMD_MASK) >> PAGE_SHIFT); + offset = swp_offset(entry) + idx; frame = swp_type(entry) | (offset << MAX_SWAPFILES_SHIFT); } @@ -1520,12 +1518,16 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, if (page && !PageAnon(page)) flags |= PM_FILE; - if (page && (flags & PM_PRESENT) && page_mapcount(page) == 1) - flags |= PM_MMAP_EXCLUSIVE; - for (; addr != end; addr += PAGE_SIZE) { - pagemap_entry_t pme = make_pme(frame, flags); + for (; addr != end; addr += PAGE_SIZE, idx++) { + unsigned long cur_flags = flags; + pagemap_entry_t pme; + + if (page && (flags & PM_PRESENT) && + page_mapcount(page + idx) == 1) + cur_flags |= PM_MMAP_EXCLUSIVE; + pme = make_pme(frame, cur_flags); err = add_to_pagemap(&pme, pm); if (err) break; -- 2.45.2