Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4118229pxb; Mon, 30 Aug 2021 19:43:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsHrSCCnu8V2gPIiyaITQKrdeKZqxFoWmtiUn7di4Dzv1ibMS9HQHvUvYNGOcgoRYHJ7x/ X-Received: by 2002:a17:907:8693:: with SMTP id qa19mr25835891ejc.95.1630377811809; Mon, 30 Aug 2021 19:43:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630377811; cv=none; d=google.com; s=arc-20160816; b=cGPyRO+u9CuKPJSbnr1oVZa7E9hdK3A3JiSEV8dcPGUN2c5vygE1TA7cjxbX2aNkbg 93YC8owWuAgZHGCVvbp//BolDpIH1nlK3v3Ub2orgNmQYqYGuJqJU85iXEKpkY3v0lLx yFvZ0aqApzHntKac1M/cweYhZKNs1tXFk0McBzpCrY5w1ZxPHrSBNERouWkj3ufv0xj3 e/Y8Z6mdM2+MVlnXDUKXBLTVxTsm+/eQ48Jlc/YIHW7ZOlB+UDzTEhfa7ANwJNqmGUvA MiQh9wJLreZEHotkyI/NiJn+eYXZQ6QfxL5nPp+ZAXto43cQScWT+1t+nfl5+hmZgmgw uXvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:cc:to:subject:message-id :from:content-transfer-encoding:date:dkim-signature:mime-version; bh=VCGJByYqb4HN6aatcmB4jj13brXPega5RyBKIfzj7kE=; b=LdDpxg0wNVnfhVYpJaFKKANIKrvsEnrXT0qsHo7IfPUzWFMF6q9YcNealEdoF7DNJy HTdLwHUtsJA4lhY67yCsPxiQ+i1hwSTcwZKh5IdlzSfmbWEcQiFpe8Dx2OTYXRyVDzaD lAQk7t2U2m1AK0sRfisP4+hwFiQ1A6J9ttSLjhXSfreVyN4AHPQAw/v5g84ovoLuonoA RhnQll+AU//TNUPraSF0tZ1SZZqFK5afGpM2SaaAsmbgR3YvO1oLHo1Nqh3RqJF3LyRi zTm+A9lIIyHgqb829sAGoOLZzTcCbBmWM7qkU7fWdDEsbEyPeuUAcRF2bYuzKvmh6q02 V6XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=nQ8zsSpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o13si14945750eje.67.2021.08.30.19.43.07; Mon, 30 Aug 2021 19:43:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=nQ8zsSpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239350AbhHaCmk (ORCPT + 99 others); Mon, 30 Aug 2021 22:42:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236452AbhHaCmj (ORCPT ); Mon, 30 Aug 2021 22:42:39 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86BBAC061575; Mon, 30 Aug 2021 19:41:45 -0700 (PDT) MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1630377703; 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=VCGJByYqb4HN6aatcmB4jj13brXPega5RyBKIfzj7kE=; b=nQ8zsSpfJM2alNO1z4AfYZt6AFj744+5h2Uxf+Ku+fWMlWFqfhNo25Pebus2tH8Xpcg6AU JlYXEssqrxJYpbwEk8wQgHHOba2fh+olSu0yDWkZ0x3Wpw3Or0H3SfCBXc1l7aLfi7SQT4 G3vdqrzqP5nE59eqZ1r5Pp36LGJueYU= Date: Tue, 31 Aug 2021 02:41:42 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: yajun.deng@linux.dev Message-ID: <4186a73958319066f76c8a7e2e833b2a@linux.dev> Subject: Re: [PATCH linux-next] PCI: Fix the order in unregister path To: "Rob Herring" Cc: "Bjorn Helgaas" , "Arnd Bergmann" , "Lorenzo Pieralisi" , "PCI" , linux-kernel@vger.kernel.org In-Reply-To: References: <20210825083425.32740-1-yajun.deng@linux.dev> <63e1e9ea1e4b74b56aeafcc6695ecfa8@linux.dev> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: yajun.deng@linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org August 30, 2021 10:55 PM, "Rob Herring" wrote:=0A=0A> O= n Thu, Aug 26, 2021 at 9:39 PM wrote:=0A> =0A>> Au= gust 26, 2021 8:01 PM, "Rob Herring" wrote:=0A>> =0A>> = On Wed, Aug 25, 2021 at 10:57 PM wrote:=0A>> =0A>>= August 25, 2021 9:55 PM, "Rob Herring" wrote:=0A>> =0A= >> On Wed, Aug 25, 2021 at 3:34 AM Yajun Deng wrot= e:=0A>> =0A>> device_del() should be called first and then called put_dev= ice() in=0A>> unregister path, becase if that the final reference count, = the device=0A>> will be cleaned up via device_release() above. So use dev= ice_unregister()=0A>> instead.=0A>> =0A>> Fixes: 9885440b16b8 (PCI: Fix p= ci_host_bridge struct device release/free handling)=0A>> Signed-off-by: Y= ajun Deng =0A>> ---=0A>> drivers/pci/probe.c | 4 +-= --=0A>> 1 file changed, 1 insertion(+), 3 deletions(-)=0A>> =0A>> NAK.=0A= >> =0A>> The current code is correct. Go read the comments for device_add= /device_del.=0A>> =0A>> But the device_unregister() is only contains devi= ce_del() and put_device(). It just put=0A>> device_del() before put_devic= e().=0A>> =0A>> And that is the wrong order as we want to undo what the c= ode above=0A>> did. The put_device here is for the get_device we did. The= put_device=0A>> in device_unregister is for the get_device that device_r= egister did=0A>> (on success only).=0A>> =0A>> Logically, it is wrong too= to call unregister if register failed. That=0A>> would be like doing thi= s:=0A> =0A> You are right that the register and unregister are different = devices.=0A> However, your change is still wrong. The device_register is = actually=0A> irrelevant.=0A> =0AOK, the original order is right, it was m= y mistake.=0A=0A>> p =3D malloc(1);=0A>> if (!p)=0A>> free(p);=0A>> =0A>>= This is the raw code:=0A>> err =3D device_register(&bus->dev);=0A>> if (= err)=0A>> goto unregister;=0A>> unregister:=0A>> put_device(&bridge->dev)= ;=0A>> device_del(&bridge->dev);=0A> =0A> The pertinent parts are this:= =0A> =0A> err =3D device_add(&bridge->dev); // which calls get_device() i= tself,=0A> so there's the first ref=0A> if (err) {=0A> put_device(&bridge= ->dev);=0A> goto free;=0A> }=0A> bus->bridge =3D get_device(&bridge->dev)= ; // This is the 2nd ref which=0A> the PCI core holds=0A> ...=0A> unregis= ter:=0A> put_device(&bridge->dev); // This is the put for the get_device= =0A> just above here.=0A> device_del(&bridge->dev); // Then this does the= 2nd put.=0A> =0A> The get_device and put_device are paired, and the devi= ce_add and=0A> device_del are paired.=0A> =0A> As I said earlier, go read= the kerneldoc for device_add. For your=0A> convenience, here's the impor= tant part:=0A> =0A> device_add:=0A> * Rule of thumb is: if device_add() s= ucceeds, you should call=0A> * device_del() when you want to get rid of i= t. If device_add() has=0A> * *not* succeeded, use *only* put_device() to = drop the reference=0A> * count.=0A> =0A> device_del:=0A> * NOTE: this sho= uld be called manually _iff_ device_add() was=0A> * also called manually.= =0A> =0A> Rob