Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp127220iof; Sun, 5 Jun 2022 23:10:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwe0detX6qWpy4jqc+uN87doXblcyPxnffEAnOpWp+ZeUBZlV9wAVfP0eyztUqi645118xP X-Received: by 2002:a17:902:f647:b0:15f:3408:60e8 with SMTP id m7-20020a170902f64700b0015f340860e8mr22110995plg.82.1654495839822; Sun, 05 Jun 2022 23:10:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495839; cv=none; d=google.com; s=arc-20160816; b=t7qo8KRLyBFr6eCawkO8X3JPHZ3jP0I+aEFNtRNJzumlUdDxEOjM1E2QW9Nzcq4TBS Jf0pG/oNTWI2/ZIcOD7m1ZzyO1WVGIzhwIIitliHguY5Q+HjxqoIGkB9ixTmj1rPd4st wsmXdWIQf0JX2TXqvhyKb1q2S0y0IX+J5yAi98bdvefdMEoxvZyaixzvYJOEM0mplZK3 dxhvaDpynxs0XORinUq4Wx9ZixfshnwDU0Gg0kAn25uwfryP41IUrb6V6gg2z2gMiceB cKSSXm2TNUIapf/fvjiz1VYZqErIgIOn80aASr4x1GJwovfkerxd8oD2bAGInMPnTVKn M1mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=0UMpW2GQFjbsAh4Z5E0cvPjEi6lZODEcP2+hYnwC+bE=; b=eR+1LXh7fD6hTFGsFhnojwEIeL0ukB280ni5yy1d3ptGlVda/nMRJ1gYYANXniPHCQ tYbuGtcFqavIQN1SqXjOfwgpsjlxskZWI4Ll2UgyQRFzENQ1t/P1beM4mjPG3YwXyK4W j+CKOGAz9ON7aaaIecCS1gvu5jemZcVxzHr0M2YrTfLYfowqLwDo3lZbk3IsLMeIxqR5 ZhaEZGN2OWjOCM8ZwW412XO90l1mnyPnOgz0DFtnqsNgJbyRuBwqpOH0dAo9TYwov57C wWnsHMvPBt5KkP5jx5eWrQ4sDuCg6w1wAefzxGHW0FGcfXQUZMPvrfTJoNRv+F+/aW2v AMZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zkYz2hGQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id i10-20020a636d0a000000b003fc48964390si6735500pgc.613.2022.06.05.23.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:10:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zkYz2hGQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A2E26C50B0; Sun, 5 Jun 2022 22:01:55 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243624AbiFCKTd (ORCPT + 99 others); Fri, 3 Jun 2022 06:19:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243620AbiFCKTa (ORCPT ); Fri, 3 Jun 2022 06:19:30 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7B683B3C5 for ; Fri, 3 Jun 2022 03:19:27 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id i19so5244873qvu.13 for ; Fri, 03 Jun 2022 03:19:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0UMpW2GQFjbsAh4Z5E0cvPjEi6lZODEcP2+hYnwC+bE=; b=zkYz2hGQ1rahMqWa937cH83pVnLOTqmdXNQFV1d1uS6u6aMj6SPNQ9yOFFN00ysLMX jUuAr5G7K4WTsp3BRQ/W+wjUDU3Dd0I0+Ku7oPNjCuEGNCC0Ww7/T88ekjo7m8hkooCW wZjrIspW94cmLUoBhvkgkad+qwItBz0RGN2yAH9oxYP1JhVS2lT+VSN2HdfejhqjN+sP VxYoY7vUAdFgjk8lpGRJ9ntj+LeaIt5Ri18B5vb3zKIVQ3Se/4O7f/hKLIJl6Qj3nHnT N1RcbTDKcuGHQzkJbstZ9TgqQiLmHFFS8rpdNWxYzMk5r2oy4EqRV1nLAUM2gu4ezrOb XicQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0UMpW2GQFjbsAh4Z5E0cvPjEi6lZODEcP2+hYnwC+bE=; b=BBn6KY89HzVRqBu+pUxjHT20Giua6RLfrfTf8R30eIIgarQi/9p8PfF32de6PpXZb2 NapzaMVTWXQWoLees/bmJt68EBFpnMNvHHfIPaEJfzJRlHp79I6F5zMf0ssb4wyqDqKd CQHZaoDPklHbyBbzIhHvtgCDg6Hzm8yjPMDSFViyi7DCm1aaV2BGgZNrQeTu8TO2ILiZ KiCLEOa+gwzGQMftOoZ+yyBH70tLvBatxf7qAvTr6Nt3AKDbFCChDgpa+qHF1dpQs2sh qbvtbxv9ykx3DTLmojrhYBhaTvUs66LW4/VoxBT7/xh11tvAz0deW5rK3ZMwFB31Kmz+ vk0A== X-Gm-Message-State: AOAM532ZvyqlaH3D4D3nYQ+6dp37kBSz4FQ4jm4XJkUrbWI/bjMhZ6rT dSVTXCniaA1SIVTvRF1yQrDCkBJaRSUgi9UiPHYi3g== X-Received: by 2002:ad4:5b81:0:b0:465:ded8:780 with SMTP id 1-20020ad45b81000000b00465ded80780mr8480006qvp.119.1654251567078; Fri, 03 Jun 2022 03:19:27 -0700 (PDT) MIME-Version: 1.0 References: <20220510192944.2408515-1-dianders@chromium.org> <20220510122726.v3.3.Iba4b9bf6c7a1ee5ea2835ad7bd5eaf84d7688520@changeid> <20220521091751.opeiqbmc5c2okdq6@houat> <20220603082139.sfdxb5ndwpvlhklh@penduick> In-Reply-To: <20220603082139.sfdxb5ndwpvlhklh@penduick> From: Dmitry Baryshkov Date: Fri, 3 Jun 2022 13:19:16 +0300 Message-ID: Subject: Re: [PATCH v3 3/4] drm/bridge: Add devm_drm_bridge_add() To: Maxime Ripard Cc: Doug Anderson , dri-devel , Hsin-Yi Wang , Abhinav Kumar , Philip Chen , Sankeerth Billakanti , Robert Foss , freedreno , linux-arm-msm , Stephen Boyd , Daniel Vetter , David Airlie , Maarten Lankhorst , Thomas Zimmermann , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 Fri, 3 Jun 2022 at 11:21, Maxime Ripard wrote: > > On Tue, May 31, 2022 at 02:06:34PM -0700, Doug Anderson wrote: > > On Mon, May 23, 2022 at 10:00 AM Doug Anderson wrote: > > > On Sat, May 21, 2022 at 2:17 AM Maxime Ripard wrote: > > > > On Tue, May 10, 2022 at 12:29:43PM -0700, Douglas Anderson wrote: > > > > > This adds a devm managed version of drm_bridge_add(). Like other > > > > > "devm" function listed in drm_bridge.h, this function takes an > > > > > explicit "dev" to use for the lifetime management. A few notes: > > > > > * In general we have a "struct device" for bridges that makes a good > > > > > candidate for where the lifetime matches exactly what we want. > > > > > * The "bridge->dev->dev" device appears to be the encoder > > > > > device. That's not the right device to use for lifetime management. > > > > > > > > > > Suggested-by: Dmitry Baryshkov > > > > > Signed-off-by: Douglas Anderson > > > > > > > > If we are to introduce more managed helpers, I think it'd be wiser to > > > > introduce them as DRM-managed, and not device managed. > > > > > > > > Otherwise, you'll end up in a weird state when a device has been removed > > > > but the DRM device is still around. > > > > > > I'm kinda confused. In this case there is no DRM device for the bridge > > > and, as per my CL description, "bridge-dev->dev" appears to be the > > > encoder device. I wasn't personally involved in discussions about it, > > > but I was under the impression that this was expected / normal. Thus > > > we can't make this DRM-managed. > > > > Since I didn't hear a reply, > > Gah, I replied but it looks like somehow it never reached the ML... > > Here was my original reply: > > > > > This adds a devm managed version of drm_bridge_add(). Like other > > > > "devm" function listed in drm_bridge.h, this function takes an > > > > explicit "dev" to use for the lifetime management. A few notes: > > > > * In general we have a "struct device" for bridges that makes a good > > > > candidate for where the lifetime matches exactly what we want. > > > > * The "bridge->dev->dev" device appears to be the encoder > > > > device. That's not the right device to use for lifetime management. > > > > > > > > Suggested-by: Dmitry Baryshkov > > > > Signed-off-by: Douglas Anderson > > > > > > If we are to introduce more managed helpers, I think it'd be wiser to > > > introduce them as DRM-managed, and not device managed. > > > > > > Otherwise, you'll end up in a weird state when a device has been removed > > > but the DRM device is still around. > >=20 > > I'm kinda confused. In this case there is no DRM device for the bridge > > and, as per my CL description, "bridge-dev->dev" appears to be the > > encoder device. > > bridge->dev seems right though? > > > I wasn't personally involved in discussions about it, but I was under > > the impression that this was expected / normal. Thus we can't make > > this DRM-managed. > > Still, I don't think devm is the right solution to this either. > > The underlying issue is two-fold: > > - Encoders can have a pointer to a bridge through of_drm_find_bridge > or similar. However, bridges are traditionally tied to their device > lifetime (by calling drm_bridge_add in probe, and drm_bridge_remove > in remove). Encoders will typically be tied to the DRM device > however, and that one sticks around until the last application > closes it. We can thus very easily end up with a dangling pointer, > and a use-after-free. > > - It's not the case yet, but it doesn't seem far fetch to expose > properties of bridges to the userspace. In that case, the userspace > would be likely to still hold references to objects that aren't > there anymore when the bridge is gone. > > The first is obviously a larger concern, but if we can find a solution > that would accomodate the second it would be great. > > As far as I can see, we should fix in two steps: > > - in drm_bridge_attach, we should add a device-managed call that will > unregister the main DRM device. We don't allow to probe the main DRM > device when the bridge isn't there yet in most case, so it makes > sense to remove it once the bridge is no longer there as well. The problem is that I do not see a good way to unregister the main DRM device outside of it's driver code. > > - When the DRM device is removed, have the core cleanup any bridge > registered. That will remove the need to have drm_bridge_remove in > the first place. > > > I'll assume that my response addressed your concerns. Assuming I get > > reviews for the other two patches in this series I'll plan to land > > this with Dmitry's review. > > I still don't think it's a good idea to merge it. It gives an illusion > of being safe, but it's really far from it. It is more of removing the boilerplate code spread over all the drivers rather than about particular safety. I'd propose to land devm_drm_bridge_add (and deprecate calling drm_bridge_remove from the bridge driver at some point) and work on the whole drm_device <-> drm_bridge problem in the meantime. -- With best wishes Dmitry