Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp473898imn; Wed, 3 Aug 2022 11:49:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tZl3WEq5ax3Jub/a5/f4E/0jiR1R1LmuWcodFlveJ/ZsvGqGl6nZIIU+Z1aRdE+gDF5wqD X-Received: by 2002:a63:6b06:0:b0:41b:79cd:f364 with SMTP id g6-20020a636b06000000b0041b79cdf364mr22594588pgc.119.1659552564500; Wed, 03 Aug 2022 11:49:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659552564; cv=none; d=google.com; s=arc-20160816; b=FxNnvYUs0forQlkPACpbO3MdV5R2PaTRf4JyqGs/NlpuqsDyeUnXK28Bk30FjgF9D2 5rAh9CFa5cPbn10+XgI1Km/qK3HaBggfwqv1Cwb/TLsuWe4o558XMWfYt/02R9imv6Bv vCDofT9gmzvHzk0nxDZMOBWIrGnrLG8KZedPg7jJm5F2G4VrGxFrsGvh0Wid1bbMmxFj uDysBt0SKlelbDe4PQ3JPDTdluQD13irzZbzjMVfVyleliVXSGjGBgO45aHAo3rgLGe8 ClXGsvNs/9UDu9RBiobATIOxJdxU9bL1M79cpFWRM0X/Ps3mdrtST3sC/uoa4p/FYZE0 mSWA== 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=oJmOeTx06Lwu2fiuMzcG5dIKwqxwvQUWSEnoeJ++UWc=; b=qnyPTZ1v5X8uSlta1AefHz1HxeeAAck4FkP0ijsKhb8IPZZN4/C5COtzx+nl+ATNiu UHsmzhaspPl+h6Fu+BsnO+GQNvR7m9Os98mUeQRAKGAt3CfUc36vsDRn2q34rgIktaSW 753hL4NHewM3N4ecKW8Vf8mePgeL2h5tBBLiBviPTCAtScADp/BsXVREF/RsV6aA3tMx HJGUiQOxjK0XkajvhPdXY8FyDATROOwKPHHTpk8jDi4lG9yktm5TwYuRoBlUy1Hwceop CET3XnRyfhXXCZLn9HJXnYJvotlWhtE1xqIhVEn6Bxe6CvwTwdG5O9WzFeyYYhlbJ+uO FQxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JmYucQwk; 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 np2-20020a17090b4c4200b001f540d802besi2984326pjb.131.2022.08.03.11.49.10; Wed, 03 Aug 2022 11:49:24 -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=JmYucQwk; 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 S238124AbiHCS3H (ORCPT + 99 others); Wed, 3 Aug 2022 14:29:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236751AbiHCS3F (ORCPT ); Wed, 3 Aug 2022 14:29:05 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4902A3DF17; Wed, 3 Aug 2022 11:29:04 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id n185so9104607wmn.4; Wed, 03 Aug 2022 11:29:04 -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=oJmOeTx06Lwu2fiuMzcG5dIKwqxwvQUWSEnoeJ++UWc=; b=JmYucQwk2UfoYXlb90FNHzTAHQvr9D2+kHxYT4XYngWqkCCo6RtGwUz21B785dhdnX HkOhgATfzaOWyr+haRcQpS3WeyVQuREVxWKzs6tbeX1P0XFo7iT1knUcUPeFeZXhxeIa dzjREPw6h/c4pQ8yu0R+vWm89jUE/h3QPVOFLzIzas+nhl+SzRRSRnhZq1Cqk9XUYXFK u1KWIqJ/t2fHBBHO07LINKBod4uH1YWiM2YxtS02l2hJb+lI69QBW9hbdDo/Pr1ZTrdK NKSg97XHPHVoSi7xj00wGu4sDNgaAFVS7KhVqm8IlwlM/paEm1YO3bDQjgXF3PgtecRl nmEQ== 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=oJmOeTx06Lwu2fiuMzcG5dIKwqxwvQUWSEnoeJ++UWc=; b=zCseoWTXrhpQjdDEv8JDVfO7Q+vFwpWV6F+UbKggGBTKv3St8/sFRzqPBNnFPfM92i SXOvKovT3MSiEp0YaHRaNBhX/S0f3NJGkcv5thNDguj2mMDVJjGf4KGOxXuqfHCWE2ki PWtDJFmu1obF/UPwOjUkqLwoF935gY73Di3wB71RAP/qTBxWpayTWIrYE9L00wEhOiEx VMO15Hu7g+lgcqyI28i9KWQrxO7j6Zar/fzYN/pcS5T5bgCuMNgS1igJJ3R47h6s/cWj V0KNnET50s3oI1gHAKspVjQpakmmcelthl50E17pUfVhNyvkev3n/U/6QwgbTTGC+k/I 4Qyg== X-Gm-Message-State: ACgBeo300MTyEfuFiTckbcDeddAQ/QigUX3frL7Yl6MAH5ffSLPjrLLq cniuMZhSGCTa/B1udljNzKs= X-Received: by 2002:a7b:ca48:0:b0:3a3:365d:1089 with SMTP id m8-20020a7bca48000000b003a3365d1089mr3853699wml.153.1659551342790; Wed, 03 Aug 2022 11:29:02 -0700 (PDT) Received: from localhost.localdomain (host-79-27-108-198.retail.telecomitalia.it. [79.27.108.198]) by smtp.gmail.com with ESMTPSA id m13-20020a05600c4f4d00b003a3200bc788sm3491904wmq.33.2022.08.03.11.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Aug 2022 11:29:01 -0700 (PDT) From: "Fabio M. De Francesco" To: Alexander Viro , Eric Biederman , Kees Cook , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Ira Weiny Subject: [PATCH v2] fs: Replace kmap{,_atomic}() with kmap_local_page() Date: Wed, 3 Aug 2022 20:28:56 +0200 Message-Id: <20220803182856.28246-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 The use of kmap() and kmap_atomic() are being deprecated in favor of kmap_local_page(). There are 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). It is faster than kmap() in kernels with HIGHMEM enabled. Furthermore, the tasks can be preempted and, when they are scheduled to run again, the kernel virtual addresses are restored and are still valid. Since the use of kmap_local_page() in exec.c is safe, it should be preferred everywhere in exec.c. As said, since kmap_local_page() can be also called from atomic context, and since remove_arg_zero() doesn't (and shouldn't ever) rely on an implicit preempt_disable(), this function can also safely replace kmap_atomic(). Therefore, replace kmap() and kmap_atomic() with kmap_local_page() in fs/exec.c. Tested with xfstests on a QEMU/KVM x86_32 VM, 6GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Eric W. Biederman Suggested-by: Ira Weiny Reviewed-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- v1->v2: Added more information to the commit log to address some objections expressed by Eric W. Biederman[1] in reply to v1. No changes have been made to the code. Forwarded a tag from Ira Weiny (thanks!). [1] https://lore.kernel.org/lkml/8735fmqcfz.fsf@email.froward.int.ebiederm.org/ fs/exec.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 5fd73915c62c..b51dd14e7388 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -584,11 +584,11 @@ static int copy_strings(int argc, struct user_arg_ptr argv, if (kmapped_page) { flush_dcache_page(kmapped_page); - kunmap(kmapped_page); + kunmap_local(kaddr); put_arg_page(kmapped_page); } kmapped_page = page; - kaddr = kmap(kmapped_page); + kaddr = kmap_local_page(kmapped_page); kpos = pos & PAGE_MASK; flush_arg_page(bprm, kpos, kmapped_page); } @@ -602,7 +602,7 @@ static int copy_strings(int argc, struct user_arg_ptr argv, out: if (kmapped_page) { flush_dcache_page(kmapped_page); - kunmap(kmapped_page); + kunmap_local(kaddr); put_arg_page(kmapped_page); } return ret; @@ -880,11 +880,11 @@ int transfer_args_to_stack(struct linux_binprm *bprm, for (index = MAX_ARG_PAGES - 1; index >= stop; index--) { unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0; - char *src = kmap(bprm->page[index]) + offset; + char *src = kmap_local_page(bprm->page[index]) + offset; sp -= PAGE_SIZE - offset; if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0) ret = -EFAULT; - kunmap(bprm->page[index]); + kunmap_local(src); if (ret) goto out; } @@ -1683,13 +1683,13 @@ int remove_arg_zero(struct linux_binprm *bprm) ret = -EFAULT; goto out; } - kaddr = kmap_atomic(page); + kaddr = kmap_local_page(page); for (; offset < PAGE_SIZE && kaddr[offset]; offset++, bprm->p++) ; - kunmap_atomic(kaddr); + kunmap_local(kaddr); put_arg_page(page); } while (offset == PAGE_SIZE); -- 2.37.1