Simple mutex_destroy question

Lucas Tanure tanure at linux.com
Thu Nov 20 05:27:06 EST 2025


On Wed, Nov 19, 2025 at 4:27 PM Billie Alsup (balsup) <balsup at cisco.com> wrote:
>
> Since you're using devm_kzalloc, why not simply use devm_mutex_init and not worry about it?  Then you can remove your explicit mutex_destroy call.

I am developing drivers for Android 13 with kernel 5.10/5.4, which
doesn't have devm_mutex_init.
So, mutex_destroy is only needed for debugging locks? I see a few
drivers don't have this call in their remove functions or in fail
probe paths.

>
>
> ________________________________________
> From: Lucas Tanure <tanure at linux.com>
> Sent: Friday, November 14, 2025 2:42 AM
> To: kernelnewbies <kernelnewbies at kernelnewbies.org>
> Subject: Simple mutex_destroy question
>
> Hi,
>
> Do I need to call mutex_destroy in a failed probe exit, or in the remove
> module function, if the mutex is located in memory allocated with
> devm_kzalloc and family?
> Like:
> static int mychip_i2c_probe(struct i2c_client *client)
> {
>      struct chip *chip;
>      int ret;
>
>      chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
>      if (!chip)
>          return -ENOMEM;
>
>      mutex_init(&chip->lock);
>      i2c_set_clientdata(client, chip);
>
>      /* Do my chip stuf
>      */
>
>
>      return ret;
>
> probe_fail:
>      mutex_destroy(&chip->lock);
>      return ret;
> }
>
> I understand that mutex_destroy will only invalidate the memory, but as
> I am freeing this is not necessary?
>
> Thanks
> Lucas Tanure
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



More information about the Kernelnewbies mailing list