Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp640474ybl; Thu, 22 Aug 2019 02:34:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqxaIMbOfOd1pC3FqZ1roTOOW6l0gEzBm4zm5xQE/fD4QvkWAHZiSnxdWJbbYCZXgs9eYFy+ X-Received: by 2002:aa7:8498:: with SMTP id u24mr40992266pfn.61.1566466447025; Thu, 22 Aug 2019 02:34:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566466447; cv=none; d=google.com; s=arc-20160816; b=uUQCOBrYC10OQRKnoTTs9e3f7sPP+Xbg0n6jEx90ktv1aubILtYLnexZhKUrdIHtWl 2vdpG9ISD1/qgaQU6t6aWUN98d/w+JzWY8sIF0oytfmvWYeXs+K39BHd4eSgRHMwMArr 0ib+sVIv+S5ml7scGBxQNV96RjjFGcpOpZQLJP9LFwRpdNbqusVriNjxVz2Y6lFWLcgp P2G+fJKUGBPLl1CMWbYLkVlqQil/YGNLE+2s6KL7vqtt2lXeqyL6eR3o1C01JbaAjGwO 1mKHOyyrJAk2dQ+0RzOnDRnKM/g9xeOtLyJvDKidUSV0VBi9p08jhaPlmaZO+OgRjnz7 kIzg== 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=xMCHFrQ30O+cahLWeqCDfI4x6SiJAjgtCYEm9W581aQ=; b=qiRuhLG1pvU/GjGJRofuVg08Q1BZzZAX+1axqfGtWOzR6DG+SE6dsNGR+DNmMuWIZr O8jQsIAmPthKiVAP4Lwo1ZcuKezAmvuQan3jebBwlK6f5uQLE7Lrr8iGkl4t+OtgzF9s mwLdEvAfRar9t6IvJlHqYqC5nQGrD0fSZD/nNFt/3rDjro1Lc7p2HKVzd7UbY1tsqM2I 5/SgFwhNLk5EnKUzNyCNcjd17dUtPQ+ykQjGZjitNDGOy9ZUYmNfSPUU3CTRSoflU+N8 6Ga4yRqOyY8m3InSy88zTBCg8zdk6imtIA/e1r1hZhGBdzjl0sTdBZYPEsTEGSEpu6av CUZw== 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 k15si16541531pgj.216.2019.08.22.02.33.51; Thu, 22 Aug 2019 02:34: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 S1731563AbfHVHaK (ORCPT + 99 others); Thu, 22 Aug 2019 03:30:10 -0400 Received: from mail.ispras.ru ([83.149.199.45]:41652 "EHLO mail.ispras.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729718AbfHVHaK (ORCPT ); Thu, 22 Aug 2019 03:30:10 -0400 Received: from [10.68.32.192] (broadband-188-32-48-208.ip.moscow.rt.ru [188.32.48.208]) by mail.ispras.ru (Postfix) with ESMTPSA id 7FD1C540089; Thu, 22 Aug 2019 10:30:07 +0300 (MSK) Subject: Re: [PATCH] lib/memweight.c: optimize by inlining bitmap_weight() To: Andrew Morton Cc: Akinobu Mita , Jan Kara , Matthew Wilcox , linux-kernel@vger.kernel.org, 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> <20190821182507.b0dea16f57360cf0ac40deb6@linux-foundation.org> From: Denis Efremov Message-ID: Date: Thu, 22 Aug 2019 10:30:07 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190821182507.b0dea16f57360cf0ac40deb6@linux-foundation.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 On 22.08.2019 04:25, Andrew Morton wrote: > On Wed, 21 Aug 2019 10:42:00 +0300 Denis Efremov wrote: > >> This patch inlines bitmap_weight() call. > > It is better to say the patch "open codes" the bitmap_weight() call. > >> Thus, removing the BUG_ON, > > Why is that OK to do? BUG_ON was necessary here to check that bitmap_weight will return a correct value, i.e. the computed weight will fit the int type: static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits); BUG_ON was added in the memweight v2 https://lore.kernel.org/lkml/20120523092113.GG10452@quack.suse.cz/ Jan Kara wrote: >> + >> + for (longs = bytes / sizeof(long); longs > 0; ) { >> + size_t bits = min_t(size_t, INT_MAX & ~(BITS_PER_LONG - 1), > + longs * BITS_PER_LONG); > I find it highly unlikely that someone would have such a large bitmap > (256 MB or more on 32-bit). Also the condition as you wrote it can just > overflow so it won't have the desired effect. Just do > BUG_ON(longs >= ULONG_MAX / BITS_PER_LONG); > and remove the loop completely. If someone comes with such a huge bitmap, > the code can be modified easily (after really closely inspecting whether > such a huge bitmap is really well justified). >> + >> + w += bitmap_weight(bitmap.ptr, bits); >> + bytes -= bits / BITS_PER_BYTE; >> + bitmap.address += bits / BITS_PER_BYTE; >> + longs -= bits / BITS_PER_LONG; Akinobu Mita wrote: > The bits argument of bitmap_weight() is int type. So this should be > > BUG_ON(longs >= INT_MAX / BITS_PER_LONG); We don't need this check, since we removed the bitmap_weight call and control the computation directly with size_t everywhere. We could add BUG_ON(bytes >= SIZE_MAX / BITS_PER_BYTE); at the very beginning of the function to check that the array is not very big (>2000PiB), but it seems excessive. > > I expect all the code size improvements are from doing this? Yes, but I thought it's good to show that the total size is not increasing because of the manual "inlining". > >> and 'longs to bits -> bits to longs' conversion by directly calling >> hweight_long(). >> >> ./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 >> > Regards, Denis