Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp586837ybh; Sat, 7 Mar 2020 05:33:32 -0800 (PST) X-Google-Smtp-Source: ADFU+vttvGlu3wI813VGkC3apdAQaARsiJmeAeViurMjfJ9Ndgokjmo85wi1tLSOE9YuJZBE5tiB X-Received: by 2002:a9d:6f14:: with SMTP id n20mr6357986otq.271.1583588011875; Sat, 07 Mar 2020 05:33:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583588011; cv=none; d=google.com; s=arc-20160816; b=JRvdVdo3deXBu5oSmlMmKj0ehmlQY63ciWU6rWEENDZR3Q14kF9VzMuc1GFQ5drACK 1SdAUpATqsR684U59jVj4acTtwZNahZ5AuVdfJ3thhCAfis2NYxz8rhowF5F9ytxRNJD upPYENwDw7WyU/e83ftQbsY7EEwNK26zo40yKZ7lvGV5vUrwlJSBjz9S1DRHO/PVSrfj w7RJSWU6X/oolIRFie1t+rJBe18Z8FUGvfdZececcU7jICQm0ah7yfF8xyTy73VV9dL6 ka8/2KqGZ5/CbEhurSEZwfJEg63elIsgNGgToRGvXcQFGhC5hhXE++HZeu2BTdsPjjAn bQTQ== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=wE+8D6wqrnHL9Mq/JhFBCUv1aIjop/eLPpNsmTusecY=; b=qBI5GsKjnm6k/8McQ32PH4EN4shzMEneq+xhuy+hylfdndw04+hTUFBn8CES9gbiZR t+UKyS/GyeHj1xJbJ56pQlKHFxPrH5KAV2uhsWzM0HAyj8lX57hKGUst4p++45BSX4r4 JwgOkfR2ABPrQIWmB0ox04a3mNfUbuHnqMELZMPxtC/2gsItwKi3P5DKurv6aub3yqag BdH0HIMrtjR7tHn0Dbmz1999ohP1FvQFU0Bnagv+OQiEB1MiAKxuqXvYO3hOvnfasOXV A9PG9NvcDQn/QcUPsppBLDi754t9ICp7zGAeevq8S8zYY5aJuMD9PoV43qMXGWhrhhDe X+7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CrndxEdi; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b14si4123056ots.62.2020.03.07.05.33.19; Sat, 07 Mar 2020 05:33:31 -0800 (PST) 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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CrndxEdi; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726139AbgCGNc5 (ORCPT + 99 others); Sat, 7 Mar 2020 08:32:57 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:27703 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726109AbgCGNc4 (ORCPT ); Sat, 7 Mar 2020 08:32:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583587975; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wE+8D6wqrnHL9Mq/JhFBCUv1aIjop/eLPpNsmTusecY=; b=CrndxEdiQDNpQWhTAqvln3GnSL80Rcp7VkZ4MSZ8ys0zE0f0bAtYCWj+hK34UePCgE3Mo6 qawIqj9AIdGRde7nBQzwnJNpLvUE0AXW0D8ceWJOmVLOfFu0kNOFAWLQAXrm4v3MJiPCrw lwh+gnWeAPZV4Bll9pveK1ZnFmTJ4wM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-39-jUT8g7WYMCec7kGLFhCrcQ-1; Sat, 07 Mar 2020 08:32:54 -0500 X-MC-Unique: jUT8g7WYMCec7kGLFhCrcQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A6B2E107ACC4; Sat, 7 Mar 2020 13:32:51 +0000 (UTC) Received: from elisabeth (ovpn-200-26.brq.redhat.com [10.40.200.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 050C290A03; Sat, 7 Mar 2020 13:32:48 +0000 (UTC) Date: Sat, 7 Mar 2020 14:32:43 +0100 From: Stefano Brivio To: Yury Norov Cc: Pablo Neira Ayuso , Andrew Morton , Andy Shevchenko , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: Re: [PATCH] lib/bitmap: rework bitmap_cut() Message-ID: <20200307143243.0ec974da@elisabeth> In-Reply-To: <20200307081208.GA21695@yury-thinkpad> References: <20200306221423.18631-1-yury.norov@gmail.com> <20200307001856.5181eda7@elisabeth> <20200307081208.GA21695@yury-thinkpad> Organization: Red Hat MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 7 Mar 2020 00:12:08 -0800 Yury Norov wrote: > On Sat, Mar 07, 2020 at 12:18:56AM +0100, Stefano Brivio wrote: > > Hi Yuri, > > > > I haven't reviewed the new implementation yet, just a few comments so > > far: > > > > On Fri, 6 Mar 2020 14:14:23 -0800 > > Yury Norov wrote: > > > > > bitmap_cut() refers src after memmove(). If dst and src overlap, > > > it may cause buggy behaviour because src may become inconsistent. > > > > I don't see how: src is always on the opposite side of the cut compared > > to dst, and bits are copied one by one. > > Consider this example: > int main() > { > char str[] = "Xabcde"; > char *s = str+1; > char *d = str; // overlap > > memmove(d, s, 5); > printf("%s\n", s); > printf("%s\n", d); > } > > yury:linux$ ./a.out > bcdee > abcdee > > After memmove(), s[0] == 'b', which is wrong. > > In current version src is used after memmove() to set 'keep', which > may cause similar problem Ah, yes, good point. This doesn't happen on a complete overlap (current usage), but I see what you meant now. Actually, to fix this, it would be enough to move the assignment of 'keep' before the memmove(), or assign 'keep' from 'dst'. > > Also note that I originally designed this function for the single usage > > it has, that is, with src being the same as dst, and this is the only > > way it is used, so this case is rather well tested. Do you have any > > specific case in mind? > > No. Do you have in mind a dst != src usecase? I don't, I was just wondering about the reason behind your patch. > > > The function complexity is of O(nbits * cut_bits), which can be > > > improved to O(nbits). > > > > Nice, indeed. > > > > > We can also rely on bitmap_shift_right() to do most of the work. > > > > Also nice. > > > > > I don't like interface of bitmap_cut(). The idea of copying of a > > > whole bitmap inside the function from src to dst doesn't look > > > useful in practice. The function is introduced a few weeks ago and > > > was most probably inspired by bitmap_shift_*. Looking at the code, > > > it's easy to see that bitmap_shift_* is usually passed with > > > dst == src. bitmap_cut() has a single user so far, and it also > > > calls it with dst == src. > > > > I'm not fond of it either, but this wasn't just "inspired" by > > bitmap_shift_*: I wanted to maintain a consistent interface with those, > > and all the other functions of this kind taking separate dst and src. > > > > For the current usage, performance isn't exceedingly relevant. If you > > have another use case in mind where it's relevant, by all means, I > > think it makes sense to change the interface. > > > > Otherwise, I would still have a slight preference towards keeping the > > interface consistent. > > There is no consistent interface. Bitmap_{set,clear) uses one > notaton, bitmap_{and,or,shift) - another. I think that 'unary' > operations should not copy the whole bitmap. If user wants, he > can easily do it. In practice, nobody wants. bitmap_set() and bitmap_clear() are conceptually in another class, I think. In any case, I agree that (map-wise) unary operations should naturally not copy bitmaps, but I'm still not convinced that "fixing" this just for bitmap_cut() is a good idea -- because of the inconsistency it adds. How bad would it be to also adjust all usages of bitmap_{and,or,shift} to behave in the same way? > > By the way, I don't think it's possible to do that keeping the > > memmove(), and at the same time implement the rest of this change, > > because then we might very well hit some unexpected behaviour, using > > bitmap_shift_right() later. > > I think it should work. Can you elaborate? If you keep the memmove(), then use bitmap_shift_right() on dst, some excess bits will be affected, unless you copy them back from src... which at that point, you don't have anymore. -- Stefano