Received: by 2002:a05:6a10:87d6:0:0:0:0 with SMTP id g22csp437786pxr; Sun, 10 Apr 2022 20:15:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuw5P/1ZmH8lNNqwphITQfPNQhcddzS2oWeUAtj9X+R7Kkr48bSNfD9d1vJiQrD8QlQ6LW X-Received: by 2002:a63:2d06:0:b0:39c:f643:8720 with SMTP id t6-20020a632d06000000b0039cf6438720mr12126823pgt.371.1649646958086; Sun, 10 Apr 2022 20:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649646958; cv=none; d=google.com; s=arc-20160816; b=J+B/eID72K8NuTT9oSMHBOtwFKBMXJqFV0C6w5WTglugkYNwL0zM7J9fOFiWs0F+7G UIFyhT18YuVA/VKGYUMnVpRQeHvkd38mQhCruGlcmI3xmPxDaEW+vggehN11mubQ8NJj XVXQWf12P7JZq954j87JMWkCzlr8QQekydo/UosO+CGc0+kuw2MxOphvPXUAwTWnpVrj yhBh9F5pzmbtj8zK9CQCvUufd+dlx9jiaQQJGpL00GKERahm7KqiToSEBbVAXeZnEyJ2 bgHyZJdLA1tjXh7LN+6/n9WUeEbjgqXEokA2vK+n7R4mOd7fDir5hXFVvkMZstJaXZsm q0TA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=ByRdUYwsUrRmXZCxQJJZezHsqvrPoLpkrEGfURfn69A=; b=kXJwG+RjqxVUfAjV8eFeVLn2ccz8C5JbOt1Nm7GLisdgCHy4ZlasbmjHpK3KOOjuQQ Y7KLsmoepeRoSh0LGruqgLFRnoC1ZQ3LvfbsD8JG+Xg7oUoGTOkgNVjs1I+0xUJVYsph k5nj74sr+Sr4ctRKyvGlefqD57/cMtjIw24mfrbWFhCpysyjs8Tb4EbDkVTMtrAg2679 MT1QZJcevB2tYLYO5VKUp0vej/e11doZUKLOdvhEIia25PRNy/fd8QurE732DuyP7Epu 7alEcM6k09SwYu/W5Rhef26ui+T3htJAFM+LoJqk1kQhof6mh1GMEhV3O4O2L76B3oDr W/sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=G1zqJXHz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d13-20020a65424d000000b0039cd17a62aesi8226793pgq.508.2022.04.10.20.15.42; Sun, 10 Apr 2022 20:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=G1zqJXHz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236210AbiDISvW (ORCPT + 99 others); Sat, 9 Apr 2022 14:51:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231682AbiDISvV (ORCPT ); Sat, 9 Apr 2022 14:51:21 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3DC558811; Sat, 9 Apr 2022 11:49:12 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id u18so2918356eda.3; Sat, 09 Apr 2022 11:49:12 -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:mime-version :content-transfer-encoding; bh=ByRdUYwsUrRmXZCxQJJZezHsqvrPoLpkrEGfURfn69A=; b=G1zqJXHzmARcE8RwkpRebw9HiuiJnk+wy1TeLiFH/oymM4GpsheLe93aOhZzPnGyuA rYJaDdf0P+wa/JgrYcRkvwkS+W+vkHx9d/KowIe+4Jd1rlR/a09qIKLvzusGWjLoUL/d FcXGq3fqW5hraeucZaucbHVaj0jNhtViQI/urOCDcQcdyjnIuSKt4Ae4Y2RAuZK4ZBbR hQjUdP0o+07bw0755waLD80g8ZzpPouIFWsi0tHyyDSnZsVxG8vR6LVoXVSqgo3XeQ0C dZongh3GMC6iOBu+dnYhblN35JazJzzy6FDDEt6UJLiNFi6ifuyaOsixVB9L1g+2tO4h Djcw== 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:mime-version :content-transfer-encoding; bh=ByRdUYwsUrRmXZCxQJJZezHsqvrPoLpkrEGfURfn69A=; b=wv7zoeWLGyXBsp7LIUBCGV4096+pUOUHHGpu9dASyMbC7OBUXM8DQs1g+zQ6bRhqlV H+XTgIDRCbRT6uTAKvEo74Ykj8efh/s4q95R63ZxyIVuaWgomhxBQln9pBtc+LyJSQnO OzT4CzgRkep7RXf26mBKfwfnu1AdOHTph/bs7DATuzW+XS9phfS9c16MGBhFNRHx6KIo 6mmMSVdmj8FvmOfosam7Vavzv3PKbjYMFT2eafeCg0m8fHePnUA5UchMXrLVpu1zx6YB aZnEhmJlSAUC3HB68mL7wxriVRREJZ9YQVjBC2zg1tcbZ6Q5HXqNGXuPlyWA9gO2+eah KgIw== X-Gm-Message-State: AOAM531QCO5t84ucznRPFUZN/vUT2e4EMIg83wttVY5zeqEGruRkh1gQ Ha/WPd2CrsDpro+4AHOC6bs= X-Received: by 2002:a05:6402:3554:b0:419:402f:f632 with SMTP id f20-20020a056402355400b00419402ff632mr24866670edd.1.1649530151322; Sat, 09 Apr 2022 11:49:11 -0700 (PDT) Received: from localhost.localdomain (host-87-4-4-217.retail.telecomitalia.it. [87.4.4.217]) by smtp.gmail.com with ESMTPSA id u6-20020a17090626c600b006e74ef7f092sm8143843ejc.176.2022.04.09.11.49.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Apr 2022 11:49:09 -0700 (PDT) From: "Fabio M. De Francesco" To: Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Ira Weiny , Thomas Gleixner Cc: "Fabio M. De Francesco" Subject: [PATCH] Documentation/vm: Extend "Temporary Virtual Mappings" in highmem.rst Date: Sat, 9 Apr 2022 20:49:07 +0200 Message-Id: <20220409184907.25122-1-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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. 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 and in-code comments from ./include/linux/highmem.h. Cc: Jonathan Corbet Cc: Thomas Gleixner Cc: Ira Weiny Signed-off-by: Fabio M. De Francesco --- Documentation/vm/highmem.rst | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/Documentation/vm/highmem.rst b/Documentation/vm/highmem.rst index 0f69a9fec34d..d9ec26d921c8 100644 --- a/Documentation/vm/highmem.rst +++ b/Documentation/vm/highmem.rst @@ -52,25 +52,65 @@ 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. +* 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(). This can be used to make long 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, page must be released with kunmap(). -* 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. + 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. - 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. + 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; therefore, + newer code is discouraged from using kmap(). - It may be assumed that k[un]map_atomic() won't fail. +* 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 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 (see below). + + 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. + +* kmap_local_*(). These provide a set of functions similar to kmap_atomic() and + are used to require short term mappings. They can be invoked from any context + (including interrupts). + + The mapping can only be used in the context which acquired it, it is 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. Using kmap_atomic ================= -- 2.34.1