Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp53524pxb; Tue, 12 Apr 2022 16:35:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPS0IIcFUpk5ntlQ8+1Ji7KbKQS3ODUEO7kPoduc6rTQXtR+sGhTMrs/KxFKKNOIFXUs58 X-Received: by 2002:a17:90b:380e:b0:1c7:74f6:ae60 with SMTP id mq14-20020a17090b380e00b001c774f6ae60mr7441634pjb.5.1649806548233; Tue, 12 Apr 2022 16:35:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649806548; cv=none; d=google.com; s=arc-20160816; b=k9DhusdxQQ7NJwcMn2UomnL3ZUMYRKHST6maxwVoeb1oT/v4J78Rt56QQeFIQrA2Tr Fg9tvVNngjScbNtcEXrIiL5bIIEt2ZVRdqpyGoC01olCXFIau5P/SxobWAA7Cis48rQz yssFeNwAfi8wToeKgKPqYde1H2vZeyBJp1XJ+yXogh+zkrZOxLf7UCD79WkHYpfSXDLv mbAP5oV9/xx8hqU/9O2JNd4Ci4JGL6JkYfsj1IuWtiyq+dCugApys2SNlNXlHDIDRP6e lu3Gyll255MjUMXCQoborFfQXHJFtx1zIMcZk/MxqELRX458DfbsKH5Ixz5QaDRfERKO jj1w== 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=lAPe5KLDjByGGButyDlRAtwWbq0RQOm3lpOxpFEkjZM=; b=DQWky6YnVFC8xRmFmS04xvr3YaPUNXq7ef43yokdPJtIDjEFaq8GL1wMu6tzVVHiDi AITalD9l573mRTLQ780ZRccAC628SV/sZjegL4EtbI7gHiWRltMhSFIzk7QD67kzpwQB rniLks+EnKWT4P8gxmtnXwfFyZoSXHOwwdLQz+tDliBulfuAqPNN9QFfr4nSG2PbS4J5 f9OtP9EO93nwrCNBhFlD2uOwX90hWWhdJxQSQYY5Zxa6A26RzhBjXmDohhwZS64nKboN Qkxb0R6NhOenHsI57y9EuMEteSWkJkMPSTljzdkrE6d0I3+YlD1jSkIrkbqS6et/08EX IIDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kKKwpQym; 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 p9-20020a635b09000000b0039d6e35354asi4009047pgb.170.2022.04.12.16.35.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:35:48 -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=kKKwpQym; 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 B30B11B72F5; Tue, 12 Apr 2022 14:27:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355087AbiDLNCQ (ORCPT + 99 others); Tue, 12 Apr 2022 09:02:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351833AbiDLNCJ (ORCPT ); Tue, 12 Apr 2022 09:02:09 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 528466007E; Tue, 12 Apr 2022 05:40:09 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id l7so31625313ejn.2; Tue, 12 Apr 2022 05:40:09 -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=lAPe5KLDjByGGButyDlRAtwWbq0RQOm3lpOxpFEkjZM=; b=kKKwpQym3uM9gA+kAZ3Q2JSdsU6Fj13jhXXwUYhelJ5h/YQZEprLlEo+NFEtbmiZmb y7FUXefqMvevdwxFGVjdfb6xAp/KmwM4zkuCD+WMSH/r1TnwetcGp1wAxzmEW2/kvOSv Y1pZwhX+4+kI83Wy7JLpCIuhjZSaxu+C6fuZoxFVUzNp0rD6ScFbKUtjrLTQJjmhAWiY WDRZuF9Q5s2UhZu8takRrpu9uZvmzbnSm/IiO7OiTFzBtmAFGclpbZkO2DjEtwy5jogu e5lwEvNvy4Vl3vR5hTgFANtMltR9b75mDMSeJIsBdi79bLQ/vpTOZBB+kpEHQhVeiXk0 GLlw== 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=lAPe5KLDjByGGButyDlRAtwWbq0RQOm3lpOxpFEkjZM=; b=XB+UfidMhpcMlAxRAOIB/sAB30uZDZ0/s9ULG3IIP8RaMLPu5h+RzJxMhhxSJe++Lz Y7SETiJHQMymZa4VS9CvBb+ouqv79hskcgTpEjr/Rtm/Xid035ni56JbQNnp1yIcYnOT ooMiAih+HIXj03td/yP+h90hosrdq3pFBjYoq567ZyB5OVt3zGfUCKrxwP9q/qJHLVfR SVP2Uw7AXTX+qPn0/KTuk5k0AOPO2anDo8WY7pnSFopigI89vUS4pdFd9QaiHT1B80EH 72DjZiYayP6PSzwSLtqMLtryQzNU1E8bUbwOeZ6HP3bQiagRc1dL68hBLO6Nj97gG7Zi jVEA== X-Gm-Message-State: AOAM532AA/KP/2BcacQIKsETUdDt0O/H6soCn1CAAeQ8a8o2sJoriOWp 9Xh5LkIYjIGPjuDKrOhzbDI= X-Received: by 2002:a17:907:160a:b0:6e8:3c5d:9ce5 with SMTP id hb10-20020a170907160a00b006e83c5d9ce5mr23576285ejc.606.1649767207759; Tue, 12 Apr 2022 05:40:07 -0700 (PDT) Received: from localhost.localdomain (host-82-60-208-254.retail.telecomitalia.it. [82.60.208.254]) by smtp.gmail.com with ESMTPSA id kv22-20020a17090778d600b006e8a072fb1dsm1670417ejc.172.2022.04.12.05.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 05:40:06 -0700 (PDT) From: "Fabio M. De Francesco" To: Jonathan Corbet , Andrew Morton , SeongJae Park , Jiajian Ye , Alistair Popple , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Thomas Gleixner , Ira Weiny , Matthew Wilcox , Peter Zijlstra Subject: [PATCH v2] Documentation/vm: Extend "Temporary Virtual Mappings" section in highmem.rst Date: Tue, 12 Apr 2022 14:40:03 +0200 Message-Id: <20220412124003.10736-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=-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 --- v1->v2: According to comments on v1 by Mattew Wilcox and Ira Weiny, correct a mistake in text, format paragraphs to stay within the 75 characters limit, re-order the flow of the same paragraphs in decreasing order of preference of usage. 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