Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759829AbbBILxo (ORCPT ); Mon, 9 Feb 2015 06:53:44 -0500 Received: from mail-lb0-f171.google.com ([209.85.217.171]:47837 "EHLO mail-lb0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752442AbbBILxm (ORCPT ); Mon, 9 Feb 2015 06:53:42 -0500 From: Rasmus Villemoes To: "George Spelvin" Cc: akpm@linux-foundation.org, chris@chris-wilson.co.uk, davem@davemloft.net, dborkman@redhat.com, hannes@stressinduktion.org, klimov.linux@gmail.com, laijs@cn.fujitsu.com, msalter@redhat.com, takahiro.akashi@linaro.org, tgraf@suug.ch, valentinrothberg@gmail.com, yury.norov@gmail.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/3] lib: find_*_bit reimplementation Organization: D03 References: <1423404619-10653-2-git-send-email-yury.norov@gmail.com> <20150209083211.11953.qmail@ns.horizon.com> X-Hashcash: 1:20:150209:yury.norov@gmail.com::/iaEdikBGk2epR5h:000000000000000000000000000000000000000000Gjv X-Hashcash: 1:20:150209:valentinrothberg@gmail.com::KDOlLgDLQ9Rj4Nyl:0000000000000000000000000000000000005I0 X-Hashcash: 1:20:150209:linux@horizon.com::4ZmRZq3ZrHgaBFb7:000000000000000000000000000000000000000000000Lve X-Hashcash: 1:20:150209:chris@chris-wilson.co.uk::oyZ2yqLSCRhMXoCs:00000000000000000000000000000000000000vgq X-Hashcash: 1:20:150209:linux-kernel@vger.kernel.org::G211hElLPJ/ZXC9R:0000000000000000000000000000000001KXO X-Hashcash: 1:20:150209:laijs@cn.fujitsu.com::i/1Vnsfsn9yHNdDc:000000000000000000000000000000000000000001+zv X-Hashcash: 1:20:150209:hannes@stressinduktion.org::BuhOsBZTIOyL5JWp:000000000000000000000000000000000002Rgg X-Hashcash: 1:20:150209:msalter@redhat.com::tn62Eip+rguHDH41:00000000000000000000000000000000000000000002OqW X-Hashcash: 1:20:150209:akpm@linux-foundation.org::D7BDsx6cVo4kUScu:000000000000000000000000000000000000285k X-Hashcash: 1:20:150209:tgraf@suug.ch::8d/45A0x28qG6zT4:0000394R X-Hashcash: 1:20:150209:davem@davemloft.net::DW2771AUFKwXk+lN:00000000000000000000000000000000000000000041iD X-Hashcash: 1:20:150209:takahiro.akashi@linaro.org::RxbeD+CPCarfl4f1:000000000000000000000000000000000005VVN X-Hashcash: 1:20:150209:klimov.linux@gmail.com::oh9H9qKaSu9dkwzm:0000000000000000000000000000000000000007Tzw X-Hashcash: 1:20:150209:dborkman@redhat.com::uZAmk95dSOSamC9m:0000000000000000000000000000000000000000009pw+ Date: Mon, 09 Feb 2015 12:53:38 +0100 In-Reply-To: <20150209083211.11953.qmail@ns.horizon.com> (George Spelvin's message of "9 Feb 2015 03:32:11 -0500") Message-ID: <87wq3rs3lp.fsf@rasmusvillemoes.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2715 Lines: 81 [Yury, please do remember to Cc everyone who has previously participated] On Mon, Feb 09 2015, "George Spelvin" wrote: > Two more comments on the code. Two minor, but one that > seems like a bug, so for now, it's > > Nacked-by: George Spelvin > > Specifically, it seems like find_last_bit used to ignore trailing > garbage in the bitmap, but now will stop searching if the last word > contains some set bits not within size. True, though see below. > The minor one is that I don't think the first-word masking needs to > be conditional. The general code works fine if the start is aligned > (HIGH_BITS_MASK just generates an all-ones mask), is quite quick, and > saves a test & conditional branch. > I also noted that during the first review, but when I tried to compile it gcc actually generated slightly worse code, so I decided not to comment on it. I don't have a strong preference either way, though. > > Previously, the last word was masked, so bits beyond "size" were ignored. > With the revised code, something like find_last_bit(array, 96) will return 96 > if array[1] >> 32 is non-zero, even if array[1] & 0xffffffff is zero. > > Looking through the callers, I haven't found a case where this matters yet > so perhaps it's a safe optimization, but this really needs to be more > clearly documented if intentional. > > If no change was desired, I'd think a good way to do this would be: > > unsigned long find_last_bit(const unsigned long *addr, unsigned long size) > { > size_t idx = DIV_ROUND_UP(size, BITS_PER_LONG); > unsigned long tmp = addr[--idx]; > > tmp &= (2UL << (size % BITS_PER_LONG)) - 1; /* Mask last word */ > > while (!tmp) { > if (!idx) > return size; > tmp = addr[--idx]; > } > return idx * BITS_PER_LONG + __fls(tmp); > } How should that work? If size is for example 1, the mask evaluates to 3UL, while what is needed is 1UL. If size is aligned, the mask becomes 1UL, which is also not right. Also, I think it is best to handle size==0 appropriately, meaning that one cannot dereference addr in any way (and certainly not addr[-1]). So how about unsigned long find_last_bit(const unsigned long *addr, unsigned long size) { size_t idx = DIV_ROUND_UP(size, BITS_PER_LONG); unsigned long mask = LAST_WORD_MASK(size); while (idx--) { unsigned long val = addr[idx] & mask; if (val) return idx * BITS_PER_LONG + __fls(val); mask = ~0ul; } return size; } Rasmus -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/