Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1883743rwb; Fri, 19 Aug 2022 11:02:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR69jPoYPkO04yDeywItc8LRF5bM0e1xDDILDCLtxSb7arzDze1FLJtQzGO07fShSN5Ez0TW X-Received: by 2002:a63:445a:0:b0:42a:306e:5e30 with SMTP id t26-20020a63445a000000b0042a306e5e30mr4941748pgk.40.1660932133355; Fri, 19 Aug 2022 11:02:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660932133; cv=none; d=google.com; s=arc-20160816; b=aaQACyGyQA7X/yaVJ5bgvYzwd3E7YKcNVyqAhIMiuAfdngJs0UeOqYb5jcDrZwumcD DvLIaB/dqI2qRqxG6kM3YMpsh+9Ikb5HFerXM/uhIeNheBdutKRdy3m+GHLqqhHwhnMB qIPQ9U9Zpte5lGXUcqztiZwwzj5gZrDTwGDJQt9eVTAWZOsfXnost8DqQ4Ixy3Tgjh5t bbbP6obip9nVbL/Stv6zgjvzrqerCxV0jHlVaV3f5Qjyn3TddcfxwuJRk95/c32j25iN vypJyeWBxHSE0p7vlBeFhiQ/TXVrTwyDuvM41DqSAAl0p1RVEYguXqOTQ86riIWkE78A MGAw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KPUcv3CN8T9Oa8T8m6vqGsbHp+tSUNmvYj/LtDCc9aM=; b=NGsvaeleUkLHTyyjy1Blx43bawwEHtFa9mb1MC5m8S32Oe1irLElDZg2ovTCgb3qRC NPbxfw/Ewz+GIyeQYmicNrIxokE+VswipRPEPHyNpT4Wq+iCPpmw18EWfCjcPJpa2sxI Xg02yYmrdgSZX844M15RMPNqz8eiOqbdgHUE/NR4PrUL08R9NpUOwMFdP3ljcdoT74y5 qST6tM8ej2Qlgqe5ayl9Q88qNIuqYcaVEveVkvBiQnB24izCpumxrQQYJklZeeOLI+F/ T10xLcoeD/sNjVsKjZQMv8+/wepxprB7kwPqMxQrw1VFgcSAgP/5amowdt348MSltdqn st0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gKj+gwGK; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s11-20020a056a0008cb00b0052d4f3e07a2si5054395pfu.21.2022.08.19.11.02.01; Fri, 19 Aug 2022 11:02:13 -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=@linuxfoundation.org header.s=korg header.b=gKj+gwGK; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350394AbiHSPvs (ORCPT + 99 others); Fri, 19 Aug 2022 11:51:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350458AbiHSPuf (ORCPT ); Fri, 19 Aug 2022 11:50:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADF957E319; Fri, 19 Aug 2022 08:48:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D5208616E8; Fri, 19 Aug 2022 15:48:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1B31C433C1; Fri, 19 Aug 2022 15:48:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660924094; bh=5dukMsRD07uxta7hAVW9LUcDVBnPXNhu7XZL/vzxyq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gKj+gwGKaUOKyArGuy3aikt0MNWv4QBxbfUbYe4J4qsonvAe96ASZSjPnBsODVNtw oTzFW8neFh0rtb+5a6NeQfJlz+mIXR/rxtHGUcoUsqtKR46i3bNWK3saWqgYYIHtQb 7+5H5trdwviq9FSoqg838TH3Er1hShE3mSBdKI24= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Chinner , Mel Gorman , "Darrick J. Wong" , Amir Goldstein Subject: [PATCH 5.10 023/545] mm: Add kvrealloc() Date: Fri, 19 Aug 2022 17:36:33 +0200 Message-Id: <20220819153830.232332727@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Dave Chinner commit de2860f4636256836450c6543be744a50118fc66 upstream. During log recovery of an XFS filesystem with 64kB directory buffers, rebuilding a buffer split across two log records results in a memory allocation warning from krealloc like this: xfs filesystem being mounted at /mnt/scratch supports timestamps until 2038 (0x7fffffff) XFS (dm-0): Unmounting Filesystem XFS (dm-0): Mounting V5 Filesystem XFS (dm-0): Starting recovery (logdev: internal) ------------[ cut here ]------------ WARNING: CPU: 5 PID: 3435170 at mm/page_alloc.c:3539 get_page_from_freelist+0xdee/0xe40 ..... RIP: 0010:get_page_from_freelist+0xdee/0xe40 Call Trace: ? complete+0x3f/0x50 __alloc_pages+0x16f/0x300 alloc_pages+0x87/0x110 kmalloc_order+0x2c/0x90 kmalloc_order_trace+0x1d/0x90 __kmalloc_track_caller+0x215/0x270 ? xlog_recover_add_to_cont_trans+0x63/0x1f0 krealloc+0x54/0xb0 xlog_recover_add_to_cont_trans+0x63/0x1f0 xlog_recovery_process_trans+0xc1/0xd0 xlog_recover_process_ophdr+0x86/0x130 xlog_recover_process_data+0x9f/0x160 xlog_recover_process+0xa2/0x120 xlog_do_recovery_pass+0x40b/0x7d0 ? __irq_work_queue_local+0x4f/0x60 ? irq_work_queue+0x3a/0x50 xlog_do_log_recovery+0x70/0x150 xlog_do_recover+0x38/0x1d0 xlog_recover+0xd8/0x170 xfs_log_mount+0x181/0x300 xfs_mountfs+0x4a1/0x9b0 xfs_fs_fill_super+0x3c0/0x7b0 get_tree_bdev+0x171/0x270 ? suffix_kstrtoint.constprop.0+0xf0/0xf0 xfs_fs_get_tree+0x15/0x20 vfs_get_tree+0x24/0xc0 path_mount+0x2f5/0xaf0 __x64_sys_mount+0x108/0x140 do_syscall_64+0x3a/0x70 entry_SYSCALL_64_after_hwframe+0x44/0xae Essentially, we are taking a multi-order allocation from kmem_alloc() (which has an open coded no fail, no warn loop) and then reallocating it out to 64kB using krealloc(__GFP_NOFAIL) and that is then triggering the above warning. This is a regression caused by converting this code from an open coded no fail/no warn reallocation loop to using __GFP_NOFAIL. What we actually need here is kvrealloc(), so that if contiguous page allocation fails we fall back to vmalloc() and we don't get nasty warnings happening in XFS. Fixes: 771915c4f688 ("xfs: remove kmem_realloc()") Signed-off-by: Dave Chinner Acked-by: Mel Gorman Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Signed-off-by: Amir Goldstein Acked-by: Darrick J. Wong Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_log_recover.c | 4 +++- include/linux/mm.h | 2 ++ mm/util.c | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) --- a/fs/xfs/xfs_log_recover.c +++ b/fs/xfs/xfs_log_recover.c @@ -2061,7 +2061,9 @@ xlog_recover_add_to_cont_trans( old_ptr = item->ri_buf[item->ri_cnt-1].i_addr; old_len = item->ri_buf[item->ri_cnt-1].i_len; - ptr = krealloc(old_ptr, len + old_len, GFP_KERNEL | __GFP_NOFAIL); + ptr = kvrealloc(old_ptr, old_len, len + old_len, GFP_KERNEL); + if (!ptr) + return -ENOMEM; memcpy(&ptr[old_len], dp, len); item->ri_buf[item->ri_cnt-1].i_len += len; item->ri_buf[item->ri_cnt-1].i_addr = ptr; --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -788,6 +788,8 @@ static inline void *kvcalloc(size_t n, s return kvmalloc_array(n, size, flags | __GFP_ZERO); } +extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, + gfp_t flags); extern void kvfree(const void *addr); extern void kvfree_sensitive(const void *addr, size_t len); --- a/mm/util.c +++ b/mm/util.c @@ -661,6 +661,21 @@ void kvfree_sensitive(const void *addr, } EXPORT_SYMBOL(kvfree_sensitive); +void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) +{ + void *newp; + + if (oldsize >= newsize) + return (void *)p; + newp = kvmalloc(newsize, flags); + if (!newp) + return NULL; + memcpy(newp, p, oldsize); + kvfree(p); + return newp; +} +EXPORT_SYMBOL(kvrealloc); + static inline void *__page_rmapping(struct page *page) { unsigned long mapping;