From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D17F7C433E0 for ; Wed, 3 Jun 2020 19:30:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8C9752067B for ; Wed, 3 Jun 2020 19:30:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="pYX8WSvG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8C9752067B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2BED08E0007; Wed, 3 Jun 2020 15:30:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 26F488E0006; Wed, 3 Jun 2020 15:30:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15D858E0007; Wed, 3 Jun 2020 15:30:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by kanga.kvack.org (Postfix) with ESMTP id F08F78E0006 for ; Wed, 3 Jun 2020 15:30:28 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id A8820181AC9BF for ; Wed, 3 Jun 2020 19:30:28 +0000 (UTC) X-FDA: 76888892136.15.power71_59806f2bbaa29 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 773711814AF8C for ; Wed, 3 Jun 2020 19:30:15 +0000 (UTC) X-HE-Tag: power71_59806f2bbaa29 X-Filterd-Recvd-Size: 5861 Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Jun 2020 19:30:14 +0000 (UTC) Received: by mail-qk1-f195.google.com with SMTP id q8so3414953qkm.12 for ; Wed, 03 Jun 2020 12:30:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=HvznEnzV+HRJyOZqdmIsBfXXgi2q857VNMTI/lWqqUk=; b=pYX8WSvGiUcHnZYmypMSDM3XS83clLrqguMbokWHtX1j+TNBGYFLPmZzzomEZpD+Ll efM0e+VKBYFLw40X+m/vJ5PXxNxMUAE7ytbux9ykJ/jl0wfyyYYQqwZ5LaZLkNRTvgKI 9yd/HorVj/PX3fOe1WQuODI6Wb5V1SKwb5ifwH6yPNkA9Y19m6yX24YAQcKQCt02cGL5 hQnn0sJ6kOlmhIOj+50IDoO+H+LyWiMMipoTrAzaeOLT2YU8xAej+tEq1vs/jJ7kxjU0 0lTCLl5noCiRkDgQPsaqJ+Xr+ZcAkEvqtwVji76GBKyksSZIQZBclExcxWmVNgf7C+oX ngyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=HvznEnzV+HRJyOZqdmIsBfXXgi2q857VNMTI/lWqqUk=; b=QBO4MYGX2YVkoy06MDoknjY7r1N49NsLHWbLwoHt1wJdRE4hj+ISxQADVTGvhRjtRw 8HA6INAxUxOgSezzvKGW/Q/mNLY+aHicDS7Eb4ojlGq64K+7Hjy7E8QM9QzwqN2+zxHR 9xFWA4l4/XcT95ASRDHhVsx2OU35SPHRJEzxiQKW+RaDK76uMZu6tdNuaQJK1dQqHLh1 7idKNWc0Dk4UpayRwqrbv7svfXVvMrh3l0zU7sNe9c36AqP1GFo28O6omoMjE3VpRrid 8QwuWvHZeghee2ug9/jKaAPA+mW3GUATaQVHnOjm4UNUHzLlFZS//oL2k0azcCTiFhGU PAEg== X-Gm-Message-State: AOAM533ty9rkN0da2HEr1RzzASkIFR4nDsTt2IqQ2qAX2XNnNmA9BVxh VjUueHripyNTwFrQUtM/WePD3A== X-Google-Smtp-Source: ABdhPJzEsmvZzzrBYgh28D8B3cuOKKWtUVzwpL/VgReBvZTpiSClwaI3lHYdVSOuaK6hBzbKDNBlHg== X-Received: by 2002:a37:be43:: with SMTP id o64mr1230241qkf.322.1591212614308; Wed, 03 Jun 2020 12:30:14 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id 80sm2229655qkl.116.2020.06.03.12.30.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jun 2020 12:30:13 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.93) (envelope-from ) id 1jgZ5Z-000ul9-4V; Wed, 03 Jun 2020 16:30:13 -0300 Date: Wed, 3 Jun 2020 16:30:13 -0300 From: Jason Gunthorpe To: James Bottomley Cc: Greg Kroah-Hartman , Matthew Wilcox , Wang Hai , cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, khlebnikov@yandex-team.ru, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: kobject_init_and_add is easy to misuse Message-ID: <20200603193013.GJ6578@ziepe.ca> References: <20200602121035.GL19604@bombadil.infradead.org> <1591111514.4253.32.camel@HansenPartnership.com> <20200602173603.GB3579519@kroah.com> <1591127656.16819.7.camel@HansenPartnership.com> <20200602200756.GA3933938@kroah.com> <1591134670.16819.18.camel@HansenPartnership.com> <20200603002205.GE6578@ziepe.ca> <1591207475.4462.41.camel@HansenPartnership.com> <20200603183650.GI6578@ziepe.ca> <1591210928.13983.24.camel@HansenPartnership.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1591210928.13983.24.camel@HansenPartnership.com> X-Rspamd-Queue-Id: 773711814AF8C X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Jun 03, 2020 at 12:02:08PM -0700, James Bottomley wrote: > On Wed, 2020-06-03 at 15:36 -0300, Jason Gunthorpe wrote: > > On Wed, Jun 03, 2020 at 11:04:35AM -0700, James Bottomley wrote: > > > On Tue, 2020-06-02 at 21:22 -0300, Jason Gunthorpe wrote: > > > > On Tue, Jun 02, 2020 at 02:51:10PM -0700, James Bottomley wrote: > > > > > > > > > My first thought was "what? I got suckered into creating a > > > > > patch", thanks ;-) But now I look, all the error paths do > > > > > unwind back to the initial state, so kfree() on error looks to > > > > > be completely correct. > > > > > > > > It doesn't fully unwind if the kobject is put into a kset, then > > > > another thread can get the kref during kset_find_obj() and > > > > kfree() won't wait for the kref to go to 0. It must use put. > > > > > > That does seem a bit contrived: the only failure > > > kobject_add_internal() can get after kobj_kset_join() is from > > > directory creation. If directory creation fails, no name appears > > > in sysfs and no event for the name is sent, how did another thread > > > get the name to pass in to kset_find_obj()? > > > > The other thread just guesses in a hostile way? > > > > Eg it looks like the iommu stuff just feeds in user data to > > kobj_kset_join(). > > Well, if we have to go down the rabbit hole this far, it turns out to > be fixable because of the state_in_sysfs flag: > > @@ -899,7 +903,8 @@ struct kobject *kset_find_obj(struct kset *kset, const char *name) > spin_lock(&kset->list_lock); > > list_for_each_entry(k, &kset->list, entry) { > - if (kobject_name(k) && !strcmp(kobject_name(k), name)) { > + if (kobject_name(k) && k->state_in_sysfs && > + !strcmp(kobject_name(k), name)) { > ret = kobject_get_unless_zero(k); > break; > } > > That would ensure the name can't be found until the sysfs directory > creation has succeeded, which would be the point from which > kobject_init_and_add() can't fail. Convoluted, and needs something on the store of state_in_sysfs too, but could work. It feels more robust to stick with the put though.. Jason