Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp123376lqo; Wed, 27 Mar 2024 08:31:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUzXjumUXATQ0sAH7P670cO+fpYcxmlB0aBdx1gePQfF3TX8T7OqyYVNdAtP6qEQIOEifoCqcnvGVycuXX5kpx84wLuAFdYNhvWZ8GpgA== X-Google-Smtp-Source: AGHT+IECqvlYjJgjUXyO0XC+nfi03rElDXQgnLyLYnJrOlkLA84jp72I3MVr5W8qy0NRyNsrvuMV X-Received: by 2002:ac8:5988:0:b0:430:a6eb:5b12 with SMTP id e8-20020ac85988000000b00430a6eb5b12mr455828qte.11.1711553478280; Wed, 27 Mar 2024 08:31:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711553478; cv=pass; d=google.com; s=arc-20160816; b=oalsRs73jjvoj17XKqooALM2CtZJA2pcACJb95pUql4dVr9hiHFFv0P/LlpoZRCcK5 mje0H+1Ns2v5M2gqXKqe/8WazEybO25bsy1Ie/jtSh8J/abn1eJOsxBbbGhV9CavXOKa 20H+dRY6cdZKe/zCaSoTC+fN6Z0vrFeMaQtPWRXXYVhsZht24Fh6faM/IeAv8nanbi1V s43KGvbxkjLyC1dVxx5nBrA2M9IuRoVTsx+VSu0wohXMunvUofEgfyIHgUjyP7DZdcYQ HLsTv18dVxTcYWIq+LjHFW1UVoljrDil6VGFnvVP+JRkIBc2mkNzi1zsDdZwb/ky7OSK 4a0Q== 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=tgFg6sTtz6QRJVA8Re8iHDgua9BHlrBDvW701XIhinw=; fh=EQGTCMinBdKHoIpeHsh9sDySYYowQDRWP6HoHm52/BI=; b=wOHwn9wVoOgBIfI2OH78IqSCpnJ+5bb/DK6Y2XspCgAbaUs09X0QpmVWandKwSbghy xMi577wOZE+KvA8edZ3DuFN0RxzkeJC21C+DuwA6sWaIwgxfgVqpaXxKBwxCT6sil7jq xV45kkMK9GuSUKJ8hh9g9A+kRNpplvyzZdIfuz8gwzkbWrc72B9Tn+bwORF7lGo4Exww 2JqUKJzdwUxNYXrSA9R94im4dNXbydCin5F4oEzdmeHfTLl4H+2zgfWLdcPjTgd4EPAq cHh9ZIyVYtjr91/NT9+tIKrWLTahzcY0aHmnyIYvjdsCt07+XpZSFXOf3EGUXVe2nMqG 5epw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QHHBPxWl; 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-121511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121511-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id w2-20020ac857c2000000b004313e1c659dsi10006421qta.225.2024.03.27.08.31.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 08:31:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QHHBPxWl; 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-121511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121511-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 ADC981C307E5 for ; Wed, 27 Mar 2024 15:31:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A7D1F137C51; Wed, 27 Mar 2024 15:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QHHBPxWl" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (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 C0CE7135401; Wed, 27 Mar 2024 15:24:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553092; cv=none; b=jYpotzcIbeQoHHNqN66w4U1malaCAAMaFS4fVIBCd50KmRo4/SEBYH+z2q+w2OgM6KlaaEPAJxSj3+5eLf/v5/KefPfrMVPh037zB5N6//2ZuNb4LOZZDcjZSQSwoylKYeTuU4L4mpzcVNRmC+McoPFs0suKjEEWlecfkvip9S8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553092; c=relaxed/simple; bh=8qODhYyvaUk14kxfdEdYZD1YkZRxZEhZyVTOUCP3lbk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oLhOoeMS5Q0xvgnmhgLCCb1AAE2LnjQxqbwWmapHn3Sap6EEBMGRzcSGKKOXRY4YeJ98y3lSyi2fNd6VfKyYRe0nqEOd517QUUT5qVz27g9nfIjoQdGSc6GLMsCtVA9gA8Cl4jXAygsf7KIEpo29qDykda1fF5lAAFW6SIRXieQ= 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=QHHBPxWl; arc=none smtp.client-ip=198.175.65.19 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=1711553090; x=1743089090; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8qODhYyvaUk14kxfdEdYZD1YkZRxZEhZyVTOUCP3lbk=; b=QHHBPxWlXsMRRaUs51SsxCCWE/aGkWnIcvOh3Kkulls4aYPq6KRxygZD NomBbXTTd0H8pA3YdE7yBkfD/WcKmZhBaajQ8l5USG6mJOLiqE/crsJuc 3fFxuyw99xk5zLZu+q9JhXdP97rpMwVPROqBuWwywZL8cQb/C+ELLSwPm lZutvAG8REYJAAPEeeIcbpGZflf8ZOwn6YbyF5DOAXd2SUR12WKZ+QeBM yV8I00i6918cv1bGhopmGkx/bdY/62M3K3ohgAvEbip7ODKRNh7bhgpZH XL0oYWuaml4k/P3cwF3Wjln36ueY5P51Qnj8LfT+B9mL5U1yqkPEsqcaz w==; X-CSE-ConnectionGUID: /rpbcGdhSQylLKx4lf8CvQ== X-CSE-MsgGUID: GciS/9vSS/ya0zS+7YlNmQ== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="6518081" X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="6518081" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 08:24:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="16414078" Received: from newjersey.igk.intel.com ([10.102.20.203]) by fmviesa008.fm.intel.com with ESMTP; 27 Mar 2024 08:24:36 -0700 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Alexander Lobakin , Yury Norov , Alexander Potapenko , nex.sw.ncis.osdt.itp.upstreaming@intel.com, 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 v6 01/21] lib/bitmap: add bitmap_{read,write}() Date: Wed, 27 Mar 2024 16:23:38 +0100 Message-ID: <20240327152358.2368467-2-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327152358.2368467-1-aleksander.lobakin@intel.com> References: <20240327152358.2368467-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: 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 aa4096126553..914c23e96f26 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -83,6 +83,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 @@ -754,6 +758,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.44.0