Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1294869lqa; Mon, 29 Apr 2024 04:40:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVuA5w2nePQNNwUoNYwCaQgePbk9FcyVEvXz+BWbhAaDAQNiSUHBxDDGvHADku1mH90HOaCRE55vGOsYUB/c3ek2vhGxVywsQAAdczitQ== X-Google-Smtp-Source: AGHT+IGKAmMfuE6mJRqNOPg/zgKg8x/m1qa9IOdbEO5V6SqSsBZcqUkxPsGDJ+ZX72VLYvxxKjF0 X-Received: by 2002:ad4:4b28:0:b0:6a0:acf9:5dcc with SMTP id s8-20020ad44b28000000b006a0acf95dccmr7546749qvw.41.1714390844440; Mon, 29 Apr 2024 04:40:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714390844; cv=pass; d=google.com; s=arc-20160816; b=mlhRjqFQ/OdjOwnN/RsQr3p4BVpzA9grsUDD2uzPdTUrHVMGm1a/XUb5gDBMt8iBYM rhU4JAe//VKEg/omTWQkQLoglXTOV2s2Afy9x2/f53bIYFIwdphyVBguzfJkp+JpWNVW 51Ig9AoC/WHuui13IXFZuirA3PMYRg7Ska3HPZXNgNLwMX+tReWtJ2gqOAbYoJ9Z94rh 1cGGkQaRF87ZawO7MTa5xo8rQA/EdXddNGc5xYlBUWbDkKqooW8erXwrqFSOSyjg51xa 2Y/MAvlJXqKPCok5Ju1LGLMCMIFnGPmymezQ22bPu6Qj3egnYoundq923nUt39k40XX3 QTqQ== 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:message-id:date:subject:cc:to :from; bh=R2Jcau7Bu6vaIGEaxiC9S+VJiIdJFmU2CpH4Fptjcts=; fh=7LjKlt7j7wkfuH3q8Yvy6/OIqUoxE1f624Xs/9Gv6+U=; b=zekIepanmQYTSup82fJjb66N6O/uZhrPO3pinhm5Ymfn0pBtBswMVtRgy2Sgaqibfo x44iySBk8woV1WuLoGOiVARHU16W+3FZ/nLj7jlPrEJzO5ei6ooTO/LkXcVedVniTk9/ vZcnN7tGn1KNJk+M4DpqV3xSl0Ri7giPIwewBdBVHxH79RevrVFKB6aigU2FCE6qWmnl WP6fm3T8nTllfgfJ6cNNAkbHnp1SpgYdULDUVC9eWq9ywA5jdDDqsthO4rQ1dB/yeKpO QVgcxAzMHSzdYxHc1gyZ6dnVBVthWQS071MzXakQwszoKGGz1uN1d3SvOuRMezId/fi7 JlKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-162167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 u4-20020ad45aa4000000b006a0cde16c5dsi2446404qvg.445.2024.04.29.04.40.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 04:40:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162167-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; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-162167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 2F3831C217CD for ; Mon, 29 Apr 2024 11:40:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72B2046B9A; Mon, 29 Apr 2024 11:40:34 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 62CDD3C24; Mon, 29 Apr 2024 11:40:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714390834; cv=none; b=QE+/t90mZMMvHkE6l6LOn3ShoiUqA5EAgGyIPi9Ka6xPw+Kx0eM8E7fDg/DUOGc55Igx2+IE2+vj1B56SyoxLViCFNzQUK2v9nizMulAKG15N82Kx7LXPh3Y8mXjjKGQBiIZWe/Bx4az2tX/Km25m6JKfzN8vMMhq6ZTU2mEDlk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714390834; c=relaxed/simple; bh=/gI/+z7qi46TOsKZwW+XKxc4nIlP1Z7bWqZ7E6UNQhA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=REf6t3PH68m/SlSQVKTkTKaKxwbMXSy+3XFWQiZJgFmlQg9/KCZQTJ5urc/c5piuUKNJlMJZzsSDw74UDM2/8RwJest5lZ2z8wUnuHrmpk9D9jpD0fZ8tw5H3jyV0Vehtjnqtnsn829MBoAPLz0HYKbB/OkdUsBs9VOKxCpNs+E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 307C52F4; Mon, 29 Apr 2024 04:40:57 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 607543F793; Mon, 29 Apr 2024 04:40:29 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , David Hildenbrand , Muhammad Usama Anjum , Peter Xu Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan Date: Mon, 29 Apr 2024 12:40:17 +0100 Message-Id: <20240429114017.182570-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit make_uffd_wp_pte() was previously doing: pte = ptep_get(ptep); ptep_modify_prot_start(ptep); pte = pte_mkuffd_wp(pte); ptep_modify_prot_commit(ptep, pte); But if another thread accessed or dirtied the pte between the first 2 calls, this could lead to loss of that information. Since ptep_modify_prot_start() gets and clears atomically, the following is the correct pattern and prevents any possible race. Any access after the first call would see an invalid pte and cause a fault: pte = ptep_modify_prot_start(ptep); pte = pte_mkuffd_wp(pte); ptep_modify_prot_commit(ptep, pte); Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs") Signed-off-by: Ryan Roberts --- fs/proc/task_mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 23fbab954c20..af4bc1da0c01 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1825,7 +1825,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma, pte_t old_pte; old_pte = ptep_modify_prot_start(vma, addr, pte); - ptent = pte_mkuffd_wp(ptent); + ptent = pte_mkuffd_wp(old_pte); ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); } else if (is_swap_pte(ptent)) { ptent = pte_swp_mkuffd_wp(ptent); -- 2.25.1