Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp854985ybe; Fri, 13 Sep 2019 07:21:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzPzv9Mi/JtGnz2uQC1BDQYKbrIriH/Fi0N9IrNZ/9+kfez0jM4pnQMWY0gZQMxygEN3JeN X-Received: by 2002:a17:907:42cc:: with SMTP id nz20mr19139570ejb.67.1568384495722; Fri, 13 Sep 2019 07:21:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568384495; cv=none; d=google.com; s=arc-20160816; b=s/Ieyc/2PUXmhBw8ruCc1Vw+HLj4gYWn/Ogx47oBClWCQK+RjpkAnWiE541M7RxnQZ 8P3Ny0pWGeC7ngfikVGafzmYOYwX+pVr8X8r2ufEJAl4iwl3t3iu5v3iHEz8HwUMg4O4 N+GCzhbinAPrzCw424Kgfs+Ld5i9rWdok86Q4bKTc5yBh82jaBKOZAhCplfHLfXdHcxZ csNnqhpKR+Bz6V3i9MTdzwrF4LT9u70UrVfP4EJBGr4Acf2tpgoAjL/6ufFK8u74LL+R 1AZ95bnQC6Xi5wciUR7t6AcSB8uioJdIh6QbZr/CNAc9jMlKeByE+JGRFcKc1EwaoPLK aRkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=urQ+QxnsgILwMNt36pIdOLh10LKW7B8nO29J49VHqQY=; b=O9tyFwVQe8IzNK1f1IjoDGNbfV/zg7MaXXcahEazWFe0GaaOA+Q8ypSe/bR3zCLmBX Eh+dZsJNBmjJUw9TbVMGt1UetByoGoBO4VIx5a91kP7Dl8AM+VAn4yatcOk4GMaG+c90 8WLGhj/xt7Cb0RKp2Znr9NF+kwYP9lW0RjYOdcbRmj3+q8/Hiq4SAg5Lo6KBlgpjrJ5z Y7dJliFA9Alov9lgYOHc2Arpc3XM0CtaXCyx9inKKr93WGJZE3MbMomL1VG6r2o9cFjX OfobpsafNYvG3y7lng4DcP2AFqDv3XuCSvmrKDatLSncboGTctyiW1+XlwTQS/4q94KL Giyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h15si1159445ejx.211.2019.09.13.07.21.11; Fri, 13 Sep 2019 07:21:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729191AbfIMLsT (ORCPT + 99 others); Fri, 13 Sep 2019 07:48:19 -0400 Received: from mail.ispras.ru ([83.149.199.45]:49012 "EHLO mail.ispras.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728621AbfIMLsT (ORCPT ); Fri, 13 Sep 2019 07:48:19 -0400 Received: from [192.168.43.132] (unknown [176.59.5.179]) by mail.ispras.ru (Postfix) with ESMTPSA id 39039540089; Fri, 13 Sep 2019 14:48:16 +0300 (MSK) Subject: Re: [PATCH v2] lib/memweight.c: open codes bitmap_weight() To: Matthew Wilcox Cc: akpm@linux-foundation.org, Akinobu Mita , Jan Kara , linux-kernel@vger.kernel.org, Matthew Wilcox , dm-devel@redhat.com, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, Erdem Tumurov , Vladimir Shelekhov References: <20190821074200.2203-1-efremov@ispras.ru> <20190824100102.1167-1-efremov@ispras.ru> <20190825061158.GC28002@bombadil.infradead.org> <20190826183956.GF15933@bombadil.infradead.org> From: Denis Efremov Message-ID: <85d9e45a-9631-a139-2d65-86a6753a35e6@ispras.ru> Date: Fri, 13 Sep 2019 14:48:15 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.0 MIME-Version: 1.0 In-Reply-To: <20190826183956.GF15933@bombadil.infradead.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Sorry for reviving this conversation, but it looks to me like this function could be reduced to a single bitmap_weight call: static inline size_t memweight(const void *ptr, size_t bytes) { BUG_ON(bytes >= UINT_MAX / BITS_PER_BYTE); return bitmap_weight(ptr, bytes * BITS_PER_BYTE); } Comparing to the current implementation https://elixir.bootlin.com/linux/latest/source/lib/memweight.c#L11 this results in a signification simplification. __bitmap_weight already count last bits with hweight_long as we discussed earlier. int __bitmap_weight(const unsigned long *bitmap, unsigned int bits) { ... if (bits % BITS_PER_LONG) w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits)); ... } and __arch_hweight* functions use popcnt instruction. I've briefly tested the equivalence of 2 implementations on x86_64 with fuzzing here: https://gist.github.com/evdenis/95a8b9b8041e09368b31c3a9510491a5 What do you think making this function static inline and moving it to include/linux/string.h? I could prepare a patch for it and add some tests for memweight and bitmap_weight. Or maybe I miss something again? Best regards, Denis