Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp180725lqf; Fri, 26 Apr 2024 03:40:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUXXKeuYusvJkcZ3wxXMHkikycjlikQ9X04fYRc+u1oYUK1OwC3ryJ1Dciuzg49zOzP7zyhIOR1R1i18xPjlW/4nFpF4ybd0XCpuMuDAg== X-Google-Smtp-Source: AGHT+IF+rDd44a2C9ZmeMuLbl8Vtpn3QUvDrbm7PhAifArHfVh0ZWf/UKJ3p1VLV/3y9+nvJv+Cc X-Received: by 2002:a17:903:1c4:b0:1e3:c5d0:9fb8 with SMTP id e4-20020a17090301c400b001e3c5d09fb8mr2480292plh.42.1714128013123; Fri, 26 Apr 2024 03:40:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714128013; cv=pass; d=google.com; s=arc-20160816; b=eKutltvXj+4m6ji5jfu+74wHhP9of1DNFBNz1I4rH8AVVnLpZFTMQskAz9WNGK/UzI 1NzfO1ihfNkYdj48oKNt7qTDZ2UsHxsAr0/OVdUhgy6+PoftABfM6zvoOWf5KeVBxQQB oAFEMeYrYTYRHZPfGlookYM8mO68vDTzExQmFf0WA4uR5ntHgw436CBN1NDVZl5RmqRX sNX+6Ccw3AWiZqdpZTM83Ts4fuiD7fciwquosCeleE5GL4UHMROG7Ru43IamQyA0swHy BgyOH3RdrmlMMzS5fGB3y6wgeEKsTXPaSmsnZYIUcu5cXns36x/NoiuZClrYhrYbNV/k pNkQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:subject:cc:to:from :message-id:dkim-signature; bh=YaSU9M5opPOL0ZuAUzC5OKbwbd83LKiNUAukNIWA6AI=; fh=/vJGAtQWatubdDmYTPjswJUwBE21Luu4idnkuXIEugw=; b=gQq9/McaswwCDT4Mp4GJEDiCBAFcVfjBwpGVaLGDKxipA2965z6uHAlVjX5t5AHrJN pIvVJ6rtkcpYyxaoaIH1Zj7cFhHRvZc4qLKvbE3MKVoxkPDo2yEpTHa6ktzrIc/GqnTi tS6egv9GuWyTz2oAvfLd0U0xpaqDb42gIwb5yeAE6TZiSXnf2IGRBcGg/InldVb7Q7U2 nMQheerwL256Gwv6xOmPuy7FHAnNGG0Bx4ThZTNF6+g8M9VD9MQux74OogxXLF6jKk/i vK90rnouqH70KPU7DeAI7U8KUPc4sngdLDuhlURki/NAkys+kNUD1Z+4FDl2tuyjjQVs drIQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@qq.com header.s=s201512 header.b=eytmQ4oQ; arc=pass (i=1 spf=pass spfdomain=qq.com dkim=pass dkdomain=qq.com dmarc=pass fromdomain=qq.com); spf=pass (google.com: domain of linux-kernel+bounces-159880-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159880-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=qq.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2-20020a170902cec200b001def0abd873si15458993plg.198.2024.04.26.03.40.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 03:40:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159880-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@qq.com header.s=s201512 header.b=eytmQ4oQ; arc=pass (i=1 spf=pass spfdomain=qq.com dkim=pass dkdomain=qq.com dmarc=pass fromdomain=qq.com); spf=pass (google.com: domain of linux-kernel+bounces-159880-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159880-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=qq.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 78489B210FC for ; Fri, 26 Apr 2024 10:40:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9C38B13F457; Fri, 26 Apr 2024 10:40:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b="eytmQ4oQ" Received: from out203-205-251-66.mail.qq.com (out203-205-251-66.mail.qq.com [203.205.251.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AADE51E877; Fri, 26 Apr 2024 10:39:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.205.251.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714128000; cv=none; b=fsJmJ3MOm2FKPGUlQiuU3X7Px0yhqn60izr6VjWJLQ722Xo2uruw+mdYTwBIpZfBqFAnwnC4lrVOfesl0m6keouBFuOAe4b8V315qa1yXWMGe4A7ZilBtmPGg88LPQjZCrhuhIxRd4W5TzJZ9ekLvHFARyf5DLcjQ9aYG3ImbvQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714128000; c=relaxed/simple; bh=UQcaRiCSJspg2olyfffBCz8/gfvZUnul2zGBfTUZ3vs=; h=Message-ID:From:To:Cc:Subject:Date:MIME-Version; b=t4If7/fCm+7DB1F+X/NWrgF1aQQ8r6FS4ul826gXhoGzWddgyAAT7AjgXeo9YQBMEGblMED4KqTKlj8imTRJTo/AOzqGOYeMjcJOzRPV15LkBOblhrNb5xHu5vmX5ys42nj/S72+vgm/nVsFXgsTfFJLFwhNqwT8dI/bgeYzYlE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=qq.com; spf=pass smtp.mailfrom=qq.com; dkim=pass (1024-bit key) header.d=qq.com header.i=@qq.com header.b=eytmQ4oQ; arc=none smtp.client-ip=203.205.251.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1714127688; bh=YaSU9M5opPOL0ZuAUzC5OKbwbd83LKiNUAukNIWA6AI=; h=From:To:Cc:Subject:Date; b=eytmQ4oQvlQimhkQ+gpVq8tbkvCbliKHBhP4t2pGTRhjWsKbazHK7cxAVpwevO4Ps Ds6f06Lu6KrT7sc6dETvoto/VcMpdR8DnsJKRfEGBdeaUrnjrVGOCX7VmGPfD4oYoN U+l5FYU8YnLBz8B6DTm2E8PeRyIwVFEPok5/8Dgo= Received: from localhost.localdomain ([58.213.8.145]) by newxmesmtplogicsvrszc19-0.qq.com (NewEsmtp) with SMTP id 8AE048E5; Fri, 26 Apr 2024 18:34:46 +0800 X-QQ-mid: xmsmtpt1714127686thx4nk91c Message-ID: X-QQ-XMAILINFO: Mv8oRO7AAqEFYV3Cjn4yCL78Ulh553OpkZiIPiaBTvXhd77YcXyVgbIeusUCT9 AqV6cO8EynyjXxlvX+BIRueV6xD2XAGrel2JReVImDY/ZdTHnh6mRdr+zM63vYSTKLlsZj16MeYh yICPoJvV5sa5vXeFkRYoR39G6Cn5NwwzI4wi1JTWT2eRjA8aN7jgT71HpkfW8M9AstnEksgZ3B7i ZfqFk9l8I2wFAtIk9B7GH1uwLikfeYIL9w9C9CvTjp2iJfzqLVWy+iCycX6NZvul6r1r3WM62qXc 5O9kipN0are4xNW9mb5+rWa8Fki1psa78dM5tBYQPLhe1BQbQKMpgiVq9pDZQzs0f9R6xE3FyGuT sUheD0NarEwrt8uuPpaVLumC08uvixswVf+Jy/5TWQanDq7+rrnVWh7v3dwb0Zp4eaDc+ojGkNVQ tignwhXkllHOhT9NkhA4OhgOdbspd+5/YsHTHQakqWX+knGyp6phceURpp0nq7bMOQaW6Jw1bavf 3og9CEewpXlVi8KhvIBFa/x2m9GK2SFkUN7BIruhgGjQqbYPJ7AZG0g4HkQhOf/RzbDx0M6dYnWg oCGUnDdZcM2Gvzreng6wlKdetna2EWDbhhiCGuMfDshS6YWMNUV1GR1DPUanNCO9AA7KYvNuJwxs +5nGF66eqT9MFpHW7LJ8mBMjMQcWAaGrrCHmpvN1hCMob+hNV0SWiRy6M9JhsA/5s5vskbghjTS6 pxqJCVMA1I+VU8/14jvT2X1OxOm68XzYuuEl66bvrWW9xwAkGRdeoagvUzhpzKNepG6TW/oaSFOZ l92/9qEvxElxvmSrdZNYDOtKH+SjtXUDrO2zeXqcsSlmfJbYsDTL3TJLzs2fJQCNSWQhtbQCtwag KPYgDPF1xJZX1M06aD+eQg2vh/qKxs77ToNPWvcLCSI5hSQdCvMArF+yAt6G9upHif2UOQeZdZ6A XeLXzOvZ5JSm+OeTNrXEcWlNI91UanlGDjrP+3FmpveAUAarlVhWr+y0diDfG7mGnhUhETLLbgZk ql7mVj2swvIuK39HTZ X-QQ-XMRINFO: NS+P29fieYNw95Bth2bWPxk= From: linke li To: Cc: xujianhao01@gmail.com, linke li , Jens Axboe , Andrew Morton , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] sbitmap: use READ_ONCE to access map->word Date: Fri, 26 Apr 2024 18:34:44 +0800 X-OQ-MSGID: <20240426103444.19386-1-lilinke99@qq.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In __sbitmap_queue_get_batch(), map->word is read several times, and update atomically using atomic_long_try_cmpxchg(). But the first two read of map->word is not protected. This patch moves the statement val = READ_ONCE(map->word) forward, eliminating unprotected accesses to map->word within the function. It is aimed at reducing the number of benign races reported by KCSAN in order to focus future debugging effort on harmful races. Signed-off-by: linke li --- lib/sbitmap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 92c6b1fd8989..1e453f825c05 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -494,18 +494,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, struct sbitmap_word *map = &sb->map[index]; unsigned long get_mask; unsigned int map_depth = __map_depth(sb, index); + unsigned long val; sbitmap_deferred_clear(map); - if (map->word == (1UL << (map_depth - 1)) - 1) + val = READ_ONCE(map->word); + if (val == (1UL << (map_depth - 1)) - 1) goto next; - nr = find_first_zero_bit(&map->word, map_depth); + nr = find_first_zero_bit(&val, map_depth); if (nr + nr_tags <= map_depth) { atomic_long_t *ptr = (atomic_long_t *) &map->word; - unsigned long val; get_mask = ((1UL << nr_tags) - 1) << nr; - val = READ_ONCE(map->word); while (!atomic_long_try_cmpxchg(ptr, &val, get_mask | val)) ; -- 2.39.3 (Apple Git-146)