Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp4593987imm; Tue, 7 Aug 2018 04:19:44 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeUr7pC+3NPsZjx5VssgoY21HTQhVA73/oVEeCM7G6BSZqJpRNWMY40hCNPBS0i0e5f0HDU X-Received: by 2002:a62:990f:: with SMTP id d15-v6mr21333139pfe.162.1533640784713; Tue, 07 Aug 2018 04:19:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533640784; cv=none; d=google.com; s=arc-20160816; b=dz17idf7JdjR506+SfhdEaHlP4e7nk5nLYoy7gIa4EU32sw1SPXfNYWqFpBZHkAtuR PCVY9U9Bph+hNPFdLRfygm5h5Won5HeCN7b6w9HE+G/zmEv/3ibLNvU0WnrmoonJI4ND t4UZORbKW+tfwR+yIS62Kq7Yq5V1auZkid2KPRFpiRuldexpaRXN7wiMEat7j9d7+joL HAJwO5ov6vUaOaMoTzwfY83HGv5rn3Fh93mtdJZngbXV4ESpmU25fqK35WrZxnEHtO1y EYpuzaQe3WIJQYfU/x8LTnWiqh652jS5h6cPHGZJoapHnxRhwbW8r1rAOuFLiq+b50cr Izxg== 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:in-reply-to :references:subject:cc:to:mime-version:user-agent:from:date :message-id:arc-authentication-results; bh=KWY1vaKIsSbPIbiUjTgi27TAJvesXsL2QcCD7g6eius=; b=k/X2tKjXGzeyqyVjg/Io+9Rf54so1yDxthifJO4XI0d0lN9b9wZpbr9pfX0mY9p2Cq rWas6+q1PyiAfb2rL1L3lLMUPKK9nZnR5js14j5o9rrGEr4QeeKa+aZvQ/4+HBvGLk0O rnoArv3N/fcdF8nIW0G9wjX9ntILi7IPYmNMHX8Dkz2+IWKYjdAid+ISwEGeArALnFMc RWzaAIICdpd6stg4+ft4pW+nMY2lk7Ukv8tAdk2EV58cMlAeDVh0M5vP+Jq/G2dx6bWg s/TCMF+DlovZ2qs/YqfgBfVf6LfczcDoVwfURR2x/r19c6rrhZFm23AZl46rBo4e2RwB RiSw== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r23-v6si1163852pgk.582.2018.08.07.04.19.29; Tue, 07 Aug 2018 04:19:44 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388880AbeHGNcY (ORCPT + 99 others); Tue, 7 Aug 2018 09:32:24 -0400 Received: from mga12.intel.com ([192.55.52.136]:20540 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388764AbeHGNcY (ORCPT ); Tue, 7 Aug 2018 09:32:24 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Aug 2018 04:18:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,455,1526367600"; d="scan'208";a="251997279" Received: from unknown (HELO [10.239.13.97]) ([10.239.13.97]) by fmsmga005.fm.intel.com with ESMTP; 07 Aug 2018 04:18:21 -0700 Message-ID: <5B698106.5080806@intel.com> Date: Tue, 07 Aug 2018 19:22:46 +0800 From: Wei Wang User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Andy Shevchenko CC: Rasmus Villemoes , Yury Norov , Linux Kernel Mailing List , Andrew Morton , Jonathan Corbet , dgilbert@redhat.com Subject: Re: [PATCH] linux/bitmap.h: fix BITMAP_LAST_WORD_MASK References: <1532592471-21177-1-git-send-email-wei.w.wang@intel.com> <20180726093728.GA9069@yury-thinkpad> <5B599F5F.2070705@intel.com> <08410774-8b10-d620-064c-fdf4399d7336@rasmusvillemoes.dk> <5B69445D.1000107@intel.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/07/2018 06:26 PM, Andy Shevchenko wrote: >> Probably it's more clear to post the entire function here for a discussion: >> >> int __bitmap_weight(const unsigned long *bitmap, unsigned int bits) >> { >> unsigned int k, lim = bits/BITS_PER_LONG; >> int w = 0; >> >> for (k = 0; k < lim; k++) >> w += hweight_long(bitmap[k]); >> >> if (bits % BITS_PER_LONG) >> ==> w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits)); >> >> return w; >> } >> >> When the execution reaches "==>", isn't "k=lim"? > Let's check again, if bits == 0, bits % whatever == 0 as well, thus, > no execution. When bits < BITS_PER_LONG, the execution is fine (k = 0 > and still 0). > When bits >= BITS_PER_LONG, but not aligned, k goes from 0 to lim and > last word is exactly the partially filled one. No problem here. Las > case if bits % BITS_PER_LONG == 0, but hey, we have a guard against > this. > > So, where is the problem exactly? There is no problem here. All the discussions here are about if it is better to 1) put this guard to the macro or 2) have each callers to do it. If we go with 2) as we discussed before, then do we need to add notes above the macro to people who will use/port this macro. > >>> OTOH, for nbits=0, there _is_ no last word (since there are no words at >>> all), so by the time you want to apply the result of >>> BITMAP_LAST_WORD_MASK(0) to anything, you already have a bug, probably >>> either having read or being about to write into bitmap[0], which you >>> cannot do. Please check that user-space port and see if there are bugs >>> of that kind. >> Yes, some callers there don't check for nbits=0, that's why I think it is >> better to offload that check to the macro. The macro itself can be robust to >> handle all the cases. > Where they are? Can't you point out? "there", I meant that user-space port, not in the kernel. e.g. Line 225 at https://github.com/qemu/qemu/blob/master/include/qemu/bitmap.h (there are a couple of other places) >> nbits=64, means all the 64 bits need to mask >> >> The two are different cases, I'm not sure why we let the macro to return the >> same value. > The point is macro mustn't be called when nbits==0. > Yes, I fully agree with that point, but it seems Rasmus NAK-ed that point. To conclude the point of the discussion: with the current macro, there is no doc saying 0 can't be given to this macro and "BITMAP_LAST_WORD_MASK(0)=0xffffffff" doesn't seem correct to me. Best, Wei