Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp5637578imw; Wed, 20 Jul 2022 09:24:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sQFNVObp5mn6wZndy8yUYjEU0a/Q+/I+RcnqYZigt2XYZ+TXlX17Iw07u8KCoZ7762Hxsd X-Received: by 2002:a92:cd85:0:b0:2dc:7a9f:e14d with SMTP id r5-20020a92cd85000000b002dc7a9fe14dmr18254310ilb.314.1658334280835; Wed, 20 Jul 2022 09:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658334280; cv=none; d=google.com; s=arc-20160816; b=arvllHDR27o8x1b7lVSwGa5tdckX0DwHLv1Vr3OfhoA+BgedsJfKSupyAxSSxS3bcr sW3QOxUwPd1+soaFubHAKvY+DoApWu5L/Xe93Ht7orq9+95FmYnbBoGJ95bG/VPEDkqf P3D7Bdwe55hzFI1cNZgvZNYlj/tK3xqnfRUIIv6TC0fAbH2BrNQBo4TwtYGBCPoHgHUB 1vc9Ad0pHNc+yeZjO6HP6hHfhSNzN/aTNwz/6nymup6BWZUR5mExLqwzrL1whiW8jpOd 6eVmAOfRCIWr1vH3ShkzlBJc2jIdHw5cPfjuED+einJ6O/t8eFf8S36wHThqA7jBks2y vgZw== 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=EjQ9X1UjGVK0ekLcqOx1pwSEWHylQ1/QnduEAyjuRmc=; b=aJRNIAjWm4WhE2vNgXGXXTPlH+JvfCvDEBOL4Jfw7U/avMjfWmEb+lwN7YRwhnNzBR XL1/H6ofROgtgt2w202JIF7VYvHM+tPkboXBVxsFSRCEZs/4ENb75pIPYV7S6pCZ97Nl m9NYsIdLo2dpYHLzqhyOAxpQbi+a+vzAKcbzuhLD89ARamNnSN3BJb6dUEECkWuNUrwH Pm5ypblR0wP2wb0Rv+1En2uEXxeH/JVrj+ZTUi+j6VWIuiCc7j4hqlx8biyz0VTVnx/j Wnb/fSKn4QZC/SROel4FOgzsx32zWc1qZxbWTxBnArcei3m0Y/itM94zn2F6bx12G3FF xSzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="TByb/N/D"; 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 i1-20020a056602134100b00674fbd92d7asi22048134iov.100.2022.07.20.09.24.26; Wed, 20 Jul 2022 09:24:40 -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="TByb/N/D"; 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 S238559AbiGTQTm (ORCPT + 99 others); Wed, 20 Jul 2022 12:19:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232026AbiGTQTj (ORCPT ); Wed, 20 Jul 2022 12:19:39 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2D0156BBE; Wed, 20 Jul 2022 09:19:38 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id n12so13988260wrc.8; Wed, 20 Jul 2022 09:19:38 -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=EjQ9X1UjGVK0ekLcqOx1pwSEWHylQ1/QnduEAyjuRmc=; b=TByb/N/DHTn7/Z8JvsFqoLDpF8Ud+M5a3LoilYUmA/eyfKEx42r81eG6MYdOEfgT1b CyOLK1u3RyiMHhsl5/6FO7PYqJKe6sxhKsAM8Uld9SbQQI36YnFbtxuVWIuq/SxEl0Rr L9AmlXzOhJ9xDl7t2/vsIaL6hr6McskDftxnSeG6mW4hFGjj4+IRjwn6WzrN32tHV3hw lECz+WC6NQloeLCM0lHGCHaloIDT2og3ckuOwG+1g8OdLGB/ShhY+CK+WuBU+HaGG4Hi 1d2gd0DC/4WBF/iXZFvoN8P6zsVLKSL23TDicgJ7Lm1HqGw5mfTo5G4Fv1vias2Z8R7g Cxdw== 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=EjQ9X1UjGVK0ekLcqOx1pwSEWHylQ1/QnduEAyjuRmc=; b=MVc0fFv5fX8AkKnelFO5TBSZVkvVf9xS4z1Coox7urWEYp2/fL/7Km33kneBA7yaC/ S/s2nY5V51swPLELDiWWUv1524s4WwE89I7reRO0sPOtNcixL1GcGGJsKaLi7sH7yC06 f4qtRr/V9EOtBW2GKEDREK3HhQKBW2vW00Jda5du6hjePuO25fCSyQcS2SugzHdjLAp0 R5T3602FPRWtlBt35mmxnrq2UVu1BY7W4CXTS3a/Byewt3fOsyQYxzI47q7Hwspcuwc2 sCady3YgeSv+RSOQf+BQZBfCW/leiEClKgvtpWz2krOlv3yCiKY5Uc3iXi3UYl4KnCSn NsVw== X-Gm-Message-State: AJIora+tAcKaChBR8HygejJtTN/Y3tU0OG30SVFfaE4CRq98iC50mGoT VPO9iqHpbaP6MEcagVB+nAc= X-Received: by 2002:adf:d1e8:0:b0:21d:ac9c:983d with SMTP id g8-20020adfd1e8000000b0021dac9c983dmr31067113wrd.629.1658333977372; Wed, 20 Jul 2022 09:19:37 -0700 (PDT) Received: from localhost.localdomain (host-95-235-102-55.retail.telecomitalia.it. [95.235.102.55]) by smtp.gmail.com with ESMTPSA id p20-20020a1c5454000000b003a30c3d0c9csm3044513wmi.8.2022.07.20.09.19.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 09:19:36 -0700 (PDT) From: "Fabio M. De Francesco" To: Luis Chamberlain , Matthew Wilcox , Ira Weiny , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Takashi Iwai , Adam Manzanares , Davidlohr Bueso Cc: "Fabio M. De Francesco" , Matthew Wilcox Subject: [PATCH v2] module: Replace kmap() with kmap_local_page() Date: Wed, 20 Jul 2022 18:19:32 +0200 Message-Id: <20220720161932.9567-1-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 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 kmap() is being deprecated in favor of kmap_local_page(). Two main problems with kmap(): (1) It comes with an overhead as mapping space is restricted and protected by a global lock for synchronization and (2) it 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. With kmap_local_page() the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Tasks can be preempted and, when scheduled to run again, the kernel virtual addresses are restored and still valid. kmap_local_page() is faster than kmap() in kernels with HIGHMEM enabled. Since the use of kmap_local_page() in module_gzip_decompress() and in module_xz_decompress() is safe (i.e., it does not break the strict rules of use), it should be preferred over kmap(). Therefore, replace kmap() with kmap_local_page(). Tested on a QEMU/KVM x86_32 VM with 4GB RAM, booting kernels with HIGHMEM64GB enabled. Modules compressed with XZ or GZIP decompress properly. Cc: Matthew Wilcox Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- v1->v2: Add information which says that the pointers returned by kmap_local_page() are still valid if tasks are preempted and then rescheduled to run again (thanks to Luis Chamberlain). kernel/module/decompress.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/module/decompress.c b/kernel/module/decompress.c index 2fc7081dd7c1..4d0bcb3d9e44 100644 --- a/kernel/module/decompress.c +++ b/kernel/module/decompress.c @@ -119,10 +119,10 @@ static ssize_t module_gzip_decompress(struct load_info *info, goto out_inflate_end; } - s.next_out = kmap(page); + s.next_out = kmap_local_page(page); s.avail_out = PAGE_SIZE; rc = zlib_inflate(&s, 0); - kunmap(page); + kunmap_local(s.next_out); new_size += PAGE_SIZE - s.avail_out; } while (rc == Z_OK); @@ -178,11 +178,11 @@ static ssize_t module_xz_decompress(struct load_info *info, goto out; } - xz_buf.out = kmap(page); + xz_buf.out = kmap_local_page(page); xz_buf.out_pos = 0; xz_buf.out_size = PAGE_SIZE; xz_ret = xz_dec_run(xz_dec, &xz_buf); - kunmap(page); + kunmap_local(xz_buf.out); new_size += xz_buf.out_pos; } while (xz_buf.out_pos == PAGE_SIZE && xz_ret == XZ_OK); -- 2.37.1