From patchwork Mon Jun 5 07:12:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 9765641 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 72C6E60364 for ; Mon, 5 Jun 2017 07:12:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F98227480 for ; Mon, 5 Jun 2017 07:12:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54A1027F8C; Mon, 5 Jun 2017 07:12:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD87327480 for ; Mon, 5 Jun 2017 07:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751325AbdFEHMm (ORCPT ); Mon, 5 Jun 2017 03:12:42 -0400 Received: from mail-pg0-f46.google.com ([74.125.83.46]:33796 "EHLO mail-pg0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751297AbdFEHMl (ORCPT ); Mon, 5 Jun 2017 03:12:41 -0400 Received: by mail-pg0-f46.google.com with SMTP id v18so6692552pgb.1 for ; Mon, 05 Jun 2017 00:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=tRkElwymS6ItALnvms2JxLBV9g4QU2PXznqXUzIFP3g=; b=JMsZjxGgc33vVvP4nkQprZ7+SCdtMQtwaZjiDzwwAKGiyIQHQpIi0kPDHVtBqmRaA3 LhLIlMezZMVkQGz1jjsI4rW9+cjivHCZy48/VhLue9fj0wlvLZOcSb0hBClU8H42Toye spMCRVFk5Q7QF3P9pJEtaE5k27/2IebqA81pm+u9+woYwJ/Y9gyt9pC4r4Qa7Mqjc4Lq 9K2NodFYbySnRNqkDT4FbG/zSSm6f/zyJNaaPSqegZirW6a1UkaGBur+x1+L+n4jNzry KtgZy3SCYoT1G74SZk+IcVsCObDv/bHfRxycsRFxZTmirp5fqiodkGxOb8zdkFRyUlbv Q47g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=tRkElwymS6ItALnvms2JxLBV9g4QU2PXznqXUzIFP3g=; b=pwvqGjV8iuBHs2FQV6jLlP+cW1z1lZhWIo2aHXX8q0B28j4hGsjgO5sSsomULdkUME WE2VgftpHodBaSMc9lgRnYlKfYBq3Gb07mn468mPcdNhdvKSHMQegQjuAn0zFl9+xbnm fR9BwxNNIcyE0WsjerNI5o7b1D1V7sWU/NEjCHKGxEhOy/t8PoUCsWdP4Jxs/CbyKKvK CrHzUhDyhNis1TkqE+hRucRJdvJH3l3U/wkRejJ1HwrdpfZg6I7tGUa8ccP4tO4tisNv uWPWge8/v+tmeSa0Lk9whubDEBSIZLa42DzCypavzAvK1ciLE/W3TeLOs+RueSxA87zQ rNIA== X-Gm-Message-State: AODbwcAmUwAF6MSwIIe5e+8qi8AUbpr6q4REOCl9Om2puqxrmL5kFH8U d7XaAF4dq1L/T4E/YiUhSQ== X-Received: by 10.99.106.66 with SMTP id f63mr19259256pgc.150.1496646760396; Mon, 05 Jun 2017 00:12:40 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:58da]) by smtp.gmail.com with ESMTPSA id g20sm54956012pfk.21.2017.06.05.00.12.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Jun 2017 00:12:39 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com Subject: [PATCH] Btrfs: use memalloc_nofs and kvzalloc() for free space tree bitmaps Date: Mon, 5 Jun 2017 00:12:31 -0700 Message-Id: X-Mailer: git-send-email 2.13.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval First, instead of open-coding the vmalloc() fallback, use the new kvzalloc() helper. Second, use memalloc_nofs_{save,restore}() instead of GFP_NOFS, as vmalloc() uses some GFP_KERNEL allocations internally which could lead to deadlocks. Signed-off-by: Omar Sandoval Reviewed-by: David Sterba --- fs/btrfs/free-space-tree.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/fs/btrfs/free-space-tree.c b/fs/btrfs/free-space-tree.c index fc0bd8406758..e5e92e6ba734 100644 --- a/fs/btrfs/free-space-tree.c +++ b/fs/btrfs/free-space-tree.c @@ -17,7 +17,7 @@ */ #include -#include +#include #include "ctree.h" #include "disk-io.h" #include "locking.h" @@ -153,21 +153,21 @@ static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize) static u8 *alloc_bitmap(u32 bitmap_size) { - void *mem; + u8 *ret; + unsigned int nofs_flag; /* - * The allocation size varies, observed numbers were < 4K up to 16K. - * Using vmalloc unconditionally would be too heavy, we'll try - * contiguous allocations first. + * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse + * into the filesystem as the free space bitmap can be modified in the + * critical section of a transaction commit. + * + * TODO: push the memalloc_nofs_{save,restore}() to the caller where we + * know that recursion is unsafe. */ - if (bitmap_size <= PAGE_SIZE) - return kzalloc(bitmap_size, GFP_NOFS); - - mem = kzalloc(bitmap_size, GFP_NOFS | __GFP_NOWARN); - if (mem) - return mem; - - return __vmalloc(bitmap_size, GFP_NOFS | __GFP_ZERO, PAGE_KERNEL); + nofs_flag = memalloc_nofs_save(); + ret = kvzalloc(bitmap_size, GFP_KERNEL); + memalloc_nofs_restore(nofs_flag); + return ret; } int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,