Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp107938imb; Thu, 28 Feb 2019 17:37:41 -0800 (PST) X-Google-Smtp-Source: APXvYqxkKV4P+hdN5FS1hxnVk1Nd09QADqw+3zzatm2ie5RHm66EYP4ABsu6wyCk64JPxaN4oV+d X-Received: by 2002:a63:fd07:: with SMTP id d7mr2193695pgh.163.1551404261507; Thu, 28 Feb 2019 17:37:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551404261; cv=none; d=google.com; s=arc-20160816; b=TkwGtBlCRfFmIinoynyKrYgDJcTTqdW2MewZHBK2tw5yHPP7ddkNy5nmOizN8ZjY2r a/Sx1MCUiEqChQGtMi0YPI9A+Hnnxr9bCp7mnxj7DdCXnpRmgkViygpOGepm8F2FpSwT ZTaM0V3HCxZ+2yDW0FjcBjEwnUrX9sirPrARzU4G6uApl0Kk0NnV7PHtQyOmfAj56Ovs ZAk8+TBwPT2baYDmjJsPQnRLqios9rgRe6SK8b15ehom+WaMgYzk1wphxnqq239u0k2w McLRXd5v2tLTUf/5QXf7ijFOz/iPgy4EZ+svDVPPDN4XOSbxcTkZM1CcJ80vC8ALBK1A ka/g== 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=EOTGZ+5Eu2EmKbrbnAz29ggRWNNjEvRXpsrCD+kwHSk=; b=X3CTzE9RxXePyiAkd3KSDtJ6Kbbd+j3uPge4oob5F1RixdHpkpkRsv7XfYRfSG2eN4 yEiwsEUiYTagZPzb6XfO2k06L6dHPXe/U/hQG9FOj9uFPvpyv02JgpRf/Cq0+ARU/rE6 +2gzHZMLSHeSHaasVAfQzdFqqGzq+h/yj7cvwDbFG/lwL+SMbGdSLI8ooaiJxIkjxEpu Gw7r/1BJvLWlMNJ1iOaikewlx8+1QCWHRySMBKktl/ewSQz7lTLdwjqDSEnihhcMrxc/ cu3FIBExWc4yyXaNaiZ/FCeu4vmzl/YAkwJEM7YalMFW/nuj7OXFpLfJokFvInqsJ16d igFA== 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=utah.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j9si18422671plk.359.2019.02.28.17.37.25; Thu, 28 Feb 2019 17:37:41 -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; 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=utah.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729119AbfB1XSm (ORCPT + 99 others); Thu, 28 Feb 2019 18:18:42 -0500 Received: from mail-svr1.cs.utah.edu ([155.98.64.241]:35756 "EHLO mail-svr1.cs.utah.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725955AbfB1XSm (ORCPT ); Thu, 28 Feb 2019 18:18:42 -0500 Received: from localhost (localhost [127.0.0.1]) by mail-svr1.cs.utah.edu (Postfix) with ESMTP id 2BB106500C0; Thu, 28 Feb 2019 16:18:41 -0700 (MST) Received: from mail-svr1.cs.utah.edu ([127.0.0.1]) by localhost (mail-svr1.cs.utah.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NKWgAgrdid1s; Thu, 28 Feb 2019 16:18:40 -0700 (MST) Received: from [155.98.67.65] (kalnik.cs.utah.edu [155.98.67.65]) by smtps.cs.utah.edu (Postfix) with ESMTPSA id 8153A6500BF; Thu, 28 Feb 2019 16:18:40 -0700 (MST) Subject: Re: [PATCH] cxgb4: fix undefined behavior in mem.c To: Bart Van Assche , linux-rdma@vger.kernel.org Cc: Steve Wise , Doug Ledford , Jason Gunthorpe , open list References: <1551393519-96595-1-git-send-email-shaobo@cs.utah.edu> <1551394596.31902.209.camel@acm.org> From: Shaobo He Message-ID: Date: Thu, 28 Feb 2019 16:18:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.2 MIME-Version: 1.0 In-Reply-To: <1551394596.31902.209.camel@acm.org> Content-Type: text/plain; charset=utf-8; format=flowed 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 I can't afford a pdf version of the C standard. So I looked at the draft version used in the link I put in the commit message. It says (in 6.2.4:2), ``` The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime. ``` I couldn't find the definition of lifetime over a dynamically allocated object in the draft of C standard. I refer to this link (https://en.cppreference.com/w/c/language/lifetime) which suggests that the lifetime of an allocated object ends after the deallocation function is called upon it. I think maybe the more problematic issue is that the value of a freed pointer is intermediate. Shaobo On 2/28/19 3:56 PM, Bart Van Assche wrote: > On Thu, 2019-02-28 at 15:38 -0700, Shaobo He wrote: >> In function `c4iw_dealloc_mw`, variable mhp's value is printed after >> freed, which triggers undefined behavior according to this post: >> https://trust-in-soft.com/dangling-pointer-indeterminate/. >> >> This commit fixes it by swapping the order of `kfree` and `pr_debug`. >> >> Signed-off-by: Shaobo He >> --- >> drivers/infiniband/hw/cxgb4/mem.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/infiniband/hw/cxgb4/mem.c b/drivers/infiniband/hw/cxgb4/mem.c >> index 7b76e6f..bb8e0bc 100644 >> --- a/drivers/infiniband/hw/cxgb4/mem.c >> +++ b/drivers/infiniband/hw/cxgb4/mem.c >> @@ -684,8 +684,8 @@ int c4iw_dealloc_mw(struct ib_mw *mw) >> mhp->wr_waitp); >> kfree_skb(mhp->dereg_skb); >> c4iw_put_wr_wait(mhp->wr_waitp); >> - kfree(mhp); >> pr_debug("ib_mw %p mmid 0x%x ptr %p\n", mw, mmid, mhp); >> + kfree(mhp); >> return 0; >> } > > Please quote the relevant paragraphs from the C standard. All I have found > about free() in ISO/IEC 9899:2017 is the following: > > Description > The free function causes the space pointed to by ptr to be deallocated, that > is, made available for further allocation. If ptr is a null pointer, no > action occurs. Otherwise, if the argument does not match a pointer earlier > returned by a memory management function, or if the space has been > deallocated by a call to free or realloc, the behavior is undefined. > > That is not sufficient to claim that the above code triggers undefined > behavior. > > Bart. >