Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp681105ybz; Fri, 1 May 2020 06:43:18 -0700 (PDT) X-Google-Smtp-Source: APiQypKTAPPOwNwdLDcui3JWdzfqTojUWhdHwEsOLSW1dj8b41grRiNkVKxLMzkMO3IijFvRI65c X-Received: by 2002:a17:906:2604:: with SMTP id h4mr3104351ejc.307.1588340598494; Fri, 01 May 2020 06:43:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588340598; cv=none; d=google.com; s=arc-20160816; b=L0tT5GssOjgLmXm+Y0jf9LVpw+J+7ZHL5mKoSTMD/d9mUDK0vE7TJof5VUC+X9krU/ gpTo6Q/qAoKp6mDP/yOYS4l+b3gToONLLd1zzLQf876kXlTHtIqB7om/eDJNleIUdeWv 3Pkp7g08SmUvd47sEQOFxJb3RsP9tyin4pqPHjwSHGHUTfc6nfe1FBN+D5gol1RD0Lgl n5BNFVkm0YOIF7J64Y2DAmL8GpZ/ZF9uDIJEkUBZmsvTRWf5QNAKRT9sZDagQWmg/Z4m fddFj06RLAMjr9Lev3z/4G5iZmxJP4IWNZ03XtnJkl//9LQ1JLIvvxeweZcPzdxFafiM cS3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yh1e/n/aLwY9J6mBXuGQS1UR3KbUwQ8ULEfdsnlDdUs=; b=BGuO+nwsI70+rnCX4h72V5kT6S76vpMMDYb+LAAuNWhLQWKgBzzbsm6bLEEfP8Cx7q XuWhlgkLwJmP/hLyHSeo0yhIlPSY40VzeeCduUHawRUuK+o4Hv2v/eiLMkVYkCYnSz5P 0U1gFk2vkdnYN60yjVTTyLXvwl96SGOAg8ShsH/lpYFDVwRd09shLoqMp9NinW8zh+8e u9ZIIbO/daU1eWoHSq+RME4FxEEG/+X8O1KBlvtzjY+8Fx6HekJh9A6D9pN+WowPpCcL ovcXBuJM6DNXZmLMWhu8BzD2ZLbf36wHFEH2XNHmFIelbuPW7h5M6V23GrPBk6b2aTBL gPFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=a1F3GiOj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce18si1732389ejc.54.2020.05.01.06.42.55; Fri, 01 May 2020 06:43:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=a1F3GiOj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731202AbgEANje (ORCPT + 99 others); Fri, 1 May 2020 09:39:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:39254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731194AbgEANjc (ORCPT ); Fri, 1 May 2020 09:39:32 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BC041205C9; Fri, 1 May 2020 13:39:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588340371; bh=r0+dkasYLcoUZBILv3W7IOkelCzd5PwNPf5GSTGG628=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a1F3GiOjGPfzeMFhoVq2ZPnr2KOfXu4TrsnCAsZBiUD5v69abiYvzMfwdYXxTVxjr wobp9YPwvB6WlANm2O7FeuFNy+NTla6ioT9pdmWPQkvQUrQ0eXR9xMUQ0xp1W3pV0s rE3YMdCPGPqKApuX+3XXTCntA0YyTJXMhy+iUbNs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+1f9dc49e8de2582d90c2@syzkaller.appspotmail.com, Eric Biggers , "Darrick J. Wong" , Christoph Hellwig Subject: [PATCH 5.4 51/83] xfs: clear PF_MEMALLOC before exiting xfsaild thread Date: Fri, 1 May 2020 15:23:30 +0200 Message-Id: <20200501131538.436403691@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200501131524.004332640@linuxfoundation.org> References: <20200501131524.004332640@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit 10a98cb16d80be3595fdb165fad898bb28b8b6d2 upstream. Leaving PF_MEMALLOC set when exiting a kthread causes it to remain set during do_exit(). That can confuse things. In particular, if BSD process accounting is enabled, then do_exit() writes data to an accounting file. If that file has FS_SYNC_FL set, then this write occurs synchronously and can misbehave if PF_MEMALLOC is set. For example, if the accounting file is located on an XFS filesystem, then a WARN_ON_ONCE() in iomap_do_writepage() is triggered and the data doesn't get written when it should. Or if the accounting file is located on an ext4 filesystem without a journal, then a WARN_ON_ONCE() in ext4_write_inode() is triggered and the inode doesn't get written. Fix this in xfsaild() by using the helper functions to save and restore PF_MEMALLOC. This can be reproduced as follows in the kvm-xfstests test appliance modified to add the 'acct' Debian package, and with kvm-xfstests's recommended kconfig modified to add CONFIG_BSD_PROCESS_ACCT=y: mkfs.xfs -f /dev/vdb mount /vdb touch /vdb/file chattr +S /vdb/file accton /vdb/file mkfs.xfs -f /dev/vdc mount /vdc umount /vdc It causes: WARNING: CPU: 1 PID: 336 at fs/iomap/buffered-io.c:1534 CPU: 1 PID: 336 Comm: xfsaild/vdc Not tainted 5.6.0-rc5 #3 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ?-20191223_100556-anatol 04/01/2014 RIP: 0010:iomap_do_writepage+0x16b/0x1f0 fs/iomap/buffered-io.c:1534 [...] Call Trace: write_cache_pages+0x189/0x4d0 mm/page-writeback.c:2238 iomap_writepages+0x1c/0x33 fs/iomap/buffered-io.c:1642 xfs_vm_writepages+0x65/0x90 fs/xfs/xfs_aops.c:578 do_writepages+0x41/0xe0 mm/page-writeback.c:2344 __filemap_fdatawrite_range+0xd2/0x120 mm/filemap.c:421 file_write_and_wait_range+0x71/0xc0 mm/filemap.c:760 xfs_file_fsync+0x7a/0x2b0 fs/xfs/xfs_file.c:114 generic_write_sync include/linux/fs.h:2867 [inline] xfs_file_buffered_aio_write+0x379/0x3b0 fs/xfs/xfs_file.c:691 call_write_iter include/linux/fs.h:1901 [inline] new_sync_write+0x130/0x1d0 fs/read_write.c:483 __kernel_write+0x54/0xe0 fs/read_write.c:515 do_acct_process+0x122/0x170 kernel/acct.c:522 slow_acct_process kernel/acct.c:581 [inline] acct_process+0x1d4/0x27c kernel/acct.c:607 do_exit+0x83d/0xbc0 kernel/exit.c:791 kthread+0xf1/0x140 kernel/kthread.c:257 ret_from_fork+0x27/0x50 arch/x86/entry/entry_64.S:352 This bug was originally reported by syzbot at https://lore.kernel.org/r/0000000000000e7156059f751d7b@google.com. Reported-by: syzbot+1f9dc49e8de2582d90c2@syzkaller.appspotmail.com Signed-off-by: Eric Biggers Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_trans_ail.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) --- a/fs/xfs/xfs_trans_ail.c +++ b/fs/xfs/xfs_trans_ail.c @@ -529,8 +529,9 @@ xfsaild( { struct xfs_ail *ailp = data; long tout = 0; /* milliseconds */ + unsigned int noreclaim_flag; - current->flags |= PF_MEMALLOC; + noreclaim_flag = memalloc_noreclaim_save(); set_freezable(); while (1) { @@ -601,6 +602,7 @@ xfsaild( tout = xfsaild_push(ailp); } + memalloc_noreclaim_restore(noreclaim_flag); return 0; }