Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4412955imw; Tue, 12 Jul 2022 07:33:01 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tmGW4K2J0ay23rU9u2z7IQhJ/U1HWc9pnDCRN9UzfK6SGQU87dkX5McUf2HNVcw+k0EI9V X-Received: by 2002:a63:710c:0:b0:40c:adcf:ce72 with SMTP id m12-20020a63710c000000b0040cadcfce72mr21355935pgc.310.1657636380720; Tue, 12 Jul 2022 07:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657636380; cv=none; d=google.com; s=arc-20160816; b=qPubns1YHiiUqQLRGSpYTEAB9JHu3CLU8FrTzERdg6sppENtxy26fYBP94GzbFQbGf sdAoUD/+5wORdcgUEnbc/OG+ZvooF+A4oiIXewclxkwqMCNxHKFNxru8L8lnEc9hR+Tq ZmPmeqbfWQeVh13zsNpZpoNikFAN9UBLch6z6FzWpLw/trXgBbmY6jThDI6LPgkwGLgs iHJ7CErg6hHzKfLJgoyXMEIxaCy/PcOiTDLvSrGB/K1BjvKTysrESPNkosJ/6DJ0CbEX O33d27+JHzQwNSSe2FqAjw9XKQDyIJD/EdUwMbzUS7GG1CTtTTsUPGFGYzTRzAcUoFfR DpEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Md6jdHoeFnPVdoJQUXH6TznVJo5lY+mYYDDG4QrkfYo=; b=t9LlR4noa62mHxsPNDqo5zucbiShTq78HJOQDF1Gl6M7/Amc9c3xIKKsbg0cx1fn0V yJQ7H7LNIrmSpOudEAdDNCMdGU+P/2mtJQInYcGWFjKcTsyu7RhFhqILPAUvzohTdmG4 48Z6rzLXkQET1decSkF37bFY5kZs/WtE0OOTOZ3oDdn2ycldCyYTug+yh95psN4bsbMR //b0ScVVqaXNegRO7hhXIWcp5nW3XJiWsh/cOIBS/bsKwWOHhW98OQiZNoZWU0QK5n2P gWBCY/nHZxpEaLzHUl9OnSf1YIsp0piar2wc6iyomMVuJ833yJwdUE0y7qNNBx6AXFmC pCcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=RVXeFay3; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 32-20020a631160000000b0040dd6d54504si13134128pgr.511.2022.07.12.07.32.47; Tue, 12 Jul 2022 07:33:00 -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=@infradead.org header.s=casper.20170209 header.b=RVXeFay3; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233084AbiGLNfQ (ORCPT + 99 others); Tue, 12 Jul 2022 09:35:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232995AbiGLNfM (ORCPT ); Tue, 12 Jul 2022 09:35:12 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65FE9B6545 for ; Tue, 12 Jul 2022 06:35:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=Md6jdHoeFnPVdoJQUXH6TznVJo5lY+mYYDDG4QrkfYo=; b=RVXeFay3fDWWaT84A2USdwmozx IiH4CKiXmARArCMbjzuPbYuwgytND/kwHA6nIPWlTtUMrgamo8XXaoIMiKfeMtQMQ/2vm8n3/DR5N pyARBKkrVNgdxPaBPrrWfivhhdk0o1Leha1S6ge6RsW71mgnyeJkP3YFEf+TndLvS36WVkQzMOhQO 0yxs2HAbcUgqHYS1KIPCSHsmObt4wbnUsSC35FZxNJ1q0h3z6KRwA1IHxLK7Vquse2d/ZCu7n5A3P 1kRqMk76XS6rUtPJuTlA9SxZp267MBz4XLOKZzH5TOL96hSDYKVOggm2hbtK2V8r3xX5/LK/7t09H To+1IGXw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1oBG2Y-006xcN-1q; Tue, 12 Jul 2022 13:35:02 +0000 Date: Tue, 12 Jul 2022 14:35:02 +0100 From: Matthew Wilcox To: Dmitry Vyukov Cc: Andrew Morton , syzbot , linux-kernel@vger.kernel.org, linux-mm@kvack.org, syzkaller-bugs@googlegroups.com, Zach O'Keefe , Yang Shi , Liam Howlett Subject: Re: [syzbot] memory leak in xas_create Message-ID: References: <000000000000eb2d6c05e35a0d73@google.com> <20220711133808.d86400ce9960febcb0fd537b@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,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 On Tue, Jul 12, 2022 at 01:57:59PM +0100, Matthew Wilcox wrote: > > So I assumed the nodes stored in the xas object, which is local to the > > collapse_file() function. > > Yes, that's a reasonable thing to think, but it's actually not how > it works. When we allocate a node in xas_create(), we put it straight > into the tree without storing it in xas->xa_alloc. We may then end > up not using it, but the node isn't leaked because it's in the tree. > > If the GFP_NOWAIT allocation fails (it didn't in these stack traces), > we call xas_nomem(), which sees an -ENOMEM, allocates a node and stores > it in xas->xa_alloc; then we go round the loop again where xas_create() > will take the node from xas->xa_alloc. But the backtraces here don't > implicate xas_nomem(). There is actually a leak here, but it's not the one that's been found. do { xas_lock_irq(&xas); xas_create_range(&xas); if (!xas_error(&xas)) break; xas_unlock_irq(&xas); if (!xas_nomem(&xas, GFP_KERNEL)) { result = SCAN_FAIL; goto out; } } while (1); If xas_create() fails, it sets xas error to -ENOMEM. So we unlock the xarray lock and call xas_nomem(). xas_nomem() sees the error is -ENOMEM and allocates a node with GFP_KERNEL, putting the node in xas->xa_alloc. We re-take the spinlock and call xas_create() again. If we still need a node, xas_alloc() will take the node stored in xas->xa_alloc. If we raced and don't need a node, xas_create_range() succeeds and we break out, failing to free the xas->xa_alloc node. We should call xas_destroy() at the out: label. However, this does not explain what is going on, and will not fix what is going on, since any nodes allocated during xas_create_range() will be stored safely in the tree and will not be freed by xas_destroy().