Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5903868rdb; Thu, 14 Dec 2023 03:07:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEtZtCKIE3gyudS/WOj8LdqCBC5/ZolsSkImgli1J5X/Tgt5RrWI0QxnlaAo/NwNy6UwgFG X-Received: by 2002:a17:902:c943:b0:1d0:5357:f1ad with SMTP id i3-20020a170902c94300b001d05357f1admr14005733pla.18.1702552028549; Thu, 14 Dec 2023 03:07:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702552028; cv=none; d=google.com; s=arc-20160816; b=phohjDPhIsoCQdshyvitod5OBNLmlbSewwAaa6+uOEzpI3/jYfZak+BeJprH9SB7jd 2jqD3ACCD1ONW95r485NwCIUZHFSK/mDG9NjEHW9/MGJfqTWjuoAF7BQajkGQgOMj5bh jEhryt8YHflct2kK974dr+s+8RGTZpFgdkIVGyJQAtj7rd81eygmBho8WPAAIIkitPfY DvSAiwqRLr0rNaHtfrpkjTdUr6YH5XUZT1IbOwW5g07hI3zaGp/VyfLxjiAkdJ3HCs5R winSuDQoKRo3sT6W8IVVLDm0jWXEobpwohGqUy5qLp9ADgVrCadA41GPXtNZ5Rvficcm kwbQ== 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=EyXxVtW+rdQsX3V7pnuyU4+nkx4xXk2D+H4JW0Q7bfM=; fh=GmoqtqK5o+3hw8duGAYBcfcrbmWz8zn7GJB0HJXGJL0=; b=P26itV08cSNKJR8EKx6ZxU4Ev3B6ed7ncZlV44HuYxwMwf/HDDgyo5fRlbpHIaEuF4 OA7wa+eD9LdOtHxhuOAjGgocAY/4G0Q+EUkLCiegBtkD3u75N3HiMwqsbHbi257GxZYX Ac5A8JYgbw/0U/R8rmLxrEMwBRBDOn/GFeOBXYBdsKhQY6x4BEB2n0aGhydHF/ekWizA JLrrnMnP+L5DxiT15DE2hYSUpGtbPTtv7RYvaf9k4odwOLaZfER06l3ULbE9LjT1dC3m kG6KAncQKecCcAe3LTWV+9JFmYNXidyakH0c2erlMD0CpgQRcLqwnd6ysbYWragJeYul SWBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ORiigBak; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x20-20020a631714000000b0057e21f51ab3si10989099pgl.665.2023.12.14.03.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 03:07:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ORiigBak; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 40FF4802A83D; Thu, 14 Dec 2023 03:07:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443910AbjLNLGr (ORCPT + 99 others); Thu, 14 Dec 2023 06:06:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443916AbjLNLGo (ORCPT ); Thu, 14 Dec 2023 06:06:44 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05A6D121 for ; Thu, 14 Dec 2023 03:06:50 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e33a2f0cacso3382427b3.0 for ; Thu, 14 Dec 2023 03:06:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702552009; x=1703156809; 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=EyXxVtW+rdQsX3V7pnuyU4+nkx4xXk2D+H4JW0Q7bfM=; b=ORiigBak76Q7vNINF+/57DM7oV0LGo6TmBnYP5jwjHVS0mehV7EjB67KE2bm6bgrUP CKbU0+1oA/r5r69s/hStCi0P2LRmG4cZvgs+rEeN8MyU+S2dRwq4hlUHyiXjqkVQ5k0d 8Rdwi1zZIdemUXjdCVdXX+LMv80jJFSfecbAdy8+4kHZFur1pmicnfl0sqTyvJ9gOLK4 TEfAQkySnMyR9B3pW8Tuu2PEO4ezcBJCABFgG9j2R8LfW5qeOKoL/KxPlUo178g3yjt8 Cze+sNTcHac1gH7eUIbnu+ys75rQJdqMMZJHnxT6u+yhet5S+tphU8fCgh2cEdxYaEbv nwcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702552009; x=1703156809; 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=EyXxVtW+rdQsX3V7pnuyU4+nkx4xXk2D+H4JW0Q7bfM=; b=HejZxyWQldq0hay/jNCYEJq8PF/0Cn2kkTYabLd56HG3VOvqxaOEITb0BeMF9ZICzn xEDivyD+gbv2n3WLNUtE/2Iyx5rtEy3EEXnVOAKNG21f3roWsWdHMGpu5l6KNuGFzOLB ofXtWTjiS7IAo3YbvYtZ1nYoxbbOSyQoeXScK7ZWL9sYptE+0MQEqNlh3CZI1LVmjc1Q kp0OE/HdQozl8C8KgOq6Z7MqkJpTMrf6skqpE4MEGRdcNalIOK7i6Jz/7TOpjU6O/taz K3eeqLUvxSHjsFp+ej0bZDVjs//Nj7wQqbsOS07wOpI/wFvyYhcg0l8eEQJnTAzTt43/ StpQ== X-Gm-Message-State: AOJu0YzDD7mxKUd1cAymZ4UAvQc3S4qKYSS4UidO4hzZ0WtQW6hFd6md sIxTUWxwuY5aLcrJop5qjFjTPaez+Ic= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:8447:3e89:1f77:ff8a]) (user=glider job=sendgmr) by 2002:a05:690c:a8c:b0:5d3:e8b8:e1fd with SMTP id ci12-20020a05690c0a8c00b005d3e8b8e1fdmr182658ywb.3.1702552009224; Thu, 14 Dec 2023 03:06:49 -0800 (PST) Date: Thu, 14 Dec 2023 12:06:33 +0100 In-Reply-To: <20231214110639.2294687-1-glider@google.com> Mime-Version: 1.0 References: <20231214110639.2294687-1-glider@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231214110639.2294687-2-glider@google.com> Subject: [PATCH v10-mte 1/7] 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, alexandru.elisei@arm.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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 14 Dec 2023 03:07:06 -0800 (PST) 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 --- v10-mte: - send this patch together with the "Implement MTE tag compression for swapped pages" Revisions v8-v12 of bitmap patches were reviewed separately from the "Implement MTE tag compression for swapped pages" series (https://lore.kernel.org/lkml/20231109151106.2385155-1-glider@google.com/) This patch was previously called "lib/bitmap: add bitmap_{set,get}_value()" (https://lore.kernel.org/lkml/20230720173956.3674987-2-glider@google.com/) v11: - rearrange whitespace as requested by Andy Shevchenko, add Reviewed-by:, update a comment v10: - update comments as requested by Andy Shevchenko v8: - as suggested by Andy Shevchenko, handle reads/writes of more than BITS_PER_LONG bits, add a note for 32-bit systems v7: - Address comments by Yury Norov, Andy Shevchenko, Rasmus Villemoes: - update code comments; - get rid of GENMASK(); - s/assign_bit/__assign_bit; - more vertical whitespace for better readability; - more compact code for bitmap_write() (now for real) 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 | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 99451431e4d65..7ca0379be8c13 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.472.g3155946c3a-goog