From patchwork Thu Jun 20 17:56:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 806150 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95D731B583B; Thu, 20 Jun 2024 17:57:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906234; cv=none; b=IV5oNt9rTBFrOOIFeUNuHVzq/YbBN4wqaT8gmNf6N9yYXa3IOyIbxs9PvSmTMtxY+IER6wQybArOQmO+iaaEIgetxxK330XfYFLa8IL3RjxqecmomNzMvmOT9lsd7OYNPp6vUXlhjWLSQJebgc1dS9GbMF83j7ebkymux/HFyWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906234; c=relaxed/simple; bh=NJiSXR/2q9k2YAA0gEDwZCqD1aqsLC5sqUDA5CWKQZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VkIFafNLrLI8S1P9g3VUjeNKg+ZLpIW3w84KBEOAPX1tJyam3kbrVrRVUdkOXba2UtZIZehybYvPY7G6sDWkhb/fx9WlLc+nlOfLt7VH8QkaHIh7m3aBacCqAa7BpXaxCAY5MufzxrezOeR2ic4j4lffjKtV1oaceggaRsGjPdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UEmSor8g; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UEmSor8g" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1f6a837e9a3so8127765ad.1; Thu, 20 Jun 2024 10:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718906232; x=1719511032; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HgI5gEOuAZO9UYkmZKgqrzsmCGfdpvzU4+T+xEGqCGc=; b=UEmSor8gpsJWxrtWzxyHfWjhaYbI1h3q1L6shSKcG32gc31J0LUvUypWmTfNI80ahQ zgY2cL+Q3NtV3Z7kqPg5UcwEXmVsire7NaL3WMb2mtLaLbxH82jgBHQ/76l7OojHhh5J rBzo975VVn2Yj92aiiF9e4QPVkldTN7fSASVnd4+AUyqkU+uz+jEIKsN681OKNUFNSRD HarD+QtvPGas5G7xEpyHnABurCQrqYmoKxeCw4G4AbwObb9tBiGS1oGEjdG77/aC3X/M wL9UGZM1od3RvEX2HJWNtettC9qpi8wr7A1yQiiVzLHxAqjb1MIMiQaZgjYhTCSJ8h0+ /qjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718906232; x=1719511032; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HgI5gEOuAZO9UYkmZKgqrzsmCGfdpvzU4+T+xEGqCGc=; b=qLaDBMQXNXHvma1DJ3WNbD3+rQys5b+VN1z6078l6EH8iIfOZSHYAc3DZgMkIAA7Yp 1gXQNOVAkh+iptgY4v52OR4gwj1irYncpuoyxATg/KYipdBUIoF6YQHAkcvmX1Enx001 4v3lgGqqVxAgsBzK2Af3LF9QfzKFqphzZSuNvwx43TyJmgiJWHOKBAW5YuWOPfiSWLjP 2efG2OEjxD0nQdrZ81gF4w4BCExQTz/mjhjxCDsP8Gn0hML9O00Z/lb5WXP+l25Py4Ai RsHaY8IIBRgg8GlOjsWZ5G7PPYHIWCKEsxa5C+psxwtEkhNNWCogsbMZD+yIqsBr1nzz BjLA== X-Forwarded-Encrypted: i=1; AJvYcCVthfaWAW2t9NKpSmG/8WT5o8I3rG18NaWnknfPNfYPsSa91mIcTe3y4fXrRVOqcCWorN+kpbUTWk4eVY3paWtpFncPoC/q6hvWUozLhg4O9hftq6xJaVi26s9y+ysoiciBLY0WxDukaBkqlA+fl5QRKyV0BZZwKH/QcU+hLz8ENWCczQRYHB8j2gT8CT+SZWRJIWEgdSt5bjrUPkfHDVHSYLwjqipLwFG8Mgy4rQ1IptynHdgNI7xQuJlekXxNfMqDxjuMYH+aj5h0gGgQFD380UE68QaQZzmLzKMWjs5pACHJ1EGAcm3XrVVDMm3hDgGzDfJUqqQbSGFQEf82/5jQBPjw5hrSLPpwH9/CoXmJhL9ZwcVrh/c2lZmAGbUmCBQvWKxKpNCagtYYYyro/vPAGUAjgoBkYDpO+zp+GcN+sv+W3E8v/+TBuwBFB79rP2lHqDNgDEt11dpAseLoELBXRKpt0WkjqO0gt117aOWZoZ2iJV7A8kzHeNG6G5+3ruUts4z/OYzChQv5oXmecIDkWj2h7xs55ssZuqSG13unf+M2YJjK1uIQdd//EhWC1nOj5s03GRiCJgbBufkcWzJJt7xxtWwGxOV3r2S4zzQFdiXOGZ/2z95sn4HEBkZk8MOa2eUxuElRt32DNWnnZMcfTpfZdR5sHHsiFJMzveWuxMjpB3IZ75/E55wDhYefToJNPpVYSRbHm0VU4J7eDsnKUruMfNSvtAs+/kt4dH9uzwy7AC96tfbWz7ghfA1mjp1V4vy6hg== X-Gm-Message-State: AOJu0YwlmbvP9FnD34/VHC7xRpiPfMI8dRbe6nth3LkPC0HwafJxa/Sd 5z0UyYnrn6vbvYC5hjlJiqEf1RcLAZvHcFxtzV73NrJ6xGE2gbvPctGBK1N4XH8= X-Google-Smtp-Source: AGHT+IELGiFFQYXDLuYwffpBy+2gvOLdiLXmpqp8gF/i2JmFW6wub2L8+md2wz99GOx5tRmisC4VJQ== X-Received: by 2002:a17:903:2445:b0:1f9:d282:7a28 with SMTP id d9443c01a7336-1f9d2829be3mr19305765ad.41.1718906231479; Thu, 20 Jun 2024 10:57:11 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9d0ec7a7asm15684145ad.251.2024.06.20.10.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 10:57:10 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, "David S. Miller" , "H. Peter Anvin" , "James E.J. Bottomley" , "K. Y. Srinivasan" , "Md. Haris Iqbal" , Akinobu Mita , Andrew Morton , Bjorn Andersson , Borislav Petkov , Chaitanya Kulkarni , Christian Brauner , Damien Le Moal , Dave Hansen , David Disseldorp , Edward Cree , Eric Dumazet , Fenghua Yu , Geert Uytterhoeven , Greg Kroah-Hartman , Gregory Greenman , Hans Verkuil , Hans de Goede , Hugh Dickins , Ingo Molnar , Jakub Kicinski , Jaroslav Kysela , Jason Gunthorpe , Jens Axboe , Jiri Pirko , Jiri Slaby , Kalle Valo , Karsten Graul , Karsten Keil , Kees Cook , Leon Romanovsky , Mark Rutland , Martin Habets , Mauro Carvalho Chehab , Michael Ellerman , Michal Simek , Nicholas Piggin , Oliver Neukum , Paolo Abeni , Paolo Bonzini , Peter Zijlstra , Ping-Ke Shih , Rich Felker , Rob Herring , Robin Murphy , Sean Christopherson , Shuai Xue , Stanislaw Gruszka , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Valentin Schneider , Vitaly Kuznetsov , Wenjia Zhang , Will Deacon , Yoshinori Sato , GR-QLogic-Storage-Upstream@marvell.com, alsa-devel@alsa-project.org, ath10k@lists.infradead.org, dmaengine@vger.kernel.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-block@vger.kernel.org, linux-bluetooth@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-net-drivers@amd.com, linux-pci@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, linux-serial@vger.kernel.org, linux-sh@vger.kernel.org, linux-sound@vger.kernel.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, mpi3mr-linuxdrv.pdl@broadcom.com, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Cc: Yury Norov , Alexey Klimov , Bart Van Assche , Jan Kara , Linus Torvalds , Matthew Wilcox , Mirsad Todorovac , Rasmus Villemoes , Sergey Shtylyov Subject: [PATCH v4 01/40] lib/find: add atomic find_bit() primitives Date: Thu, 20 Jun 2024 10:56:24 -0700 Message-ID: <20240620175703.605111-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240620175703.605111-1-yury.norov@gmail.com> References: <20240620175703.605111-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add helpers around test_and_{set,clear}_bit() to allow searching for clear or set bits and flipping them atomically. Using atomic search primitives allows to implement lockless bitmap handling where only individual bits are touched by concurrent processes, and where people have to protect their bitmaps to search for a free or set bit due to the lack of atomic searching routines. The typical locking routines may look like this: unsigned long alloc_bit() { unsigned long bit; spin_lock(bitmap_lock); bit = find_first_zero_bit(bitmap, nbits); if (bit < nbits) __set_bit(bit, bitmap); spin_unlock(bitmap_lock); return bit; } void free_bit(unsigned long bit) { spin_lock(bitmap_lock); __clear_bit(bit, bitmap); spin_unlock(bitmap_lock); } Now with atomic find_and_set_bit(), the above can be implemented lockless, directly by using it and atomic clear_bit(). Patches 36-40 do this in few places in the kernel where the transition is clear. There is likely more candidates for refactoring. The other important case is when people opencode atomic search or atomic traverse on the maps with the patterns looking like: for (idx = 0; idx < nbits; idx++) if (test_and_clear_bit(idx, bitmap)) do_something(idx); Or like this: do { bit = find_first_bit(bitmap, nbits); if (bit >= nbits) return nbits; } while (!test_and_clear_bit(bit, bitmap)); return bit; In both cases, the opencoded loop may be converted to a single function or iterator call. Correspondingly: for_each_test_and_clear_bit(idx, bitmap, nbits) do_something(idx); Or: return find_and_clear_bit(bitmap, nbits); Obviously, the less routine code people have to write themself, the less probability to make a mistake. The new API is not only a handy helpers - it also resolves a non-trivial issue of using non-atomic find_bit() together with atomic test_and_{set,clear)_bit(). The trick is that find_bit() implies that the bitmap is a regular non-volatile piece of memory, and compiler is allowed to use such optimization techniques like re-fetching memory instead of caching it. For example, find_first_bit() is implemented like: for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { val = addr[idx]; if (val) { sz = min(idx * BITS_PER_LONG + __ffs(val), sz); break; } } On register-memory architectures, like x86, compiler may decide to access memory twice - first time to compare against 0, and second time to fetch its value to pass it to __ffs(). When running find_first_bit() on volatile memory, the memory may get changed in-between, and for instance, it may lead to passing 0 to __ffs(), which is undefined. This is a potentially dangerous call. find_and_clear_bit() as a wrapper around test_and_clear_bit() naturally treats underlying bitmap as a volatile memory and prevents compiler from such optimizations. Now that KCSAN is catching exactly this type of situations and warns on undercover memory modifications. We can use it to reveal improper usage of find_bit(), and convert it to atomic find_and_*_bit() as appropriate. In some cases concurrent operations with plain find_bit() are acceptable. For example: - two threads running find_*_bit(): safe wrt ffs(0) and returns correct value, because underlying bitmap is unchanged; - find_next_bit() in parallel with set or clear_bit(), when modifying a bit prior to the start bit to search: safe and correct; - find_first_bit() in parallel with set_bit(): safe, but may return wrong bit number; - find_first_zero_bit() in parallel with clear_bit(): same as above. In last 2 cases find_bit() may not return a correct bit number, but it may be OK if caller requires any (not exactly the first) set or clear bit, correspondingly. In such cases, KCSAN may be safely silenced with data_race(). But in most cases where KCSAN detects concurrency we should carefully review their code and likely protect critical sections or switch to atomic find_and_bit(), as appropriate. This patch adds the following atomic primitives: find_and_set_bit(addr, nbits); find_and_set_next_bit(addr, nbits, start); ... Here find_and_{set,clear} part refers to the corresponding test_and_{set,clear}_bit function. Suffixes like _wrap or _lock derive their semantics from corresponding find() or test() functions. For brevity, the naming omits the fact that we search for zero bit in find_and_set, and correspondingly search for set bit in find_and_clear functions. The patch also adds iterators with atomic semantics, like for_each_test_and_set_bit(). Here, the naming rule is to simply prefix corresponding atomic operation with 'for_each'. CC: Bart Van Assche CC: Sergey Shtylyov Signed-off-by: Yury Norov --- MAINTAINERS | 1 + include/linux/find.h | 4 - include/linux/find_atomic.h | 324 ++++++++++++++++++++++++++++++++++++ lib/find_bit.c | 86 ++++++++++ 4 files changed, 411 insertions(+), 4 deletions(-) create mode 100644 include/linux/find_atomic.h diff --git a/MAINTAINERS b/MAINTAINERS index b68c8b25bb93..54f37d4f33dd 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3730,6 +3730,7 @@ F: include/linux/bitmap-str.h F: include/linux/bitmap.h F: include/linux/bits.h F: include/linux/cpumask.h +F: include/linux/find_atomic.h F: include/linux/find.h F: include/linux/nodemask.h F: include/vdso/bits.h diff --git a/include/linux/find.h b/include/linux/find.h index 5dfca4225fef..a855f82ab9ad 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -2,10 +2,6 @@ #ifndef __LINUX_FIND_H_ #define __LINUX_FIND_H_ -#ifndef __LINUX_BITMAP_H -#error only can be included directly -#endif - #include unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits, diff --git a/include/linux/find_atomic.h b/include/linux/find_atomic.h new file mode 100644 index 000000000000..a9e238f88d0b --- /dev/null +++ b/include/linux/find_atomic.h @@ -0,0 +1,324 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_FIND_ATOMIC_H_ +#define __LINUX_FIND_ATOMIC_H_ + +#include +#include + +unsigned long _find_and_set_bit(volatile unsigned long *addr, unsigned long nbits); +unsigned long _find_and_set_next_bit(volatile unsigned long *addr, unsigned long nbits, + unsigned long start); +unsigned long _find_and_set_bit_lock(volatile unsigned long *addr, unsigned long nbits); +unsigned long _find_and_set_next_bit_lock(volatile unsigned long *addr, unsigned long nbits, + unsigned long start); +unsigned long _find_and_clear_bit(volatile unsigned long *addr, unsigned long nbits); +unsigned long _find_and_clear_next_bit(volatile unsigned long *addr, unsigned long nbits, + unsigned long start); + +/** + * find_and_set_bit - Find a zero bit and set it atomically + * @addr: The address to base the search on + * @nbits: The bitmap size in bits + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the bitmap. It's also not + * guaranteed that if >= @nbits is returned, the bitmap is empty. + * + * The function does guarantee that if returned value is in range [0 .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and set bit, or >= @nbits if no bits found + */ +static inline +unsigned long find_and_set_bit(volatile unsigned long *addr, unsigned long nbits) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr | ~GENMASK(nbits - 1, 0); + if (val == ~0UL) + return nbits; + ret = ffz(val); + } while (test_and_set_bit(ret, addr)); + + return ret; + } + + return _find_and_set_bit(addr, nbits); +} + + +/** + * find_and_set_next_bit - Find a zero bit and set it, starting from @offset + * @addr: The address to base the search on + * @nbits: The bitmap nbits in bits + * @offset: The bitnumber to start searching at + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the bitmap, starting from + * @offset. It's also not guaranteed that if >= @nbits is returned, the bitmap + * is empty. + * + * The function does guarantee that if returned value is in range [@offset .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and set bit, or >= @nbits if no bits found + */ +static inline +unsigned long find_and_set_next_bit(volatile unsigned long *addr, + unsigned long nbits, unsigned long offset) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr | ~GENMASK(nbits - 1, offset); + if (val == ~0UL) + return nbits; + ret = ffz(val); + } while (test_and_set_bit(ret, addr)); + + return ret; + } + + return _find_and_set_next_bit(addr, nbits, offset); +} + +/** + * find_and_set_bit_wrap - find and set bit starting at @offset, wrapping around zero + * @addr: The first address to base the search on + * @nbits: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * Returns: the bit number for the next clear bit, or first clear bit up to @offset, + * while atomically setting it. If no bits are found, returns >= @nbits. + */ +static inline +unsigned long find_and_set_bit_wrap(volatile unsigned long *addr, + unsigned long nbits, unsigned long offset) +{ + unsigned long bit = find_and_set_next_bit(addr, nbits, offset); + + if (bit < nbits || offset == 0) + return bit; + + bit = find_and_set_bit(addr, offset); + return bit < offset ? bit : nbits; +} + +/** + * find_and_set_bit_lock - find a zero bit, then set it atomically with lock + * @addr: The address to base the search on + * @nbits: The bitmap nbits in bits + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the bitmap. It's also not + * guaranteed that if >= @nbits is returned, the bitmap is empty. + * + * The function does guarantee that if returned value is in range [0 .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and set bit, or >= @nbits if no bits found + */ +static inline +unsigned long find_and_set_bit_lock(volatile unsigned long *addr, unsigned long nbits) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr | ~GENMASK(nbits - 1, 0); + if (val == ~0UL) + return nbits; + ret = ffz(val); + } while (test_and_set_bit_lock(ret, addr)); + + return ret; + } + + return _find_and_set_bit_lock(addr, nbits); +} + +/** + * find_and_set_next_bit_lock - find a zero bit and set it atomically with lock + * @addr: The address to base the search on + * @nbits: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the range. It's also not + * guaranteed that if >= @nbits is returned, the bitmap is empty. + * + * The function does guarantee that if returned value is in range [@offset .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and set bit, or >= @nbits if no bits found + */ +static inline +unsigned long find_and_set_next_bit_lock(volatile unsigned long *addr, + unsigned long nbits, unsigned long offset) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr | ~GENMASK(nbits - 1, offset); + if (val == ~0UL) + return nbits; + ret = ffz(val); + } while (test_and_set_bit_lock(ret, addr)); + + return ret; + } + + return _find_and_set_next_bit_lock(addr, nbits, offset); +} + +/** + * find_and_set_bit_wrap_lock - find zero bit starting at @ofset and set it + * with lock, and wrap around zero if nothing found + * @addr: The first address to base the search on + * @nbits: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * Returns: the bit number for the next set bit, or first set bit up to @offset + * If no bits are set, returns >= @nbits. + */ +static inline +unsigned long find_and_set_bit_wrap_lock(volatile unsigned long *addr, + unsigned long nbits, unsigned long offset) +{ + unsigned long bit = find_and_set_next_bit_lock(addr, nbits, offset); + + if (bit < nbits || offset == 0) + return bit; + + bit = find_and_set_bit_lock(addr, offset); + return bit < offset ? bit : nbits; +} + +/** + * find_and_clear_bit - Find a set bit and clear it atomically + * @addr: The address to base the search on + * @nbits: The bitmap nbits in bits + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the bitmap. It's also not + * guaranteed that if >= @nbits is returned, the bitmap is empty. + * + * The function does guarantee that if returned value is in range [0 .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and cleared bit, or >= @nbits if no bits found + */ +static inline unsigned long find_and_clear_bit(volatile unsigned long *addr, unsigned long nbits) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr & GENMASK(nbits - 1, 0); + if (val == 0) + return nbits; + ret = __ffs(val); + } while (!test_and_clear_bit(ret, addr)); + + return ret; + } + + return _find_and_clear_bit(addr, nbits); +} + +/** + * find_and_clear_next_bit - Find a set bit next after @offset, and clear it atomically + * @addr: The address to base the search on + * @nbits: The bitmap nbits in bits + * @offset: bit offset at which to start searching + * + * This function is designed to operate in concurrent access environment. + * + * Because of concurrency and volatile nature of underlying bitmap, it's not + * guaranteed that the found bit is the 1st bit in the range It's also not + * guaranteed that if >= @nbits is returned, there's no set bits after @offset. + * + * The function does guarantee that if returned value is in range [@offset .. @nbits), + * the acquired bit belongs to the caller exclusively. + * + * Returns: found and cleared bit, or >= @nbits if no bits found + */ +static inline +unsigned long find_and_clear_next_bit(volatile unsigned long *addr, + unsigned long nbits, unsigned long offset) +{ + if (small_const_nbits(nbits)) { + unsigned long val, ret; + + do { + val = *addr & GENMASK(nbits - 1, offset); + if (val == 0) + return nbits; + ret = __ffs(val); + } while (!test_and_clear_bit(ret, addr)); + + return ret; + } + + return _find_and_clear_next_bit(addr, nbits, offset); +} + +/** + * __find_and_set_bit - Find a zero bit and set it non-atomically + * @addr: The address to base the search on + * @nbits: The bitmap size in bits + * + * A non-atomic version of find_and_set_bit() needed to help writing + * common-looking code where atomicity is provided externally. + * + * Returns: found and set bit, or >= @nbits if no bits found + */ +static inline +unsigned long __find_and_set_bit(unsigned long *addr, unsigned long nbits) +{ + unsigned long bit; + + bit = find_first_zero_bit(addr, nbits); + if (bit < nbits) + __set_bit(bit, addr); + + return bit; +} + +/* same as for_each_set_bit() but atomically clears each found bit */ +#define for_each_test_and_clear_bit(bit, addr, size) \ + for ((bit) = 0; \ + (bit) = find_and_clear_next_bit((addr), (size), (bit)), (bit) < (size); \ + (bit)++) + +/* same as for_each_set_bit_from() but atomically clears each found bit */ +#define for_each_test_and_clear_bit_from(bit, addr, size) \ + for (; (bit) = find_and_clear_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++) + +/* same as for_each_clear_bit() but atomically sets each found bit */ +#define for_each_test_and_set_bit(bit, addr, size) \ + for ((bit) = 0; \ + (bit) = find_and_set_next_bit((addr), (size), (bit)), (bit) < (size); \ + (bit)++) + +/* same as for_each_clear_bit_from() but atomically clears each found bit */ +#define for_each_test_and_set_bit_from(bit, addr, size) \ + for (; \ + (bit) = find_and_set_next_bit((addr), (size), (bit)), (bit) < (size); \ + (bit)++) + +#endif /* __LINUX_FIND_ATOMIC_H_ */ diff --git a/lib/find_bit.c b/lib/find_bit.c index 0836bb3d76c5..a322abd1e540 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -128,6 +129,91 @@ unsigned long _find_first_and_and_bit(const unsigned long *addr1, } EXPORT_SYMBOL(_find_first_and_and_bit); +unsigned long _find_and_set_bit(volatile unsigned long *addr, unsigned long nbits) +{ + unsigned long bit; + + do { + bit = FIND_FIRST_BIT(~addr[idx], /* nop */, nbits); + if (bit >= nbits) + return nbits; + } while (test_and_set_bit(bit, addr)); + + return bit; +} +EXPORT_SYMBOL(_find_and_set_bit); + +unsigned long _find_and_set_next_bit(volatile unsigned long *addr, + unsigned long nbits, unsigned long start) +{ + unsigned long bit; + + do { + bit = FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start); + if (bit >= nbits) + return nbits; + } while (test_and_set_bit(bit, addr)); + + return bit; +} +EXPORT_SYMBOL(_find_and_set_next_bit); + +unsigned long _find_and_set_bit_lock(volatile unsigned long *addr, unsigned long nbits) +{ + unsigned long bit; + + do { + bit = FIND_FIRST_BIT(~addr[idx], /* nop */, nbits); + if (bit >= nbits) + return nbits; + } while (test_and_set_bit_lock(bit, addr)); + + return bit; +} +EXPORT_SYMBOL(_find_and_set_bit_lock); + +unsigned long _find_and_set_next_bit_lock(volatile unsigned long *addr, + unsigned long nbits, unsigned long start) +{ + unsigned long bit; + + do { + bit = FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start); + if (bit >= nbits) + return nbits; + } while (test_and_set_bit_lock(bit, addr)); + + return bit; +} +EXPORT_SYMBOL(_find_and_set_next_bit_lock); + +unsigned long _find_and_clear_bit(volatile unsigned long *addr, unsigned long nbits) +{ + unsigned long bit; + + do { + bit = FIND_FIRST_BIT(addr[idx], /* nop */, nbits); + if (bit >= nbits) + return nbits; + } while (!test_and_clear_bit(bit, addr)); + + return bit; +} +EXPORT_SYMBOL(_find_and_clear_bit); + +unsigned long _find_and_clear_next_bit(volatile unsigned long *addr, + unsigned long nbits, unsigned long start) +{ + do { + start = FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start); + if (start >= nbits) + return nbits; + } while (!test_and_clear_bit(start, addr)); + + return start; +} +EXPORT_SYMBOL(_find_and_clear_next_bit); + #ifndef find_first_zero_bit /* * Find the first cleared bit in a memory region. From patchwork Thu Jun 20 17:56:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 806485 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7123B1B4C58; Thu, 20 Jun 2024 17:57:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906237; cv=none; b=FJ8phEsz78lRnitDHCh+w4eVHrzyGi3vpzRIvhrz06weKgk5cyYWuE3yQs1NitoS998K2+bYUik7yk1BIlPmPrveaCW3l55OwWvH74Mhtwem695h1yCxIFMwseUJKorXNQMvwHYX/XLWx10aR3QdRAtxqxhmG5bX/R5yKL3lLT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906237; c=relaxed/simple; bh=AaGeTjrw0FfF+0h9/U20FA8dwnbicgFR6M2oz4CBhwo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=saYHHK/gETN5oulQHjzRo+QjFppqP/bhScEzFupoMpZXpjHlXC676vdwyHmVjGxpJtgOjtsy8M5LZuXp3wFO0OIw7BhSZX8I21rTvoTi/kcy32AijbckXhOYSWUctodNyeFkLKGwzb8GtGvvKtuFYwH4KR6+YcKnSlogJlfwvEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KKbhWgKz; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KKbhWgKz" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-6e3ff7c4cc8so879911a12.3; Thu, 20 Jun 2024 10:57:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718906235; x=1719511035; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=93DcC67mVeY/+nPj/RabbHCmpdvAxDoR+WvyjbP4Fv8=; b=KKbhWgKz9TSMkT6GJx+oHzkl14SBbVxhVprFE8KF/u/HwKkgFovEPas7JNigg1VVpd IzxkpMh6nT0UVYcmNhjwq9MIlDRt7g4yYmm1O8iWkPVeTFXREfOb3nd8oxVGrFb4hivT frnx7AjBvakMunPftb+HPJfaJzv7MEmRKL11bKx73fKKPvm19ZVUblrFVREqAa9HlPBz RnyS7gfOlRGCSNyKlrjaT9fU7aG15/2c0Sivad/bbqBvx9C6z5bFVhQxYqskkBgTBcvI dFbp2Vcg+7ym42AldglDeF/T0hQztGy2EkhncU7SuJCrN+Lx13DpCFbN8QA+JJYBhXJr ugWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718906235; x=1719511035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=93DcC67mVeY/+nPj/RabbHCmpdvAxDoR+WvyjbP4Fv8=; b=aUGjv/A8mdkdH7XtdU+XpiM18b9wp6RwlvA94ToqCLV3JXdwI8UxzOGZ4/Pe7s7Vrs tYS3qxbCw8BrH3DA2S+Uw7m2acxtI8VWt3UGstQHXMwOeHQ9Y4RvWyhpYVpsyuKWYwx0 7W/mqV+n6PjY85hFK1TnEc2oKbj3TIdgKI8rfdV4gJgO3hS5szd5Dg+Ximz0NMcdAxx8 UAnfIyKkRewgk17aTUR8LpSw7iz1fiL7Mw/WxemlVlJC6J+BrDrp6mL6Jx2KepoJqPVd okjrEEY9AjEj/rZPKkORGxp27RQgRjTeZ5twhRtfOVktktAOcg75oJRtMFKKsQE2q0zh jHXg== X-Forwarded-Encrypted: i=1; AJvYcCWV+K9ndBXcS6nR4BZCdr5FZ4oAm28DWAbaj9VRGgYwjY6fUR68oH1iagyeEJVBdUKps7X87/GebWzADXXo6w6OzrnClXfJY87eLd3E2m4pT50G23zy4yri8dtA7nQU9A+6fzUTW3obwUBw0PAcTCrWeeHtl7EzDoh9XWk8ex0M8SekmlNEb0AnlgrOZ+KB5f7X5O0ii7i/jAONdEMK32sofaIWhTVqmyeXzfAzVF+9mRHzR80cbpLfQ5ev/N00Z0Yh/ROyXB+WSIeEcZajeO/odnO4SWWQMRDyWpnm6m15o8WfhGJs02LAEV+id3dUTxYT1kKfCLWzdY78Sj+bDnXp5N8BS8WHc0aQ6MoHUYpupvGFtkVRAah9ekciSunxeEuQJTY7onBVQ9J9D3SM/ATLzj+SrLhAD+F7BCUXRY+QWkYmn0VipDEP14SL94St6JmJPYNJQg+ajN8AJ9Z1OeLqB9UzcIms6BiF1P0T89EYthNGaiBDxaNEhxlK8suAGKHDDuEg9dO1r+lS4uw5TxCJHimQRooivTK4/xIBon44uO9wSxYwe7OEQHtUj3qXOuEs7FWJ2YNUPdEyzaITVE6xf/eHRMTdP1hMXEYdpyurFHUFDU9hOd0VWmhnjn4CZ1Oc4mkuYsfc12fFH/EPvNRI7cq823/C2uORlIyZw8eN2s2iSCcEzsjOUgpqHAKslh23shccBpso9RelSLs9SjETXtwyljbfrjwL/oq4Ox9u80UTZJFJQ6ADDftE3kskwueQZJTPqQ== X-Gm-Message-State: AOJu0Yx5frqbE7XwDrj7MGKWoBtAdKKoiU8VtdHN2IEA/JB4LxxmDYel YuuoFvMDzov7GE7AMhbcIt4V2nRa0W4yqzZOH4x6UlAyn+vMhtKMV29e1XxwYF0= X-Google-Smtp-Source: AGHT+IF9ITZbJ7vVNEYTX8RCffN7BEtSM9imchEPcbZOZxImDJdvzQARxKbAqm1aXg+Hi0vjNW3ABQ== X-Received: by 2002:a17:902:f687:b0:1f6:fcd9:5b86 with SMTP id d9443c01a7336-1f9aa3ecca8mr64371665ad.12.1718906234591; Thu, 20 Jun 2024 10:57:14 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9d28ce155sm15196715ad.259.2024.06.20.10.57.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 10:57:14 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, "David S. Miller" , "H. Peter Anvin" , "James E.J. Bottomley" , "K. Y. Srinivasan" , "Md. Haris Iqbal" , Akinobu Mita , Andrew Morton , Bjorn Andersson , Borislav Petkov , Chaitanya Kulkarni , Christian Brauner , Damien Le Moal , Dave Hansen , David Disseldorp , Edward Cree , Eric Dumazet , Fenghua Yu , Geert Uytterhoeven , Greg Kroah-Hartman , Gregory Greenman , Hans Verkuil , Hans de Goede , Hugh Dickins , Ingo Molnar , Jakub Kicinski , Jaroslav Kysela , Jason Gunthorpe , Jens Axboe , Jiri Pirko , Jiri Slaby , Kalle Valo , Karsten Graul , Karsten Keil , Kees Cook , Leon Romanovsky , Mark Rutland , Martin Habets , Mauro Carvalho Chehab , Michael Ellerman , Michal Simek , Nicholas Piggin , Oliver Neukum , Paolo Abeni , Paolo Bonzini , Peter Zijlstra , Ping-Ke Shih , Rich Felker , Rob Herring , Robin Murphy , Sean Christopherson , Shuai Xue , Stanislaw Gruszka , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Valentin Schneider , Vitaly Kuznetsov , Wenjia Zhang , Will Deacon , Yoshinori Sato , GR-QLogic-Storage-Upstream@marvell.com, alsa-devel@alsa-project.org, ath10k@lists.infradead.org, dmaengine@vger.kernel.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-block@vger.kernel.org, linux-bluetooth@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-net-drivers@amd.com, linux-pci@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, linux-serial@vger.kernel.org, linux-sh@vger.kernel.org, linux-sound@vger.kernel.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, mpi3mr-linuxdrv.pdl@broadcom.com, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Cc: Yury Norov , Alexey Klimov , Bart Van Assche , Jan Kara , Linus Torvalds , Matthew Wilcox , Mirsad Todorovac , Rasmus Villemoes , Sergey Shtylyov Subject: [PATCH v4 02/40] lib/find: add test for atomic find_bit() ops Date: Thu, 20 Jun 2024 10:56:25 -0700 Message-ID: <20240620175703.605111-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240620175703.605111-1-yury.norov@gmail.com> References: <20240620175703.605111-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add basic functionality test for new API. Signed-off-by: Yury Norov --- lib/test_bitmap.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index 65a75d58ed9e..405f79dd2266 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -6,6 +6,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#include #include #include #include @@ -221,6 +222,65 @@ static void __init test_zero_clear(void) expect_eq_pbl("", bmap, 1024); } +static void __init test_find_and_bit(void) +{ + unsigned long w, w_part, bit, cnt = 0; + DECLARE_BITMAP(bmap, EXP1_IN_BITS); + + /* + * Test find_and_clear{_next}_bit() and corresponding + * iterators + */ + bitmap_copy(bmap, exp1, EXP1_IN_BITS); + w = bitmap_weight(bmap, EXP1_IN_BITS); + + for_each_test_and_clear_bit(bit, bmap, EXP1_IN_BITS) + cnt++; + + expect_eq_uint(w, cnt); + expect_eq_uint(0, bitmap_weight(bmap, EXP1_IN_BITS)); + + bitmap_copy(bmap, exp1, EXP1_IN_BITS); + w = bitmap_weight(bmap, EXP1_IN_BITS); + w_part = bitmap_weight(bmap, EXP1_IN_BITS / 3); + + cnt = 0; + bit = EXP1_IN_BITS / 3; + for_each_test_and_clear_bit_from(bit, bmap, EXP1_IN_BITS) + cnt++; + + expect_eq_uint(bitmap_weight(bmap, EXP1_IN_BITS), bitmap_weight(bmap, EXP1_IN_BITS / 3)); + expect_eq_uint(w_part, bitmap_weight(bmap, EXP1_IN_BITS)); + expect_eq_uint(w - w_part, cnt); + + /* + * Test find_and_set{_next}_bit() and corresponding + * iterators + */ + bitmap_copy(bmap, exp1, EXP1_IN_BITS); + w = bitmap_weight(bmap, EXP1_IN_BITS); + cnt = 0; + + for_each_test_and_set_bit(bit, bmap, EXP1_IN_BITS) + cnt++; + + expect_eq_uint(EXP1_IN_BITS - w, cnt); + expect_eq_uint(EXP1_IN_BITS, bitmap_weight(bmap, EXP1_IN_BITS)); + + bitmap_copy(bmap, exp1, EXP1_IN_BITS); + w = bitmap_weight(bmap, EXP1_IN_BITS); + w_part = bitmap_weight(bmap, EXP1_IN_BITS / 3); + cnt = 0; + + bit = EXP1_IN_BITS / 3; + for_each_test_and_set_bit_from(bit, bmap, EXP1_IN_BITS) + cnt++; + + expect_eq_uint(EXP1_IN_BITS - bitmap_weight(bmap, EXP1_IN_BITS), + EXP1_IN_BITS / 3 - bitmap_weight(bmap, EXP1_IN_BITS / 3)); + expect_eq_uint(EXP1_IN_BITS * 2 / 3 - (w - w_part), cnt); +} + static void __init test_find_nth_bit(void) { unsigned long b, bit, cnt = 0; @@ -1482,6 +1542,8 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); + + test_find_and_bit(); } KSTM_MODULE_LOADERS(test_bitmap); From patchwork Thu Jun 20 17:56:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 806149 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E83CD1C689B; Thu, 20 Jun 2024 17:58:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906288; cv=none; b=VbByJxYfXpVoxy3txFkb7d7hyivi/fG5N58cccLyhzt+dstQ3dOYhs+Gu/XkSDnQhFoojU5J24aOrXUMfKaJ4kt1gPvi4Waz73jQG3ZZWfSBM+xYNLyc9NzSeF8QibTuDad7TVsEWFpWCDaDrlsY+cKhHLMOaNzSYAmpqPC+fuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906288; c=relaxed/simple; bh=rbpkUlmlOme1ikdxKqZBWeti6AjXTa90yhLh7ntoYNQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YjvnAhWj00yYCdT2ylyQpEPFsyUZilTFspcE7C2oxzBYrPvQ4E4kc0yl9uFMWKS1eWKwvu2KxxlEKr9KA2GJnYfWnxIaynp19Y7P8m9ltoQmxnLfWlkPiXgtwSauoJFT1j3S5IvT8kmSXenAcP+4uZMOlbaBihaUpSxkms/8Lx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RTnd2+Ww; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RTnd2+Ww" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1f44b45d6abso9824665ad.0; Thu, 20 Jun 2024 10:58:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718906286; x=1719511086; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SFjixNs7es/to5XyEjk1SDKnVmCrXPTnsKlxKMUnV5E=; b=RTnd2+Ww4D8I3qYAX+wiRl47Ig/lyjyAHqWZ2Sd3TlnxQHuGzxZx6ngzqlFhmhGQt6 N9mk/PUfHTWE2rbV+grMl03x2h9BB9mM02ZyZG+HixVzU/BNZwOZnhbd03IJPSHmLLQ8 kvFcQxOurvVzGnXVx6i8HWlgs7VsB96LvPkE9dy394+mJccSeJ6jw9aYDjszWWhwabpB Uik/C7EJIx0YYLcK1KYgihn6IJMn/K+YtLcf1ciKmBiFGRW3vEG0QSRgQ3ax+7dUJ4pk /lQUesoG/4o7RA9YxhOfqXn84q6N15woGm4JKJhY8YP/3KpuZ8fIwO0EvjT01J3A3X0p RZlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718906286; x=1719511086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SFjixNs7es/to5XyEjk1SDKnVmCrXPTnsKlxKMUnV5E=; b=s0ivuSK4n9XW1EhAto3B0BTdEOK3a36qBpH4TOerExHogQQhSERDxoGRbXypAkiTNh PTvtuaI2kfjfAHuCJRj2j0mzWgyb8a44Yog/WZvNvQOX6+4V2pG2H5Mcq84l+yrK5pOe uC/4U3H7qRAS51DBxnvKptyTqw5esyXJMMHSeiPfUCQo0nRxFkEkWRRChZxjPQ9KrMjG 8g+qu+QKSkUUbxvwQi0mset7TGGbRE6nC4uEiI6qJB7thlt3FK75SJTJj6JWDYWeRLR9 ReqZw4Ioon3nzLFUy2bxKakZb0EctbONEN9F8ioAsbliMp5meogSJaULHMPpTC+UbxWO PgEg== X-Forwarded-Encrypted: i=1; AJvYcCWOuo7/0GjkmXHeYeQd3OA+j8SXl6xeNyxkwzU2Aiw6fkLDwPMhZa+AKZsh5hLC5D4C6O4IKbCgfgrWEsI9y0HMJ7BjrRzTK+qOHCk= X-Gm-Message-State: AOJu0YyDj6hy4ZC7+5BHePtDL+IlEHn8YffqdQ7dOGeCdL9560mbRkMS PFnlZS3sELecJ6MeQQX/d0VDhGdMT48flq4R7E4kY+2H//nXJ9jmMFXaElfo6p0= X-Google-Smtp-Source: AGHT+IF/POMq9aLYdhYAN8mGl+02ev+tjcxf5SZfsiQcx3hYPUqBs5YJM468bSW3OlFox/Lm2qYWhA== X-Received: by 2002:a17:903:2c6:b0:1f4:977e:bf with SMTP id d9443c01a7336-1f9aa3c1bf6mr80297755ad.19.1718906286292; Thu, 20 Jun 2024 10:58:06 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855e55c96sm139667235ad.61.2024.06.20.10.58.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 10:58:05 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Hans Verkuil , Mauro Carvalho Chehab , linux-media@vger.kernel.org Cc: Yury Norov , Alexey Klimov , Bart Van Assche , Jan Kara , Linus Torvalds , Matthew Wilcox , Mirsad Todorovac , Rasmus Villemoes , Sergey Shtylyov , Hans Verkuil Subject: [PATCH v4 20/40] media: radio-shark: optimize the driver by using atomic find_bit() API Date: Thu, 20 Jun 2024 10:56:43 -0700 Message-ID: <20240620175703.605111-21-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240620175703.605111-1-yury.norov@gmail.com> References: <20240620175703.605111-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Despite that it's only 2- or 3-bit maps, convert for-loop followed by test_bit() to for_each_test_and_clear_bit() as it makes the code cleaner. Signed-off-by: Yury Norov Acked-by: Hans Verkuil --- drivers/media/radio/radio-shark.c | 6 ++---- drivers/media/radio/radio-shark2.c | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/drivers/media/radio/radio-shark.c b/drivers/media/radio/radio-shark.c index 127a3be0e0f0..c7e5c08d034a 100644 --- a/drivers/media/radio/radio-shark.c +++ b/drivers/media/radio/radio-shark.c @@ -21,6 +21,7 @@ * GNU General Public License for more details. */ +#include #include #include #include @@ -158,10 +159,7 @@ static void shark_led_work(struct work_struct *work) container_of(work, struct shark_device, led_work); int i, res, brightness, actual_len; - for (i = 0; i < 3; i++) { - if (!test_and_clear_bit(i, &shark->brightness_new)) - continue; - + for_each_test_and_clear_bit(i, &shark->brightness_new, 3) { brightness = atomic_read(&shark->brightness[i]); memset(shark->transfer_buffer, 0, TB_LEN); if (i != RED_LED) { diff --git a/drivers/media/radio/radio-shark2.c b/drivers/media/radio/radio-shark2.c index e3e6aa87fe08..d897a3e6fcb0 100644 --- a/drivers/media/radio/radio-shark2.c +++ b/drivers/media/radio/radio-shark2.c @@ -21,6 +21,7 @@ * GNU General Public License for more details. */ +#include #include #include #include @@ -145,10 +146,7 @@ static void shark_led_work(struct work_struct *work) container_of(work, struct shark_device, led_work); int i, res, brightness, actual_len; - for (i = 0; i < 2; i++) { - if (!test_and_clear_bit(i, &shark->brightness_new)) - continue; - + for_each_test_and_clear_bit(i, &shark->brightness_new, 2) { brightness = atomic_read(&shark->brightness[i]); memset(shark->transfer_buffer, 0, TB_LEN); shark->transfer_buffer[0] = 0x83 + i; From patchwork Thu Jun 20 17:56:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 806484 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16AD51CCCA5; Thu, 20 Jun 2024 17:58:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906306; cv=none; b=Jiv6ZMxUMQHZO56erfKSCitcqnobr5Xtphw36L3fhL/fzhHM2jp/6T3VmdYzyWSkZkJrda1oYqijHw1QLNoWHv7GpP2g1RS/4P/TqqTiRnuc9PcsHY0JuSZQbLcDlyMBxfBMiyZZczJ8ykOVFqLNVqx671cu9q2vcXhw01/X36c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718906306; c=relaxed/simple; bh=JZiYOORmljoK1sw2ane9GWAOBNpWFrHsg6e0OWJ8vSI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t3+OpDNxvhlse8w6M6ItVukiFyBm1GVud4FILQ45j8qPTQguMhW7ZYAuMMwaJaKHDwFbK+bMLgwIauC1+fmp1cXy+NGIk9upAM1q5GLFXrDNqtMl7Txd00BAc5bSUM3i25AaQy2pIyaPlduGOaHys7ulxw8+0o0nCVNlLSUS6zw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Q7tf5tcQ; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q7tf5tcQ" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1f99fe4dc5aso10187265ad.0; Thu, 20 Jun 2024 10:58:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718906303; x=1719511103; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ML3sp6993QhuntHDiwMgBg5++AU4tTHxgPTWBrSRxD4=; b=Q7tf5tcQdUK5OwNm3iP6c/cOYh+E6F12X6Gf7qGvh9OYF4/XrLJM84inLbVIg+OdSX 89SX6RwCZEzVjLevRLmx8LRMSzsVtmX+Ne4AdcsoYjIBl9ZJzhzoKVp1aUx40Ehlf1wm h6pV4691kzAsmp5s2YEeh4y9sn0LRcgBXhKepxtGPnN0lmza/AsRZKOdxSgPXnPUYr2K s1rLdrmHTv+3pEFsqR7P1pFo38BKXeK+p0S+dnFklkWjMbXcxIR13mMnLQS/X6DLvlOk xyKyzcT0TKzsrkOnUXmkOrUsTkgsIjFkkw4iRA6ULusGW/IQi8Qls4uKEj8NYwicujCU aweg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718906303; x=1719511103; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ML3sp6993QhuntHDiwMgBg5++AU4tTHxgPTWBrSRxD4=; b=URNvW+8R0iOXQCezgzQT8uZPKtDJjLRhYrYWQBiTCw87qL1CCqpLADDCWjw6BSE42G iH3JdupvyVJBV8/1EqSfn/Q/thiZm0KUrVFoufKOv7AYxfgQFXCb+ShVrlhyX0UpTKIh NlcgLB0eQAYjCtR4q9cQ0xYEhukXZR+tsiNX3HASI+GG3343IrWMjSl/dOjcrityRLh4 XfgwbKJoEPDInquozllB1F9OlSnwpv7SiS1G+l88eQqtszQR6jO1Ykswtj1MOtSGwCXB TL9pK4XpBeg5iJZO6k6Dq6eHq+ohjNWpTNwrMrp0yvn/BvQIfYz3gnGYbxmA8VBSvM/z Erlw== X-Forwarded-Encrypted: i=1; AJvYcCUoNGf7L2LzKEQPiTB51EcpeuW7pnXwWG59+d7NAN6+nbG08IWqOpIysWdY990KIsHpSMnK0z/IGU+7ZP0kV+4vPGNRcCmdBYJ7wh8= X-Gm-Message-State: AOJu0YwtItbCBdbJKbujhs82kTjZRHsjiygGgmSZj9xxelIZNcipTUPv gvts/Pwu2oByT8jyFcomRhEtfR42l7BC2brAiKkBF9mOXnHqXtrLQiZhmTtuMF0= X-Google-Smtp-Source: AGHT+IFBQBMGehB+oQoKQYHD/qYtBbqhxOtOkxopDaJNky+s/Sb/aQTrReWOcCKSQ26MccV2yDTi7g== X-Received: by 2002:a17:902:cf11:b0:1f3:1061:60fe with SMTP id d9443c01a7336-1f9aa3d24f7mr77142485ad.18.1718906303339; Thu, 20 Jun 2024 10:58:23 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9c81f48e3sm19719915ad.230.2024.06.20.10.58.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 10:58:22 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Mauro Carvalho Chehab , Yury Norov , linux-media@vger.kernel.org Cc: Alexey Klimov , Bart Van Assche , Jan Kara , Linus Torvalds , Matthew Wilcox , Mirsad Todorovac , Rasmus Villemoes , Sergey Shtylyov , Hans Verkuil Subject: [PATCH v4 26/40] media: em28xx: cx231xx: optimize drivers by using find_and_set_bit() Date: Thu, 20 Jun 2024 10:56:49 -0700 Message-ID: <20240620175703.605111-27-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240620175703.605111-1-yury.norov@gmail.com> References: <20240620175703.605111-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Functions in the media/usb drivers opencode find_and_set_bit(). Simplify them by using the function. Signed-off-by: Yury Norov Acked-by: Hans Verkuil --- drivers/media/usb/cx231xx/cx231xx-cards.c | 17 +++++----- drivers/media/usb/em28xx/em28xx-cards.c | 38 ++++++++++------------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/drivers/media/usb/cx231xx/cx231xx-cards.c b/drivers/media/usb/cx231xx/cx231xx-cards.c index 92efe6c1f47b..8bdfbc4454f1 100644 --- a/drivers/media/usb/cx231xx/cx231xx-cards.c +++ b/drivers/media/usb/cx231xx/cx231xx-cards.c @@ -9,6 +9,7 @@ */ #include "cx231xx.h" +#include #include #include #include @@ -1708,16 +1709,12 @@ static int cx231xx_usb_probe(struct usb_interface *interface, return -ENODEV; /* Check to see next free device and mark as used */ - do { - nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS); - if (nr >= CX231XX_MAXBOARDS) { - /* No free device slots */ - dev_err(d, - "Supports only %i devices.\n", - CX231XX_MAXBOARDS); - return -ENOMEM; - } - } while (test_and_set_bit(nr, &cx231xx_devused)); + nr = find_and_set_bit(&cx231xx_devused, CX231XX_MAXBOARDS); + if (nr >= CX231XX_MAXBOARDS) { + /* No free device slots */ + dev_err(d, "Supports only %i devices.\n", CX231XX_MAXBOARDS); + return -ENOMEM; + } udev = usb_get_dev(interface_to_usbdev(interface)); diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c index bae76023cf71..59e6d7f894ad 100644 --- a/drivers/media/usb/em28xx/em28xx-cards.c +++ b/drivers/media/usb/em28xx/em28xx-cards.c @@ -11,6 +11,7 @@ #include "em28xx.h" +#include #include #include #include @@ -3684,17 +3685,14 @@ static int em28xx_duplicate_dev(struct em28xx *dev) return -ENOMEM; } /* Check to see next free device and mark as used */ - do { - nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS); - if (nr >= EM28XX_MAXBOARDS) { - /* No free device slots */ - dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n", - EM28XX_MAXBOARDS); - kfree(sec_dev); - dev->dev_next = NULL; - return -ENOMEM; - } - } while (test_and_set_bit(nr, em28xx_devused)); + nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS); + if (nr >= EM28XX_MAXBOARDS) { + /* No free device slots */ + dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n", EM28XX_MAXBOARDS); + kfree(sec_dev); + dev->dev_next = NULL; + return -ENOMEM; + } sec_dev->devno = nr; snprintf(sec_dev->name, 28, "em28xx #%d", nr); sec_dev->dev_next = NULL; @@ -3827,17 +3825,13 @@ static int em28xx_usb_probe(struct usb_interface *intf, udev = usb_get_dev(interface_to_usbdev(intf)); /* Check to see next free device and mark as used */ - do { - nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS); - if (nr >= EM28XX_MAXBOARDS) { - /* No free device slots */ - dev_err(&intf->dev, - "Driver supports up to %i em28xx boards.\n", - EM28XX_MAXBOARDS); - retval = -ENOMEM; - goto err_no_slot; - } - } while (test_and_set_bit(nr, em28xx_devused)); + nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS); + if (nr >= EM28XX_MAXBOARDS) { + /* No free device slots */ + dev_err(&intf->dev, "Driver supports up to %i em28xx boards.\n", EM28XX_MAXBOARDS); + retval = -ENOMEM; + goto err_no_slot; + } /* Don't register audio interfaces */ if (intf->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {