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 B5F9FC433EF for ; Wed, 23 Feb 2022 17:07:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C95A8D002C; Wed, 23 Feb 2022 12:07:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 34FDA8D0011; Wed, 23 Feb 2022 12:07:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A30C8D002C; Wed, 23 Feb 2022 12:07:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0186.hostedemail.com [216.40.44.186]) by kanga.kvack.org (Postfix) with ESMTP id DFF6C8D0011 for ; Wed, 23 Feb 2022 12:07:06 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6B868181CAEFE for ; Wed, 23 Feb 2022 17:07:06 +0000 (UTC) X-FDA: 79174674852.16.0D963D2 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf25.hostedemail.com (Postfix) with ESMTP id B23C6A000E for ; Wed, 23 Feb 2022 17:07:05 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 621FA1F43E; Wed, 23 Feb 2022 17:07:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1645636024; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CaDuGVMSnVYMBtbixokplU//+MPnASve3hmmEe3B/h8=; b=TqKpsWRJetKNQIjMX62ryxEm53gPFT4Ddlge7AjxiZy+3ipmpZ8sKf9k553nqoSIx38g8J bD4fpwJdFVt+kt3HK0s0ljvU4i6bmpdbmRI1AZjjLZ9z46UYnHgezq0Eo69vWMq48UCn4U Wx4ISi8z/fBoyStzt9b6kBK4fb58q3U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1645636024; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CaDuGVMSnVYMBtbixokplU//+MPnASve3hmmEe3B/h8=; b=YMy5kpWxlF/sLctza+/jeSBv105CdyfrVALqxr6yuDfU4nDWqnWaEj8ptKr9SsVhWjpB1W vxJA4wQa5lkyiHBA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3A5C613C94; Wed, 23 Feb 2022 17:07:04 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 12KWDbhpFmK3GQAAMHmgww (envelope-from ); Wed, 23 Feb 2022 17:07:04 +0000 Message-ID: <87b056da-80e8-17f5-41ae-9fb493a7f0da@suse.cz> Date: Wed, 23 Feb 2022 18:07:03 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: read() data corruption with CONFIG_READ_ONLY_THP_FOR_FS=y Content-Language: en-US To: Matthew Wilcox Cc: "stable@vger.kernel.org" , Miaohe Lin , Christoph Hellwig , Jan Kara , Takashi Iwai , "linux-mm@kvack.org" , patches@lists.linux.dev, LKML , "Kirill A. Shutemov" References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B23C6A000E X-Stat-Signature: pxapmee9jw1at3nq3nfxpojsm4fyya6e Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=TqKpsWRJ; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=YMy5kpWx; spf=pass (imf25.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none X-Rspam-User: X-HE-Tag: 1645636025-855023 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 2/23/22 15:33, Matthew Wilcox wrote: > On Wed, Feb 23, 2022 at 02:54:43PM +0100, Vlastimil Babka wrote: >> we have found a bug involving CONFIG_READ_ONLY_THP_FOR_FS=y, introduced in >> 5.12 by cbd59c48ae2b ("mm/filemap: use head pages in >> generic_file_buffered_read") >> and apparently fixed in 5.17-rc1 by 6b24ca4a1a8d ("mm: Use multi-index >> entries in the page cache") >> The latter commit is part of folio rework so likely not stable material, so >> it would be nice to have a small fix for e.g. 5.15 LTS. Preferably from >> someone who understands xarray :) > > [...] > >> I've hacked some printk on top 5.16 (attached debug.patch) >> which gives this output: >> >> i=0 page=ffffea0004340000 page_offset=0 uoff=0 bytes=2097152 >> i=1 page=ffffea0004340000 page_offset=0 uoff=0 bytes=2097152 >> i=2 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=3 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=4 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=5 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=6 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=7 page=ffffea0004340000 page_offset=0 uoff=0 bytes=0 >> i=8 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=9 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=10 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=11 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=12 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=13 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> i=14 page=ffffea0004470000 page_offset=2097152 uoff=0 bytes=0 >> >> It seems filemap_get_read_batch() should be returning pages ffffea0004340000 >> and ffffea0004470000 consecutively in the pvec, but returns the first one 8 >> times, so it's read twice and then the rest is just skipped over as it's >> beyond the requested read size. >> >> I suspect these lines: >> xas.xa_index = head->index + thp_nr_pages(head) - 1; >> xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK; >> >> commit 6b24ca4a1a8d changes those to xas_advance() (introduced one patch >> earlier), so some self-contained fix should be possible for prior kernels? >> But I don't understand xarray well enough. > > I figured it out! > > In v5.15 (indeed, everything before commit 6b24ca4a1a8d), an order-9 > page is stored in 512 consecutive slots. The XArray stores 64 entries > per level. So what happens is we start looking at index 0 and we walk > down to the bottom of the tree and find the THP at index 0. > > xas.xa_index = head->index + thp_nr_pages(head) - 1; > xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK; > > So we've advanced xas.xa_index to 511, but advanced xas.xa_offset to 63. > Then we call xas_next() which calls __xas_next(), which moves us along to > array index 64 while we think we're looking at index 512. > > We could make __xas_next() more resistant to this kind of abuse (by > extracting the correct offset in the parent node from xa_index), but > as you say, we're looking for a small fix for LTS. I suggest this > will probably do the right thing: Great! Just so others are aware: the final fix is here: https://lore.kernel.org/all/20220223155918.927140-1-willy@infradead.org/ > +++ b/mm/filemap.c > @@ -2354,8 +2354,7 @@ static void filemap_get_read_batch(struct address_space *mapping, > break; > if (PageReadahead(head)) > break; > - xas.xa_index = head->index + thp_nr_pages(head) - 1; > - xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK; > + xas_set(&xas, head->index + thp_nr_pages(head) - 1); > continue; > put_page: > put_page(head); > > but I'll start trying the reproducer now. > >