Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1738222rwb; Wed, 26 Jul 2023 18:18:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlHf9iFDXZYQZ/KBsL5VpPQb7uoURysC6e8suGRL0t2nDQOGZ5oKosAEAVFpCItChFG0xTnM X-Received: by 2002:a17:906:2096:b0:98d:696a:531c with SMTP id 22-20020a170906209600b0098d696a531cmr742619ejq.40.1690420680219; Wed, 26 Jul 2023 18:18:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690420680; cv=none; d=google.com; s=arc-20160816; b=alB9WscO6G7JdhShrPszqEV/JhD3+ZiZw7+lFAOEa6vGtGeAq2CSWtszL0NxLAngyu TLa554K809AxjplZ939c+V1smTq4dCqKs8xQllPcmx+wv76oa5cYaABVq4hu8t5BcEM1 mVJzZOh/xMaBNZMwPtK4HchC5UKZU2nSDrBJ91czRLlZw7xAIZmzsdY8dVwbqXqir1Kb gBoTQ9juAJIPft6Z4z29rL0FJqGNnAf6irroTuKYMmeVgl3KXCEp1fMOky33jawNFtRu Lbb/uqozKwqAYCzFUs8yFFCDz4pV1GNuO5R+mSBS+GuNTH3GMPjBc8h+0ut9/0apU+4c h2uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ueENmT0DIXCVaUSH4rOLoKo2KUhTNxrToyXv/aFduM0=; fh=d0NYWzlzWTM6vHUSJ3AuzOnx+2vuzVYW8PUsSOOCs6I=; b=vUiTP2Z+bGiEC0SsyYESrt6tEWHFzlQwpMYocpV/0MfVdyWqXzQwFnKbngDExx5k2O jPzbjfvP6N2ql7i1OvL0VBi4/kE3RHH8lQXdH6Hc7KZgyySPT9LVssgdDVz9ZCbYEv+H e3Wxh7enBm4dYXsl+8yG1wX0cPUOzwZwebIJ75zA/+igsBr9MySMGv6BXkly1+huRKri 3xjKa0X8Ug+xEh24sborP9L5zQAxkanHg2VfhrCFSyd20MJWYiNNl43y55bcZC2HilSS Kc6B0HgL3Phz/CaOHjkW1GYxjuig1xJO/fc0OkTTjTwWlI5NFTfNXCImydY79BfgOVgJ zdKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=kvy9161s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b8-20020a1709065e4800b0099bc86f70a9si130243eju.748.2023.07.26.18.17.33; Wed, 26 Jul 2023 18:18:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=kvy9161s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229822AbjG0AOv (ORCPT + 99 others); Wed, 26 Jul 2023 20:14:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229495AbjG0AOv (ORCPT ); Wed, 26 Jul 2023 20:14:51 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF5461AD for ; Wed, 26 Jul 2023 17:14:49 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-686ea67195dso335304b3a.2 for ; Wed, 26 Jul 2023 17:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690416889; x=1691021689; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=ueENmT0DIXCVaUSH4rOLoKo2KUhTNxrToyXv/aFduM0=; b=kvy9161sn4Hxo1x6t+xx0pZ/ZoZymqHOCwF+5vDNn4ghPPfr6eQt0jnFx4qflvimfu wTAmHiEZwSt5N/fgXePw1BqbsQPe1lSZ7VkSwBpqcmoUbQvnWb/qFUxLwHUJVvmTeNoQ 1tH9xWJhCmdPWUEmLpyLmcl8sfwtM8fPItox6Hdb1uhZnPvEQ0V4xbYfrzCS4XtKjR47 cB5ndary9xxRVqEgtgwuB1Krssb0vSG6P87n9mrgESnGUNBWZ4SAcTupLBJIF8r0f+Fj DxEnCBbDAgQ8faKqJaoz/hMxbFi/B/kkHb/AvgHcUfdhf2izoK2d2fGi4Oh2lBbkUnT0 ELRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690416889; x=1691021689; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ueENmT0DIXCVaUSH4rOLoKo2KUhTNxrToyXv/aFduM0=; b=aNWQMy7/vgMpFWCC3lDF8LCdcV/9lltIishi/sDRakRgvpDhgtoAMk7okTI4cCs3IA rLup7VoR2Dj6rCoY+dxVjQ2v/rsNe6A3dblT25+HAugzIqPv/z8dmG4e3yZVgN+uZGiu GdG6BaVmsyk0kk8hAtksKv9G5PtZvpv3Uko/batStc6CkaWjy/5ISgQQgSfVQgsQHD2x WhsM8z11HnKVSAipC4o2Aet95ZedXCxl0FNB5McwjciYABE2qTO+cVEyqineqG/q0c3d HsSirZarH+SDtJ16f4ljujyYqjOku/Bqxz16nLR0vmWIvpOsumXxj31iORt20oTMmPug qR0w== X-Gm-Message-State: ABy/qLaLs4/B3kdWKJjIOBAEafYQsNVzKqahwhx4UxK6OjNdJY2E6jF+ cem7JulZFL7kFSpHbP/3hfI= X-Received: by 2002:a05:6a00:b4c:b0:680:98c:c58d with SMTP id p12-20020a056a000b4c00b00680098cc58dmr4336170pfo.2.1690416889150; Wed, 26 Jul 2023 17:14:49 -0700 (PDT) Received: from localhost ([216.228.127.130]) by smtp.gmail.com with ESMTPSA id x9-20020a056a00270900b00682c8637109sm185143pfv.34.2023.07.26.17.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 17:14:48 -0700 (PDT) Date: Wed, 26 Jul 2023 17:14:45 -0700 From: Yury Norov To: Alexander Potapenko Cc: catalin.marinas@arm.com, will@kernel.org, pcc@google.com, andreyknvl@gmail.com, andriy.shevchenko@linux.intel.com, linux@rasmusvillemoes.dk, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, eugenis@google.com, syednwaris@gmail.com, william.gray@linaro.org, Arnd Bergmann Subject: Re: [PATCH v4 1/5] lib/bitmap: add bitmap_{set,get}_value() Message-ID: References: <20230720173956.3674987-1-glider@google.com> <20230720173956.3674987-2-glider@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Wed, Jul 26, 2023 at 10:08:28AM +0200, Alexander Potapenko wrote: > On Sun, Jul 23, 2023 at 3:57 AM Yury Norov wrote: > > > > On Thu, Jul 20, 2023 at 07:39:52PM +0200, Alexander Potapenko wrote: > > > +/** > > > + * bitmap_write - write n-bit value within a memory region > > > + * @map: address to the bitmap memory region > > > + * @value: value of nbits > > > + * @start: bit offset of the n-bit value > > > + * @nbits: size of value in bits, up to BITS_PER_LONG > > > + */ > > > +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; > > > + > > > + if (unlikely(!nbits)) > > > + return; > > > + value &= GENMASK(nbits - 1, 0); > > > > Strictly speaking, a 'value' shouldn't contain set bits beyond nbits > > because otherwise it's an out-of-bonds type of error. > > I can easily imagine someone passing -1 (or ~0) as a value, but > wanting to only write n bits of n. This is an abuse of new API because we've got a bitmap_set(). But whatever, let's keep that masking. ... > I like the idea of sharing the first write between the branches, and > it can be made even shorter: > > =========================================================== > void bitmap_write_new(unsigned long *map, unsigned long value, > unsigned long start, unsigned long nbits) > { > unsigned long offset; > unsigned long space; > size_t index; > bool fit; > > if (unlikely(!nbits)) > return; > > value &= GENMASK(nbits - 1, 0); > offset = start % BITS_PER_LONG; > space = BITS_PER_LONG - offset; > index = BIT_WORD(start); > fit = space >= nbits; space >= nbits <=> BITS_PER_LONG - offset >= nbits <=> offset + nbits <= BITS_PER_LONG > map[index] &= (fit ? (~(GENMASK(nbits - 1, 0) << offset)) : So here GENMASK(nbits + offset - 1, offset) is at max: GENMASK(BITS_PER_LONG - 1, offset). And it never overflows, which is my point. Does it make sense? > ~BITMAP_FIRST_WORD_MASK(start)); As I said, ~BITMAP_FIRST_WORD_MASK() is the same as BITMAP_LAST_WORD_MASK() and vise-versa. > map[index] |= value << offset; > if (fit) > return; > > map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits); > map[index + 1] |= (value >> space); > } > =========================================================== > > According to Godbolt (https://godbolt.org/z/n5Te779bf), this function > is 32 bytes shorter than yours under x86 Clang, and 8 bytes - under > GCC (which on the other hand does a poor job optimizing both). > > Overall, given that there's currently a single user of these > functions, isn't it premature to optimize them without knowing > anything about their performance? > > > In previous iteration, I asked you to share disassembly listings for the > > functions. Can you please do that now? > > Will godbolt work for you (see above)? I don't know for how long an external resource will keep the reference alive. My SSD keeps emails long enough. ... > > You're mentioning that the compression ratio is 2 to 20x. Can you > > share the absolute numbers? If it's 1k vs 2k, I think most people > > just don't care... > > I'll provide the exact numbers with the next patch series. Last time I > checked, the order of magnitude was tens of megabytes. That's impressive. Fruitful idea. It would be important for embedded guys who may disable MTE because of memory overhead. I think it's worth to mention that in Kconfig together with associate performance overhead, if it ever measurable. > > Can you share the code that you used to measure the compression ratio? > > Would it make sense to export the numbers via sysfs? > > For out-of-line allocations the data can be derived from > /proc/slabinfo, but we don't calculate inline allocations. > Agreed, a debugfs interface won't hurt.