Re: [PATCH] wfx: use container_of() to get vif

From: Dan Carpenter
Date: Wed May 04 2022 - 05:36:07 EST


On Mon, May 02, 2022 at 02:10:07PM -0400, Jaehee wrote:
> On Wed, Apr 20, 2022 at 7:58 AM Kalle Valo <kvalo@xxxxxxxxxx> wrote:
> >
> > Jaehee Park <jhpark1013@xxxxxxxxx> writes:
> >
> > > Currently, upon virtual interface creation, wfx_add_interface() stores
> > > a reference to the corresponding struct ieee80211_vif in private data,
> > > for later usage. This is not needed when using the container_of
> > > construct. This construct already has all the info it needs to retrieve
> > > the reference to the corresponding struct from the offset that is
> > > already available, inherent in container_of(), between its type and
> > > member inputs (struct ieee80211_vif and drv_priv, respectively).
> > > Remove vif (which was previously storing the reference to the struct
> > > ieee80211_vif) from the struct wfx_vif, define a function
> > > wvif_to_vif(wvif) for container_of(), and replace all wvif->vif with
> > > the newly defined container_of construct.
> > >
> > > Signed-off-by: Jaehee Park <jhpark1013@xxxxxxxxx>
> >
> > [...]
> >
> > > +static inline struct ieee80211_vif *wvif_to_vif(struct wfx_vif *wvif)
> > > +{
> > > + return container_of((void *)wvif, struct ieee80211_vif, drv_priv);
> > > +}
> >
> > Why the void pointer cast? Avoid casts as much possible.
> >
>
> Hi Kalle,
>
> Sorry for the delay in getting back to you about why the void pointer
> cast was used.
>
> In essence, I'm taking private data with a driver-specific pointer
> and that needs to be resolved back to a generic pointer.
>
> The private data (drv_priv) is declared as a generic u8 array in struct
> ieee80211_vif, but wvif is a more specific type.
>
> I wanted to also point to existing, reasonable examples such as:
> static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
> {
> struct iwl_mvm *mvm;
> struct iwl_mvm_vif *mvmvif;
> struct ieee80211_vif *vif;
>
> mvmvif = container_of(wk, struct iwl_mvm_vif,
> uapsd_nonagg_detected_wk.work);
> vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
>
> in drivers/net/wireless$ less intel/iwlwifi/mvm/utils.c, which does the
> same thing.
>
> There are fifteen of them throughout:

The cast is fine, but this email is frustrating.

It sounds like you are saying that you copied it from other code and
that's not a good answer... :/ It's easiest if you just copy and paste
the build error and we can figure out why the cast is need for our
selves...

drivers/net/wireless/silabs/wfx/data_rx.c: In function ‘wvif_to_vif’:
./include/linux/build_bug.h:78:41: error: static assertion failed: "pointer type mismatch in container_of()"
78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
| ^~~~~~~~~~~~~~
./include/linux/build_bug.h:77:34: note: in expansion of macro ‘__static_assert’
77 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
| ^~~~~~~~~~~~~~~
./include/linux/container_of.h:19:9: note: in expansion of macro ‘static_assert’
19 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \
| ^~~~~~~~~~~~~
drivers/net/wireless/silabs/wfx/data_rx.c:20:16: note: in expansion of macro ‘container_of’
20 | return container_of(wvif, struct ieee80211_vif, drv_priv);
| ^~~~~~~~~~~~

regards,
dan carpenter