Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2932562yba; Fri, 10 May 2019 23:37:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+1WghJh/mwHSJXRe2bJRBlIrb0KgmSJmLkXq+pHmAenYAAs0DTxR03R3GnHz1YKceqw/J X-Received: by 2002:aa7:99dd:: with SMTP id v29mr20348519pfi.252.1557556661139; Fri, 10 May 2019 23:37:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557556661; cv=none; d=google.com; s=arc-20160816; b=t66JRptQJupsiH65NR0gHCpg0i5P77MB5dIjoYz5PIOmy+iSvZdoAIRJgsA5spR3H3 5UK1O91rzqgjMHUI5ey50va4Z1axv//AI/XcAJx+mJ6buY+Hrb3lgV5K4q6MnAFXuZYm GgH2KLfhagunQdgmMhWoAMAUWskSRjumj5PPuOvdOd/Esu46rUJPWrTOqZdn0ELqk56X sweUr7aiiHGt+CfCbDIhRMxk+m0ObGcsa4oxRkAnLn69GQPFvyBKSFl+nO+i4CLs9vaD 7dWJLDIyy3KKlqYje+JO4VZFXtnkHcwLwAuxt+rGDdZKMCBH/dzS8t4OYQEn4zWGF2MA YEjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:cc:to:from:date:references :in-reply-to:message-id:mime-version:user-agent:dkim-signature :dkim-signature; bh=urVdJJyBFXeY2/ER+RB7IsvUiLq/kfApyA+UFACJ3qQ=; b=ERITkNyn5gkHz8+q1XVz8VFAQP6A9Xif08sDotVZ3GpDO+PoDcbf+azvk5JYDHzMeP clKiZ9ty97yV92ABvKQFOcGPXOCLuVN59kKvGveTriG4xOQkiAuS1c1Xh1NOBgChYRJm bmnad6Ay1r5pvjXPyXyMoF+1fgAGeP/28tSFI+h9vUTEQ+ZQFk93sfshu22jvEgw7yFK g9KnukShz44YaWSAa3/F+gnDigO47Qpk8uFSdLTK7FrCqLs+lrROM/k5tiVvPqsfopOB cFZeqpNySX3Q0KoNSuXnOzLsbFtkXXiUCQrZQyD8dslmr6gz1JJg5sJxM1Cst504MLg4 frag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tobin.cc header.s=fm3 header.b=BVXw6bzp; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=rDyd390K; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j71si11727463pfc.127.2019.05.10.23.37.24; Fri, 10 May 2019 23:37:41 -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=pass header.i=@tobin.cc header.s=fm3 header.b=BVXw6bzp; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=rDyd390K; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728322AbfEKGdA (ORCPT + 99 others); Sat, 11 May 2019 02:33:00 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:55481 "EHLO out2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbfEKGdA (ORCPT ); Sat, 11 May 2019 02:33:00 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 022E821E44; Sat, 11 May 2019 02:32:59 -0400 (EDT) Received: from imap37 ([10.202.2.87]) by compute5.internal (MEProxy); Sat, 11 May 2019 02:32:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tobin.cc; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type; s=fm3; bh=urVdJJyBFXeY2/ER+RB7IsvUiLq/kfA pyA+UFACJ3qQ=; b=BVXw6bzp908KUqhjY+XV7cr7cONQU9BDoqIAawsxRGXFhAy ajielz/saTIYTHRjMMzS/yoY3O9aT/svpND+YNewT2ty6ENF6OoYOovUQlvb1OnP JBgDT4D2BfT5cNMzI6fwlYax2LWZuzxW6paRgpEgfc70YeDoyOJbxw7QCISoHDYD 2SrH/isHrwE38HArpKrjUsD8Vt4wTwu4ZAFwqqGUcRaqyLLcFaDq10pjzAV6la+X 4J8qAW2AqXnLs+mPFyPLZSFQqRtJwwNxDJHaNC+sNKQw3qsTy1l1AnEdcUfaQ4yo pGR5w2NL8krMwTOE9hdeuj8qZs1JgO+X7hWb6nA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=urVdJJ yBFXeY2/ER+RB7IsvUiLq/kfApyA+UFACJ3qQ=; b=rDyd390KW5fIk+qSnIygNh Gj9p/62GIv/M+jHVVJlrw4hpGIeR8eN1X1RkZ81Anh0pxs7ZOyc3mxbHecZFpT5r ZkOv/UIVzG4B20aXRN1ne+wPyWjrJzMRfLo9desvZ/lwdJxdhr9yNzr/I3JVtMgn Na8zY2QIGfVHVypafhAJ6a1iQ3kOFZpwnz8JCRHPRfJZuYuSBxWQszKPstjDQN26 otKrmINI+/60tg6YFH8oEoZrDYfUXs1QDCxH33bZnTkwX4BbdqQrV8HcOvVsNGXR KyPWtv/cKroDp1bAU9uoiQyv4a89j+D/KBeXrUz/gbUrNLs7+2A0q0sfCl1IGUng == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrkeelgdduudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdludehmdenucfjughrpefofgggkfgjfhffhffvufgtsehttdertder redtnecuhfhrohhmpedfvfhosghinhcuvedrucfjrghrughinhhgfdcuoehmvgesthhosg hinhdrtggtqeenucfrrghrrghmpehmrghilhhfrhhomhepmhgvsehtohgsihhnrdgttgen ucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 22D9EDEC25; Sat, 11 May 2019 02:32:58 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.6-449-gfb3fc5a-fmstable-20190430v1 Mime-Version: 1.0 Message-Id: <1aa4ce74-b0a5-474b-95ad-7402a4a2710a@www.fastmail.com> In-Reply-To: <20190510094025.wiw37baomizk6bip@pathway.suse.cz> References: <20190430233803.GB10777@eros.localdomain> <20190510023538.GA10356@eros.localdomain> <20190510094025.wiw37baomizk6bip@pathway.suse.cz> Date: Sat, 11 May 2019 02:32:19 -0400 From: "Tobin C. Harding" To: "Petr Mladek" Cc: "Rafael J. Wysocki" , "Greg Kroah-Hartman" , "Tyrel Datwyler" , "Andy Shevchenko" , "Miroslav Benes" , "Viresh Kumar" , "Linux Kernel Mailing List" Subject: Re: kobject_init_and_add() confusion Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 10, 2019, at 19:40, Petr Mladek wrote: > On Fri 2019-05-10 12:35:38, Tobin C. Harding wrote: > > On Wed, May 01, 2019 at 09:54:16AM +0200, Rafael J. Wysocki wrote: > > > On Wed, May 1, 2019 at 1:38 AM Tobin C. Harding wrote: > > > > TODO > > > > ---- > > > > > > > > - Fix all the callsites to kobject_init_and_add() > > > > - Further clarify the function docstring for kobject_init_and_add() [perhaps] > > > > - Add a section to Documentation/kobject.txt [optional] > > > > - Add a sample usage file under samples/kobject [optional] > > > > > > The plan sounds good to me, but there is one thing to note IMO: > > > kobject_cleanup() invokes the ->release() callback for the ktype, so > > > these callbacks need to be able to cope with kobjects after a failing > > > kobject_add() which may not be entirely obvious to developers > > > introducing them ATM. > > > > It has taken a while for this to soak in. This is actually quite an > > insidious issue. If I give an example and perhaps we can come to a > > solution. This example is based on the code (and assumptions) in > > mm/slub.c > > > > If a developer has an object that they wish to add to sysfs they go > > ahead and embed a kobject in it. Correctly set up a ktype including > > release function that just frees the object (using container of). Now > > assume that the object is already set up and in use when we go to set up > > the sysfs entry. > > It would say that this is a bad design. I see the creation of the sysfs > entry as part of the initialization. The object should not be made > usable before it is fully initialized. It may be a case of my lack of understanding of object lifecycles here and not bad design, if as you say creation of sysfs is always part of initialisation then the problem I describe above should not exist (and it may well not, assumptions behind code are hard to grok). > > If kobject_init_and_add() fails and we correctly call > > kobject_put() the containing object will be free'd. Yet the calling > > code may not be done with the object, more to the point just because > > sysfs setup fails the object is now unusable. Besides the interesting > > theoretical discussion this means we cannot just go and willy-nilly add > > calls to kobject_put() in the error path of kobject_init_and_add() if > > the original code was not written under the assumption that the release > > method could be called during the error path (I have found 2 places at > > least where behaviour of calling the release method is non-trivial to > > ascertain). > > kobject usage is complicated and it is easy to make it wrong. I think > that this is motivation to improve the documentation and adding > good examples. Cool, I did work on adding your example from last week into samples/kobject but I wasn't able to come up with anything that I was totally happy with. Hard to use API needs minimal concise correct examples right, I'm going to keep at that as I learn more from seeing/patching current kobject code. > > I guess, as Greg said, its just a matter that reference counting within > > the kernel is a hard problem. So we fix the easy ones and then look a > > bit harder at the hard ones ... > > The people working on the affected subsystem should be able to help. > They might have misunderstood kobjects. But they should be more > familiar with the other dependencies. Sure thing. > Thanks for working on it. Things that bend ones brain are the funnest to work on ;) Cheers, Tobin.