Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1076890imw; Thu, 14 Jul 2022 17:02:08 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t1LKeiyIgdJliI5TlzP+aW6ZIqBAH/ShSqrLh4AVXU7wwB6yH8C3Zwv9AG/a+4xBtGjyCG X-Received: by 2002:a05:6870:c1cd:b0:fe:1295:6e34 with SMTP id i13-20020a056870c1cd00b000fe12956e34mr6052084oad.137.1657843328463; Thu, 14 Jul 2022 17:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657843328; cv=none; d=google.com; s=arc-20160816; b=RBjzQSnHPbaZH/Bc1cFrOrZCctk/Uyd5v/5v/Mj83iRTAIPqRjD3nefmkeHHxBtGL6 Bv9FiJgXyUTS35EcgT4L6DANWveoUDQ2nTteXwryIzwGP+gX0komhmXVyVsWcDlXlmw9 y/Ugn5+EvXX1YlpJI2N9NHNBBekiI/QaJGh3XQDBLM8Jxyl8skHCrCA0HzmT8dkKYZFq PpzG4rvnrZgG3UiyArPpSse/uJ80hMgjaJwRpuTaUukvLKggGtMqaN0cSNIQsFaoivEH dq8m1sH3Dw4Rs0R8cIUBRZ2WQdZVRwZAygzNt/tP1b0ebclWdiQ5GH/A7B4nISa3DQlB 4a5Q== 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=14gDsozi2dxw36iEkIGIYX8ruOdQSltClzgyGBSvwFk=; b=ZDC0IjHMcNnkcBdY6mxEAi13fOOmbonH0r6kqhcOMVjF8FHgwkJxaYbaLjNsRfw+w/ y0Wd0cyLfNdt7a8BQG4qzO0aME5eOfZb/yQvcodf9j00+ApHmoTgl4UqunaGUOjDS/zm 33bjlTEsJpFwVMPwlh0kgmc/da8wDbGUXrnDdEuzAyBtOHXqrRGJSEcRIdScWr6sQZ4v Ss/vq6iyl/wRLcF2Eo9Qyi9UELmKVdXDfEYCW/R/ecb6owjO2iBrSNdS2srEHC+P1IL1 fcJbORMIIPW4XWN6iST58s+vHr8lF28o9UQRkmx+2/lVuokoiz8nvNwURVEWYm/znmcs XscQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QTSLPpcM; 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 m29-20020a056870059d00b00101c3edb492si3262817oap.129.2022.07.14.17.01.54; Thu, 14 Jul 2022 17:02:08 -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=QTSLPpcM; 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 S240282AbiGNXuv (ORCPT + 99 others); Thu, 14 Jul 2022 19:50:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229556AbiGNXuu (ORCPT ); Thu, 14 Jul 2022 19:50:50 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 663756D9D9; Thu, 14 Jul 2022 16:50:48 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id ay25so1934653wmb.1; Thu, 14 Jul 2022 16:50:48 -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=14gDsozi2dxw36iEkIGIYX8ruOdQSltClzgyGBSvwFk=; b=QTSLPpcMK6UNDDFzArozTzjeYq/T6FkU0p6ZFqv4CsYkMU8UYFGCmpowTvfGWqdJnh NpJ649Yh6MHohMCeHxVX0VVypptQOczDV13puf7wGqbVtvj+G38QMQR6qtzih2N9VKDL kP8bVj1ZQ1tdrz87DWtY6WbV/wRfu+mYszLGaTqbbNUt+/Rn6Vv0Emg7hlsijzOaqIo2 vUMqKhc7UXRkRHKPfaGMrOpsLDmWPtLT/qHqJekrphGjH7tQ1HlP3GTmW6U/ewDFfq5u JLMGa+XoAKJioLjAG09khYm/UC+3OsHfcSEnGc8wRBiaUmzt4DVdjQdToR7esSIKOqpB 78UQ== 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=14gDsozi2dxw36iEkIGIYX8ruOdQSltClzgyGBSvwFk=; b=LBpRfxK2Uk+YSMvquNuOaUpzEKGwfoXl4jo/378/KVhdEBtFo/7ysOyAPK+m8YRTRe bfOq2GTdmE+aSMTYLxibBpqzKOMUfXy31fkgh5sFYw0Q2afhQy+hFHu1+Go0+8FxWLFV WIpjCTc8WZkOKlmNmrWSQ0JkgAXPIT+1vffOa1Ynxp0dGib0sRZnER/zYPmFjRryAk3b R7HwtHWX7LfU1vwj9UJatTwwOrO8XsARglpiJ81oREpxzdROHvfGs8/JdRjW/R+QeZhn j/Jp09SThTyKR0f87cY4RIZCqOJ6g6voiwbL3wRACY7G0rsHE+Ceb59C3g+U7cjhPCmx A3dw== X-Gm-Message-State: AJIora9fE6tP83jTpSJdWA7v6SP9Sdz8zWHBOh3EL645W/r1nu10KH+E 9oYJn0SoJbfqsALePLLUDl7Y5ppKBJk= X-Received: by 2002:a05:600c:4f08:b0:3a1:99ed:4f1f with SMTP id l8-20020a05600c4f0800b003a199ed4f1fmr17363082wmq.199.1657842646873; Thu, 14 Jul 2022 16:50:46 -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 a16-20020adfdd10000000b0021d6e917442sm2920854wrm.72.2022.07.14.16.50.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Jul 2022 16:50:45 -0700 (PDT) From: "Fabio M. De Francesco" To: Shuah Khan , Greg Kroah-Hartman , Takashi Iwai , Luis Chamberlain , Tianfei zhang , Russ Weight , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Ira Weiny , kernel test robot Subject: [PATCH v2] firmware_loader: Replace kmap() with kmap_local_page() Date: Fri, 15 Jul 2022 01:50:30 +0200 Message-Id: <20220714235030.12732-1-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.0 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,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 The use of 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) 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. kmap_local_page() is preferred over kmap() and kmap_atomic(). Where it cannot mechanically replace the latters, code refactor should be considered (special care must be taken if kernel virtual addresses are aliases in different contexts). With kmap_local_page() the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Call kmap_local_page() in firmware_loader wherever kmap() is currently used. In firmware_rw() use the helpers copy_{from,to}_page() instead of open coding the local mappings + memcpy(). Successfully tested with "firmware" selftests on a QEMU/KVM 32-bits VM with 4GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Greg Kroah-Hartman Cc: Luis Chamberlain Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- v1->v2: According to the comments from Greg Kroah-Hartman (thanks!), extend the commit message adding information about why kmap() should be avoided. Delete an unused variable left in the code of v1, which has been Reported-by: kernel test robot drivers/base/firmware_loader/main.c | 4 ++-- drivers/base/firmware_loader/sysfs.c | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c index ac3f34e80194..7c3590fd97c2 100644 --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -435,11 +435,11 @@ static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv, /* decompress onto the new allocated page */ page = fw_priv->pages[fw_priv->nr_pages - 1]; - 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); fw_priv->size += xz_buf.out_pos; /* partial decompression means either end or error */ if (xz_buf.out_pos != PAGE_SIZE) diff --git a/drivers/base/firmware_loader/sysfs.c b/drivers/base/firmware_loader/sysfs.c index 5b0b85b70b6f..77bad32c481a 100644 --- a/drivers/base/firmware_loader/sysfs.c +++ b/drivers/base/firmware_loader/sysfs.c @@ -242,19 +242,17 @@ static void firmware_rw(struct fw_priv *fw_priv, char *buffer, loff_t offset, size_t count, bool read) { while (count) { - void *page_data; int page_nr = offset >> PAGE_SHIFT; int page_ofs = offset & (PAGE_SIZE - 1); int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); - page_data = kmap(fw_priv->pages[page_nr]); - if (read) - memcpy(buffer, page_data + page_ofs, page_cnt); + memcpy_from_page(buffer, fw_priv->pages[page_nr], + page_ofs, page_cnt); else - memcpy(page_data + page_ofs, buffer, page_cnt); + memcpy_to_page(fw_priv->pages[page_nr], page_ofs, + buffer, page_cnt); - kunmap(fw_priv->pages[page_nr]); buffer += page_cnt; offset += page_cnt; count -= page_cnt; -- 2.37.0