Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2303556rwb; Thu, 17 Nov 2022 09:02:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf4JSdMug2zv7OqGgqNHYYt5R8ig4enGeZYdopHkFYg6AdFOj9T3/5xjzqlOLTtDyh0nZBhZ X-Received: by 2002:a17:902:e5cd:b0:188:f570:7bb6 with SMTP id u13-20020a170902e5cd00b00188f5707bb6mr1982171plf.74.1668704533012; Thu, 17 Nov 2022 09:02:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668704533; cv=none; d=google.com; s=arc-20160816; b=N6So4jRgVUY2RC7IyEmQA7yDfUHt+eAcBKdkm8EIUgqiPUxpQHU8r9Y+aJqDGrGxdq S07PtRebAah/ZtY2vrggxdxnMP2CUm1mPS2rF+ZWjcccX0tYLIC15kgE3GssdgfsRZ6U uP1s9BhyhtmHne+ErjiGUuH80H/a4zRRVZ3T/16Guoqt6VdJAohCG/bHbr8Y7FRhyl+c wm/d/H9xSCyCpyH2EsP/nzEe9P2a6kEeRZBtIpO8a2RBd1Iz+gCp1MyYuyJH7bKwrIjF sBIhkTlvRe00iLcC44PVzfMUzw4Yp6a+QyLd+fr/50BOFsdlgPHtilR+PLy6l6mzztym ddVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=bKTngFRTlf7+davIfNkj1SHm8acZ1IPDgfWb80w02HA=; b=baIa+AjiPoiVedBBK2Jh1Z8haSzI+tSfcRrIy7OTr5SBHkjn5dR68jdNESxAf3z8lq yxBVSLkrjorsq+EAcHaoabsvY6TqBGVQgPYMXVCwqarqL7mqYpPPzXNj5PPUDjxxDiwV DrdpQuqsROI9Y81orkzWrT/FUWqHh6C9pP4NLsv0sKl959mgvmgrO3dgGDdfC5AGOTa+ eJJJ5v7suzugeK/Rsdy5l86pxm5WrgGwLpsBoDbDp1ZUiqRNPMfH5upu2wzGmHzXZl6w F1p/oownrYswKsxfXOv2rPB6Zf6DsqbXz+3SaKyh90A+UMbMLMMSzJw/FVejQUDFnOYF ODew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j24-20020a63cf18000000b004624e24b4e9si1416826pgg.543.2022.11.17.09.01.50; Thu, 17 Nov 2022 09:02:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231377AbiKQQsH (ORCPT + 93 others); Thu, 17 Nov 2022 11:48:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234683AbiKQQsF (ORCPT ); Thu, 17 Nov 2022 11:48:05 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 0A47540903 for ; Thu, 17 Nov 2022 08:48:00 -0800 (PST) Received: (qmail 5505 invoked by uid 1000); 17 Nov 2022 11:47:59 -0500 Date: Thu, 17 Nov 2022 11:47:59 -0500 From: Alan Stern To: Lee Jones Cc: Greg KH , balbi@kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: Re: [PATCH 1/1] usb: gadget: f_hid: Conduct proper refcounting on shared f_hidg pointer Message-ID: References: <20221117120813.1257583-1-lee@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 17, 2022 at 01:46:26PM +0000, Lee Jones wrote: > On Thu, 17 Nov 2022, Greg KH wrote: > > > On Thu, Nov 17, 2022 at 12:08:13PM +0000, Lee Jones wrote: > > > +static inline bool f_hidg_is_open(struct f_hidg *hidg) > > > +{ > > > + return !!kref_read(&hidg->cdev.kobj.kref); > > > +} > > > > Ick, sorry, no, that's not going to work and is not allowed at all. > > That's some major layering violations there, AND it can change after you > > get the value as well. > > This cdev belongs solely to this driver. Hence the *.*.* and not > *->*->*. What is preventing us from reading our own data? If we > cannot do this directly, can I create an API to do it 'officially'? > > I do, however, appreciate that a little locking wouldn't go amiss. > > If this solution is not acceptable either, then we're left up the > creak without a paddle. The rules you've communicated are not > compatible with each other. > > Rule 1: Only one item in a data structure can reference count. > > Due to the embedded cdev struct, this rules out my first solution of > giving f_hidg its own kref so that it can conduct its own life-time > management. > > A potential option to satisfy this rule would be to remove the cdev > attribute and create its data dynamically instead. However, the > staticness of cdev is used to obtain f_hidg (with container_of()) in > the character device handling component, so it cannot be removed. You have not understood this rule correctly. Only one item in a data structure can hold a reference count _for that structure_. But several items in a structure can hold reference counts for themselves. So for example, you could put a kref in f_hidg which would hold the reference count for the f_hidg structure, while at the same time including an embedded cdev with its own reference counter. The point is that the refcount in the embedded cdev refers to the lifetime of the cdev, not the lifetime of the f_hidg. To make this work properly, you have to do two additional things: When the cdev's refcount is initialized, increment the kref in f_hidg. When the cdev's refcount drops to 0, decrement the kref (and release f_hidg if the kref hits 0). Alan Stern > Rule 2: Reading the present refcount causes a laying violation > > So we're essentially saying, if data is already being reference > counted, you have to use the present implementation instead of adding > additional counts, but there is no way to actually do that, which > kind of puts us at stalemate. > > Since this is a genuine issue, doing anything is not really an option > either. So where do we go from here? > > -- > Lee Jones [李琼斯]