From patchwork Fri Sep 22 08:08:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 143368 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5485349vqi; Fri, 22 Sep 2023 04:16:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHz3gPEgnH4uzCuL5PPXMEN0XB1pjcbflJxEvQXAB1eJS1Z3KayFJgTT5NIRs6tgXzL1bU6 X-Received: by 2002:a05:6358:4429:b0:134:e301:2c21 with SMTP id z41-20020a056358442900b00134e3012c21mr10700225rwc.15.1695381369066; Fri, 22 Sep 2023 04:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695381369; cv=none; d=google.com; s=arc-20160816; b=jmaigCMWD2u5pMVfTqTafXUxV3NuC1H95weOXyNAoW9T7OopvEPgXJb9lQqY8mrSRT cmYaEa/5GADLWkQaKWNn+Ql3WQkmkT+vMEKCqVVUVNUjW9ea0b/O2cHBSWZDyqzvlxTw nJDv+iplNfCmso2qLJoGPAsHS39e5l0yuxzjIa4kSrjQkA1GIZzJ5tDakG+D+zCN8zGp 6JJyYVtVUrAg3OOiAMTdP5UfDOlTKSLcZ5VAXHtz2XyOcHz6vZSywUSVNI4EIndBpR00 LzGcRPs6gKVhS/0Pt2z4isB+UanSiv94QU1aUu9pXM2ThhWJH+jSjdS1/TPXS63KF6pZ zvfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Elogy5+qJLTNkCkBSFtWFfKDNFj74CbHYILaVC5jihg=; fh=wujYxelwYCC1wZBDzBd5XHkhdGoivaRM7zFRRDyxWqw=; b=pWSrYR/KegzP5HKBejE8zYhEgA2/bj6sNuczORAMcTvCh9fhOpoA9RPukAR84dadNy TntNtNIQ6B432uxOJRfj1cz4ML4j7A2z6hT0RU6jHYx2US7MGx0rNYFFS4eCF37EIgtM NfGa9vwrn+HupchWJ1xmT1F/dSXS9q/D+d59sVYPFPYpYTeYFOE8gxSkdzk79Wgvc4O1 DhlyZPupsdEYtNgwWwVKUMcFJqxDX8oLGlAXnhJUOJ0hMjrFOf+fbKzLfd5VL2vqNv6C ppsVLLaJ4O7O8vy4L3Ab4f6Le4dorA6hC0eNCRmlljy52sRd+RghJkQGXaHVuoAQiN8+ /Kbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0GcVYMl7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u5-20020a656705000000b005680cb68678si3357758pgf.446.2023.09.22.04.16.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 04:16:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0GcVYMl7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 0DA1C837D487; Fri, 22 Sep 2023 01:09:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232271AbjIVIJE (ORCPT + 30 others); Fri, 22 Sep 2023 04:09:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231888AbjIVIJB (ORCPT ); Fri, 22 Sep 2023 04:09:01 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 769D2CC3 for ; Fri, 22 Sep 2023 01:08:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d81ff714678so2554890276.2 for ; Fri, 22 Sep 2023 01:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695370134; x=1695974934; darn=vger.kernel.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=Elogy5+qJLTNkCkBSFtWFfKDNFj74CbHYILaVC5jihg=; b=0GcVYMl7twYtfjutYXgqAJSbiTT/vcDXiio2QOjMt5s0rtQ3gQng4zjEwjSAS1lfPA 0oxdCdF9bYNkdInjHy92YSIcRd0PqUyqqm+CmqjmLrUTB1OT3tQLk13a9Ipgee5iAu1t rIIDC5M3NN9RDkvcf6/chRf3ulgjiO8d/Wfqup3IGcJv1XkJ+YMeKck7yVsSdEqgsbYb HAs4iUsIqWA0wnpsepIiwayR86huK4R13RS0RUo7v80AaDvb4+mWyZvkukzzBytq5PON UlqojhBdbxpXr1aZHasBHeIZZVncw2C1RcUIf3X2skS6WDiyRYS7p1A7M3hpdHNba0IB M7xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695370134; x=1695974934; 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=Elogy5+qJLTNkCkBSFtWFfKDNFj74CbHYILaVC5jihg=; b=IweEgGYVqYVJqGgcGK8O9kr+f+vD+3zG/5YKOuBgnfDU/DSB/zpzMYhLf2ylbFLcVs LKRYAEkKQxpUeMdmW6dFRyqVOlJQSBcqhZHeTZjG8LxvCsdpJliLd5+p4P/RVc7vJdn0 umWWEcwXgF0K1nwSCNFj9KW4ja0zmK5Q/Odp/l1W9VeoL/rWT+DIRFzKVd9l8XhBvA5t TWWZarl5dcu5DpwXsx8hftOA0QiloVrd2lmifi+qBjQYTHDDjfkEr3PHXhhX2mMiqals 2p07oALk1WUz6G3jeg3BOQQLsl2th3CrQns0p3PtvdoTdiP4Mk/ZAiKLKOmdyG6cvhEn 2/Rg== X-Gm-Message-State: AOJu0YxBL/K1XIdbT8zaqGt3+Dfr+vqzPW0zt4IsW/1QwVAhoWkaD7CR 65YV8nRS7ESogGW5G5U4gUJp5LkLpKM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:4da0:80d4:9480:7d5c]) (user=glider job=sendgmr) by 2002:a25:f43:0:b0:d81:754a:7cbb with SMTP id 64-20020a250f43000000b00d81754a7cbbmr101892ybp.11.1695370134613; Fri, 22 Sep 2023 01:08:54 -0700 (PDT) Date: Fri, 22 Sep 2023 10:08:43 +0200 In-Reply-To: <20230922080848.1261487-1-glider@google.com> Mime-Version: 1.0 References: <20230922080848.1261487-1-glider@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230922080848.1261487-2-glider@google.com> Subject: [PATCH v5 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, 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-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 22 Sep 2023 01:09:38 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777736214461254851 X-GMAIL-MSGID: 1777736214461254851 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/) 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..25998a3a4b2b9 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, 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, 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 */