From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 47ACCE81DFD for ; Fri, 6 Oct 2023 13:46:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Bqtt9bmKQCarOBcqA+SsICkQEPkctdztessbatOlkRg=; b=hnjlgi0o0vYMFWr1vq7QTLHqMS CoMYa2lDZ3dkyxdH8RZ5K/Gf0OK9SUhXoGZCB5VVzYXALYWHQvIFm+IHF94/2oj3Ss+uR56b0Gc8a KbnzAxHuvCIiMloCNesp2rwwTedwTDEqN9Y4O0n+HPMlKCVaUnF2xlWJm3nFbMX+0GSJFOJTWGfpC /1pzFqHVtcug1KhZhXGUKVOvqWhw41vMDqfZbSW/PREgp4+Ycrb82gPeVYXAtY5gxz/z5z//s78MI 7KvcTta9tOHxDrpzBUpXtafIwvhqP/cGpS+/uePY6XQwT+DOp1uQXR//y8z09MKPjd0jlCIVoCRNM khBc26qg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qol9E-005uV7-0t; Fri, 06 Oct 2023 13:45:44 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qol99-005uPI-2i for linux-arm-kernel@lists.infradead.org; Fri, 06 Oct 2023 13:45:43 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a22029070bso29394097b3.3 for ; Fri, 06 Oct 2023 06:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696599936; x=1697204736; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FVc3w80Slds1rDJeGDtC5d5l1mYuX/jp9UzDI84XpOg=; b=o1FPind1drs4KdJfP262utjDbYOK7VXiCA3onUxCbaNKGhA6wgswFy1T9kQQ+1V4Rb dU0EhxyX8WeWKxWF01sungKNiBLL98o/kIM/QngWln2vvrmA/AhO3+IfeUhDToYishvD EWjHID+CoyJgeDTMe+Rn7VVD3LpRx+qHSJAUC3ppFAjkNqBURGjYfuVcsD9xCmv4BkyL el83ZIvUBmKNdI3Gv2aFDRkjgilGKdyrPokAJvIvZzniTI/DaCpzsy51+GAb57s1Wtes CvzwuzjrmCGPz5TUmCLYqWSko2opf5dKSyP9ES4Tn4iNvoj+J6LrMTsvYyO9DccrnHPz VfKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696599936; x=1697204736; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FVc3w80Slds1rDJeGDtC5d5l1mYuX/jp9UzDI84XpOg=; b=l+Z3vAEc0X5wPOxzWNC9Lf2Sc3kJaSdb7SW34B9Y8Q1sZQHUxWfKqpVxxqHjs0bQ2/ WeHZ95eG2SqWl3rdsrAs1qI3sEOZIWvUOBR8AQuxeRym7zwGGqlwoVEcO0n3AUkxxXGj 3W1v/Z9bJHTsufTuH9yd09geeLgzeKGm6SBdG+ZcekqMCqjDi2JTyaqLbHQFZOa8aPmZ LL28LAJ8EnuwrB506TlIi9VfCpYZQsTlXCivVAsLh2E96T2DznH4mT0WsN+hvCWrjByH KPCd6MCymC5tDUtr6DMS7vNmF4WRQPjllQlYXnFxpaexnQgiZUv0ObAWEQy9Jo68kwBn iD2g== X-Gm-Message-State: AOJu0YwaqMipiqcCYEU27oGw5h4nfT3bs9shUnT1Xp3OA7Po14cIm9Ni ihUfw1qzfFy5KPOapQQWY1YDx3Vi7/w= X-Google-Smtp-Source: AGHT+IGBAwP4WDOXMvPWUp8IcF7MTJBHGAe+KbBVrfCsWOIPDnl6zmllpTVlILvsZDLh/Z18+aAlyE1J9ho= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:2691:23e9:f01f:964]) (user=glider job=sendgmr) by 2002:a05:6902:13cc:b0:d91:8876:2040 with SMTP id y12-20020a05690213cc00b00d9188762040mr133955ybu.5.1696599936015; Fri, 06 Oct 2023 06:45:36 -0700 (PDT) Date: Fri, 6 Oct 2023 15:45:25 +0200 In-Reply-To: <20231006134529.2816540-1-glider@google.com> Mime-Version: 1.0 References: <20231006134529.2816540-1-glider@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Message-ID: <20231006134529.2816540-2-glider@google.com> Subject: [PATCH v6 1/5] lib/bitmap: add bitmap_{read,write}() From: Alexander Potapenko To: glider@google.com, catalin.marinas@arm.com, will@kernel.org, pcc@google.com, andreyknvl@gmail.com, andriy.shevchenko@linux.intel.com, aleksander.lobakin@intel.com, linux@rasmusvillemoes.dk, yury.norov@gmail.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, eugenis@google.com, syednwaris@gmail.com, william.gray@linaro.org, Arnd Bergmann X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231006_064539_886855_02E5106F X-CRM114-Status: GOOD ( 18.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- This patch was previously called "lib/bitmap: add bitmap_{set,get}_value()" (https://lore.kernel.org/lkml/20230720173956.3674987-2-glider@google.com/) v6: - As suggested by Yury Norov, do not require bitmap_read(..., 0) to return 0. v5: - Address comments by Yury Norov: - updated code comments and patch title/description - replace GENMASK(nbits - 1, 0) with BITMAP_LAST_WORD_MASK(nbits) - more compact bitmap_write() implementation v4: - Address comments by Andy Shevchenko and Yury Norov: - prevent passing values >= 64 to GENMASK() - fix commit authorship - change comments - check for unlikely(nbits==0) - drop unnecessary const declarations - fix kernel-doc comments - rename bitmap_{get,set}_value() to bitmap_{read,write}() --- include/linux/bitmap.h | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 03644237e1efb..e72c054d21d48 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -76,7 +76,11 @@ struct device; * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst * 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_read(map, start, nbits) Read an nbits-sized value from + * map at start * bitmap_set_value8(map, value, start) Set 8bit value to 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 @@ -583,6 +587,33 @@ static inline unsigned long bitmap_get_value8(const unsigned long *map, return (map[index] >> offset) & 0xFF; } +/** + * 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 located at the @start bit offset within the @map + * memory region. + */ +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)) + return 0; + if (space >= nbits) + return (map[index] >> offset) & GENMASK(nbits - 1, 0); + 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_set_value8 - set an 8-bit value within a memory region * @map: address to the bitmap memory region @@ -599,6 +630,43 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value, map[index] |= value << offset; } +/** + * 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 similarly to @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)); + */ +static inline void bitmap_write(unsigned long *map, + unsigned long value, + 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 mask; + + if (unlikely(!nbits)) + return; + mask = BITMAP_LAST_WORD_MASK(nbits); + value &= mask; + if (space >= nbits) { + map[index] &= ~(mask << offset); + map[index] |= value << offset; + return; + } + map[index] &= ~BITMAP_FIRST_WORD_MASK(start); + map[index] |= value << offset; + map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits); + map[index + 1] |= (value >> space); +} + #endif /* __ASSEMBLY__ */ #endif /* __LINUX_BITMAP_H */ -- 2.42.0.609.gbb76f46606-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel