From patchwork Thu Nov 14 21:12:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13875630 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 00785D6A22F for ; Thu, 14 Nov 2024 21:12:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29F6E6B0085; Thu, 14 Nov 2024 16:12:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24FCB6B0089; Thu, 14 Nov 2024 16:12:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 117D16B008C; Thu, 14 Nov 2024 16:12:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EB4266B0085 for ; Thu, 14 Nov 2024 16:12:52 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5EA7BA04E0 for ; Thu, 14 Nov 2024 21:12:52 +0000 (UTC) X-FDA: 82785947916.13.938A3A2 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf05.hostedemail.com (Postfix) with ESMTP id 19DAB100015 for ; Thu, 14 Nov 2024 21:11:25 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=x78aAgK6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of jannh@google.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731618706; a=rsa-sha256; cv=none; b=5wxWOFnvvbPPq+47tXLHCNy9hTKrWF8WXLC3EgAWbEkGKNukiKcwxwg097CFfEJ6um5dZq mXtzjNHJ/FzamqXWCF0+KX5TifA3CDW23+q1OSmfvafcythu3mE3/iu45jqctMFLyCJ9Go 7U99vKNv7fLSTTXN5mN9BYq6FZ/YFXo= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=x78aAgK6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of jannh@google.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731618706; 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=rIZtYchTaSQMndO2ePTqJcpy4qTHfh94M480QB+cOJ0=; b=WDbbnZSUAN6Vz7hsF4+7lelwhBudiM4GjDpB6mxPJ4fYj/WHtrVXe/Mr+ycIqV4dewr7RU O2cn8KNHxmG9jfTw1VrMIwE0yBrKDf54qCQ/u9hHAvdxsIqh/h/8+564M/20BJQxaafeGe r3xqxRmEZx5/Mtt01VqcTliBlUTX6tI= Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-43150ea2db6so15575e9.0 for ; Thu, 14 Nov 2024 13:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731618769; x=1732223569; darn=kvack.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=rIZtYchTaSQMndO2ePTqJcpy4qTHfh94M480QB+cOJ0=; b=x78aAgK6Jdr90UcDjft4XtULalgOAQ5trDVK0MJltVneCeR9Civ4lTItDqePN/xZTh ZxBMFzEgj8g3ULRW0MqezG7mOgvLfWyuXWMAWLQ/g/voiTqLX94zoYgoYQ9I+FK4Kitn sdWrHVE8GuTGID2m4qrp6xAhyJjo1zSeSXKqGeUzNpL4hCBqvUr9xLXxHwDlzoCPdPDo OLg5qK2sVYKSoNYX2a3yvND5DTowew/ZmrsRU+V/HwkSrR2Q8qahZVyp1Zeid4DQ1qzR rWsizhH281E0oJrJLi/eQkz+HDEPFPmp9YXZfZabo+eKXCiDoCvN2FJznc5yeOjQWlL3 j2Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731618769; x=1732223569; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rIZtYchTaSQMndO2ePTqJcpy4qTHfh94M480QB+cOJ0=; b=DYZwvkpScQ6Bhc8qXrPDgGagevOdJjax0C/wpILVAeBUutq1pkZ6A4tfzDeRQiKCgy Wc3HfdzNYvV6bVW/A6veg/3BcFmzhgmUDc0A/RqWFqUKgMsjWgSFrSfuQno8fHlgcnyV Jky9uTBL4afLl1tUIvkY8P1Gby+XhmftudZhc3bsF3IqspEpYkReGsnYTzTOudCwHNIE 24AZQbCdPoJnZnmlZUfcGMJRvKqmEKXUY0TwGD57SvkpOhxAaveYA7GyCBvGbA2+7ngf ABw57a9hozwT89GVlaxSauIxEmlPkDaI6XiPC9LYWH0aHkbUYoEMMb9HgoE6aHV+I0X1 MCjw== X-Forwarded-Encrypted: i=1; AJvYcCWQ5jNnJqcEQHiew3xABFjXyZw+S3D8HqKol13CP30sTTvJ4isnSbGcYjsADe+aQzIaJ/ObR7rXgQ==@kvack.org X-Gm-Message-State: AOJu0YwyaWcWRRsZtUitQs+/K+auvWU58xyVzb/xTtLniFR0NiwgtKPH 6kQijVnP5m9Jm0AP+gwp4+1ac1bqp3SZXl9Ib2PWPmOd7lBJ+vOdTQV4tFi4+Q== X-Gm-Gg: ASbGncumvSZ1HDpaR3PgE54v6Zxz0YbtPoV1UBAGKBh2GqJbLX+803HDVoskmsb2zZd SoIPN7vfSQxceNT6eD0Wa89qshbSid020j4sQsZAU5rkiNMvz7GIjJ/DRRcQMnnzZK+/oTA+1ez EgVoH86/Bw126or0LDK+IuKPjo1RPihy5J5ucKuVzTuydFFT1EQnlENJf2lc0/i3bwHdpU4tFWB DQ/NEfJ4m8fvNHL6N1VGtBjuGpgpU5rV50CekU= X-Google-Smtp-Source: AGHT+IGST0bOj1r190JJJgGSMv6FxZ8grtKUGVs3XjXSDeF3CSW+8Y3q4Vex/qRtfWljzlpkG5hiWQ== X-Received: by 2002:a05:600c:5491:b0:431:43a1:4cac with SMTP id 5b1f17b1804b1-432df4a10e3mr168045e9.3.1731618768334; Thu, 14 Nov 2024 13:12:48 -0800 (PST) Received: from localhost ([2a00:79e0:9d:4:1e15:9767:4741:f790]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3821ae15d95sm2500636f8f.85.2024.11.14.13.12.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 13:12:47 -0800 (PST) From: Jann Horn Date: Thu, 14 Nov 2024 22:12:00 +0100 Subject: [PATCH] docs/mm: add more warnings around page table access MIME-Version: 1.0 Message-Id: <20241114-vma-docs-addition1-onv3-v1-1-ff177a0a2994@google.com> X-B4-Tracking: v=1; b=H4sIAJ9nNmcC/x2MMQrDMAwAv2I0V1A5JkO/UjIYS2o1xC52MAWTv 8dkPLi7AU2qSYOXG1ClW7OSJ9DDQfrG/BE0ngz+6QMRBex7RC6pYWS2Y9qEJfcFF88aZGXVpDD rXxW1/31+b+d5AQDUXKZpAAAA X-Change-ID: 20241114-vma-docs-addition1-onv3-32df4e6dffcf To: Andrew Morton , Jonathan Corbet , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka Cc: Alice Ryhl , Boqun Feng , Matthew Wilcox , Mike Rapoport , Suren Baghdasaryan , Hillf Danton , Qi Zheng , SeongJae Park , Bagas Sanjaya , linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Matteo Rizzo , Jann Horn X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1731618721; l=3159; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=rjKvFVzezYepUMqBsrgyRnxEy+YeuC3YPo8a88Ns+xs=; b=TwUQmmgfdu9Jzp5V6NkkZvOoWYGpMCSwmvDP6/56zXTXJGJkvpkMW/wo0jPBS6oKq1XQImG8V 6rwLw94tuwOCbSfaKKCS0nbwNRd9jEXEkQdHEy9w+U/Eo5ApzkGQ/Ce X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= X-Rspam-User: X-Rspamd-Queue-Id: 19DAB100015 X-Rspamd-Server: rspam11 X-Stat-Signature: f73gip7fx97n6k3i67rcnc9ebe6c4d8k X-HE-Tag: 1731618685-120949 X-HE-Meta: U2FsdGVkX1/bx0rG12+XnRFD2wSD+9TMVZW8++nPF3X34s5r8yT5UfLwUNpagPiI6mtr75b/V1JrqVf02t6nBkABRuVTPfDEUXjhAbbHX7GnbkchLPI1YykCLtkbnwIDWlXdxvx0NwmO51AZjcDEoXm15FWRyK3xgqDACUdklxS05NqmKGdQUxIU/N+sG/GdfCjflHaXwBdl8XXnJwKv78eWyoaI2oEeH9hv5M3MGL5WH/t/Lwb6JY/vLfyQL6y8b2yXIH6dogTxhQtb7Hgoz90GpsBDR5YtKF0ab2iEOvot8P0wIjh0raUSlbOgX4OjSXDdJE0u2ias7rNBdzjxZE1tdOL4MuQGWTXDX7ACIkMCkZXJIGMvxhzTxUauvHQKnVmhHSqneu6Evzp4Kkuo2XdLXE89XK2GZOBqLTRXMyWVFRhM6pmkNILZMiFa4XpYyxXapmdcbNwoEWdnxvS0/WcRZUfYYqQ0xmtrQ7Y5IFvnLEne2u7INZNwd7XCyjxvJOFK25Yix2EYnyWS6UBozTX6b9B0/WaPTHaHsZDUd/2zNKIHQvi1DLKdf1H6U3U+ED0zJ6g3pmuaNBQaBzfXqX+xbbtT6qUTE35Rq046+f3QVwG5QrP7PX6+DwVGC46echiuD5WrMBvcqwV/3JqE6zWjqv2VdlUAaJKspFJIQt/rOVv0KN2LFxoOoz4VhL8ZfDcI2IzUnLja6f6s11e29XqAdLtPq1XVEGT+C9ge0v90qh2aafsmydzSVPdfdNaIMl3Eo0FfkHEEY69t7sz63YlFzg/rTfd+9665IfqtngZ+FBj3C8zwO9Zvsu8YmXBSC6QwfvXq1FD4fOp22+1OTSGwQYXmoONkNfTzirr6Y6W+OV29QyY7rcEbp8vPoSZw58IM9bzWvh+amu261ugfBd2Nwinn85NzJtRYQrCfayLtdqw/5clQ7g3v2LxW363+4LyvjOKWODCxCmT/wi6 UnG5TXJ7 +v6p14wlfFp2B0sxzdH6jWrlNdtzMaEe1G9FoLXlaLBLuDyCTQxoGvmaavSdxDBcrbvroxtZ+jpEsqT+6FzcGqCiy3ZAFQP/eKvfX8AtKdtDs4UESKCn2G5Pn4QWInADQifkbfWy4JWNs6y82T6C00DLeEQc/IVR70mikV04MspBoJ5F9vJ3Dl1i+Z885CZKmk6Vg9vn173fXl4OeMbFtHYsKuG1sFIsgjdLu23U5cs/RcSXCWxiVritXAKyrKzVQtQH7+0gw6YxZPa5nvJM86wOxhVuTnbIkpLxH5EpfkpMiNSy1rV5W4WGHfUDmIsaZoSyKk0175HcOm8h6RAPuvflCSpxlpTI8od0lZobQILlR/TIHqKj1/+tPKQbKDcGE00xIFOEgnxWDTSc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make it clearer that holding the mmap lock in read mode is not enough to traverse page tables, and that just having a stable VMA is not enough to read PTEs. Suggested-by: Matteo Rizzo Signed-off-by: Jann Horn --- @akpm: Please don't put this in your tree before Lorenzo has replied. @Lorenzo: This is intended to go on top of your documentation patch. If you think this is a sensible change, do you prefer to squash it into your patch or do you prefer having akpm take this as a separate patch? IDK what works better... --- Documentation/mm/process_addrs.rst | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) --- base-commit: 1e96a63d3022403e06cdda0213c7849b05973cd5 change-id: 20241114-vma-docs-addition1-onv3-32df4e6dffcf diff --git a/Documentation/mm/process_addrs.rst b/Documentation/mm/process_addrs.rst index 1bf7ad010fc063d003bb857bb3b695a3eafa0b55..9bdf073d0c3ebea1707812508a309aa4a6163660 100644 --- a/Documentation/mm/process_addrs.rst +++ b/Documentation/mm/process_addrs.rst @@ -339,6 +339,16 @@ When **installing** page table entries, the mmap or VMA lock must be held to keep the VMA stable. We explore why this is in the page table locking details section below. +.. warning:: Taking the mmap lock in read mode **is not sufficient** for + traversing page tables; you must also ensure that a VMA exists that + covers the range being accessed. + This ensures you can't race with concurrent page table removal + which happens with the mmap lock in read mode, in regions whose + VMAs are no longer present in the VMA tree. + + (Alternatively, the mmap lock can be taken in write mode, but that + is heavy-handed and almost never the right choice.) + **Freeing** page tables is an entirely internal memory management operation and has special requirements (see the page freeing section below for more details). @@ -450,6 +460,9 @@ the time of writing of this document. Locking Implementation Details ------------------------------ +.. warning:: Locking rules for PTE-level page tables are very different from + locking rules for page tables at other levels. + Page table locking details -------------------------- @@ -470,8 +483,12 @@ additional locks dedicated to page tables: These locks represent the minimum required to interact with each page table level, but there are further requirements. -Importantly, note that on a **traversal** of page tables, no such locks are -taken. Whether care is taken on reading the page table entries depends on the +Importantly, note that on a **traversal** of page tables, sometimes no such +locks are taken. However, at the PTE level, at least concurrent page table +deletion must be prevented (using RCU) and the page table must be mapped into +high memory, see below. + +Whether care is taken on reading the page table entries depends on the architecture, see the section on atomicity below. Locking rules