Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp130262rdb; Thu, 1 Feb 2024 04:24:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXb6zzEDWgY0cODAf3cQCXh0EBu3eg/gRrTS6oHw8RVuQ58XG7UQZoLTQ3mrGHS1oNNE6P X-Received: by 2002:a9d:618e:0:b0:6e1:29f3:6fbd with SMTP id g14-20020a9d618e000000b006e129f36fbdmr4415113otk.22.1706790259938; Thu, 01 Feb 2024 04:24:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706790259; cv=pass; d=google.com; s=arc-20160816; b=JHLj3ycjgvywuRfjKeAViGvv5j3crzNVtnxO859EAcpvtnnHxizPPR4vL7aPo8Mi7m JISxUQPR3Ebs3gjQ+OzZ3nwr/paXNpNdU9vvPkraiqY41y0V1LKec2OV1wEeBwKLdhAL ir97oraOj+R4YwlRc3JI3KUud0HLuRHAZnaGQi56q1sc/Kj1kRQ2zCV4O2XcIpJOqd0K iczmpjKt2gZyo/cihNQ56KKfW9Y694Wm6kS4gLMmS7zJzatJHMF3+IQ/2lxByVWh7FwP DAzrgwLPZ5Nuu12kxtSPjMzBHvvUwVnwm7lC8m9RxshSuMeP/AFyS7JquBc7yLtEbJQs fDWA== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Q0BNlSYt7ZCB7hoYItJjSIrlxWvVSvfT2DOXsN86B20=; fh=nHVb0uhW+Zw46QCPDRj/roBTEn8ShoyQ/02GkBYBa9I=; b=vwHFP2FuJfDO7MP8As9GBlHHJp9RbyH+RgilaYJ91Oq2Ks/Rds0IlVttnnIjk5i73p hFbVl6REMVZ7DHTNYQTSzywidIPdZai7RD2SZ+C6pdPwetFuxe4vo9UAYo0k1wZSS6me U3bYWTS1MU8TZjF6n/2izkgQdD/jUNnuRK1USHZjUmmcj9lfgXJdf5mHUBArbpxLkA6f 8r0pVacr8uHvHB056DNgAfMpAoe0RqY6EPmKEztq188Y/T5q+CBwgPqHzZXNdGVEx8Mn KMl13pZ7x/cBIzVEbDllJ6T41+YYRzwb4vM+NMGKU/R+uZdYTSabeXWDp7YKNSY/sNoE mo9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d0DU34it; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-48112-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48112-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCX2EMM9t3DrpTcBoBm9BXUELfo32Kmw5e94u7+AyhJfHcXXwZundfEoIIRru2GcQf3UinYr0bXMZ95SaE4OkWqcbfUtXBEytgRb9sHRzA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d18-20020a05622a101200b0042bfc948bf7si260214qte.317.2024.02.01.04.24.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 04:24:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48112-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d0DU34it; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-48112-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48112-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 66ADC1C25AC0 for ; Thu, 1 Feb 2024 12:24:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9B3D816086A; Thu, 1 Feb 2024 12:23:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="d0DU34it" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (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 E11F715F31C; Thu, 1 Feb 2024 12:23:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706790218; cv=none; b=puMw3AP7YfK95QX47ApqVnixhPJqrE0hL2W8x1D6rEa4xZq1RUd6bhncMoiD5DrtjY1vXI//7/36mNCQc8Y8KPWKFZI2fa2ji25LzSFfVSdzXv+o7aSxVflfDG3rVi7IPc3nJmIeRW47uyrkh+3dsfh5NjHeUrSiv14QlAjDSoQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706790218; c=relaxed/simple; bh=FkdWCDGhSzmNJ65dmfIHiTVxvqOrslZqMpuEArCTxEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uEFYDcvrbBXGvri5/yysb+7LdlgMMQKzlofehsjH09xzYkkOez0s7tLP4z8qJgZvsDzEQ2YCODcixj04enVXmrz1SrsTJnU9BVlXeS+T8GbJqymXT4hEVzWydt3I8K2gFk6oHBrIoxA1dflEysMOW/rfZ+sh2yauLPD9qdDalbc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=d0DU34it; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706790218; x=1738326218; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FkdWCDGhSzmNJ65dmfIHiTVxvqOrslZqMpuEArCTxEw=; b=d0DU34itVs0PV8ayeqXPcJ9RDc4UJpkZD8sqBojExjn6shKlp7etVYkX e9QdCHuGjZZmjwfq47NBfJGldbp0kZN6UFD8ruZIb+4rBTk37tuBqOY6q Ek0dTWxHaV/7T13qtVHN/IdRscp8GZ9EJ486UdG9e5I8G+8bc1rPH5xz5 IZlGPRS1Nxl5GzXsMldbRLPNubjM3oNvZxWyAU2VbyAljVTtjPpRTCDi2 +2DWZnLY/Z8Lrjigm6ry4pcb0LU921SAFwsLr1KIDSsQlWVJpikMnkwmA kQWoIenZuC/c4hxdLxRCoVugjTV4cJ4dnrwFSr4j8yboffbMSk3onOOSd w==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="3746870" X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="3746870" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2024 04:23:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="4499077" Received: from newjersey.igk.intel.com ([10.102.20.203]) by orviesa004.jf.intel.com with ESMTP; 01 Feb 2024 04:23:31 -0800 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Alexander Lobakin , Michal Swiatkowski , Marcin Szycik , Wojciech Drewek , Yury Norov , Andy Shevchenko , Rasmus Villemoes , Alexander Potapenko , Jiri Pirko , Ido Schimmel , Przemek Kitszel , Simon Horman , linux-btrfs@vger.kernel.org, dm-devel@redhat.com, ntfs3@lists.linux.dev, linux-s390@vger.kernel.org, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Syed Nayyar Waris , Arnd Bergmann , William Breathitt Gray , Andy Shevchenko Subject: [PATCH net-next v5 01/21] lib/bitmap: add bitmap_{read,write}() Date: Thu, 1 Feb 2024 13:21:56 +0100 Message-ID: <20240201122216.2634007-2-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240201122216.2634007-1-aleksander.lobakin@intel.com> References: <20240201122216.2634007-1-aleksander.lobakin@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Syed Nayyar Waris The two new functions allow reading/writing values of length up to BITS_PER_LONG bits at arbitrary position in the bitmap. The code was taken from "bitops: Introduce the for_each_set_clump macro" by Syed Nayyar Waris with a number of changes and simplifications: - instead of using roundup(), which adds an unnecessary dependency on , we calculate space as BITS_PER_LONG-offset; - indentation is reduced by not using else-clauses (suggested by checkpatch for bitmap_get_value()); - bitmap_get_value()/bitmap_set_value() are renamed to bitmap_read() and bitmap_write(); - some redundant computations are omitted. Cc: Arnd Bergmann Signed-off-by: Syed Nayyar Waris Signed-off-by: William Breathitt Gray Link: https://lore.kernel.org/lkml/fe12eedf3666f4af5138de0e70b67a07c7f40338.1592224129.git.syednwaris@gmail.com/ Suggested-by: Yury Norov Co-developed-by: Alexander Potapenko Signed-off-by: Alexander Potapenko Reviewed-by: Andy Shevchenko Acked-by: Yury Norov Signed-off-by: Alexander Lobakin --- include/linux/bitmap.h | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 99451431e4d6..7ca0379be8c1 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -79,6 +79,10 @@ struct device; * bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst * bitmap_get_value8(map, start) Get 8bit value from map at start * bitmap_set_value8(map, value, start) Set 8bit value to map at start + * bitmap_read(map, start, nbits) Read an nbits-sized value from + * map at start + * bitmap_write(map, value, start, nbits) Write an nbits-sized value to + * map at start * * Note, bitmap_zero() and bitmap_fill() operate over the region of * unsigned longs, that is, bits behind bitmap till the unsigned long @@ -636,6 +640,79 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value, map[index] |= value << offset; } +/** + * bitmap_read - read a value of n-bits from the memory region + * @map: address to the bitmap memory region + * @start: bit offset of the n-bit value + * @nbits: size of value in bits, nonzero, up to BITS_PER_LONG + * + * Returns: value of @nbits bits located at the @start bit offset within the + * @map memory region. For @nbits = 0 and @nbits > BITS_PER_LONG the return + * value is undefined. + */ +static inline unsigned long bitmap_read(const unsigned long *map, + unsigned long start, + unsigned long nbits) +{ + size_t index = BIT_WORD(start); + unsigned long offset = start % BITS_PER_LONG; + unsigned long space = BITS_PER_LONG - offset; + unsigned long value_low, value_high; + + if (unlikely(!nbits || nbits > BITS_PER_LONG)) + return 0; + + if (space >= nbits) + return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits); + + value_low = map[index] & BITMAP_FIRST_WORD_MASK(start); + value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits); + return (value_low >> offset) | (value_high << space); +} + +/** + * bitmap_write - write n-bit value within a memory region + * @map: address to the bitmap memory region + * @value: value to write, clamped to nbits + * @start: bit offset of the n-bit value + * @nbits: size of value in bits, nonzero, up to BITS_PER_LONG. + * + * bitmap_write() behaves as-if implemented as @nbits calls of __assign_bit(), + * i.e. bits beyond @nbits are ignored: + * + * for (bit = 0; bit < nbits; bit++) + * __assign_bit(start + bit, bitmap, val & BIT(bit)); + * + * For @nbits == 0 and @nbits > BITS_PER_LONG no writes are performed. + */ +static inline void bitmap_write(unsigned long *map, unsigned long value, + unsigned long start, unsigned long nbits) +{ + size_t index; + unsigned long offset; + unsigned long space; + unsigned long mask; + bool fit; + + if (unlikely(!nbits || nbits > BITS_PER_LONG)) + return; + + mask = BITMAP_LAST_WORD_MASK(nbits); + value &= mask; + offset = start % BITS_PER_LONG; + space = BITS_PER_LONG - offset; + fit = space >= nbits; + index = BIT_WORD(start); + + map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start)); + map[index] |= value << offset; + if (fit) + return; + + map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits); + map[index + 1] |= (value >> space); +} + #endif /* __ASSEMBLY__ */ #endif /* __LINUX_BITMAP_H */ -- 2.43.0