Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1717525ybl; Sat, 24 Aug 2019 03:03:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqxRGY2kmRrEPKYmcDdm8MZi0juL6QzljRAEa0wz4IH+x1DA7SsFguTtvvVaVS3jCRT3uRs2 X-Received: by 2002:aa7:8c03:: with SMTP id c3mr9866795pfd.139.1566640986347; Sat, 24 Aug 2019 03:03:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566640986; cv=none; d=google.com; s=arc-20160816; b=URrWS20AaxS92cE2Us+c9cAAOvaCwMpfJDrzHQD1voFHe226abP1AEl6uh0X+lL4qx JQ6RePHA/y1ZWPNP1tLBXpDO6Cqof13htLMLnQj46Bci3qD4dsO5PK1LuvgLRWiwfyC4 +M7J4b+Uwjggy34EeCy9HxL5muY1b/jYPmsDRWcCJ1O7kbIJHdUjFdbywla/hOj1J1Ns XgNgOaXtLL+XtPYCWFb7pqOyeAPLpvKmqCU3hwX6amJM1ThozlBaFK/fkAHGIx6XS25/ rBvUpJ3XXs4+zbizxDR3k+TyoCKr+sPkGQuGR+iprif+VtNfhrWu3p7FPNtNjvkjb/HN d0jQ== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OdmJ42IUwytpbxpZj1orbGgnLgCg5pJJJIA/JfL49Wo=; b=h0TBCUSaoCIeAURqhKFpXRebmfuSShv8yUXi5ZY6yAuLLayqds6ooJDlVjVwv8pXGx b4Hk9VWmhbDFd+mtj6CqI9wRNWSpprqr3E90BTWhEtMBgjnabsq08LypY1TMNtlgzJMB i9rEzX7Q1iD/RaxA2JDipFgZ1Hg1kjZSbR1j9O+dURNPKrZHJyqxr4Yurfq5RC7a1J3F MVpvE7m9MtBtrzA7VdyBAUfuIH6IZ3ornauta2Qz+bFv+SQcCHfO5RpYN6Tl+6sOPSI3 L8ZexobJZ+Fgp/c2D54aCqtzGrJmvNw/FNbhYLg7MzF80f/W1TRhgNtrWyJCRtOVapv9 hiDQ== 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 n1si4432954pjo.28.2019.08.24.03.02.40; Sat, 24 Aug 2019 03:03:06 -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 S1727012AbfHXKBT (ORCPT + 99 others); Sat, 24 Aug 2019 06:01:19 -0400 Received: from mail.ispras.ru ([83.149.199.45]:51218 "EHLO mail.ispras.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726072AbfHXKBT (ORCPT ); Sat, 24 Aug 2019 06:01:19 -0400 Received: from black.home (broadband-188-32-48-208.ip.moscow.rt.ru [188.32.48.208]) by mail.ispras.ru (Postfix) with ESMTPSA id 2678854006A; Sat, 24 Aug 2019 13:01:16 +0300 (MSK) From: Denis Efremov To: akpm@linux-foundation.org Cc: Denis Efremov , 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 Subject: [PATCH v2] lib/memweight.c: open codes bitmap_weight() Date: Sat, 24 Aug 2019 13:01:02 +0300 Message-Id: <20190824100102.1167-1-efremov@ispras.ru> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190821074200.2203-1-efremov@ispras.ru> References: <20190821074200.2203-1-efremov@ispras.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch open codes the bitmap_weight() call. The direct invocation of hweight_long() allows to remove the BUG_ON and excessive "longs to bits, bits to longs" conversion. BUG_ON was required to check that bitmap_weight() will return a correct value, i.e. the computed weight will fit the int type of the return value. With this patch memweight() controls the computation directly with size_t type everywhere. Thus, the BUG_ON becomes unnecessary. Total size reduced: ./scripts/bloat-o-meter lib/memweight.o.old lib/memweight.o.new add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-10 (-10) Function old new delta memweight 162 152 -10 Co-developed-by: Erdem Tumurov Signed-off-by: Erdem Tumurov Co-developed-by: Vladimir Shelekhov Signed-off-by: Vladimir Shelekhov Signed-off-by: Denis Efremov --- lib/memweight.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/memweight.c b/lib/memweight.c index 94dd72ccaa7f..f050b2b4c5e2 100644 --- a/lib/memweight.c +++ b/lib/memweight.c @@ -20,11 +20,13 @@ size_t memweight(const void *ptr, size_t bytes) longs = bytes / sizeof(long); if (longs) { - BUG_ON(longs >= INT_MAX / BITS_PER_LONG); - ret += bitmap_weight((unsigned long *)bitmap, - longs * BITS_PER_LONG); + const unsigned long *bitmap_long = + (const unsigned long *)bitmap; + bytes -= longs * sizeof(long); - bitmap += longs * sizeof(long); + for (; longs > 0; longs--, bitmap_long++) + ret += hweight_long(*bitmap_long); + bitmap = (const unsigned char *)bitmap_long; } /* * The reason that this last loop is distinct from the preceding -- 2.21.0