From patchwork Sun Aug 21 18:10:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 12950031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39D00C00140 for ; Sun, 21 Aug 2022 18:11:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231522AbiHUSK7 (ORCPT ); Sun, 21 Aug 2022 14:10:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229491AbiHUSKz (ORCPT ); Sun, 21 Aug 2022 14:10:55 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 921101F61C; Sun, 21 Aug 2022 11:10:54 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id b16so11298643edd.4; Sun, 21 Aug 2022 11:10:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=jYj5mCWvOjlW6xecbhNlhHuS+q5rYaMaftIgTax2JSA=; b=gwxheF0L63ElIWReq3YUmOQgIR5oDk9yrM2IxdTf2crJHflaisczuqAld101Dy9z+E ygFa3mhHF+BSGtQHEitM4N++r6v0XwS9wIr+HcLldcb2kwcwNMmDhgOez5saqz5l/p3v guNfB/qZQ/AK5WWdb07dEbcmBF50+DmF2orUboAerretT9TttojJCS2Psgw6EOzWDkQj W/DPy89ESIP0Yx1CaPd/5t5Q+E5N/6UlKInE5h0nZNwiteLnJrFG/U0ZkBgLwl1AuvBL MEw+ptmTGSqP2KezZALPrNRAeoPlcjTwWdwWM4GmGPyX7cyh4V8SL3z2CXvbLaJaPh1M mNQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=jYj5mCWvOjlW6xecbhNlhHuS+q5rYaMaftIgTax2JSA=; b=jvLBfuXbZV2r6EmfVmtSpAlX0/PNFerPCUSAgfs9hQxhVR3uVHpzVqKbmbm939pTiR abQtq6oND9DU44qZU4B2vc3YnRZNzSAVeBfIgx/pEFGT5HVxpGMqM7mRYeEBgMmTXE14 06k2o9wjkLRQ+monHBtau3pUFXGLdr/kwjb6QQqdbQkAeFL5Rysw0KmyjNKAwLuQmjcW JQ7kVL5oymf6R7Qq0yU1Mt4NJ6DYjn+026AjF3PlAvVTGwL6LEaP4KXmqkASmdzEWYfE pFJaZCb6mcFcSIne+5C0UMeEonAqYv81X6l0Iwy/q510H+FrLq7bGiUcFeZhhHv5mjf0 sEtA== X-Gm-Message-State: ACgBeo0MpTJKMJ7RJgPoqIZ9ALnbETARn6zG5D2fH5AJmGZ+ChOfV4TB yxgXgpUujBCfD70C4cCxuxY= X-Google-Smtp-Source: AA6agR5z2XjUVoKdvWOyMY/zDACnaLsPEY3+Ahzzq7coE6eLa+4SccWoksduivlZnk/t+Gch85cX3w== X-Received: by 2002:a05:6402:438d:b0:446:a0b4:630d with SMTP id o13-20020a056402438d00b00446a0b4630dmr3126032edc.118.1661105453171; Sun, 21 Aug 2022 11:10:53 -0700 (PDT) Received: from localhost.localdomain (host-87-17-106-94.retail.telecomitalia.it. [87.17.106.94]) by smtp.gmail.com with ESMTPSA id g12-20020a056402114c00b004404e290e7esm6854178edw.77.2022.08.21.11.10.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 11:10:51 -0700 (PDT) From: "Fabio M. De Francesco" To: Ira Weiny , "Matthew Wilcox (Oracle)" , Jens Axboe , Bart Van Assche , Christian Brauner , Andrew Morton , Kees Cook , Jeff Layton , Damien Le Moal , Arnd Bergmann , Roman Gushchin , Muchun Song , Chaitanya Kulkarni , "Martin K. Petersen" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Viacheslav Dubeyko Subject: [RESEND PATCH 1/4] hfsplus: Unmap the page in the "fail_page" label Date: Sun, 21 Aug 2022 20:10:42 +0200 Message-Id: <20220821181045.8528-2-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220821181045.8528-1-fmdefrancesco@gmail.com> References: <20220821181045.8528-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Several paths within hfs_btree_open() jump to the "fail_page" label where put_page() is called while the page is still mapped. Call kunmap() to unmap the page soon before put_page(). Cc: Viacheslav Dubeyko Reviewed-by: Ira Weiny Reviewed-by: Viacheslav Dubeyko Signed-off-by: Fabio M. De Francesco --- fs/hfsplus/btree.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c index 66774f4cb4fd..3a917a9a4edd 100644 --- a/fs/hfsplus/btree.c +++ b/fs/hfsplus/btree.c @@ -245,6 +245,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) return tree; fail_page: + kunmap(page); put_page(page); free_inode: tree->inode->i_mapping->a_ops = &hfsplus_aops; From patchwork Sun Aug 21 18:10:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 12950032 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89563C32789 for ; Sun, 21 Aug 2022 18:11:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231535AbiHUSLA (ORCPT ); Sun, 21 Aug 2022 14:11:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231501AbiHUSK6 (ORCPT ); Sun, 21 Aug 2022 14:10:58 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 628661F610; Sun, 21 Aug 2022 11:10:57 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id o22so11289797edc.10; Sun, 21 Aug 2022 11:10:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=ioMrSAs8sNjfAfDitsku8c0xpDfGqYRpL6V1iOYxAGA=; b=ps+4SVtTdwy2RegcovcQEopOJg75YcvyjeTXcmiueG/NTX/omweV5R+PBo68JP4Ffu 77UpqmVqPKF14Loiu45qHRLQAI1Seh1Xpz8M5fFs67Jx0cxZxqr0yzzXL5K/+YO9Nz7S XrL1W/+vFReOO2J/slfKlvt/kHJh3hvLxA3dPI4DKoEZuEwTDgfnmQRYg9B6VDMHvVWB ZJm7hbIgrYYckg8HkQMF4qaB8CQGrlaCTvZpShCQMNWyi++r7/K2EJX/1a1Dn/A5zomU mBDwnsig7fopBe6ZXSQ55psNrwaKDFa0WkiwdUdJoKBmesQ3q2Wwam1e2wz3JUUn06vd KoZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=ioMrSAs8sNjfAfDitsku8c0xpDfGqYRpL6V1iOYxAGA=; b=wanFShRbPemvRDnOOXCrQx3rrkCvqxFmpCjCPJLdqz+tj9xN+YHqolz8oX6MohYg3V ZPAkmHLlFHO5jQVwWkdQp9KPW7T2IVIcl6sLRohPjjn4wNaI4Fh2DNqLlfxB2IsWfDJR FeKWvKDIuOGGptiuNARQg4crkD8eMZSOPakkqCjkh1MnDQ1V58GL+nfbzJIo3cV/5ZYO /6umO3qTqz0eHOoS8CJoL50mSUbzDD0PRvQfn1n/1KWrJYRhvxZ+lbHkfz/vxoUGAifF sFiqNr/xgKx7uXQMJjXo3X83b1m4sYLE3qVB4L7e98hlXRzrFzxBWIcJiztyvHRrCBc/ zFaw== X-Gm-Message-State: ACgBeo3nKDL42bsejZLqjxuY1YD3YfMTZq6+o/cjlwErqBUaDDhrY7ka pWZE0dTStmwroMCgoJ6wQgU= X-Google-Smtp-Source: AA6agR6uXbmj7CWS8n4xSh4oCDNuMnyXcTgJWGZALbmrVg85eanBlXXf2YDm1y5GHQ48DAzzldbPmw== X-Received: by 2002:aa7:d508:0:b0:445:dd73:4f6f with SMTP id y8-20020aa7d508000000b00445dd734f6fmr13241787edq.231.1661105455824; Sun, 21 Aug 2022 11:10:55 -0700 (PDT) Received: from localhost.localdomain (host-87-17-106-94.retail.telecomitalia.it. [87.17.106.94]) by smtp.gmail.com with ESMTPSA id g12-20020a056402114c00b004404e290e7esm6854178edw.77.2022.08.21.11.10.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 11:10:54 -0700 (PDT) From: "Fabio M. De Francesco" To: Ira Weiny , "Matthew Wilcox (Oracle)" , Jens Axboe , Bart Van Assche , Christian Brauner , Andrew Morton , Kees Cook , Jeff Layton , Damien Le Moal , Arnd Bergmann , Roman Gushchin , Muchun Song , Chaitanya Kulkarni , "Martin K. Petersen" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Viacheslav Dubeyko Subject: [RESEND PATCH 2/4] hfsplus: Convert kmap() to kmap_local_page() in bnode.c Date: Sun, 21 Aug 2022 20:10:43 +0200 Message-Id: <20220821181045.8528-3-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220821181045.8528-1-fmdefrancesco@gmail.com> References: <20220821181045.8528-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@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). 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 still valid. Since its use in bnode.c is safe everywhere, it should be preferred. Therefore, replace kmap() with kmap_local_page() in bnode.c. Where possible, use the suited standard helpers (memzero_page(), memcpy_page()) instead of open coding kmap_local_page() plus memset() or memcpy(). Tested in a QEMU/KVM x86_32 VM, 6GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Viacheslav Dubeyko Suggested-by: Ira Weiny Reviewed-by: Ira Weiny Reviewed-by: Viacheslav Dubeyko Signed-off-by: Fabio M. De Francesco --- fs/hfsplus/bnode.c | 105 +++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c index a5ab00e54220..87974d5e6791 100644 --- a/fs/hfsplus/bnode.c +++ b/fs/hfsplus/bnode.c @@ -29,14 +29,12 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len) off &= ~PAGE_MASK; l = min_t(int, len, PAGE_SIZE - off); - memcpy(buf, kmap(*pagep) + off, l); - kunmap(*pagep); + memcpy_from_page(buf, *pagep, off, l); while ((len -= l) != 0) { buf += l; l = min_t(int, len, PAGE_SIZE); - memcpy(buf, kmap(*++pagep), l); - kunmap(*pagep); + memcpy_from_page(buf, *++pagep, 0, l); } } @@ -82,16 +80,14 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len) off &= ~PAGE_MASK; l = min_t(int, len, PAGE_SIZE - off); - memcpy(kmap(*pagep) + off, buf, l); + memcpy_to_page(*pagep, off, buf, l); set_page_dirty(*pagep); - kunmap(*pagep); while ((len -= l) != 0) { buf += l; l = min_t(int, len, PAGE_SIZE); - memcpy(kmap(*++pagep), buf, l); + memcpy_to_page(*++pagep, 0, buf, l); set_page_dirty(*pagep); - kunmap(*pagep); } } @@ -112,15 +108,13 @@ void hfs_bnode_clear(struct hfs_bnode *node, int off, int len) off &= ~PAGE_MASK; l = min_t(int, len, PAGE_SIZE - off); - memset(kmap(*pagep) + off, 0, l); + memzero_page(*pagep, off, l); set_page_dirty(*pagep); - kunmap(*pagep); while ((len -= l) != 0) { l = min_t(int, len, PAGE_SIZE); - memset(kmap(*++pagep), 0, l); + memzero_page(*++pagep, 0, l); set_page_dirty(*pagep); - kunmap(*pagep); } } @@ -142,24 +136,20 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst, if (src == dst) { l = min_t(int, len, PAGE_SIZE - src); - memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l); - kunmap(*src_page); + memcpy_page(*dst_page, src, *src_page, src, l); set_page_dirty(*dst_page); - kunmap(*dst_page); while ((len -= l) != 0) { l = min_t(int, len, PAGE_SIZE); - memcpy(kmap(*++dst_page), kmap(*++src_page), l); - kunmap(*src_page); + memcpy_page(*++dst_page, 0, *++src_page, 0, l); set_page_dirty(*dst_page); - kunmap(*dst_page); } } else { void *src_ptr, *dst_ptr; do { - src_ptr = kmap(*src_page) + src; - dst_ptr = kmap(*dst_page) + dst; + dst_ptr = kmap_local_page(*dst_page) + dst; + src_ptr = kmap_local_page(*src_page) + src; if (PAGE_SIZE - src < PAGE_SIZE - dst) { l = PAGE_SIZE - src; src = 0; @@ -171,9 +161,9 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst, } l = min(len, l); memcpy(dst_ptr, src_ptr, l); - kunmap(*src_page); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); if (!dst) dst_page++; else @@ -185,6 +175,7 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst, void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len) { struct page **src_page, **dst_page; + void *src_ptr, *dst_ptr; int l; hfs_dbg(BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len); @@ -202,27 +193,28 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len) if (src == dst) { while (src < len) { - memmove(kmap(*dst_page), kmap(*src_page), src); - kunmap(*src_page); + dst_ptr = kmap_local_page(*dst_page); + src_ptr = kmap_local_page(*src_page); + memmove(dst_ptr, src_ptr, src); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); len -= src; src = PAGE_SIZE; src_page--; dst_page--; } src -= len; - memmove(kmap(*dst_page) + src, - kmap(*src_page) + src, len); - kunmap(*src_page); + dst_ptr = kmap_local_page(*dst_page); + src_ptr = kmap_local_page(*src_page); + memmove(dst_ptr + src, src_ptr + src, len); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); } else { - void *src_ptr, *dst_ptr; - do { - src_ptr = kmap(*src_page) + src; - dst_ptr = kmap(*dst_page) + dst; + dst_ptr = kmap_local_page(*dst_page) + dst; + src_ptr = kmap_local_page(*src_page) + src; if (src < dst) { l = src; src = PAGE_SIZE; @@ -234,9 +226,9 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len) } l = min(len, l); memmove(dst_ptr - l, src_ptr - l, l); - kunmap(*src_page); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); if (dst == PAGE_SIZE) dst_page--; else @@ -251,26 +243,27 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len) if (src == dst) { l = min_t(int, len, PAGE_SIZE - src); - memmove(kmap(*dst_page) + src, - kmap(*src_page) + src, l); - kunmap(*src_page); + + dst_ptr = kmap_local_page(*dst_page) + src; + src_ptr = kmap_local_page(*src_page) + src; + memmove(dst_ptr, src_ptr, l); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); while ((len -= l) != 0) { l = min_t(int, len, PAGE_SIZE); - memmove(kmap(*++dst_page), - kmap(*++src_page), l); - kunmap(*src_page); + dst_ptr = kmap_local_page(*++dst_page); + src_ptr = kmap_local_page(*++src_page); + memmove(dst_ptr, src_ptr, l); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); } } else { - void *src_ptr, *dst_ptr; - do { - src_ptr = kmap(*src_page) + src; - dst_ptr = kmap(*dst_page) + dst; + dst_ptr = kmap_local_page(*dst_page) + dst; + src_ptr = kmap_local_page(*src_page) + src; if (PAGE_SIZE - src < PAGE_SIZE - dst) { l = PAGE_SIZE - src; @@ -283,9 +276,9 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len) } l = min(len, l); memmove(dst_ptr, src_ptr, l); - kunmap(*src_page); + kunmap_local(src_ptr); set_page_dirty(*dst_page); - kunmap(*dst_page); + kunmap_local(dst_ptr); if (!dst) dst_page++; else @@ -498,14 +491,14 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num) if (!test_bit(HFS_BNODE_NEW, &node->flags)) return node; - desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) + - node->page_offset); + desc = (struct hfs_bnode_desc *)(kmap_local_page(node->page[0]) + + node->page_offset); node->prev = be32_to_cpu(desc->prev); node->next = be32_to_cpu(desc->next); node->num_recs = be16_to_cpu(desc->num_recs); node->type = desc->type; node->height = desc->height; - kunmap(node->page[0]); + kunmap_local(desc); switch (node->type) { case HFS_NODE_HEADER: @@ -589,14 +582,12 @@ struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num) } pagep = node->page; - memset(kmap(*pagep) + node->page_offset, 0, - min_t(int, PAGE_SIZE, tree->node_size)); + memzero_page(*pagep, node->page_offset, + min_t(int, PAGE_SIZE, tree->node_size)); set_page_dirty(*pagep); - kunmap(*pagep); for (i = 1; i < tree->pages_per_bnode; i++) { - memset(kmap(*++pagep), 0, PAGE_SIZE); + memzero_page(*++pagep, 0, PAGE_SIZE); set_page_dirty(*pagep); - kunmap(*pagep); } clear_bit(HFS_BNODE_NEW, &node->flags); wake_up(&node->lock_wq); From patchwork Sun Aug 21 18:10:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 12950033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F32EC00140 for ; Sun, 21 Aug 2022 18:11:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231566AbiHUSLC (ORCPT ); Sun, 21 Aug 2022 14:11:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231557AbiHUSLB (ORCPT ); Sun, 21 Aug 2022 14:11:01 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 404AE1F61C; Sun, 21 Aug 2022 11:11:00 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id ca13so6099332ejb.9; Sun, 21 Aug 2022 11:11:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=n3IGQijfwjjT16DNWgVaC6YqoBuwArJBqNAWrQW3JW0=; b=MBx7PGIKwgNrozehT8oLgpl0Hr2CUm5Qn9Xm5L7kKZZAlG/yrEjuLXhd/wpkYxLHRi SnOF4cgeFKetPPzsaC+JAUkjlZceBHI2FcLvMBS2bWUhshxKn51YBAsVGGVzkZ3/FH4Q 2vlj7jzWsO4/i/3UcA66X6edvoFqQAaXU+CFgW6/5187576M65W1oQ4VhEJIOsUBlmmh Osji80nDWzds+QsmhWghV+ECtlvUhrpqmueUDb6UfLyRjC9O8pDt8Tp9GiMB2fXSpbLx buEv+yG09ZgvlYDTeYYodYD34N2CInWpiVQQzu74yPnB5nwPUaFmYqSVEEEuMNbpfUvO 0RLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=n3IGQijfwjjT16DNWgVaC6YqoBuwArJBqNAWrQW3JW0=; b=q3qzObKGKpHqTxsb7a6QnpiAGzoifTfMJAFh4oUszPBXSfs4Umpk7puz9kwElbZjuQ UPTutnQ/I6Qp588mNXw97OCxtqvsFNVCKlRtMP46BfAgGhQlHe/zIsZNzn4AlawCNRZF 57d7UVB3B+hIJ5cqg9LHVlaUlYuPgaWAmAey3PJuGqkHNW/0NWc4EheXh6LfPIaygmNu wu842/L/c67LiXnb/PVdGicT4THQovXV2PZU/U/XwaY4kMuDSPUPLgakekOR+dqfv1oU aBzEhT3DHtyQWJXBNinQqdo3olT7o3v4Md/iWb3xD1ePItulldcbyzNHF+TvjkObXzrb RU1A== X-Gm-Message-State: ACgBeo0pZyOYPrdgQQXAX3kh1uO+6v4dAZEARQbpCMVK0FAA8UMT434s VJ2Ag5yoHEwzwUMKZEycboU= X-Google-Smtp-Source: AA6agR6kC8UmcQw+hQRh8FLUpHWTGdZ4lMCA2WsGsDyDeukQpMtRZ2I+zpLsalXCgeeiTMrL7g20RQ== X-Received: by 2002:a17:907:842:b0:731:3310:4187 with SMTP id ww2-20020a170907084200b0073133104187mr10130884ejb.578.1661105458775; Sun, 21 Aug 2022 11:10:58 -0700 (PDT) Received: from localhost.localdomain (host-87-17-106-94.retail.telecomitalia.it. [87.17.106.94]) by smtp.gmail.com with ESMTPSA id g12-20020a056402114c00b004404e290e7esm6854178edw.77.2022.08.21.11.10.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 11:10:57 -0700 (PDT) From: "Fabio M. De Francesco" To: Ira Weiny , "Matthew Wilcox (Oracle)" , Jens Axboe , Bart Van Assche , Christian Brauner , Andrew Morton , Kees Cook , Jeff Layton , Damien Le Moal , Arnd Bergmann , Roman Gushchin , Muchun Song , Chaitanya Kulkarni , "Martin K. Petersen" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Viacheslav Dubeyko Subject: [RESEND PATCH 3/4] hfsplus: Convert kmap() to kmap_local_page() in bitmap.c Date: Sun, 21 Aug 2022 20:10:44 +0200 Message-Id: <20220821181045.8528-4-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220821181045.8528-1-fmdefrancesco@gmail.com> References: <20220821181045.8528-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org kmap() is 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 its use in bitmap.c is safe everywhere, it should be preferred. Therefore, replace kmap() with kmap_local_page() in bitmap.c. Tested in a QEMU/KVM x86_32 VM, 6GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Viacheslav Dubeyko Suggested-by: Ira Weiny Reviewed-by: Ira Weiny Reviewed-by: Viacheslav Dubeyko Signed-off-by: Fabio M. De Francesco --- fs/hfsplus/bitmap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/fs/hfsplus/bitmap.c b/fs/hfsplus/bitmap.c index cebce0cfe340..bd8dcea85588 100644 --- a/fs/hfsplus/bitmap.c +++ b/fs/hfsplus/bitmap.c @@ -39,7 +39,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } - pptr = kmap(page); + pptr = kmap_local_page(page); curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32; i = offset % 32; offset &= ~(PAGE_CACHE_BITS - 1); @@ -74,7 +74,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, } curr++; } - kunmap(page); + kunmap_local(pptr); offset += PAGE_CACHE_BITS; if (offset >= size) break; @@ -84,7 +84,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } - curr = pptr = kmap(page); + curr = pptr = kmap_local_page(page); if ((size ^ offset) / PAGE_CACHE_BITS) end = pptr + PAGE_CACHE_BITS / 32; else @@ -127,7 +127,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, len -= 32; } set_page_dirty(page); - kunmap(page); + kunmap_local(pptr); offset += PAGE_CACHE_BITS; page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL); @@ -135,7 +135,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } - pptr = kmap(page); + pptr = kmap_local_page(page); curr = pptr; end = pptr + PAGE_CACHE_BITS / 32; } @@ -151,7 +151,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, done: *curr = cpu_to_be32(n); set_page_dirty(page); - kunmap(page); + kunmap_local(pptr); *max = offset + (curr - pptr) * 32 + i - start; sbi->free_blocks -= *max; hfsplus_mark_mdb_dirty(sb); @@ -185,7 +185,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count) page = read_mapping_page(mapping, pnr, NULL); if (IS_ERR(page)) goto kaboom; - pptr = kmap(page); + pptr = kmap_local_page(page); curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32; end = pptr + PAGE_CACHE_BITS / 32; len = count; @@ -215,11 +215,11 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count) if (!count) break; set_page_dirty(page); - kunmap(page); + kunmap_local(pptr); page = read_mapping_page(mapping, ++pnr, NULL); if (IS_ERR(page)) goto kaboom; - pptr = kmap(page); + pptr = kmap_local_page(page); curr = pptr; end = pptr + PAGE_CACHE_BITS / 32; } @@ -231,7 +231,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count) } out: set_page_dirty(page); - kunmap(page); + kunmap_local(pptr); sbi->free_blocks += len; hfsplus_mark_mdb_dirty(sb); mutex_unlock(&sbi->alloc_mutex); From patchwork Sun Aug 21 18:10:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 12950034 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97EFEC00140 for ; Sun, 21 Aug 2022 18:11:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231597AbiHUSLH (ORCPT ); Sun, 21 Aug 2022 14:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231583AbiHUSLE (ORCPT ); Sun, 21 Aug 2022 14:11:04 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD8361F62E; Sun, 21 Aug 2022 11:11:03 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id c39so11311535edf.0; Sun, 21 Aug 2022 11:11:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=xSc+zCgoKxwL9Nm5hCo9PDGdvwe/GSgOKgfEouLfAZQ=; b=Acx4OVI4Bkjv3z6rKateHfHQZvswRQnfj5bqlwzd8fAqLMGIju7zlixRkY8fSe3CeW IoyCFWjIjeheBMxEdtXEY7I3LuK3E0O+2pgHn8Mvpdlj7vY5GRAyN48mftTGR/BBm857 3nSQwvChGywoHlAoUeb7bG6uHqqwx0gAqljDpj/NWZIY3PuOcwmTnGajkHBbg+SUObpl +TjNi69JlwqANiFfN8FDhegzyksSu7VHCqMhRj3afNiifAj6L+paHwamDqk9oyi7dVaI hvJaXcy4EYCyQFfh/GcMxuQQouMiaoyHj7P+l7ll7kRjYyWoKXOaVITIyV2vLs7XE5sr M1ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=xSc+zCgoKxwL9Nm5hCo9PDGdvwe/GSgOKgfEouLfAZQ=; b=TgfPovwPg0PbIDJlldok+LqotF8KGcoLiKwsDydjG6Gw4huChxYpsFog3n43zhp+/s 3z91BBoigQ6QRMatWiWhlSRgyopfTSlz1BATAxz0k52tkfWk+gquQuTSs0GUibR4pB/6 qA/k+w2cVi+ke+wPfRUMW/bkLJ1RGaJXZks8jcAjSuNyVr0KeRjwDbxiLdauDMQi7vH3 COp7+Koy8S6prh03Qf9wBg9iO199yrUkW6Y92hQavaPeHtroiEvatDcvIWlpSpF/E9DQ vo1jYeQ8WsP0yfEiUVGGNMSj/QS5xDoiRU/K9t2FANYBpnjYcfJ3HJtz3GGE75D6N8ld reMA== X-Gm-Message-State: ACgBeo3N095y1b+iNiOAsnkJtQzEiJP1pIeppt7VplVL1WhkaG0zHsqb GmYwmxFdf2NNd6IikHVdu+w= X-Google-Smtp-Source: AA6agR53O5qdB5bjK4EoanRKtdvrBsKxtRZG7LZNAfJBTGsNFJvu+5nFekfVu3REws7mlODDUvMVYw== X-Received: by 2002:a05:6402:176a:b0:445:d87f:b42d with SMTP id da10-20020a056402176a00b00445d87fb42dmr13119999edb.157.1661105462272; Sun, 21 Aug 2022 11:11:02 -0700 (PDT) Received: from localhost.localdomain (host-87-17-106-94.retail.telecomitalia.it. [87.17.106.94]) by smtp.gmail.com with ESMTPSA id g12-20020a056402114c00b004404e290e7esm6854178edw.77.2022.08.21.11.10.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Aug 2022 11:11:00 -0700 (PDT) From: "Fabio M. De Francesco" To: Ira Weiny , "Matthew Wilcox (Oracle)" , Jens Axboe , Bart Van Assche , Christian Brauner , Andrew Morton , Kees Cook , Jeff Layton , Damien Le Moal , Arnd Bergmann , Roman Gushchin , Muchun Song , Chaitanya Kulkarni , "Martin K. Petersen" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Viacheslav Dubeyko Subject: [RESEND PATCH 4/4] hfsplus: Convert kmap() to kmap_local_page() in btree.c Date: Sun, 21 Aug 2022 20:10:45 +0200 Message-Id: <20220821181045.8528-5-fmdefrancesco@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220821181045.8528-1-fmdefrancesco@gmail.com> References: <20220821181045.8528-1-fmdefrancesco@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org kmap() is 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 its use in btree.c is safe everywhere, it should be preferred. Therefore, replace kmap() with kmap_local_page() in btree.c. Tested in a QEMU/KVM x86_32 VM, 6GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Viacheslav Dubeyko Suggested-by: Ira Weiny Reviewed-by: Ira Weiny Reviewed-by: Viacheslav Dubeyko Signed-off-by: Fabio M. De Francesco --- fs/hfsplus/btree.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c index 3a917a9a4edd..9e1732a2b92a 100644 --- a/fs/hfsplus/btree.c +++ b/fs/hfsplus/btree.c @@ -163,7 +163,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) goto free_inode; /* Load the header */ - head = (struct hfs_btree_header_rec *)(kmap(page) + + head = (struct hfs_btree_header_rec *)(kmap_local_page(page) + sizeof(struct hfs_bnode_desc)); tree->root = be32_to_cpu(head->root); tree->leaf_count = be32_to_cpu(head->leaf_count); @@ -240,12 +240,12 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) (tree->node_size + PAGE_SIZE - 1) >> PAGE_SHIFT; - kunmap(page); + kunmap_local(head); put_page(page); return tree; fail_page: - kunmap(page); + kunmap_local(head); put_page(page); free_inode: tree->inode->i_mapping->a_ops = &hfsplus_aops; @@ -292,7 +292,7 @@ int hfs_btree_write(struct hfs_btree *tree) return -EIO; /* Load the header */ page = node->page[0]; - head = (struct hfs_btree_header_rec *)(kmap(page) + + head = (struct hfs_btree_header_rec *)(kmap_local_page(page) + sizeof(struct hfs_bnode_desc)); head->root = cpu_to_be32(tree->root); @@ -304,7 +304,7 @@ int hfs_btree_write(struct hfs_btree *tree) head->attributes = cpu_to_be32(tree->attributes); head->depth = cpu_to_be16(tree->depth); - kunmap(page); + kunmap_local(head); set_page_dirty(page); hfs_bnode_put(node); return 0; @@ -395,7 +395,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) off += node->page_offset; pagep = node->page + (off >> PAGE_SHIFT); - data = kmap(*pagep); + data = kmap_local_page(*pagep); off &= ~PAGE_MASK; idx = 0; @@ -408,7 +408,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) idx += i; data[off] |= m; set_page_dirty(*pagep); - kunmap(*pagep); + kunmap_local(data); tree->free_nodes--; mark_inode_dirty(tree->inode); hfs_bnode_put(node); @@ -418,14 +418,14 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) } } if (++off >= PAGE_SIZE) { - kunmap(*pagep); - data = kmap(*++pagep); + kunmap_local(data); + data = kmap_local_page(*++pagep); off = 0; } idx += 8; len--; } - kunmap(*pagep); + kunmap_local(data); nidx = node->next; if (!nidx) { hfs_dbg(BNODE_MOD, "create new bmap node\n"); @@ -441,7 +441,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) off = off16; off += node->page_offset; pagep = node->page + (off >> PAGE_SHIFT); - data = kmap(*pagep); + data = kmap_local_page(*pagep); off &= ~PAGE_MASK; } } @@ -491,7 +491,7 @@ void hfs_bmap_free(struct hfs_bnode *node) } off += node->page_offset + nidx / 8; page = node->page[off >> PAGE_SHIFT]; - data = kmap(page); + data = kmap_local_page(page); off &= ~PAGE_MASK; m = 1 << (~nidx & 7); byte = data[off]; @@ -499,13 +499,13 @@ void hfs_bmap_free(struct hfs_bnode *node) pr_crit("trying to free free bnode " "%u(%d)\n", node->this, node->type); - kunmap(page); + kunmap_local(data); hfs_bnode_put(node); return; } data[off] = byte & ~m; set_page_dirty(page); - kunmap(page); + kunmap_local(data); hfs_bnode_put(node); tree->free_nodes++; mark_inode_dirty(tree->inode);