Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp36746lfe; Fri, 15 Apr 2022 18:15:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHve33HPSx/IxP+NOdLguuHctsyqQkLXHUWdq2wAOH4vd81KaCa56OE1A8SK9+xDyo90II X-Received: by 2002:a63:2113:0:b0:399:2df0:ff07 with SMTP id h19-20020a632113000000b003992df0ff07mr1249868pgh.374.1650071707394; Fri, 15 Apr 2022 18:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650071707; cv=none; d=google.com; s=arc-20160816; b=F3I080mj9gQhQY7kvchBgirTKnwqTwDVIAkjueOiA41GoEM/KLK/no0I8GCSZYJNc6 AGbfiXLHJyMjUAwIUzBgRdBZxUNdFWUfkPMBCKW421DE7fUSqr1ph1KGML9JOxm8BK5K xHr5FUamOc1h6ms2ynlcXom3DJuI4LQFX1Y8rsbGfaEU6Y+9sle0m7AOga68nNW3ScR/ yXuqvUyN8N3245NhHySkXsNw7YvdkYXamBSTiG5NoNJTAxCyycftycaIWvTETBQ1z0Rv YOBnNr8zFkbTv+AjD09uOTu7wCqtTLNipR/YhmrZ8S+f9/IWpYyayYngSh2hVw2hjtfm QB+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VQ4t+4unN4Bi2tgZL0pBgqD4ZstJy3iXF/YNfaCg2WI=; b=0LBeIa0iJAGzR0bzV4uAoYXaqHGCvGe4eR3NMgCyoRhNTTkI4fpc99wN2U6GgQQhwv GIMLXNiHCPZkMm1aQ/G1i1edWBn6zF3eB2Tbuip/uG0QCm2H3qyqOpu/fBVeWtkWjRFA ATXoqWWNB0RRYDFRqKRQKZSHTxZQcG8JjSOoo4wFEIdsHzf+rDOAakCR5YcVLig3A7wx CGONuXGfjNbJ/Qikc4Rqn/aad+JALvq8gNolcck0CJKHBB+Zup8Tvu2vMIH4da0AjXaK MmjUDawoUOPB1m6Sl4/BRgcAKX/F1W08JKqCnGll7ia7G9TruHDoR735uJKJN5+pwMU2 tw6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hXNYinU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id x70-20020a633149000000b0038225bd6f70si3014934pgx.398.2022.04.15.18.15.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 18:15:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hXNYinU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D3B0A131F61; Fri, 15 Apr 2022 17:51:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356449AbiDOXWA (ORCPT + 99 others); Fri, 15 Apr 2022 19:22:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235048AbiDOXV6 (ORCPT ); Fri, 15 Apr 2022 19:21:58 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17CF25F5A; Fri, 15 Apr 2022 16:19:28 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id k23so17558516ejd.3; Fri, 15 Apr 2022 16:19:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VQ4t+4unN4Bi2tgZL0pBgqD4ZstJy3iXF/YNfaCg2WI=; b=hXNYinU44+STlYg31sonOjoHMIG2/wUcTlJCQDtleDGusHuVRKcs/INWVreUslUo1l gLR7T+I4CLzdoJpNsHs2NhywG+aqNX0KkKJHsxi0WVW9yOqqhOHNRCg/9WeNVBgB6nnB rC5nT+erQLneJZcR5bO+07dRyQukO4nzz8C/pGsnyTfdlNWRcmITz8cTpRsK/wHA+EGu loxONqNK5/KMSNkJnXNgmNolf15H0gejDzdp7bE5RKZaanzNZGLnKPXZjjjgoKjLVmfz XAa/XyVuYABi5QVtSFuWop5XieKc6l8C6aDY1LoDXomw46B48iF7URdk+n/oTvc8u/mJ qjDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VQ4t+4unN4Bi2tgZL0pBgqD4ZstJy3iXF/YNfaCg2WI=; b=17YC/9Ghn0BcB90P+3P8zyXXSk97d3VVC7pF+BjwFoEJMiKA712aX678uDexRUujyw Webcp2ygxZUZ0U71xsDxS1wAiD5JPKZk2z7pghARkKsK62dNwHcAsyP4kHeW06NKy8oi JrQz/bYrriSYKpNY5tqB8NWeVNIGqES3WrPFj43PXyw7Xd21Qe1hb1svyTnogqo19eMJ Rnqqj0USw/VYFaAhlhfIKC0pRabk4d1nTB3wZUJyzHCB8Xt+9g3P7FnTvMzlx+fCz3zh uaZa7uHNtWNCG32z4FwVlw502q58X9bF7QnYnV963GxIaX+uZLZ6O9sPHiBz4o5RKInb AZCg== X-Gm-Message-State: AOAM532Fao+MA0jnMVYzAk1EQpDNKnT/z6Rx8TDyx6U7mJj4lhn7x3Gr hnmrvZDznkpAxYvxHhKkzwE= X-Received: by 2002:a17:906:7952:b0:6da:94c9:cccb with SMTP id l18-20020a170906795200b006da94c9cccbmr906202ejo.469.1650064766635; Fri, 15 Apr 2022 16:19:26 -0700 (PDT) Received: from localhost.localdomain (host-79-43-11-75.retail.telecomitalia.it. [79.43.11.75]) by smtp.gmail.com with ESMTPSA id o15-20020a50fd8f000000b0041f95b8a90dsm3203394edt.69.2022.04.15.16.19.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 16:19:25 -0700 (PDT) From: "Fabio M. De Francesco" To: Jonathan Corbet , Andrew Morton , SeongJae Park , Jiajian Ye , Thomas Gleixner , Ira Weiny , Matthew Wilcox , Peter Zijlstra , outreachy@lists.linux.dev, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" Subject: [PATCH v3 1/2] Documentation/vm: Extend "Temporary Virtual Mappings" section Date: Sat, 16 Apr 2022 01:19:16 +0200 Message-Id: <20220415231917.25962-2-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220415231917.25962-1-fmdefrancesco@gmail.com> References: <20220415231917.25962-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Extend and rework the "Temporary Virtual Mappings" section of the highmem.rst documentation. Do a partial rework of the paragraph related to kmap() and add a new paragraph in order to document the set of kmap_local_*() functions. Re-order paragraphs in decreasing order of preference of usage. Despite the local kmaps were introduced by Thomas Gleixner in October 2020, documentation was still missing information about them. These additions rely largely on Gleixner's patches, Jonathan Corbet's LWN articles, comments by Ira Weiny and Matthew Wilcox, and in-code comments from ./include/linux/highmem.h. Cc: Jonathan Corbet Cc: Thomas Gleixner Cc: Ira Weiny Cc: Matthew Wilcox Cc: Peter Zijlstra Signed-off-by: Fabio M. De Francesco --- Documentation/vm/highmem.rst | 67 ++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/Documentation/vm/highmem.rst b/Documentation/vm/highmem.rst index 0f69a9fec34d..12dcfbee094d 100644 --- a/Documentation/vm/highmem.rst +++ b/Documentation/vm/highmem.rst @@ -52,24 +52,69 @@ Temporary Virtual Mappings The kernel contains several ways of creating temporary mappings: -* vmap(). This can be used to make a long duration mapping of multiple - physical pages into a contiguous virtual space. It needs global - synchronization to unmap. - -* kmap(). This permits a short duration mapping of a single page. It needs - global synchronization, but is amortized somewhat. It is also prone to - deadlocks when using in a nested fashion, and so it is not recommended for - new code. +* kmap_local_*(). These provide a set of functions that are used to require + short term mappings. They can be invoked from any context (including + interrupts) but the mappings can only be used in the context which acquired + it. + + These mappings are per thread, CPU local (i.e., migration from one CPU to + another is disabled - this is why they are called "local"), but they don't + disable preemption. It's valid to take pagefaults in a local kmap region, + unless the context in which the local mapping is acquired does not allow + it for other reasons. + + If a task holding local kmaps is preempted, the maps are removed on context + switch and restored when the task comes back on the CPU. As the maps are + strictly CPU local, it is guaranteed that the task stays on the CPU and + that the CPU cannot be unplugged until the local kmaps are released. + + Nesting kmap_local.*() and kmap_atomic.*() mappings is allowed to a certain + extent (up to KMAP_TYPE_NR). Nested kmap_local.*() and kunmap_local.*() + invocations have to be strictly ordered because the map implementation is + stack based. * kmap_atomic(). This permits a very short duration mapping of a single page. Since the mapping is restricted to the CPU that issued it, it performs well, but the issuing task is therefore required to stay on that CPU until it has finished, lest some other task displace its mappings. - kmap_atomic() may also be used by interrupt contexts, since it is does not - sleep and the caller may not sleep until after kunmap_atomic() is called. + kmap_atomic() may also be used by interrupt contexts, since it does not + sleep and the caller too can not sleep until after kunmap_atomic() is called. + Each call of kmap_atomic() in the kernel creates a non-preemptible section and + disable pagefaults. + + This could be a source of unwanted latency, so it should be only used if it is + absolutely required, otherwise the corresponding kmap_local_*() variant should + be used if it is feasible. + + On 64-bit systems, calls to kmap() and kmap_atomic() have no real work to do + because a 64-bit address space is more than sufficient to address all the + physical memory, so all of physical memory appears in the direct mapping. + + It is assumed that k[un]map_atomic() won't fail. - It may be assumed that k[un]map_atomic() won't fail. +* kmap(). This should be used to make short duration mapping of a single + page with no restrictions on preemption or migration. It comes with an + overhead as mapping space is restricted and protected by a global lock + for synchronization. When mapping is no more needed, the address that + the page was mapped to must be released with kunmap(). + + Mapping changes must be propagated across all the CPUs. kmap() also + requires global TLB invalidation when the kmap's pool wraps and it might + block when the mapping space is fully utilized until a slot becomes + available. Therefore, kmap() is only callable from preemptible context. + + All the above work is necessary if a mapping must last for a relatively + long time but the bulk of high-memory mappings in the kernel are + short-lived and only used in one place. This means that the cost of + kmap() is mostly wasted in such cases. kmap() was not intended for long + term mappings but it has morphed in that direction and its use is + strongly discouraged in newer code and the set of the preceding functions + should be preferred. + +* vmap(). This can be used to make a long duration mapping of multiple + physical pages into a contiguous virtual space. It needs global + synchronization to unmap. Using kmap_atomic -- 2.34.1