Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4608493rdb; Tue, 12 Dec 2023 04:42:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFYriKXsM2KSiEzdpm1RMRzceK3AJQCW5yw3EqIjlpHvBLkCOS+yXwtBOhpjktfWuGBUUOe X-Received: by 2002:a50:8d43:0:b0:54c:4837:9054 with SMTP id t3-20020a508d43000000b0054c48379054mr4534154edt.76.1702384937826; Tue, 12 Dec 2023 04:42:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702384937; cv=none; d=google.com; s=arc-20160816; b=HhU+Y0zTUc8oqJfvl7YQ6EpMGawzuYQeOyV6sK+FD0lZAH5f54aJVxV+f/fT8Y00Ns r+Kw4g6wC01uTGkxK3bTAXwo91UzV+JspYOxUqjMYlUoubSEr7GeHdje2sDkT0tE8QTf Rcne9lDw91TbNcjPPz/x4XqmwT0rNnROjJ8jjsKUwL35l61mryjUv6AR20H+H8qXlDhH KTI4wVpEpBvwPFaolRVo5og4mohi9Bee+tS6u1njUdNeEo68Iuaxh0bqa0FZNhXx/cwQ VzEJciPCKpW5dS4iT942D8hvkL1wzY9fPQ03QjEV3NgD/E510cpL8Bhb03SYr0heoRfX jAfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature; bh=GNa2E9XZNfaEdk9TRq5PEze1pvUq7Mj5mqzveUCyzc8=; fh=LpE0Ia9AbjFt3gnBRm7qpIKT6AjmghoSO93MWAy97Ig=; b=XEneORy8W6X+swU+wPGPDgmAEjMHg6eS9BAvq4WGW4EC5S5BXHW5Ck8FoLtQqvlzER TpcwtZaqrzgo8Tg/+yMxlsVU+yfXgnS0UH2O2hH49pAU9l92zuout3xyQTUPTNpLRDjd YgKNaUKn1G0JLvl2deuxvgE9TmMnxw4iqY1i2GK6u2oS1ha+xWieNjPi3Y3hDGw5qkHY rzdzXtiUb/QiOXl7ppx+fGX/6NKMDi0BJIvFswJM6LjISdtaQ6JHQi/hK1Wh+wjgTYl8 IH0toejn8v5WPc/xBj1YODUXPrT+QUzzzzAlH7KYXcq4JmtKn5EyYXci4JU29PfRTK2p EhHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=bqifFFBE; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=lJNxfJ9w; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=0KdH44DZ; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=A2WXGC5F; spf=pass (google.com: domain of linux-ext4+bounces-392-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-ext4+bounces-392-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r24-20020a50d698000000b0054c90d4c00esi4278254edi.463.2023.12.12.04.42.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 04:42:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-392-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=bqifFFBE; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=lJNxfJ9w; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=0KdH44DZ; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=A2WXGC5F; spf=pass (google.com: domain of linux-ext4+bounces-392-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-ext4+bounces-392-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 69AB11F21578 for ; Tue, 12 Dec 2023 12:42:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BB5C5FEFC; Tue, 12 Dec 2023 12:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="bqifFFBE"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="lJNxfJ9w"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="0KdH44DZ"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="A2WXGC5F" X-Original-To: linux-ext4@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DA7394; Tue, 12 Dec 2023 04:42:08 -0800 (PST) Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 727C4224EA; Tue, 12 Dec 2023 12:42:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1702384926; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GNa2E9XZNfaEdk9TRq5PEze1pvUq7Mj5mqzveUCyzc8=; b=bqifFFBE0MhFbajnZEhW24Eq1ViJGdjcEWsq8/k47JzaQI32MX0l2kkMiLOAcNPPtKJ5RD zOIv/B4mVuNNMoGK0+kV6vI5YQY7PviQqA8T490SCZVa2phLk3VERKJMsA12a1aGaOCILW m/aammhjVPXoytUIrSiVDtcqg6NgXqE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1702384926; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GNa2E9XZNfaEdk9TRq5PEze1pvUq7Mj5mqzveUCyzc8=; b=lJNxfJ9whX7J5ojbi7z5RYhIfbUF5l/rie01DhjSXemSuWafRNwkvQCIu4erc8VQGYxC1X pBgiA/J7AaxpkIDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1702384924; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GNa2E9XZNfaEdk9TRq5PEze1pvUq7Mj5mqzveUCyzc8=; b=0KdH44DZYQRGMDTrAM0LQjrUQAS8V3NaAYeDzWeKE87PTrDaebUZcqWItMEjrvBcOgiTmp 7RxHdmBMXOWgtyGJrqmZLqncXjLQUl4s9wKm4mxDw5lV6rNNfSyNwLoEQEcxjWZDpv4YKH E8q8gi3f/Zfp7AcVNkEjNZpc7gP1evA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1702384924; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GNa2E9XZNfaEdk9TRq5PEze1pvUq7Mj5mqzveUCyzc8=; b=A2WXGC5FP449rylXKqpbXvaibLTsm5cCAdgmCw5kuqGzNYV2jWUDlTNJduF61STFzuMqqc AhDFoFLB7KVT6oCA== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 5CFDF132DC; Tue, 12 Dec 2023 12:42:04 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id pImyFhxVeGWJMgAAn2gu4w (envelope-from ); Tue, 12 Dec 2023 12:42:04 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id C6A01A06E5; Tue, 12 Dec 2023 13:41:57 +0100 (CET) Date: Tue, 12 Dec 2023 13:41:57 +0100 From: Jan Kara To: Baokun Li Cc: linux-mm@kvack.org, linux-ext4@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, willy@infradead.org, akpm@linux-foundation.org, david@fromorbit.com, hch@infradead.org, ritesh.list@gmail.com, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, yukuai3@huawei.com, stable@kernel.org Subject: Re: [RFC PATCH] mm/filemap: avoid buffered read/write race to read inconsistent data Message-ID: <20231212124157.ew6q6jp2wsezvqzd@quack3> References: <20231212093634.2464108-1-libaokun1@huawei.com> Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231212093634.2464108-1-libaokun1@huawei.com> X-Spam-Level: ********** X-Spam-Score: 10.20 X-Spam-Level: X-Rspamd-Server: rspamd1 X-Rspamd-Queue-Id: 727C4224EA X-Spam-Flag: NO Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=0KdH44DZ; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=A2WXGC5F; dmarc=none; spf=softfail (smtp-out1.suse.de: 2a07:de40:b281:104:10:150:64:98 is neither permitted nor denied by domain of jack@suse.cz) smtp.mailfrom=jack@suse.cz X-Spamd-Result: default: False [-1.31 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:98:from]; TO_DN_SOME(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; R_RATELIMIT(0.00)[to_ip_from(RLipn3kch65fcrhdxgeb98n64p)]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[suse.cz:+]; MX_GOOD(-0.01)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[suse.cz]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; RCPT_COUNT_TWELVE(0.00)[16]; DBL_BLOCKED_OPENRESOLVER(0.00)[huawei.com:email,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; MID_RHS_NOT_FQDN(0.50)[]; FREEMAIL_CC(0.00)[kvack.org,vger.kernel.org,mit.edu,dilger.ca,suse.cz,infradead.org,linux-foundation.org,fromorbit.com,gmail.com,huawei.com,kernel.org]; RCVD_TLS_ALL(0.00)[]; SUSPICIOUS_RECIPS(1.50)[] X-Spam-Score: -1.31 On Tue 12-12-23 17:36:34, Baokun Li wrote: > The following concurrency may cause the data read to be inconsistent with > the data on disk: > > cpu1 cpu2 > ------------------------------|------------------------------ > // Buffered write 2048 from 0 > ext4_buffered_write_iter > generic_perform_write > copy_page_from_iter_atomic > ext4_da_write_end > ext4_da_do_write_end > block_write_end > __block_commit_write > folio_mark_uptodate > // Buffered read 4096 from 0 smp_wmb() > ext4_file_read_iter set_bit(PG_uptodate, folio_flags) > generic_file_read_iter i_size_write // 2048 > filemap_read unlock_page(page) > filemap_get_pages > filemap_get_read_batch > folio_test_uptodate(folio) > ret = test_bit(PG_uptodate, folio_flags) > if (ret) > smp_rmb(); > // Ensure that the data in page 0-2048 is up-to-date. > > // New buffered write 2048 from 2048 > ext4_buffered_write_iter > generic_perform_write > copy_page_from_iter_atomic > ext4_da_write_end > ext4_da_do_write_end > block_write_end > __block_commit_write > folio_mark_uptodate > smp_wmb() > set_bit(PG_uptodate, folio_flags) > i_size_write // 4096 > unlock_page(page) > > isize = i_size_read(inode) // 4096 > // Read the latest isize 4096, but without smp_rmb(), there may be > // Load-Load disorder resulting in the data in the 2048-4096 range > // in the page is not up-to-date. > copy_page_to_iter > // copyout 4096 > > In the concurrency above, we read the updated i_size, but there is no read > barrier to ensure that the data in the page is the same as the i_size at > this point, so we may copy the unsynchronized page out. Hence adding the > missing read memory barrier to fix this. > > This is a Load-Load reordering issue, which only occurs on some weak > mem-ordering architectures (e.g. ARM64, ALPHA), but not on strong > mem-ordering architectures (e.g. X86). And theoretically the problem AFAIK x86 can also reorder loads vs loads so the problem can in theory happen on x86 as well. > doesn't only happen on ext4, filesystems that call filemap_read() but > don't hold inode lock (e.g. btrfs, f2fs, ubifs ...) will have this > problem, while filesystems with inode lock (e.g. xfs, nfs) won't have > this problem. > > Cc: stable@kernel.org > Signed-off-by: Baokun Li > --- > mm/filemap.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/mm/filemap.c b/mm/filemap.c > index 71f00539ac00..6324e2ac3e74 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -2607,6 +2607,9 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, > goto put_folios; > end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count); > > + /* Ensure that the page cache within isize is updated. */ Barries have to be in pairs to work and it is a good practice to document this. So here I'd have comment like: /* * Pairs with a barrier in * block_write_end()->mark_buffer_dirty() or other page * dirtying routines like iomap_write_end() to ensure * changes to page contents are visible before we see * increased inode size. */ Honza > + smp_rmb(); > + > /* > * Once we start copying data, we don't want to be touching any > * cachelines that might be contended: > -- > 2.31.1 > -- Jan Kara SUSE Labs, CR