Received: by 10.192.165.148 with SMTP id m20csp829376imm; Thu, 10 May 2018 01:01:49 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqNu59wzPSsZYerxiC7zRCEpcZsyqVSOtI75VUmgTDPjw/ot1biJZAfUvCXN0slLE4AivAP X-Received: by 2002:a65:560f:: with SMTP id l15-v6mr314027pgs.186.1525939309016; Thu, 10 May 2018 01:01:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525939308; cv=none; d=google.com; s=arc-20160816; b=Dx0z9IPZFF9KGMVAdXrhwS0lBRJIBTzhTtEWCr80S73rctMqFZ5J/kalp4lpvQpx07 KyoSTCjqcpohKr4nnUYOSz4cFfQ0fG3pfpMfttg2KiqgijEAKYEJ6doJoNbT6wdiObRr qCeui8TDjroly/HEPOVQM8CTbKKumbicjDOJNPcwsa70S/K25Y0acAB5XwNFtzRHUhz+ D3vadTPx1oeCDl2+cgm9yUnS2n3BR33nmuFLvtllZpUsEvqX4LHV1zPHCiREAo60PKwl ySQknuHsl6BHEALITUQ/aaBrvTWv0DAOYout/P+lz1oOF4GakICDrjOlqTV9Qc8WoZb2 Pm9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=vOEwKsC09e5IXODjx7Ibj8O3FgE95l5M6qPnUKq/6JQ=; b=lRLMR+lUfRYxC4sNfwjzdzpwEpYk5jXvVfmZh6CdeU+iXHcWTBjWMTAeTV8HqaQbaF 5/xdfmfNGGFWGCBoAqQbO50MF5Ji7J0EXE91NL8EtsiNgFs6Htf8wI7YhR6qXZWSzisw 93/U6zJWUlcikuRYN7lfJOGWyv8wfxn71kJSQsf5Qg0q4CXf/G1EKY0G1bzqX48vGshZ t9tmSYt/xq+mf6btF0veQUcw36dDX3WVF83EabJOyO3mXhDWzCFKMD1m/9gx9FMTwTfe ocDeDn+naWFEM2BRqNvUiuI9qP7mR6IHClxLHyByK5hM62EcbhgtJ9sECxDoPgD4v50X r5zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@armlinux.org.uk header.s=pandora-2014 header.b=ZOmtafc9; 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=armlinux.org.uk Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 136-v6si211590pgh.114.2018.05.10.01.01.19; Thu, 10 May 2018 01:01:47 -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; dkim=fail header.i=@armlinux.org.uk header.s=pandora-2014 header.b=ZOmtafc9; 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=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756719AbeEJH5J (ORCPT + 99 others); Thu, 10 May 2018 03:57:09 -0400 Received: from pandora.armlinux.org.uk ([78.32.30.218]:53432 "EHLO pandora.armlinux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753944AbeEJH5I (ORCPT ); Thu, 10 May 2018 03:57:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2014; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=vOEwKsC09e5IXODjx7Ibj8O3FgE95l5M6qPnUKq/6JQ=; b=ZOmtafc9e6/ZTDyeL4TkPgDPU 4/RpgpmG2mqN5bxO1//9dQyYOiXT1/0DS3VJzabCdv7WNxjt6X2yAx7xeUJTImQkBgx90cAcjb1YC 0wF9THaskcdnribODGM2EIErGEUmG/JD3EtbMI0Ge5QBE2Uw4N+pc2fzW6wZD5ZV5BttI=; Received: from n2100.armlinux.org.uk ([2001:4d48:ad52:3201:214:fdff:fe10:4f86]:45098) by pandora.armlinux.org.uk with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) (envelope-from ) id 1fGgRg-0000bm-TZ; Thu, 10 May 2018 08:57:01 +0100 Received: from linux by n2100.armlinux.org.uk with local (Exim 4.90_1) (envelope-from ) id 1fGgRe-0007ef-8F; Thu, 10 May 2018 08:56:58 +0100 Date: Thu, 10 May 2018 08:56:57 +0100 From: Russell King - ARM Linux To: Wang YanQing Cc: illusionist.neo@gmail.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, daniel@iogearbox.net, ast@fb.com Subject: Re: [PATCH] bpf, arm32: Correct check_imm24 Message-ID: <20180510075656.GS16141@n2100.armlinux.org.uk> References: <20180510032013.GB26016@udknight> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180510032013.GB26016@udknight> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 10, 2018 at 11:20:13AM +0800, Wang YanQing wrote: > imm24 is signed, so the right range is: > [-(2<<(24 - 1)), (2<<(24 - 1)) - 1] 2 << (24 - 1) is the same as 1 << 24. > -#define check_imm(bits, imm) do { \ > - if ((((imm) > 0) && ((imm) >> (bits))) || \ > - (((imm) < 0) && (~(imm) >> (bits)))) { \ > - pr_info("[%2d] imm=%d(0x%x) out of range\n", \ > - i, imm, imm); \ > +#define check_imm_range(min, max, imm) do { \ > + if (imm < min || imm > max) { \ > + pr_info("[%2d] imm=%d is out of range\n", \ > + i, imm); \ > return -EINVAL; \ > } \ > } while (0) > -#define check_imm24(imm) check_imm(24, imm) > +#define check_imm24(imm) check_imm_range(-16777216, 16777215, imm) How is this any different? If imm is 16777216, then "imm > max" in your version is true. In the original version, "imm > 0" is true, so we then test for "16777216 >> 24" being non-zero. That's also true, so the test condition fires. If imm is 16777215, then "imm > max" is false in your version. In the original version, the conditions also evaluate to false. For the -16777217 case, "imm < min" in your version is true. In the original version, "imm < 0" is true, so we then test for "~(-16777217) >> 24" being non-zero. This is the same as "16777216 >> 24" being non-zero, which is true so the condition fires. With -16777216, the same thing happens, both end up evaluating to false. So, the two cases end up producing identical results, and there is no actual effect from this change. However, your commit message is correct - there is a bug here. That's obvious when you mask the "imm" value with 0x00ffffff, and realise that an imm value of -16777216 ends up having the same value in the instruction as an imm value of 0. So, the range of "imm" is _half_ that. #define check_imm(bits, imm) do { \ - if ((((imm) > 0) && ((imm) >> (bits))) || \ - (((imm) < 0) && (~(imm) >> (bits)))) { \ + if ((((imm) > 0) && ((imm) >> (bits - 1))) || \ + (((imm) < 0) && (~(imm) >> (bits - 1)))) { \ pr_info("[%2d] imm=%d(0x%x) out of range\n", \ i, imm, imm); \ would fix it. Alternatively: #define check_imm(bits, imm) do { \ - if ((((imm) > 0) && ((imm) >> (bits))) || \ - (((imm) < 0) && (~(imm) >> (bits)))) { \ + if ((imm) >= (1 << ((bits) - 1)) || \ + (imm) < -(1 << ((bits) - 1))) { \ pr_info("[%2d] imm=%d(0x%x) out of range\n", \ i, imm, imm); \ would also fix it. -- RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line in suburbia: sync at 8.8Mbps down 630kbps up According to speedtest.net: 8.21Mbps down 510kbps up