From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C08FBC43334 for ; Tue, 12 Jul 2022 13:35:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 416DC940073; Tue, 12 Jul 2022 09:35:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C81A940063; Tue, 12 Jul 2022 09:35:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B507940073; Tue, 12 Jul 2022 09:35:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 194A4940063 for ; Tue, 12 Jul 2022 09:35:14 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EC454331B8 for ; Tue, 12 Jul 2022 13:35:13 +0000 (UTC) X-FDA: 79678544106.01.31D3C52 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf27.hostedemail.com (Postfix) with ESMTP id 0731940076 for ; Tue, 12 Jul 2022 13:35:11 +0000 (UTC) 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: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657632912; a=rsa-sha256; cv=none; b=6aWfTk8gC9xowXj00PI5tkE7aNwxZWWQ8zpdVezvKv/TWXyOEgwnpISQG1TFIMaw9YNeXQ PXy5vxLGsdVm+r13Glh2/6cYUwSwadX27wBhe+6dXFNQfhfG6Md1lWGdBfW20901WMI4YO 7iPZuTYNb5zfLJU3lQgxqwQRx4v9tcw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=RVXeFay3; dmarc=none; spf=none (imf27.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657632912; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Md6jdHoeFnPVdoJQUXH6TznVJo5lY+mYYDDG4QrkfYo=; b=znydZGJnd1z5ykt10KVzwRGfmjyK51cldq/65mw20wcLm85z2DgkT+CCNnrWTn/buScgqN Jb9r0g2MCj0o/en/s5rQhcg5zt35sabNXwz9dhlXjblHz1R2T+8AeYPefMdDZDyDThdP1N 5o2SMG9kKLoLJqnazn87MZdSuCcenpI= X-Rspam-User: X-Stat-Signature: xcqbc9z9uoem55gpg4mt58inx3z8b7ye X-Rspamd-Queue-Id: 0731940076 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=RVXeFay3; dmarc=none; spf=none (imf27.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam03 X-HE-Tag: 1657632911-11170 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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().