From: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
To: "jassisinghbrar@gmail.com" <jassisinghbrar@gmail.com>,
"Jjian Zhou (周建)" <Jjian.Zhou@mediatek.com>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-mediatek@lists.infradead.org"
<linux-mediatek@lists.infradead.org>,
"wenst@chromium.org" <wenst@chromium.org>,
"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
"conor+dt@kernel.org" <conor+dt@kernel.org>,
Project_Global_Chrome_Upstream_Group
<Project_Global_Chrome_Upstream_Group@mediatek.com>,
"robh@kernel.org" <robh@kernel.org>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>,
"krzk+dt@kernel.org" <krzk+dt@kernel.org>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>
Subject: Re: [PATCH v5 2/2] mailbox: mediatek: Add mtk-vcp-mailbox driver
Date: Tue, 23 Sep 2025 11:06:50 +0200 [thread overview]
Message-ID: <2382077.ElGaqSPkdT@workhorse> (raw)
In-Reply-To: <eb9b0ea70abb09f45c50e88b6221948ae09be6f7.camel@mediatek.com>
On Tuesday, 23 September 2025 04:35:59 Central European Summer Time Jjian Zhou (周建) wrote:
> On Mon, 2025-09-22 at 15:10 +0200, Nicolas Frattaroli wrote:
> > External email : Please do not click links or open attachments until
> > you have verified the sender or the content.
> >
> >
> > On Monday, 22 September 2025 09:17:27 Central European Summer Time
> > Jjian Zhou (周建) wrote:
> > > On Sat, 2025-09-20 at 23:02 -0500, Jassi Brar wrote:
> > > > External email : Please do not click links or open attachments
> > > > until
> > > > you have verified the sender or the content.
> > > >
> > > >
> > > > On Fri, Sep 19, 2025 at 2:02 PM Nicolas Frattaroli
> > > > <nicolas.frattaroli@collabora.com> wrote:
> > > > >
> > > > > On Friday, 19 September 2025 18:32:12 Central European Summer
> > > > > Time
> > > > > Jassi Brar wrote:
> > > > > > On Fri, Sep 19, 2025 at 3:31 AM Chen-Yu Tsai <
> > > > > > wenst@chromium.org>
> > > > > > wrote:
> > > > > > >
> > > > > > > On Fri, Sep 19, 2025 at 7:50 AM Jassi Brar <
> > > > > > > jassisinghbrar@gmail.com> wrote:
> > > > > > > >
> > > > > > > > On Thu, Aug 21, 2025 at 9:12 PM Jjian Zhou <
> > > > > > > > jjian.zhou@mediatek.com> wrote:
> > > > > > > >
> > > > > > > > .....
> > > > > > > >
> > > > > > > > > +#include <linux/module.h>
> > > > > > > > > +#include <linux/of.h>
> > > > > > > > > +#include <linux/platform_device.h>
> > > > > > > > > +#include <linux/slab.h>
> > > > > > > > > +
> > > > > > > > > +struct mtk_vcp_mbox_priv {
> > > > > > > >
> > > > > > > > Maybe 'mtk_vcp_mbox' is a more appropriate name ?
> > > > > > > >
> > > > > > > > > + void __iomem *base;
> > > > > > > > > + struct device *dev;
> > > > > > > > > + struct mbox_controller mbox;
> > > > > > > > > + const struct mtk_vcp_mbox_cfg *cfg;
> > > > > > > > > + struct mtk_ipi_info ipi_recv;
> > > > > > > >
> > > > > > > > Maybe also have "struct mbox_chan chan[1]; " so that you
> > > > > > > > don't have to
> > > > > > > > allocate one during the probe.
> > > > > > > > Also if you have "struct mbox_controller mbox;" as the
> > > > > > > > first
> > > > > > > > member,
> > > > > > > > you could simply typecast that to get this structure.
> > > > > > > > Something like "struct mpfs_mbox" in mailbox-mpfs.c
> > > > > > >
> > > > > > > I read somewhere that this way of subclassing is not
> > > > > > > recommended.
> > > > > > > Instead the base class should explicitly not be the first
> > > > > > > member.
> > > > > > > And then container_of() should be used.
> > > > > > >
> > > > > > > I don't remember where I read this though. But I think the
> > > > > > > explicit
> > > > > > > container_of() is easier for understanding the intent.
> > > > > > >
> > > > > >
> > > > > > And how does container_of() work ? :)
> > > > > > typcasting the first member to its parent is the simplest
> > > > > > form of
> > > > > > container_of.
> > > > > >
> > > > > > -j
> > > > > >
> > > > > >
> > > > >
> > > > > Which is why it's completely equivalent and since code is
> > > > > supposed
> > > > > to communicate meaning to humans, container_of should be used.
> > > > >
> > > >
> > > > Nobody is suggesting typecasting cfg, dev or anything else.
> > > > Typecasting between mailbox controllers is fine and arguably
> > > > easier
> > > > on
> > > > the eyes than using a container_of.
> > > >
> > > > -j
> > >
> > > OK. How about:
> > > struct mtk_vcp_mbox *priv = (struct mtk_vcp_mbox *)chan-
> > > > con_priv;
> > >
> > > Thanks.
> > >
> >
> > An explicit cast would be worse, as at that point you're telling
> > C to completely ignore any semblance of a type system it has.
> >
> >
> >
> struct mtk_vcp_mbox *priv;
> priv->dev = dev;
> priv->chans[0].con_priv = priv;
> The type of con_priv is "void *".
> Would the conversion mentioned above also have the issue you mentioned?
>
> Thanks.
>
No, in that case the cast is implicit. While void pointers do
subvert the type system, they are needed in this case because
the con_priv member needs to point at structs of any type.
The problem is that when you do something like
struct apple *a = something;
struct orange *o = (struct orange *)a;
then if the two structs (apple and orange) are incompatible,
the compiler won't even yell at you, because you're explicitly
casting.
With an implicit cast:
struct apple *a = something;
struct orange *o = a;
the compiler will tell you if you're doing something wrong.
Here's a userspace code example to illustrate the point:
#include <stdio.h>
struct apple {
const char *name;
unsigned int weight;
};
struct orange {
int x;
int y;
int z;
};
int main(int argc, char** argv)
{
struct apple a = {"Granny Smith", 200};
// won't compile, good!
/* struct orange *o = &a; */
// will compile, bad!
struct orange *o = (struct orange *)&a;
printf("%d\n", o->x);
return 0;
}
If you comment out the second struct orange line and uncomment the
first, then you'll get a compilation error, which is what we want
because the two structs are incompatible and we don't want the
assignment to work in this case, as that would be a bug.
The second struct orange line always compiles, even though the two
structs are incompatible, and will cause nonsense to be printed.
I hope this illustrates the point I was trying to make, which is
that explicit casts make it harder to find issues because they
force the language to simply accept the cast rather than give us
a compilation error when something nonsensical is being done.
Kind regards,
Nicolas Frattaroli
next prev parent reply other threads:[~2025-09-23 9:07 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-22 2:12 [PATCH v5 0/2] add VCP mailbox driver Jjian Zhou
2025-08-22 2:12 ` [PATCH v5 1/2] dt-bindings: mailbox: mediatek,mt8196-vcp-mbox: add mtk vcp-mbox document Jjian Zhou
2025-08-22 5:43 ` Chen-Yu Tsai
2025-08-22 7:58 ` Krzysztof Kozlowski
2025-08-22 2:12 ` [PATCH v5 2/2] mailbox: mediatek: Add mtk-vcp-mailbox driver Jjian Zhou
2025-08-22 5:48 ` Chen-Yu Tsai
2025-09-22 7:22 ` Jjian Zhou (周建)
2025-09-18 23:50 ` Jassi Brar
2025-09-19 8:31 ` Chen-Yu Tsai
2025-09-19 16:32 ` Jassi Brar
2025-09-19 19:02 ` Nicolas Frattaroli
2025-09-21 4:02 ` Jassi Brar
2025-09-22 7:17 ` Jjian Zhou (周建)
2025-09-22 13:10 ` Nicolas Frattaroli
2025-09-23 2:35 ` Jjian Zhou (周建)
2025-09-23 9:06 ` Nicolas Frattaroli [this message]
2025-09-23 11:46 ` Jjian Zhou (周建)
2025-09-23 13:16 ` Jassi Brar
2025-09-24 1:42 ` Jjian Zhou (周建)
2025-09-19 9:16 ` Jjian Zhou (周建)
2025-09-23 3:08 ` Jjian Zhou (周建)
2025-09-23 6:46 ` Jjian Zhou (周建)
2025-09-23 13:11 ` Jassi Brar
2025-09-24 2:21 ` Jjian Zhou (周建)
2025-09-09 6:13 ` [PATCH v5 0/2] add VCP mailbox driver Jjian Zhou (周建)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2382077.ElGaqSPkdT@workhorse \
--to=nicolas.frattaroli@collabora.com \
--cc=Jjian.Zhou@mediatek.com \
--cc=Project_Global_Chrome_Upstream_Group@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jassisinghbrar@gmail.com \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=robh@kernel.org \
--cc=wenst@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.