Re: Simple mutex_destroy question

"Billie Alsup (balsup)" <[email protected]> Wed, 19 Nov 2025 16:27:40 +0000
Newsgroups org.kernelnewbies.kernelnewbies
Message-ID <BY5PR11MB4244B8D058A56724967ED4C1D9D7A@BY5PR11MB4244.namprd11.prod.outlook.com>
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.


________________________________________
From:=A0Lucas Tanure <[email protected]>
Sent:=A0Friday, November 14, 2025 2:42 AM
To:=A0kernelnewbies <[email protected]>
Subject:=A0Simple mutex_destroy question
=A0
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)
{
=A0=A0 =A0 struct chip *chip;
=A0=A0 =A0 int ret;

=A0=A0 =A0 chip =3D devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
=A0=A0 =A0 if (!chip)
=A0=A0 =A0 =A0 =A0 return -ENOMEM;

=A0=A0 =A0 mutex_init(&chip->lock);
=A0=A0 =A0 i2c_set_clientdata(client, chip);

=A0=A0 =A0 /* Do my chip stuf
=A0=A0 =A0 */


=A0=A0 =A0 return ret;

probe_fail:
=A0=A0 =A0 mutex_destroy(&chip->lock);
=A0=A0 =A0 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
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________
Kernelnewbies mailing list
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies