From patchwork Sun Jul 23 11:56:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 13323164 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 6B7F3C001B0 for ; Sun, 23 Jul 2023 12:02:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A8A36B0072; Sun, 23 Jul 2023 08:02:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 459D56B0074; Sun, 23 Jul 2023 08:02:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3210C6B0075; Sun, 23 Jul 2023 08:02:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 19AD36B0072 for ; Sun, 23 Jul 2023 08:02:39 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CF305A0676 for ; Sun, 23 Jul 2023 12:02:38 +0000 (UTC) X-FDA: 81042739596.23.03519EB Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by imf11.hostedemail.com (Postfix) with ESMTP id 073F940019 for ; Sun, 23 Jul 2023 12:02:36 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Cb2lfDRl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of fmdefrancesco@gmail.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=fmdefrancesco@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690113757; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=XLocNhdXvsCh5682u3tK4o2ag/IbV/VNJhwUsrpqvbM=; b=eeUaqmonMBWdTuGU9sjtZXMrPZ8Bhocs0shS0DXXQ76bijGxXrtfxvvXgBBPBNRqsf9CqM uYpoRUB4EbTgamLgnZjC8xi2yKj5+mzce/fgT2D5dIp2sY1FQAKI1KguZ6hwiNJR/lu7BO T6nTT6khWBGRA5vec7w+M9ew7uFLgTk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Cb2lfDRl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of fmdefrancesco@gmail.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=fmdefrancesco@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690113757; a=rsa-sha256; cv=none; b=T5zhfIKFZU1u8QLyZ5x3crhvEXnu2cmGdcHfT9MGP89K99JfFAw1sBpVxzC62VPhIxVdDt njf5iVvuoW99vbI6ppJJZP3nHOltKSuoBKLfBW2HKSGSI+mg0gvYdtMavM7eG0J7VCklWP ffUTaWRASYtffvvWWNm9j7X0I5ffufk= Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-316eabffaa6so2352274f8f.2 for ; Sun, 23 Jul 2023 05:02:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690113755; x=1690718555; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=XLocNhdXvsCh5682u3tK4o2ag/IbV/VNJhwUsrpqvbM=; b=Cb2lfDRlIrYNFNu7MM2OIF0BLCRrxQvyJKUmU5c7sAJLDXZ/VV6fli0zv1I1+j/M3N GqA+9LIteXR+lEttcbLme0xBL39wB2Nxb0KJmCzwgLIeRksm0rY4KsJh7rlx40EW2Ls8 F5Z9QCaEXQg5dOci/yYs4ojTcr4AfyLFQLVCve+g+EGLOEWPTDhY4Lpu7sTwF6XHimWX runWvZRbxnJFG1fgTuNkdajG3cYFzmWOtK5je/8kEQSj7iA4RAbFSyZcpYM5Uz3285zA HJV1yNMqd/WlfR9HJODdp0qgw4JrFDxBWPJd4HMTwog5obAgrcU53MU5OcPlu0AVXCH+ Sxtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690113755; x=1690718555; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XLocNhdXvsCh5682u3tK4o2ag/IbV/VNJhwUsrpqvbM=; b=ij6Qae1Lk+ir/x6RCteEL0LmzrFgeWzCd15jV2V88sqsa7uj2GGtwWFGmqb6VYrtnV OzU0jSW60Img7069OJMdpJFFKNm3IvMXP0thBge+9WaiG70Z719UWoNTALK3kt0iZxuX lp9hJRgPfWFV2pSeyoZX+fTLH8e1g4X0qAweXzZqEnwBdmORH45qmlsJdKlBy7ZoJUJQ Y49xPStbfHaLVCgCOz0zhBa6GoQCbZ8TucCzOB+sBxmbYQjsLLelBNlCy4NXLUf+dsWL KYw/r2ncddmDL4z1u6+yhupU81Op+6+fidRl8IYcjTT55bslF5eJAMZZ5KrDrJhEnaq4 g6SA== X-Gm-Message-State: ABy/qLbWncgCBdROEfoBhle/BRjzeACQdz1Sf0a+tvUjqAv8MwJVY9Zl 1sNRk0TR8wGgoFGVn/tP+qQ= X-Google-Smtp-Source: APBJJlHR0BT4IvdyoiCMYrkcYODlZ7zleJUj+ySiHvfmTMOTHWoNqt26QTCoF0V+jtZBTkizWiMgUg== X-Received: by 2002:a05:6000:1373:b0:307:8c47:a266 with SMTP id q19-20020a056000137300b003078c47a266mr3730382wrz.61.1690113755166; Sun, 23 Jul 2023 05:02:35 -0700 (PDT) Received: from localhost.localdomain (host-87-20-104-222.retail.telecomitalia.it. [87.20.104.222]) by smtp.gmail.com with ESMTPSA id x3-20020a05600c21c300b003fa95890484sm7561611wmj.20.2023.07.23.05.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 05:02:34 -0700 (PDT) From: "Fabio M. De Francesco" To: Jonathan Corbet , "Fabio M. De Francesco" , Jonathan Cameron , Linus Walleij , Mike Rapoport , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , Bagas Sanjaya , Matthew Wilcox , Randy Dunlap Subject: [RFC PATCH v2] Documentation/page_tables: MMU, TLB, and Page Faults Date: Sun, 23 Jul 2023 13:56:38 +0200 Message-ID: <20230723120100.5891-1-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 073F940019 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: gyqi4wxcssexs9d8nxsstrnu73mxf9n6 X-HE-Tag: 1690113756-447388 X-HE-Meta: U2FsdGVkX1/wJmowHUEN2nLoKTzdBfjDZWn85DmIYt5k9x5PTKXBvbNDTKt0amXdYte6nWTbdPVKbMTqDCUGYXZcNGcks5PhLNN2m3RRjvfV3W3GH628eeAWEDlqYkYiUxM8aCgZzVsHRNoAmQ7JqEIpV6LYbqUSnPB/1XmAm4U9QnvxGVbK4WINGj+S+7f4oNfpH3e774TtbA1n4jt6IZBe0LgVHmuzipeyT71P6uV4RcTU7FJqZz5tpHSBz4bq+IB8JqeY0KyE2+oDSLZU0nIKp4iPaN5LpF0HdHry8DYeWtryeNRMaU1mdJEssH3s8dHCXNl4eBvDDwy51Ld1vhjlQMOQz4ci3xfm7GwHXBLRv+37lKfWRZO63/kjmdp4iCqcZlYbIu4gO8364dtlJUZiedTYHllf5SrI/E5nKnwmL2XQ6B83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gwUcKSQ8d+jLJbKHMBvvfHKOqFl/Zya/MHD6VGXwJehNk7BLYgjV4SNPdZCNjNUaeeXcX4t8bzlDqn2MWuz7PImOTGDMtlybv7BCHV8OnTHPQYY74smY8/3sa/knBR0sc27ZwSkCfML8Vfg9x9JbDcgToX4oMdi1Q0IFm07mpKjcT3oZOCi2twPdCcjtR0/sJozbsC/RBoiie2vMrOjN1cCSeLNas6AjSZZ6kAHk4totbPU7GTeHvrcPjO7coPJcC3POYzEFTLoMlyeR+w31TrAu93h1ltZarAAUKpWYIX1uKyw== 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: Extend page_tables.rst by adding a small introductive section about the role of MMU and TLB in translating between virtual addresses and physical page frames. Furthermore explain the concepts behind the Page Faults exceptions and how Linux handles them. Cc: Andrew Morton Cc: Bagas Sanjaya Cc: Jonathan Cameron Cc: Jonathan Corbet Cc: Linus Walleij Cc: Matthew Wilcox Cc: Mike Rapoport Cc: Randy Dunlap Signed-off-by: Fabio M. De Francesco --- v1->v2: Add further information about lower level functions in the page fault handler and add information about how and why to disable / enable the page fault handler (provided a link to a Ira's patch that make use of pagefault_disable() to prevent deadlocks. This is an RFC PATCH because of two reasons: 1) I've heard that there is consensus about the need to revise and extend the MM documentation, but I'm not sure about whether or not developers need these kind of introductory information. 2) While preparing this little patch I decided to take a quicj look at the code and found out it currently is not how I thought I remembered it. I'm especially speaking about the x86 case. I'm not sure that I've been able to properly understand what I described as a difference in workflow compared to most of the other architecture. Therefore, for the two reasons explained above, I'd like to hear from people actively involved in MM. If this is not what you want, feel free to throw it away. Otherwise I'd be happy to write more on this and other MM topics. I'm looking forward for comments on this small work. Documentation/mm/page_tables.rst | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/Documentation/mm/page_tables.rst b/Documentation/mm/page_tables.rst index 7840c1891751..fa617894fda8 100644 --- a/Documentation/mm/page_tables.rst +++ b/Documentation/mm/page_tables.rst @@ -152,3 +152,64 @@ Page table handling code that wishes to be architecture-neutral, such as the virtual memory manager, will need to be written so that it traverses all of the currently five levels. This style should also be preferred for architecture-specific code, so as to be robust to future changes. + + +MMU, TLB, and Page Faults +========================= + +The Memory Management Unit (MMU) is a hardware component that handles virtual to +physical address translations. It uses a relatively small cache in hardware +called the Translation Lookaside Buffer (TLB) to speed up these translations. +When a process wants to access a memory location, the CPU provides a virtual +address to the MMU, which then uses the TLB to quickly find the corresponding +physical address. + +However, sometimes the MMU can't find a valid translation in the TLB. This +could be because the process is trying to access a range of memory that it's not +allowed to, or because the memory hasn't been loaded into RAM yet. When this +happens, the MMU triggers a page fault, which is a type of interrupt that +signals the CPU to pause the current process and run a special function to +handle the fault. + +One cause of page faults is due to bugs (or maliciously crafted addresses) and +happens when a process tries to access a range of memory that it doesn't have +permission to. This could be because the memory is reserved for the kernel or +for another process, or because the process is trying to write to a read-only +section of memory. When this happens, the kernel sends a Segmentation Fault +(SIGSEGV) signal to the process, which usually causes the process to terminate. + +An expected and more common cause of page faults is "lazy allocation". This is +a technique used by the Kernel to improve memory efficiency and reduce +footprint. Instead of allocating physical memory to a process as soon as it's +requested, the kernel waits until the process actually tries to use the memory. +This can save a significant amount of memory in cases where a process requests +a large block but only uses a small portion of it. + +A related technique is "Copy-on-Write" (COW), where the Kernel allows multiple +processes to share the same physical memory as long as they're only reading +from it. If a process tries to write to the shared memory, the kernel triggers +a page fault and allocates a separate copy of the memory for the process. This +allows the kernel to save memory and avoid unnecessary data copying and, by +doing so, it reduces latency. + +Now, let's see how the Linux kernel handles these page faults: + +1. For most architectures, `do_page_fault()` is the primary interrupt handler + for page faults. It delegates the actual handling of the page fault to + `handle_mm_fault()`. This function checks the cause of the page fault and + takes the appropriate action, such as loading the required page into + memory, granting the process the necessary permissions, or sending a + SIGSEGV signal to the process. + +2. In the specific case of the x86 architecture, the interrupt handler is + defined by the `DEFINE_IDTENTRY_RAW_ERRORCODE()` macro, which calls + `handle_page_fault()`. This function then calls either + `do_user_addr_fault()` or `do_kern_addr_fault()`, depending on whether + the fault occurred in user space or kernel space. Both of these functions + eventually lead to `handle_mm_fault()`, similar to the workflow in other + architectures. + +The actual implementation of the workflow is very complex. Its design allows +Linux to handle page faults in a way that is tailored to the specific +characteristics of each architecture, while still sharing a common overall +structure.