Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1113161rdg; Wed, 11 Oct 2023 15:10:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHMejFNqn6zJ+NAR8qaPDvdtczMIALw4wErT6SWwBlGKNoPvMLhAbOvjBblzR9WtoVCO8kp X-Received: by 2002:a05:6830:18e4:b0:6c4:d2a8:b78a with SMTP id d4-20020a05683018e400b006c4d2a8b78amr24220905otf.11.1697062209904; Wed, 11 Oct 2023 15:10:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697062209; cv=none; d=google.com; s=arc-20160816; b=PITH026Z5sAOWMqTlfV0YpovClGFSpKFvx/9EVJb/cnc5nPFkI7ucIX+9BMwpIOO4Y yQQI5biCFcaaG1E3LLfwVDNRXZlAFN7Cc5x9OOYZgnK9x1K4IJwrySTdPuchbsScS5Vi dxrDifsYqrnLTx9jKBdmSu42UpPL3UYB6c1K+BT4b2bRHgDO2/5sdQk4rjHQks3Na53u jga8r6qhgfxaZ1FW/SnL5adyWOHjmbkrtrmmb1B30ldX8AH5tkZHnFJ5mIr3fy5+hBrW zb4PJjzwwfIeF6qjsXnoBtgWYWjl9qE9MtCojPFrZJTw+nIem9xSo2FYQgtu2lK626vN nrLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=qXZ1ta0E6v5SBS7XDT1CohP/xNnHJeqyXp5NJst8OM4=; fh=qDPxQ2qfMt0QE8Y2FsrSQ80vmlwB2KS6OSbKSI8TnNg=; b=EJIVfrbZul5xcsBKGK24/+7foLFh7jRwmE4mMkceG9ruNClEFdc1q9N8P55iiYMFUz e7MBUnwdLsVAHWqMVn/joA8ODPwXHxffKEOW3hZme6pYKJbE+b8mA/MDxLreazzQMr3+ nIFoAbBwVHBE1TD1NoZHuepOOKWhaOb41deuKC6PubjxOL5JRV4Efd7SrT3Ghhn40I2R anuU/WTjKHLUoexBRVJc25fRZJPJka0u46HbNSbl89TnqBNRPsjZdPkcs8JWRrZc5UOy iWPz6p/e1PA08VaBvC8HygVcd3zRYnAgEYCzJe2lWB1sskAsziQ3lMoe9vQ/SZj+T5ST tBrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="tOT9/3rg"; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=biLKBP9H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id u63-20020a638542000000b0059a1cedf5b8si803224pgd.88.2023.10.11.15.10.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 15:10:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="tOT9/3rg"; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=biLKBP9H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2269680E227B; Wed, 11 Oct 2023 15:10:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233771AbjJKWJq (ORCPT + 99 others); Wed, 11 Oct 2023 18:09:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233339AbjJKWJo (ORCPT ); Wed, 11 Oct 2023 18:09:44 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCF45A9; Wed, 11 Oct 2023 15:09:41 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 58F4360152; Thu, 12 Oct 2023 00:09:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697062179; bh=6wUwXu3t7Gckky7HKI6eDTMXYXcs5vo82bMnXb4UUDQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=tOT9/3rgnQy2W+sBb84pIL0YB4w8yoiYcM9WVtH7NEYQ4DdALejQZ7qCo4B/+4qbJ Vqw6JsXyKn9Mdo5fY8+4K8C+GFkIVg+MywibbMDHjWBSp+zTJS6eCxmDWAXm3Wgt5b xp7iAaZGIZajSjXnA3a99QgEh2SAX2gq/2zRd0FaE59fLWwKGvbyzSJyvYfXSGnzlj Vx2S+pYnuvUC14/jL5AHV3ke1c6/m86wk5EJi8pGxprtDxdgYhyUBPrmDZSuJcDk3A s0CjruKh8CqBJrEw2AFkPkYKosMRzbb0a3Jag8nH+/4vsBqhGNgLCvvbIqD6n3KF61 /5ZzpIVqOI04Q== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4l_w5DQCLSvN; Thu, 12 Oct 2023 00:09:28 +0200 (CEST) Received: from [192.168.1.6] (78-1-184-43.adsl.net.t-com.hr [78.1.184.43]) by domac.alu.hr (Postfix) with ESMTPSA id 626836013C; Thu, 12 Oct 2023 00:09:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697062168; bh=6wUwXu3t7Gckky7HKI6eDTMXYXcs5vo82bMnXb4UUDQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=biLKBP9HXaDMesiqXKMQ1wGqgqWMiK6K1If429I6H6BQCn9P+WCn+S1a9RSZZapHk pu/UmowjnY0vcFVuiwF6oKp4bY6TQ5vO5Vv0gqT+ykAiYNRsA9lrCVn5Xkp4p5KV2R T1TQetSPl3RCHRbS9pWsU3rakQKNfPXck3MxeQL5Iwk4IWjQgRPb+yK5rKcEo6o9SX YRsOmov5bFm4lWjSmcN+ecxqbYyjgL9muredba5uRwHceLJWJXw2CFniQPLBfjxoaf eFgJlhyYh1yDEZ0u79UDCcfXTzm4JroW2/4SanwLNyesnQpsvgDNqj/MMLoW6eYpAE 1KQG/ivzQhOSA== Message-ID: <1b030e7d-1d8d-4c77-a6a0-870794090661@alu.unizg.hr> Date: Thu, 12 Oct 2023 00:09:27 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/1] xarray: fix the data-race in xas_find_chunk() by using READ_ONCE() Content-Language: en-US To: Jan Kara , Mirsad Todorovac Cc: Matthew Wilcox , Yury Norov , Philipp Stanner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Chris Mason , Andrew Morton , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-mm@kvack.org References: <20230918094116.2mgquyxhnxcawxfu@quack3> <22ca3ad4-42ef-43bc-51d0-78aaf274977b@alu.unizg.hr> <20230918113840.h3mmnuyer44e5bc5@quack3> <20230918155403.ylhfdbscgw6yek6p@quack3> <2c7f2acd-ef37-4504-a0e3-f74b66c455ec@alu.unizg.hr> <20231009101550.pqnkrp5cp5zbr3lr@quack3> From: Mirsad Todorovac In-Reply-To: <20231009101550.pqnkrp5cp5zbr3lr@quack3> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Wed, 11 Oct 2023 15:10:07 -0700 (PDT) X-Spam-Level: *** On 10/9/23 12:15, Jan Kara wrote: > On Fri 06-10-23 16:39:54, Mirsad Todorovac wrote: >> On 9/19/2023 6:20 AM, Matthew Wilcox wrote: >>> On Mon, Sep 18, 2023 at 11:56:36AM -0700, Yury Norov wrote: >>>> Guys, I lost the track of the conversation. In the other email Mirsad >>>> said: >>>> Which was the basic reason in the first place for all this, because something changed >>>> data from underneath our fingers .. >>>> >>>> It sounds clearly to me that this is a bug in xarray, *revealed* by >>>> find_next_bit() function. But later in discussion you're trying to 'fix' >>>> find_*_bit(), like if find_bit() corrupted the bitmap, but it's not. >>> >>> No, you're really confused. That happens. >>> >>> KCSAN is looking for concurrency bugs. That is, does another thread >>> mutate the data "while" we're reading it. It does that by reading >>> the data, delaying for a few instructions and reading it again. If it >>> changed, clearly there's a race. That does not mean there's a bug! >>> >>> Some races are innocuous. Many races are innocuous! The problem is >>> that compilers sometimes get overly clever and don't do the obvious >>> thing you ask them to do. READ_ONCE() serves two functions here; >>> one is that it tells the compiler not to try anything fancy, and >>> the other is that it tells KCSAN to not bother instrumenting this >>> load; no load-delay-reload. >>> >>>> In previous email Jan said: >>>> for any sane compiler the generated assembly with & without READ_ONCE() >>>> will be exactly the same. >>>> >>>> If the code generated with and without READ_ONCE() is the same, the >>>> behavior would be the same, right? If you see the difference, the code >>>> should differ. >>> >>> Hopefully now you understand why this argument is wrong ... >>> >>>> You say that READ_ONCE() in find_bit() 'fixes' 200 KCSAN BUG warnings. To >>>> me it sounds like hiding the problems instead of fixing. If there's a race >>>> between writing and reading bitmaps, it should be fixed properly by >>>> adding an appropriate serialization mechanism. Shutting Kcsan up with >>>> READ_ONCE() here and there is exactly the opposite path to the right direction. >>> >>> Counterpoint: generally bitmaps are modified with set_bit() which >>> actually is atomic. We define so many bitmap things as being atomic >>> already, it doesn't feel like making find_bit() "must be protected" >>> as a useful use of time. >>> >>> But hey, maybe I'm wrong. Mirsad, can you send Yury the bug reports >>> for find_bit and friends, and Yury can take the time to dig through them >>> and see if there are any real races in that mess? >>> >>>> Every READ_ONCE must be paired with WRITE_ONCE, just like atomic >>>> reads/writes or spin locks/unlocks. Having that in mind, adding >>>> READ_ONCE() in find_bit() requires adding it to every bitmap function >>>> out there. And this is, as I said before, would be an overhead for >>>> most users. >>> >>> I don't believe you. Telling the compiler to stop trying to be clever >>> rarely results in a performance loss. >> >> Hi Mr. Wilcox, >> >> Do you think we should submit a formal patch for this data-race? > > So I did some benchmarking with various GCC versions and the truth is that > READ_ONCE() does affect code generation a bit (although the original code > does not refetch the value from memory). As a result my benchmarks show the > bit searching functions are about 2% slower. This is not much but it is > stupid to cause a performance regression due to non-issue. I'm trying to > get some compiler guys look into this whether we can improve it somehow... > > Honza Dear Jan, First, I am not an expert or an authority on the subject, this is only my opinion. IMHO, 2% slower code is acceptable if it gives us data integrity. If a 16-core system manages to break and tear loads without READ_ONCE(), 2% faster code gives us nothing if the other core changes half of the location in the midst of the load, just because the optimiser did some "funny stuff". If I had a pacemaker and it is running Linux kernel, I would probably choose 2% slower but race-free code. Please allow me to assert that this is not a spin lock, memory bus lock, or a memory barrier that would affect the other cores - it will only slightly prevent some read reordering/tearing. I think you are on the good track, and that this patch is a good thing. Low-level functions have to be first safe, then fast. A faster algorithm, like replacing spinlocks with RCU, can certainly more than make up for that ... Sorry for a digression. Best regards, Mirsad