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 X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40644C35247 for ; Mon, 3 Feb 2020 16:49:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0AD162051A for ; Mon, 3 Feb 2020 16:49:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0AD162051A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 937296B0637; Mon, 3 Feb 2020 11:49:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E7596B064B; Mon, 3 Feb 2020 11:49:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8250E6B0657; Mon, 3 Feb 2020 11:49:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id 6E3FD6B0637 for ; Mon, 3 Feb 2020 11:49:35 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3362F8248047 for ; Mon, 3 Feb 2020 16:49:35 +0000 (UTC) X-FDA: 76449401910.22.work09_53a765bcb981c X-HE-Tag: work09_53a765bcb981c X-Filterd-Recvd-Size: 3553 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Mon, 3 Feb 2020 16:49:34 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 79BDAAD95; Mon, 3 Feb 2020 16:49:33 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 2CCA41E0D5D; Mon, 3 Feb 2020 17:49:33 +0100 (CET) Date: Mon, 3 Feb 2020 17:49:33 +0100 From: Jan Kara To: Matthew Wilcox Cc: Jan Kara , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: Re: Race in xarray tagged iteration Message-ID: <20200203164933.GH18591@quack2.suse.cz> References: <20200203140937.GA18591@quack2.suse.cz> <20200203163301.GJ8731@bombadil.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200203163301.GJ8731@bombadil.infradead.org> User-Agent: Mutt/1.10.1 (2018-07-13) 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 Mon 03-02-20 08:33:01, Matthew Wilcox wrote: > On Mon, Feb 03, 2020 at 03:09:37PM +0100, Jan Kara wrote: > > Hello Matthew! > > > > Lately I've been looking into speeding up page cache truncation that got > > slowed down by the conversion of page cache to xarray as we spoke about > > back in February / March [1]. Now I have relatively simple patch giving me > > around 6% improvement in truncation speeds on my test machine but when > > testing it and debugging issues, I've found out that current xarray tagged > > iteration is racy: > > > > TASK1 TASK2 > > page_cache_delete() find_get_pages_range_tag() > > xas_for_each_marked() > > xas_find_marked() > > off = xas_find_chunk() > > > > xas_store(&xas, NULL) > > xas_init_marks(&xas); > > ... > > rcu_assign_pointer(*slot, NULL); > > entry = xa_entry(off); > > > > So xas_for_each_marked() can return NULL entries as tagged thus aborting > > xas_for_each_marked() iteration prematurely (data loss possible). > > > > Now I have a patch to change xas_for_each_marked() to not get confused by > > NULL entries (because that is IMO a fragile design anyway and easy to avoid > > AFAICT) but that still leaves us with find_get_pages_range_tag() getting > > NULL as tagged entry and that causes oops there. > > > > I see two options how to fix this and I'm not quite decided which is > > better: > > > > 1) Just add NULL checking to find_get_pages_range_tag() similarly to how it > > currently checks xa_is_value(). Quick grepping seems to show that that > > place is the only place that uses tagged iteration under RCU. It is cheap > > but kind of ugly. > > > > 2) Make sure xas_find_marked() and xas_next_marked() do recheck marks after > > loading the entry. This is more convenient for the callers but potentially > > more expensive since we'd have to add some barriers there. > > > > What's your opinion? I'm leaning more towards 1) but I'm not completely > > decided... > > Thanks for debugging that! This must've been the problem I was hitting > when I originally tried to solve that problem. > > I prefer a third choice ... continue to iterate forward if we find a NULL > entry that used to have a tag set on it. That should be cheap. Yep, fair enough. I'll add this to the series I'm preparing and see whether xfstests now pass. Honza -- Jan Kara SUSE Labs, CR