Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp105793rdb; Mon, 18 Sep 2023 09:21:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCNMl5z92jucMVmm8saiqXte7kJb+MsOJWuxDAmZ5EYdBLO4/7OdYlLyZXBv6xnoAevkj0 X-Received: by 2002:a17:902:8c89:b0:1c0:a5c9:e072 with SMTP id t9-20020a1709028c8900b001c0a5c9e072mr6203247plo.11.1695054104054; Mon, 18 Sep 2023 09:21:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695054104; cv=none; d=google.com; s=arc-20160816; b=LKN3nZJpPsul/wSDeChfYvTmRHXJEbUkWdZXcLQfFESfIRZxCe7/WUU1+DWSPyK/SM r+PoducvYkBLnvkAATrqJTM8QJc5ZH6SaK9uPRf8xpQvCvmL2QvXD7v705PT0EcHP2cX rFacNhrF+usGD6gYg9/CVCAxWbXdxE7LO7kzh9Nw1qm/8uHVwddxBRAhPOhBPWhFlXDK GkY7mpkdnKUg+0+sEFg6x0dnngg3UN7kex0SoiF630WvINbR2TNyJBAa2GNQmWy33JOZ JfpB99FCiN8fRecgzEvk5Vyj5yJFJPsGnb9yZ6RxHV062GxH1Xxk4t9aj1kCetiWN4jC Y4gQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature; bh=dQP1wPJDUgx7ZLJLXNhEB9grdPp9c1uarNW8JL9bAWU=; fh=pLX0f7cl1XAJ/yFfQ3QPSITsXO84NCTsB24th5rOeos=; b=ya6aA9Vte36T0Q0FsDchwJnifkFGPEIdW7mP18yLaca5IN/w/h6ppPp5QvVH825mHl 5FkY7N9cQ9opD//dzX+mHapfzQg7tD8ybayxskWFGiPGTeyhWhw1wx3G3eNbr2Lnh8rN PvpHObXTpEXXvQ0e342Hd40fOTXXWfHDVhE4R1DBF6910T5zfQkMy4TBzUJ2oQtciAr+ nUl5Ugi+aP0wX+xiGIGmaWh9S6sg8KICJeguAG0zpSxQpOsIoogAjeckMb7Q56ZpMdSr /X5uKuSamr8er+YuofeGD9R3liNGmfqzln/Bn7+PhpEmtY4CvlnqgrHveVboHfqnxGu/ 2+JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=vlkbnDMD; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id p18-20020a170902e75200b001bdd35033ebsi8629761plf.361.2023.09.18.09.21.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 09:21:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=vlkbnDMD; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id F02DE804C522; Mon, 18 Sep 2023 02:43:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241111AbjIRJnV (ORCPT + 99 others); Mon, 18 Sep 2023 05:43:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241124AbjIRJm4 (ORCPT ); Mon, 18 Sep 2023 05:42:56 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C9A8CC7; Mon, 18 Sep 2023 02:41:18 -0700 (PDT) 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 AA89E1F37F; Mon, 18 Sep 2023 09:41:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1695030076; 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=dQP1wPJDUgx7ZLJLXNhEB9grdPp9c1uarNW8JL9bAWU=; b=vlkbnDMDju+XgrwdDYfedeuYfzjEk817K2JhOTs4N7fhhzPU59q+5+mBT5Z9PejJ0zSpOA uhL9g0wSPB2ETzIMhNpIJ13h03U1jd4hMlSFbDsHtooSPi5Hfgd1CAyMuoreAAJV5pWJdG gWpHqDNtb8u1jId+LACHTEt756NP3oY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1695030076; 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=dQP1wPJDUgx7ZLJLXNhEB9grdPp9c1uarNW8JL9bAWU=; b=cgeXizwHufg+26MLNkJsbe3TLqNfoh1bAOp/RgAN9jvLGTQfP54G8gPG/tQQAwAe5DTud7 EXTHTGujAtLNx7AA== 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 9B45513480; Mon, 18 Sep 2023 09:41:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ZCnlJTwbCGUFHwAAMHmgww (envelope-from ); Mon, 18 Sep 2023 09:41:16 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 2EA86A0759; Mon, 18 Sep 2023 11:41:16 +0200 (CEST) Date: Mon, 18 Sep 2023 11:41:16 +0200 From: Jan Kara To: Mirsad Goran Todorovac Cc: Philipp Stanner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Wilcox , Jan Kara , Chris Mason , Andrew Morton , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-mm@kvack.org, Yury Norov Subject: Re: [PATCH v1 1/1] xarray: fix the data-race in xas_find_chunk() by using READ_ONCE() Message-ID: <20230918094116.2mgquyxhnxcawxfu@quack3> References: <20230918044739.29782-1-mirsad.todorovac@alu.unizg.hr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230918044739.29782-1-mirsad.todorovac@alu.unizg.hr> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 18 Sep 2023 02:43:52 -0700 (PDT) On Mon 18-09-23 06:47:40, Mirsad Goran Todorovac wrote: > KCSAN has discovered the following data-race: > > [ 206.510010] ================================================================== > [ 206.510035] BUG: KCSAN: data-race in xas_clear_mark / xas_find_marked > > [ 206.510067] write to 0xffff963df6a90fe0 of 8 bytes by interrupt on cpu 22: > [ 206.510081] xas_clear_mark (./arch/x86/include/asm/bitops.h:178 ./include/asm-generic/bitops/instrumented-non-atomic.h:115 lib/xarray.c:102 lib/xarray.c:914) > [ 206.510097] __xa_clear_mark (lib/xarray.c:1923) > [ 206.510114] __folio_end_writeback (mm/page-writeback.c:2981) > [ 206.510128] folio_end_writeback (mm/filemap.c:1616) > [ 206.510143] end_page_writeback (mm/folio-compat.c:28) > [ 206.510155] btrfs_page_clear_writeback (fs/btrfs/subpage.c:646) btrfs > [ 206.510994] end_bio_extent_writepage (./include/linux/bio.h:84 fs/btrfs/extent_io.c:542) btrfs > [ 206.511817] __btrfs_bio_end_io (fs/btrfs/bio.c:117 fs/btrfs/bio.c:112) btrfs > [ 206.512640] btrfs_orig_bbio_end_io (fs/btrfs/bio.c:164) btrfs > [ 206.513497] btrfs_simple_end_io (fs/btrfs/bio.c:380) btrfs > [ 206.514350] bio_endio (block/bio.c:1617) > [ 206.514362] blk_mq_end_request_batch (block/blk-mq.c:837 block/blk-mq.c:1073) > [ 206.514377] nvme_pci_complete_batch (drivers/nvme/host/pci.c:986) nvme > [ 206.514437] nvme_irq (drivers/nvme/host/pci.c:1086) nvme > [ 206.514500] __handle_irq_event_percpu (kernel/irq/handle.c:158) > [ 206.514517] handle_irq_event (kernel/irq/handle.c:195 kernel/irq/handle.c:210) > [ 206.514533] handle_edge_irq (kernel/irq/chip.c:836) > [ 206.514549] __common_interrupt (./include/linux/irqdesc.h:161 arch/x86/kernel/irq.c:238 arch/x86/kernel/irq.c:257) > [ 206.514563] common_interrupt (arch/x86/kernel/irq.c:247 (discriminator 14)) > [ 206.514583] asm_common_interrupt (./arch/x86/include/asm/idtentry.h:636) > [ 206.514599] kcsan_setup_watchpoint (kernel/kcsan/core.c:705 (discriminator 1)) > [ 206.514612] __tsan_read8 (kernel/kcsan/core.c:1025) > [ 206.514626] steal_from_bitmap.part.0 (./include/linux/find.h:186 fs/btrfs/free-space-cache.c:2557 fs/btrfs/free-space-cache.c:2613) btrfs > [ 206.515491] __btrfs_add_free_space (fs/btrfs/free-space-cache.c:2689 fs/btrfs/free-space-cache.c:2667) btrfs > [ 206.516361] btrfs_add_free_space_async_trimmed (fs/btrfs/free-space-cache.c:2798) btrfs > [ 206.517231] add_new_free_space (fs/btrfs/block-group.c:550) btrfs > [ 206.518095] load_free_space_tree (fs/btrfs/free-space-tree.c:1595 fs/btrfs/free-space-tree.c:1658) btrfs > [ 206.518953] caching_thread (fs/btrfs/block-group.c:873) btrfs > [ 206.519800] btrfs_work_helper (fs/btrfs/async-thread.c:314) btrfs > [ 206.520643] process_one_work (kernel/workqueue.c:2600) > [ 206.520658] worker_thread (./include/linux/list.h:292 kernel/workqueue.c:2752) > [ 206.520672] kthread (kernel/kthread.c:389) > [ 206.520684] ret_from_fork (arch/x86/kernel/process.c:145) > [ 206.520701] ret_from_fork_asm (arch/x86/entry/entry_64.S:312) > > [ 206.520722] read to 0xffff963df6a90fe0 of 8 bytes by task 2793 on cpu 6: > [ 206.520735] xas_find_marked (./include/linux/xarray.h:1706 lib/xarray.c:1354) > [ 206.520750] filemap_get_folios_tag (mm/filemap.c:1975 mm/filemap.c:2273) > [ 206.520763] __filemap_fdatawait_range (mm/filemap.c:519) > [ 206.520777] filemap_fdatawait_range (mm/filemap.c:556) > [ 206.520790] btrfs_wait_ordered_range (fs/btrfs/ordered-data.c:839) btrfs > [ 206.521641] btrfs_sync_file (fs/btrfs/file.c:1859) btrfs > [ 206.522495] vfs_fsync_range (fs/sync.c:188) > [ 206.522509] __x64_sys_fsync (./include/linux/file.h:45 fs/sync.c:213 fs/sync.c:220 fs/sync.c:218 fs/sync.c:218) > [ 206.522522] do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > [ 206.522535] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > > [ 206.522557] value changed: 0xfffffffffff80000 -> 0xfffffffffff00000 > > [ 206.522574] Reported by Kernel Concurrency Sanitizer on: > [ 206.522585] CPU: 6 PID: 2793 Comm: tracker-extract Tainted: G L 6.5.0-rc6+ #44 > [ 206.522600] Hardware name: ASRock X670E PG Lightning/X670E PG Lightning, BIOS 1.21 04/26/2023 > [ 206.522608] ================================================================== Thanks for working on this. I guess the full KCSAN warning isn't that useful in the changelog. Rather I'd spend more time explaining the real problem here ... > As Jan Kara explained, the problem is in the function xas_find_chuck(): > > /* Private */ > static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, > xa_mark_t mark) > { > unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark]; > unsigned int offset = xas->xa_offset; > > if (advance) > offset++; > if (XA_CHUNK_SIZE == BITS_PER_LONG) { > if (offset < XA_CHUNK_SIZE) { > → unsigned long data = *addr & (~0UL << offset); > if (data) > return __ffs(data); ... which is that xas_find_chunk() is called only under RCU protection and thus the two uses of 'data' in the above code can yield different results. > } > return XA_CHUNK_SIZE; > } > > return find_next_bit(addr, XA_CHUNK_SIZE, offset); > } > > In particular, the line > > unsigned long data = *addr & (~0UL << offset); > > contains a data race that is best avoided using READ_ONCE(), which eliminated the KCSAN > data-race warning completely. Yes, this improves the situation for xarray use on 64-bit architectures but doesn't fix cases on 32-bit archs or if CONFIG_BASE_SMALL is set. As I mentioned in my previous reply, I'd rather: 1) Fix find_next_bit(), find_first_bit() and related functions in lib/find_bit.c to use READ_ONCE() - such as _find_first_bit() etc. It is quite some churn but I don't see how else to make these functions safe when the underlying contents can change. 2) Change xas_find_chunk() to unconditionally use find_next_bit() as the special case XA_CHUNK_SIZE == BITS_PER_LONG seems pointless these days because find_next_bit() is inline and does small_const_nbits(size) check. Honza > Reported-by: Mirsad Goran Todorovac > Suggested-by: Jan Kara > Fixes: b803b42823d0d ("xarray: Add XArray iterators") > Matthew Wilcox > Cc: Chris Mason > Cc: Andrew Morton > Cc: Josef Bacik > Cc: David Sterba > Cc: linux-btrfs@vger.kernel.org > Cc: linux-fsdevel@vger.kernel.org > Cc: linux-mm@kvack.org > Signed-off-by: Mirsad Goran Todorovac > --- > v1: the proposed fix (RFC) > > include/linux/xarray.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/linux/xarray.h b/include/linux/xarray.h > index cb571dfcf4b1..1715fd322d62 100644 > --- a/include/linux/xarray.h > +++ b/include/linux/xarray.h > @@ -1720,7 +1720,7 @@ static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, > offset++; > if (XA_CHUNK_SIZE == BITS_PER_LONG) { > if (offset < XA_CHUNK_SIZE) { > - unsigned long data = *addr & (~0UL << offset); > + unsigned long data = READ_ONCE(*addr) & (~0UL << offset); > if (data) > return __ffs(data); > } > -- > 2.34.1 > -- Jan Kara SUSE Labs, CR