ipmi: kcs_bmc: Miscellaneous cleanups
cover ipmi: kcs_bmc: Miscellaneous cleanups
Hello,
A cleanup of the KCS subsystem was prompted after some concerns raised by Jonathan on Konstantin's series implementing DSP0254[1] (the MCTP KCS Transport Binding Specification):
https://lore.kernel.org/all/[email protected]/
[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0254_1.0.0.pdf
The MCTP KCS patches are currently at v5:
https://lore.kernel.org/all/[email protected]/
A v6 will be necessary to rework them in terms of the cleanup done here. I've pushed a preview of that work here:
https://github.com/amboar/linux/compare/d2cc82b50335c8fcf83e1d8f396c8f8cf4333ac4...mctp-kcs
In addition to addressing some of the resource lifetime concerns I've added kerneldoc for the subsystem in anticipation of Konstantin's series moving the headers to include/linux/.
To get Konstantin's work merged I expect we'll have to either take these KCS patches through netdev or the MCTP patches through the IPMI tree. We should figure out which way we want to go, but netdev's not open right now and so that's not a pressing concern.
Please review!
Thanks,
Andrew
Andrew Jeffery (10):
ipmi: kcs_bmc: Update module description
ipmi: kcs_bmc: Include spinlock.h
ipmi: kcs_bmc: Make kcs_bmc_update_event_mask() static
ipmi: kcs_bmc: Make remove_device() callback return void
ipmi: kcs_bmc: Define client actions in terms of kcs_bmc_client
ipmi: kcs_bmc: Integrate buffers into driver struct
ipmi: kcs_bmc: Disassociate client from device lifetimes
ipmi: kcs_bmc: Track clients in core
ipmi: kcs_bmc: Add module_kcs_bmc_driver()
ipmi: kcs_bmc: Add subsystem kerneldoc
drivers/char/ipmi/kcs_bmc.c | 160 +++++++++++--------- drivers/char/ipmi/kcs_bmc.h | 41 +++++ drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 152 +++++++------------ drivers/char/ipmi/kcs_bmc_client.h | 206 +++++++++++++++++++++++--- drivers/char/ipmi/kcs_bmc_device.h | 44 +++++- drivers/char/ipmi/kcs_bmc_serio.c | 84 ++++------- 6 files changed, 448 insertions(+), 239 deletions(-)
Signature · 1 line
2.39.2
PATCH 2/10 ipmi: kcs_bmc: Include spinlock.h
struct kcs_bmc_device defines a spinlock member but the header in which it is defined failed to include the spinlock header. In the spirit of include-what-you-use, do what's necessary.
--- drivers/char/ipmi/kcs_bmc.h | 1 + 1 file changed, 1 insertion(+)
drivers/char/ipmi/kcs_bmc.h +1 -0
@@ -7,6 +7,7 @@#define __KCS_BMC_H__#include <linux/list.h> +#include <linux/spinlock.h> #define KCS_BMC_EVENT_TYPE_OBE BIT(0)#define KCS_BMC_EVENT_TYPE_IBF BIT(1)
Signature · 1 line
2.39.2
PATCH 1/10 ipmi: kcs_bmc: Update module description
In reply to Andrew JefferyKCS devices are often used for IPMI, but they're not constrained to it. Update the subsystem module description to reflect its more general capabilities.
--- drivers/char/ipmi/kcs_bmc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/ipmi/kcs_bmc.c +1 -1
@@ -187,4 +187,4 @@ EXPORT_SYMBOL(kcs_bmc_update_event_mask);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" ); -MODULE_DESCRIPTION ("KCS BMC to handle the IPMI request from system software" ); +MODULE_DESCRIPTION ("Subsystem for BMCs to communicate via KCS devices" );
Signature · 1 line
2.39.2
PATCH 3/10 ipmi: kcs_bmc: Make kcs_bmc_update_event_mask() static
In reply to Andrew JefferyThere were no users outside the subsystem core, so let's not expose it.
--- drivers/char/ipmi/kcs_bmc.c | 11 +++++------ drivers/char/ipmi/kcs_bmc_client.h | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-)
drivers/char/ipmi/kcs_bmc.c +5 -6
@@ -68,6 +68,11 @@ irqreturn_t kcs_bmc_handle_event(struct kcs_bmc_device *kcs_bmc) }EXPORT_SYMBOL (kcs_bmc_handle_event); +static void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events) +{ + kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); +} +int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) {int rc;
@@ -178,12 +183,6 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv) }EXPORT_SYMBOL (kcs_bmc_unregister_driver); -void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events) -{ - kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); -} -EXPORT_SYMBOL (kcs_bmc_update_event_mask); -MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );
drivers/char/ipmi/kcs_bmc_client.h +0 -2
@@ -35,8 +35,6 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv);int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); -void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events); -u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc);void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data);u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc);
Signature · 1 line
2.39.2
PATCH 4/10 ipmi: kcs_bmc: Make remove_device() callback return void
In reply to Andrew JefferyDon't pretend there's a valid failure path when there's not.
--- drivers/char/ipmi/kcs_bmc.c | 12 ++---------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 6 ++---- drivers/char/ipmi/kcs_bmc_client.h | 2 +- drivers/char/ipmi/kcs_bmc_serio.c | 6 ++---- 4 files changed, 7 insertions(+), 19 deletions(-)
drivers/char/ipmi/kcs_bmc.c +2 -10
@@ -135,15 +135,11 @@ EXPORT_SYMBOL(kcs_bmc_add_device);void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_driver *drv; -int rc;mutex_lock (&kcs_bmc_lock);list_del (&kcs_bmc->entry);list_for_each_entry (drv, &kcs_bmc_drivers, entry) { - rc = drv->ops->remove_device (kcs_bmc); -if (rc) -dev_err (kcs_bmc->dev,"Failed to remove chardev for KCS channel %d: %d" , - kcs_bmc->channel, rc); + drv->ops->remove_device (kcs_bmc); }mutex_unlock (&kcs_bmc_lock); }
@@ -169,15 +165,11 @@ EXPORT_SYMBOL(kcs_bmc_register_driver);void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv) {struct kcs_bmc_device *kcs_bmc; -int rc;mutex_lock (&kcs_bmc_lock);list_del (&drv->entry);list_for_each_entry (kcs_bmc, &kcs_bmc_devices, entry) { - rc = drv->ops->remove_device (kcs_bmc); -if (rc) -dev_err (kcs_bmc->dev,"Failed to remove driver for KCS channel %d: %d" , - kcs_bmc->channel, rc); + drv->ops->remove_device (kcs_bmc); }mutex_unlock (&kcs_bmc_lock); }
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +2 -4
@@ -512,7 +512,7 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -static int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_ipmi *priv =NULL , *pos;
@@ -527,7 +527,7 @@ static int kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)spin_unlock_irq (&kcs_bmc_ipmi_instances_lock);if (!priv) -return -ENODEV ; +return ;misc_deregister (&priv->miscdev);kcs_bmc_disable_device (priv->client.dev, &priv->client);
@@ -535,8 +535,6 @@ static int kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)devm_kfree (kcs_bmc->dev, priv->data_out);devm_kfree (kcs_bmc->dev, priv->data_in);devm_kfree (kcs_bmc->dev, priv); - -return 0 ; }static const struct kcs_bmc_driver_ops kcs_bmc_ipmi_driver_ops = {
drivers/char/ipmi/kcs_bmc_client.h +1 -1
@@ -10,7 +10,7 @@struct kcs_bmc_driver_ops {int (*add_device)(struct kcs_bmc_device *kcs_bmc); -int (*remove_device)(struct kcs_bmc_device *kcs_bmc); +void (*remove_device)(struct kcs_bmc_device *kcs_bmc); };struct kcs_bmc_driver {
drivers/char/ipmi/kcs_bmc_serio.c +2 -4
@@ -103,7 +103,7 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -static int kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_serio *priv =NULL , *pos;
@@ -118,7 +118,7 @@ static int kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)spin_unlock_irq (&kcs_bmc_serio_instances_lock);if (!priv) -return -ENODEV ; +return ;/* kfree()s priv->port via put_device() */ serio_unregister_port (priv->port);
@@ -127,8 +127,6 @@ static int kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)kcs_bmc_disable_device (kcs_bmc, &priv->client);devm_kfree (priv->client.dev->dev, priv); - -return 0 ; }static const struct kcs_bmc_driver_ops kcs_bmc_serio_driver_ops = {
Signature · 1 line
2.39.2
PATCH 9/10 ipmi: kcs_bmc: Add module_kcs_bmc_driver()
In reply to Andrew JefferyRemove some cruft in the client modules by adding the usual module macro for the KCS subsystem.
--- drivers/char/ipmi/kcs_bmc.c | 4 +++- drivers/char/ipmi/kcs_bmc.h | 1 + drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 15 +-------------- drivers/char/ipmi/kcs_bmc_client.h | 7 ++++++- drivers/char/ipmi/kcs_bmc_serio.c | 15 +-------------- 5 files changed, 12 insertions(+), 30 deletions(-)
drivers/char/ipmi/kcs_bmc.c +3 -1
@@ -174,7 +174,7 @@ void kcs_bmc_remove_device(struct kcs_bmc_device *dev) }EXPORT_SYMBOL (kcs_bmc_remove_device); -void kcs_bmc_register_driver (struct kcs_bmc_driver *drv) +int kcs_bmc_register_driver (struct kcs_bmc_driver *drv) {struct kcs_bmc_client *client;struct kcs_bmc_device *dev;
@@ -191,6 +191,8 @@ void kcs_bmc_register_driver(struct kcs_bmc_driver *drv)list_add (&client->entry, &kcs_bmc_clients); }mutex_unlock (&kcs_bmc_lock); + +return 0 ; }EXPORT_SYMBOL (kcs_bmc_register_driver);
drivers/char/ipmi/kcs_bmc.h +1 -0
@@ -7,6 +7,7 @@#define __KCS_BMC_H__#include <linux/list.h> +#include <linux/module.h> #include <linux/spinlock.h> #define KCS_BMC_EVENT_TYPE_OBE BIT(0)
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1 -14
@@ -523,20 +523,7 @@ static struct kcs_bmc_driver kcs_bmc_ipmi_driver = { .ops = &kcs_bmc_ipmi_driver_ops, }; -static int __init kcs_bmc_ipmi_init (void ) -{ -kcs_bmc_register_driver (&kcs_bmc_ipmi_driver); - -return 0 ; -} -module_init (kcs_bmc_ipmi_init); - -static void __exit kcs_bmc_ipmi_exit(void) -{ -kcs_bmc_unregister_driver (&kcs_bmc_ipmi_driver); -} -module_exit (kcs_bmc_ipmi_exit); - +module_kcs_bmc_driver (kcs_bmc_ipmi_driver);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );
drivers/char/ipmi/kcs_bmc_client.h +6 -1
@@ -5,6 +5,7 @@#define __KCS_BMC_CONSUMER_H__#include <linux/irqreturn.h> +#include <linux/module.h> #include "kcs_bmc.h"
@@ -44,9 +45,13 @@ struct kcs_bmc_driver {const struct kcs_bmc_driver_ops *ops; }; -void kcs_bmc_register_driver (struct kcs_bmc_driver *drv); +int kcs_bmc_register_driver (struct kcs_bmc_driver *drv);void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv); +#define module_kcs_bmc_driver (__kcs_bmc_driver) \ + module_driver(__kcs_bmc_driver, kcs_bmc_register_driver, \ + kcs_bmc_unregister_driver) +int kcs_bmc_enable_device (struct kcs_bmc_client *client);void kcs_bmc_disable_device (struct kcs_bmc_client *client);u8 kcs_bmc_read_data (struct kcs_bmc_client *client);
drivers/char/ipmi/kcs_bmc_serio.c +1 -14
@@ -123,20 +123,7 @@ static struct kcs_bmc_driver kcs_bmc_serio_driver = { .ops = &kcs_bmc_serio_driver_ops, }; -static int __init kcs_bmc_serio_init (void ) -{ -kcs_bmc_register_driver (&kcs_bmc_serio_driver); - -return 0 ; -} -module_init (kcs_bmc_serio_init); - -static void __exit kcs_bmc_serio_exit(void) -{ -kcs_bmc_unregister_driver (&kcs_bmc_serio_driver); -} -module_exit (kcs_bmc_serio_exit); - +module_kcs_bmc_driver (kcs_bmc_serio_driver);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );MODULE_DESCRIPTION ("Adapter driver for serio access to BMC KCS devices" );
Signature · 1 line
2.39.2
PATCH 5/10 ipmi: kcs_bmc: Define client actions in terms of kcs_bmc_client
In reply to Andrew JefferyOperations such as reading and writing from hardware and updating the events of interest are operations in which the client is interested, but are applied to the device. Strengthen the concept of the client in the subsystem and clean up some call-sites by translating between the client and device types in the core of the KCS subsystem.
--- drivers/char/ipmi/kcs_bmc.c | 67 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 50 ++++++++++---------- drivers/char/ipmi/kcs_bmc_client.h | 15 +++--- drivers/char/ipmi/kcs_bmc_serio.c | 10 ++-- 4 files changed, 81 insertions(+), 61 deletions(-)
drivers/char/ipmi/kcs_bmc.c +45 -22
@@ -22,33 +22,53 @@ static LIST_HEAD(kcs_bmc_drivers);/* Consumer data access */ -u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_client_validate (struct kcs_bmc_client *client) { -return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); +WARN_ONCE (client !=READ_ONCE (client->dev->client),"KCS client confusion detected" ); +} + +u8 kcs_bmc_read_data (struct kcs_bmc_client *client) +{ +struct kcs_bmc_device *dev = client->dev; + +kcs_bmc_client_validate (client); +return dev->ops->io_inputb (dev, dev->ioreg.idr); }EXPORT_SYMBOL (kcs_bmc_read_data); -void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data) +void kcs_bmc_write_data (struct kcs_bmc_client *client,u8 data) { - kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.odr, data); +struct kcs_bmc_device *dev = client->dev; + +kcs_bmc_client_validate (client); + dev->ops->io_outputb (dev, dev->ioreg.odr, data); }EXPORT_SYMBOL (kcs_bmc_write_data); -u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc) +u8 kcs_bmc_read_status (struct kcs_bmc_client *client) { -return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.str); +struct kcs_bmc_device *dev = client->dev; + +kcs_bmc_client_validate (client); +return dev->ops->io_inputb (dev, dev->ioreg.str); }EXPORT_SYMBOL (kcs_bmc_read_status); -void kcs_bmc_write_status (struct kcs_bmc_device *kcs_bmc,u8 data) +void kcs_bmc_write_status (struct kcs_bmc_client *client,u8 data) { - kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.str, data); +struct kcs_bmc_device *dev = client->dev; + +kcs_bmc_client_validate (client); + dev->ops->io_outputb (dev, dev->ioreg.str, data); }EXPORT_SYMBOL (kcs_bmc_write_status); -void kcs_bmc_update_status (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 val) +void kcs_bmc_update_status (struct kcs_bmc_client *client,u8 mask,u8 val) { - kcs_bmc->ops->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); +struct kcs_bmc_device *dev = client->dev; + +kcs_bmc_client_validate (client); + dev->ops->io_updateb (dev, dev->ioreg.str, mask, val); }EXPORT_SYMBOL (kcs_bmc_update_status);
@@ -73,36 +93,39 @@ static void kcs_bmc_update_event_mask(struct kcs_bmc_device *kcs_bmc, u8 mask, u kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); } -int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) +int kcs_bmc_enable_device (struct kcs_bmc_client *client) { +struct kcs_bmc_device *dev = client->dev;int rc; -spin_lock_irq (&kcs_bmc->lock); -if (kcs_bmc->client) { +spin_lock_irq (&dev->lock); +if (dev->client) { rc = -EBUSY ; }else {u8 mask =KCS_BMC_EVENT_TYPE_IBF ; - kcs_bmc->client = client; -kcs_bmc_update_event_mask (kcs_bmc, mask, mask); + dev->client = client; +kcs_bmc_update_event_mask (dev, mask, mask); rc =0 ; } -spin_unlock_irq (&kcs_bmc->lock); +spin_unlock_irq (&dev->lock);return rc; }EXPORT_SYMBOL (kcs_bmc_enable_device); -void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) +void kcs_bmc_disable_device (struct kcs_bmc_client *client) { -spin_lock_irq (&kcs_bmc->lock); -if (client == kcs_bmc->client) { +struct kcs_bmc_device *dev = client->dev; + +spin_lock_irq (&dev->lock); +if (client == dev->client) {u8 mask =KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ; -kcs_bmc_update_event_mask (kcs_bmc, mask,0 ); - kcs_bmc->client =NULL ; +kcs_bmc_update_event_mask (dev, mask,0 ); + dev->client =NULL ; } -spin_unlock_irq (&kcs_bmc->lock); +spin_unlock_irq (&dev->lock); }EXPORT_SYMBOL (kcs_bmc_disable_device);
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +24 -26
152 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_client.h +7 -8
@@ -32,12 +32,11 @@ struct kcs_bmc_client {void kcs_bmc_register_driver (struct kcs_bmc_driver *drv);void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv); -int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); -void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); - -u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc); -void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data); -u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc); -void kcs_bmc_write_status (struct kcs_bmc_device *kcs_bmc,u8 data); -void kcs_bmc_update_status (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 val); +int kcs_bmc_enable_device (struct kcs_bmc_client *client); +void kcs_bmc_disable_device (struct kcs_bmc_client *client); +u8 kcs_bmc_read_data (struct kcs_bmc_client *client); +void kcs_bmc_write_data (struct kcs_bmc_client *client,u8 data); +u8 kcs_bmc_read_status (struct kcs_bmc_client *client); +void kcs_bmc_write_status (struct kcs_bmc_client *client,u8 data); +void kcs_bmc_update_status (struct kcs_bmc_client *client,u8 mask,u8 val);#endif
drivers/char/ipmi/kcs_bmc_serio.c +5 -5
@@ -36,10 +36,10 @@ static irqreturn_t kcs_bmc_serio_event(struct kcs_bmc_client *client)spin_lock (&priv->lock); - status =kcs_bmc_read_status (client->dev); + status =kcs_bmc_read_status (client);if (status &KCS_BMC_STR_IBF ) - handled =serio_interrupt (priv->port,kcs_bmc_read_data (client->dev),0 ); + handled =serio_interrupt (priv->port,kcs_bmc_read_data (client),0 );spin_unlock (&priv->lock);
@@ -54,14 +54,14 @@ static int kcs_bmc_serio_open(struct serio *port) {struct kcs_bmc_serio *priv = port->port_data; -return kcs_bmc_enable_device (priv->client.dev, &priv->client); +return kcs_bmc_enable_device (&priv->client); }static void kcs_bmc_serio_close (struct serio *port) {struct kcs_bmc_serio *priv = port->port_data; -kcs_bmc_disable_device (priv->client.dev, &priv->client); +kcs_bmc_disable_device (&priv->client); }static DEFINE_SPINLOCK (kcs_bmc_serio_instances_lock);
@@ -124,7 +124,7 @@ static void kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)serio_unregister_port (priv->port);/* Ensure the IBF IRQ is disabled if we were the active client */ -kcs_bmc_disable_device (kcs_bmc, &priv->client); +kcs_bmc_disable_device (&priv->client);devm_kfree (priv->client.dev->dev, priv); }
Signature · 1 line
2.39.2
PATCH 6/10 ipmi: kcs_bmc: Integrate buffers into driver struct
In reply to Andrew JefferyConsolidate several necessary allocations into one to reduce the number of possible error paths.
--- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +8 -17
@@ -66,6 +66,10 @@ enum kcs_ipmi_errors {KCS_UNSPECIFIED_ERROR =0xFF }; +#define DEVICE_NAME "ipmi-kcs" +#define KCS_MSG_BUFSIZ 1000 +#define KCS_ZERO_DATA 0 +struct kcs_bmc_ipmi {struct list_head entry;
@@ -79,24 +83,18 @@ struct kcs_bmc_ipmi {wait_queue_head_t queue;bool data_in_avail;int data_in_idx; -u8 *data_in; +u8 data_in[KCS_MSG_BUFSIZ ];int data_out_idx;int data_out_len; -u8 *data_out; +u8 data_out[KCS_MSG_BUFSIZ ];struct mutex mutex; -u8 *kbuffer; +u8 kbuffer[KCS_MSG_BUFSIZ ];struct miscdevice miscdev; }; -#define DEVICE_NAME "ipmi-kcs" - -#define KCS_MSG_BUFSIZ 1000 - -#define KCS_ZERO_DATA 0 -/* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */ #define KCS_STATUS_STATE (state) (state << 6)#define KCS_STATUS_STATE_MASK GENMASK(7, 6)
@@ -478,19 +476,15 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)spin_lock_init (&priv->lock);mutex_init (&priv->mutex); -init_waitqueue_head (&priv->queue); priv->client.dev = kcs_bmc; priv->client.ops = &kcs_bmc_ipmi_client_ops; - priv->data_in =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); - priv->data_out =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); - priv->kbuffer =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); priv->miscdev.minor =MISC_DYNAMIC_MINOR ; priv->miscdev.name =devm_kasprintf (kcs_bmc->dev,GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); -if (!priv->data_in || !priv->data_out || !priv->kbuffer || !priv->miscdev.name) +if (!priv->miscdev.name)return -EINVAL ; priv->miscdev.fops = &kcs_bmc_ipmi_fops;
@@ -529,9 +523,6 @@ static void kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)misc_deregister (&priv->miscdev);kcs_bmc_disable_device (&priv->client); -devm_kfree (kcs_bmc->dev, priv->kbuffer); -devm_kfree (kcs_bmc->dev, priv->data_out); -devm_kfree (kcs_bmc->dev, priv->data_in);devm_kfree (kcs_bmc->dev, priv); }
Signature · 1 line
2.39.2
PATCH 7/10 ipmi: kcs_bmc: Disassociate client from device lifetimes
In reply to Andrew JefferyKCS client modules may be removed by actions unrelated to KCS devices. As usual, removing a KCS client module requires unbinding all client instances from the underlying devices to prevent further use of the code.
Previously, KCS client resource lifetimes were tied to the underlying KCS device instance with the use of devm_k[mz]alloc() APIs. This requires the use of devm_free() as a consequence. It's necessary to scrutinise use of explicit devm_free()s because they generally indicate there's a concerning corner-case in play, but that's not really the case here. Switch to explicit kmalloc()/kfree() to align expectations with the intent of the code.
--- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 28 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_serio.c | 20 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-)
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +19 -9
@@ -470,7 +470,7 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)struct kcs_bmc_ipmi *priv;int rc; - priv =devm_kzalloc (kcs_bmc->dev,sizeof (*priv),GFP_KERNEL ); + priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;
@@ -482,26 +482,35 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc) priv->client.ops = &kcs_bmc_ipmi_client_ops; priv->miscdev.minor =MISC_DYNAMIC_MINOR ; - priv->miscdev.name =devm_kasprintf (kcs_bmc->dev,GFP_KERNEL ,"%s%u" ,DEVICE_NAME , - kcs_bmc->channel); -if (!priv->miscdev.name) -return -EINVAL ; + priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); +if (!priv->miscdev.name) { + rc = -ENOMEM ; + goto cleanup_priv; + } priv->miscdev.fops = &kcs_bmc_ipmi_fops; rc =misc_register (&priv->miscdev);if (rc) { -dev_err (kcs_bmc->dev,"Unable to register device: %d\n" , rc); -return rc; +pr_err ("Unable to register device: %d\n" , rc); + goto cleanup_miscdev_name; }spin_lock_irq (&kcs_bmc_ipmi_instances_lock);list_add (&priv->entry, &kcs_bmc_ipmi_instances);spin_unlock_irq (&kcs_bmc_ipmi_instances_lock); -dev_info (kcs_bmc->dev,"Initialised IPMI client for channel %d" , kcs_bmc->channel); +pr_info ("Initialised IPMI client for channel %d\n" , kcs_bmc->channel);return 0 ; + +cleanup_miscdev_name: +kfree (priv->miscdev.name); + +cleanup_priv: +kfree (priv); + +return rc; }static void kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc)
@@ -523,7 +532,8 @@ static void kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)misc_deregister (&priv->miscdev);kcs_bmc_disable_device (&priv->client); -devm_kfree (kcs_bmc->dev, priv); +kfree (priv->miscdev.name); +kfree (priv); }static const struct kcs_bmc_driver_ops kcs_bmc_ipmi_driver_ops = {
drivers/char/ipmi/kcs_bmc_serio.c +13 -7
@@ -71,15 +71,18 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_serio *priv;struct serio *port; +int rc; - priv =devm_kzalloc (kcs_bmc->dev,sizeof (*priv),GFP_KERNEL ); + priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;/* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */ port =kzalloc (sizeof (*port),GFP_KERNEL ); -if (!port) -return -ENOMEM ; +if (!port) { + rc = -ENOMEM ; + goto cleanup_priv; + } port->id.type =SERIO_8042 ; port->open = kcs_bmc_serio_open;
@@ -98,9 +101,14 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc)serio_register_port (port); -dev_info (kcs_bmc->dev,"Initialised serio client for channel %d" , kcs_bmc->channel); +pr_info ("Initialised serio client for channel %d\n" , kcs_bmc->channel);return 0 ; + +cleanup_priv: +kfree (priv); + +return rc; }static void kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc)
@@ -122,11 +130,9 @@ static void kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)/* kfree()s priv->port via put_device() */ serio_unregister_port (priv->port); -/* Ensure the IBF IRQ is disabled if we were the active client */ kcs_bmc_disable_device (&priv->client); - -devm_kfree (priv->client.dev->dev, priv); +kfree (priv); }static const struct kcs_bmc_driver_ops kcs_bmc_serio_driver_ops = {
Signature · 1 line
2.39.2
PATCH 8/10 ipmi: kcs_bmc: Track clients in core
In reply to Andrew JefferyI ran out of spoons before I could come up with a better client tracking scheme back in the original refactoring series:
https://lore.kernel.org/all/[email protected]/
Jonathan prodded Konstantin about the issue in a review of Konstantin's MCTP patches[1], prompting an attempt to clean it up.
[1]: https://lore.kernel.org/all/[email protected]/
Prevent client modules from having to track their own instances by requiring they return a pointer to a client object from their add_device() implementation. We can then track this in the core, and provide it as the argument to the remove_device() implementation to save the client module from further work. The usual container_of() pattern gets the client module access to its private data.
--- drivers/char/ipmi/kcs_bmc.c | 68 ++++++++++++++++----------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 42 ++++------------- drivers/char/ipmi/kcs_bmc_client.h | 35 ++++++++++---- drivers/char/ipmi/kcs_bmc_device.h | 4 +- drivers/char/ipmi/kcs_bmc_serio.c | 43 +++++------------ 5 files changed, 88 insertions(+), 104 deletions(-)
drivers/char/ipmi/kcs_bmc.c +40 -28
@@ -19,6 +19,7 @@static DEFINE_MUTEX (kcs_bmc_lock);static LIST_HEAD (kcs_bmc_devices);static LIST_HEAD (kcs_bmc_drivers); +static LIST_HEAD (kcs_bmc_clients);/* Consumer data access */
@@ -129,25 +130,27 @@ void kcs_bmc_disable_device(struct kcs_bmc_client *client) }EXPORT_SYMBOL (kcs_bmc_disable_device); -int kcs_bmc_add_device (struct kcs_bmc_device *kcs_bmc) +int kcs_bmc_add_device (struct kcs_bmc_device *dev) { +struct kcs_bmc_client *client;struct kcs_bmc_driver *drv;int error =0 ; -int rc; -spin_lock_init (&kcs_bmc->lock); - kcs_bmc->client =NULL ; +spin_lock_init (&dev->lock); + dev->client =NULL ;mutex_lock (&kcs_bmc_lock); -list_add (&kcs_bmc->entry, &kcs_bmc_devices); +list_add (&dev->entry, &kcs_bmc_devices);list_for_each_entry (drv, &kcs_bmc_drivers, entry) { - rc = drv->ops->add_device (kcs_bmc); -if (!rc) -continue ; - -dev_err (kcs_bmc->dev,"Failed to add chardev for KCS channel %d: %d" , - kcs_bmc->channel, rc); - error = rc; + client = drv->ops->add_device (drv, dev); +if (IS_ERR (client)) { + error =PTR_ERR (client); +dev_err (dev->dev, +"Failed to add chardev for KCS channel %d: %d" , + dev->channel, error); +break ; + } +list_add (&client->entry, &kcs_bmc_clients); }mutex_unlock (&kcs_bmc_lock);
@@ -155,31 +158,37 @@ int kcs_bmc_add_device(struct kcs_bmc_device *kcs_bmc) }EXPORT_SYMBOL (kcs_bmc_add_device); -void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc) +void kcs_bmc_remove_device (struct kcs_bmc_device *dev) { -struct kcs_bmc_driver *drv; +struct kcs_bmc_client *curr, *tmp;mutex_lock (&kcs_bmc_lock); -list_del (&kcs_bmc->entry); -list_for_each_entry (drv, &kcs_bmc_drivers, entry) { - drv->ops->remove_device (kcs_bmc); +list_for_each_entry_safe (curr, tmp, &kcs_bmc_clients, entry) { +if (curr->dev == dev) { +list_del (&curr->entry); + curr->drv->ops->remove_device (curr); + } } +list_del (&dev->entry);mutex_unlock (&kcs_bmc_lock); }EXPORT_SYMBOL (kcs_bmc_remove_device);void kcs_bmc_register_driver (struct kcs_bmc_driver *drv) { -struct kcs_bmc_device *kcs_bmc; -int rc; +struct kcs_bmc_client *client; +struct kcs_bmc_device *dev;mutex_lock (&kcs_bmc_lock);list_add (&drv->entry, &kcs_bmc_drivers); -list_for_each_entry (kcs_bmc, &kcs_bmc_devices, entry) { - rc = drv->ops->add_device (kcs_bmc); -if (rc) -dev_err (kcs_bmc->dev,"Failed to add driver for KCS channel %d: %d" , - kcs_bmc->channel, rc); +list_for_each_entry (dev, &kcs_bmc_devices, entry) { + client = drv->ops->add_device (drv, dev); +if (IS_ERR (client)) { +dev_err (dev->dev,"Failed to add driver for KCS channel %d: %ld" , + dev->channel,PTR_ERR (client)); +continue ; + } +list_add (&client->entry, &kcs_bmc_clients); }mutex_unlock (&kcs_bmc_lock); }
@@ -187,13 +196,16 @@ EXPORT_SYMBOL(kcs_bmc_register_driver);void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv) { -struct kcs_bmc_device *kcs_bmc; +struct kcs_bmc_client *curr, *tmp;mutex_lock (&kcs_bmc_lock); -list_del (&drv->entry); -list_for_each_entry (kcs_bmc, &kcs_bmc_devices, entry) { - drv->ops->remove_device (kcs_bmc); +list_for_each_entry_safe (curr, tmp, &kcs_bmc_clients, entry) { +if (curr->drv == drv) { +list_del (&curr->entry); + drv->ops->remove_device (curr); + } } +list_del (&drv->entry);mutex_unlock (&kcs_bmc_lock); }EXPORT_SYMBOL (kcs_bmc_unregister_driver);
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +10 -32
83 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_client.h +25 -10
@@ -8,16 +8,7 @@#include "kcs_bmc.h" -struct kcs_bmc_driver_ops { -int (*add_device)(struct kcs_bmc_device *kcs_bmc); -void (*remove_device)(struct kcs_bmc_device *kcs_bmc); -}; - -struct kcs_bmc_driver { -struct list_head entry; - -const struct kcs_bmc_driver_ops *ops; -}; +struct kcs_bmc_driver ;struct kcs_bmc_client_ops {irqreturn_t (*event)(struct kcs_bmc_client *client);
@@ -26,7 +17,31 @@ struct kcs_bmc_client_ops {struct kcs_bmc_client {const struct kcs_bmc_client_ops *ops; +struct kcs_bmc_driver *drv;struct kcs_bmc_device *dev; +struct list_head entry; +}; + +struct kcs_bmc_driver_ops { +struct kcs_bmc_client *(*add_device)(struct kcs_bmc_driver *drv, +struct kcs_bmc_device *dev); +void (*remove_device)(struct kcs_bmc_client *client); +}; + +static inline void kcs_bmc_client_init (struct kcs_bmc_client *client, +const struct kcs_bmc_client_ops *ops, +struct kcs_bmc_driver *drv, +struct kcs_bmc_device *dev) +{ + client->ops = ops; + client->drv = drv; + client->dev = dev; +} + +struct kcs_bmc_driver { +struct list_head entry; + +const struct kcs_bmc_driver_ops *ops; };void kcs_bmc_register_driver (struct kcs_bmc_driver *drv);
drivers/char/ipmi/kcs_bmc_device.h +2 -2
@@ -16,7 +16,7 @@ struct kcs_bmc_device_ops { };irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc); -int kcs_bmc_add_device (struct kcs_bmc_device *kcs_bmc); -void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc); +int kcs_bmc_add_device (struct kcs_bmc_device *dev); +void kcs_bmc_remove_device (struct kcs_bmc_device *dev);#endif
drivers/char/ipmi/kcs_bmc_serio.c +11 -32
86 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH 10/10 ipmi: kcs_bmc: Add subsystem kerneldoc
In reply to Andrew JefferyProvide kerneldoc describing the relationships between and the behaviours of the structures and functions of the KCS subsystem.
--- drivers/char/ipmi/kcs_bmc.h | 39 ++++++++ drivers/char/ipmi/kcs_bmc_client.h | 151 +++++++++++++++++++++++++++++ drivers/char/ipmi/kcs_bmc_device.h | 40 ++++++++ 3 files changed, 230 insertions(+)
drivers/char/ipmi/kcs_bmc.h +39 -0
@@ -10,6 +10,36 @@#include <linux/module.h> #include <linux/spinlock.h> +/** +* DOC: KCS subsystem structure +* +* The KCS subsystem is split into three components: +* +* 1. &struct kcs_bmc_device +* 2. &struct kcs_bmc_driver +* 3. &struct kcs_bmc_client +* +* ``struct kcs_bmc_device`` (device) represents a driver instance for a +* particular KCS device. ``struct kcs_bmc_device``` abstracts away the device +* specifics allowing for device-independent implementation of protocols over +* KCS. +* +* ``struct kcs_bmc_driver`` (driver) represents an implementation of a KCS +* protocol. Implementations of a protocol either expose this behaviour out to +* userspace via a character device, or provide the glue into another kernel +* subsystem. +* +* ``struct kcs_bmc_client`` (client) associates a ``struct kcs_bmc_device`` +* instance (``D``) with a &struct kcs_bmc_driver instance (``P``). An instance +* of each protocol implementation is associated with each device, yielding +* ``D*P`` client instances. +* +* A device may only have one active client at a time. A client becomes active +* on its associated device whenever userspace "opens" its interface in some +* fashion, for example, opening a character device. If the device associated +* with a client already has an active client then an error is propagated. +*/ +#define KCS_BMC_EVENT_TYPE_OBE BIT(0)#define KCS_BMC_EVENT_TYPE_IBF BIT(1)
@@ -31,6 +61,15 @@ struct kcs_ioreg {struct kcs_bmc_device_ops ;struct kcs_bmc_client ; +/** +* struct kcs_bmc_device - An abstract representation of a KCS device +* @entry: A list node for the KCS core to track KCS device instances +* @dev: The kernel device object for the KCS hardware +* @channel: The IPMI channel number for the KCS device +* @ops: A set of callbacks for providing abstract access to the KCS hardware +* @lock: Protects accesses to, and operations on &kcs_bmc_device.client +* @client: The client instance, if any, currently associated with the device +*/ struct kcs_bmc_device {struct list_head entry;
drivers/char/ipmi/kcs_bmc_client.h +151 -0
197 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_device.h +40 -0
@@ -8,6 +8,13 @@#include "kcs_bmc.h" +/** +* struct kcs_bmc_device_ops - Callbacks operating on a KCS device +* @irq_mask_update: Update the set of events of interest +* @io_inputb: A callback to read the specified KCS register from hardware +* @io_outputb: A callback to write the specified KCS register to hardware +* @io_updateb: A callback to update a subfield of the specified KCS register +*/ struct kcs_bmc_device_ops {void (*irq_mask_update)(struct kcs_bmc_device *kcs_bmc,u8 mask,u8 enable);u8 (*io_inputb)(struct kcs_bmc_device *kcs_bmc,u32 reg);
@@ -15,8 +22,41 @@ struct kcs_bmc_device_ops { void (*io_updateb)(struct kcs_bmc_device *kcs_bmc, u32 reg, u8 mask, u8 b); }; +/** +* kcs_bmc_handle_event() - Notify the active client of a hardware interrupt +* @kcs_bmc: The device instance whose interrupt was triggered +* +* Propagate a hardware interrupt as an event to the active client. The client's +* handler should take any necessary actions for the protocol it implements, but +* must read IDR to resolve the interrupt if the interrupt was generated by the +* KCS device. +* +* Context: Interrupt context. Takes and releases &kcs_bmc_device.lock. +* +* Return: An irqreturn_t value indicating whether the interrupt was handled. +*/ irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc); + +/** +* kcs_bmc_add_device() - Register a KCS device instance with the KCS subsystem +* @dev: The &struct kcs_bmc_device instance to register +* +* Should be called by the probe() implementation of the KCS hardware's driver. +* +* Context: Process context. Takes and releases the internal KCS subsystem mutex. +* +* Return: 0 on success, or a negative errno on failure. +*/ int kcs_bmc_add_device (struct kcs_bmc_device *dev); + +/** +* kcs_bmc_remove_device() - Unregister a KCS device instance with the KCS subsystem +* @dev: The &struct kcs_bmc_device instance to unregister +* +* Should be called by the remove() implementation of the KCS hardware's driver. +* +* Context: Process context. Takes and releases the internal KCS subsystem mutex. +*/ void kcs_bmc_remove_device (struct kcs_bmc_device *dev);#endif
Signature · 1 line
2.39.2
Re: [PATCH 01/10] ipmi: kcs_bmc: Update module description
In reply to Andrew JefferyQuoted 5 lines · On Fri, 3 Nov 2023 16:45:13 +1030 Andrew Jeffery…
KCS devices are often used for IPMI, but they're not constrained to it. Update the subsystem module description to reflect its more general capabilities.
Signed-off-by Andrew Jeffery <[email protected]>
LTGM, FWIW,
Quoted 14 lines
--- drivers/char/ipmi/kcs_bmc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/ipmi/kcs_bmc.c +1
-1@@ -187,4 +187,4 @@ EXPORT_SYMBOL(kcs_bmc_update_event_mask);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" ); -MODULE_DESCRIPTION ("KCS BMC to handle the IPMI request from system software" ); +MODULE_DESCRIPTION ("Subsystem for BMCs to communicate via KCS devices" );
Re: [PATCH 02/10] ipmi: kcs_bmc: Include spinlock.h
In reply to Andrew JefferyQuoted 5 lines · On Fri, 3 Nov 2023 16:45:14 +1030 Andrew Jeffery…
struct
kcs_bmc_devicedefines a spinlock member but the header in which it is defined failed to include the spinlock header. In the spirit of include-what-you-use, do what's necessary.Signed-off-by Andrew Jeffery <[email protected]>
This is fine, but whilst checking it I noticed there is no forwards def of struct device or appropriate include.
Still that's unrelated
Quoted 16 lines
--- drivers/char/ipmi/kcs_bmc.h | 1 + 1 file changed, 1 insertion(+)drivers/char/ipmi/kcs_bmc.h +1
-0@@ -7,6 +7,7 @@#define __KCS_BMC_H__#include <linux/list.h> +#include <linux/spinlock.h> #define KCS_BMC_EVENT_TYPE_OBE BIT(0)#define KCS_BMC_EVENT_TYPE_IBF BIT(1)
Re: [PATCH 03/10] ipmi: kcs_bmc: Make kcs_bmc_update_event_mask() static
In reply to Andrew JefferyQuoted 3 lines · On Fri, 3 Nov 2023 16:45:15 +1030 Andrew Jeffery…
There were no users outside the subsystem core, so let's not expose it.
Signed-off-by Andrew Jeffery <[email protected]>
Is it worth having the wrapper?
I guess all the other cases do have wrappers (even if that's because they continue to be exported) so fair enough.
Quoted 47 lines
--- drivers/char/ipmi/kcs_bmc.c | 11 +++++------ drivers/char/ipmi/kcs_bmc_client.h | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-)drivers/char/ipmi/kcs_bmc.c +5
-6@@ -68,6 +68,11 @@ irqreturn_t kcs_bmc_handle_event(struct kcs_bmc_device *kcs_bmc) }EXPORT_SYMBOL (kcs_bmc_handle_event); +static void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events) +{ + kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); +} +int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) {int rc;@@ -178,12 +183,6 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv) }EXPORT_SYMBOL (kcs_bmc_unregister_driver); -void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events) -{ - kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); -} -EXPORT_SYMBOL (kcs_bmc_update_event_mask); -MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );drivers/char/ipmi/kcs_bmc_client.h +0
-2@@ -35,8 +35,6 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv);int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); -void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events); -u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc);void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data);u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc);
Re: [PATCH 04/10] ipmi: kcs_bmc: Make remove_device() callback return void
In reply to Andrew JefferyQuoted 3 lines · On Fri, 3 Nov 2023 16:45:16 +1030 Andrew Jeffery…
Don't pretend there's a valid failure path when there's not.
Signed-off-by Andrew Jeffery <[email protected]>
Whilst I agree returning an error code is pointless, it is perhaps useful to make sure there is a dev_err() or similar in the paths now that you've remove the one at the call site.
Minor point and up to you if you want to or not.
Quoted 120 lines
--- drivers/char/ipmi/kcs_bmc.c | 12 ++---------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 6 ++---- drivers/char/ipmi/kcs_bmc_client.h | 2 +- drivers/char/ipmi/kcs_bmc_serio.c | 6 ++---- 4 files changed, 7 insertions(+), 19 deletions(-)drivers/char/ipmi/kcs_bmc.c +2
-10@@ -135,15 +135,11 @@ EXPORT_SYMBOL(kcs_bmc_add_device);void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_driver *drv; -int rc;mutex_lock (&kcs_bmc_lock);list_del (&kcs_bmc->entry);list_for_each_entry (drv, &kcs_bmc_drivers, entry) { - rc = drv->ops->remove_device (kcs_bmc); -if (rc) -dev_err (kcs_bmc->dev,"Failed to remove chardev for KCS channel %d: %d" , - kcs_bmc->channel, rc); + drv->ops->remove_device (kcs_bmc); }mutex_unlock (&kcs_bmc_lock); }@@ -169,15 +165,11 @@ EXPORT_SYMBOL(kcs_bmc_register_driver);void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv) {struct kcs_bmc_device *kcs_bmc; -int rc;mutex_lock (&kcs_bmc_lock);list_del (&drv->entry);list_for_each_entry (kcs_bmc, &kcs_bmc_devices, entry) { - rc = drv->ops->remove_device (kcs_bmc); -if (rc) -dev_err (kcs_bmc->dev,"Failed to remove driver for KCS channel %d: %d" , - kcs_bmc->channel, rc); + drv->ops->remove_device (kcs_bmc); }mutex_unlock (&kcs_bmc_lock); }drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +2
-4@@ -512,7 +512,7 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -static int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_ipmi *priv =NULL , *pos;@@ -527,7 +527,7 @@ static int kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)spin_unlock_irq (&kcs_bmc_ipmi_instances_lock);if (!priv) -return -ENODEV ; +return ;misc_deregister (&priv->miscdev);kcs_bmc_disable_device (priv->client.dev, &priv->client);@@ -535,8 +535,6 @@ static int kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)devm_kfree (kcs_bmc->dev, priv->data_out);devm_kfree (kcs_bmc->dev, priv->data_in);devm_kfree (kcs_bmc->dev, priv); - -return 0 ; }static const struct kcs_bmc_driver_ops kcs_bmc_ipmi_driver_ops = {drivers/char/ipmi/kcs_bmc_client.h +1
-1@@ -10,7 +10,7 @@struct kcs_bmc_driver_ops {int (*add_device)(struct kcs_bmc_device *kcs_bmc); -int (*remove_device)(struct kcs_bmc_device *kcs_bmc); +void (*remove_device)(struct kcs_bmc_device *kcs_bmc); };struct kcs_bmc_driver {drivers/char/ipmi/kcs_bmc_serio.c +2
-4@@ -103,7 +103,7 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -static int kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_serio *priv =NULL , *pos;@@ -118,7 +118,7 @@ static int kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)spin_unlock_irq (&kcs_bmc_serio_instances_lock);if (!priv) -return -ENODEV ; +return ;/* kfree()s priv->port via put_device() */ serio_unregister_port (priv->port);@@ -127,8 +127,6 @@ static int kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)kcs_bmc_disable_device (kcs_bmc, &priv->client);devm_kfree (priv->client.dev->dev, priv); - -return 0 ; }static const struct kcs_bmc_driver_ops kcs_bmc_serio_driver_ops = {
Re: [PATCH 06/10] ipmi: kcs_bmc: Integrate buffers into driver struct
In reply to Andrew JefferyQuoted 4 lines · On Fri, 3 Nov 2023 16:45:18 +1030 Andrew Jeffery…
Consolidate several necessary allocations into one to reduce the number of possible error paths.
Signed-off-by Andrew Jeffery <[email protected]>
Gets rid of some of the devm_kfree() fun, so I'm in favor of the change :)
One trivial comment inline.
Quoted 48 lines
--- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +7
-9@@ -66,6 +66,10 @@ enum kcs_ipmi_errors {KCS_UNSPECIFIED_ERROR =0xFF }; +#define DEVICE_NAME "ipmi-kcs" +#define KCS_MSG_BUFSIZ 1000 +#define KCS_ZERO_DATA 0 +struct kcs_bmc_ipmi {struct list_head entry;@@ -79,24 +83,18 @@ struct kcs_bmc_ipmi {wait_queue_head_t queue;bool data_in_avail;int data_in_idx; -u8 *data_in; +u8 data_in[KCS_MSG_BUFSIZ ];int data_out_idx;int data_out_len; -u8 *data_out; +u8 data_out[KCS_MSG_BUFSIZ ];struct mutex mutex; -u8 *kbuffer; +u8 kbuffer[KCS_MSG_BUFSIZ ];struct miscdevice miscdev; }; -#define DEVICE_NAME "ipmi-kcs" - -#define KCS_MSG_BUFSIZ 1000 - -#define KCS_ZERO_DATA 0 -/* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */ #define KCS_STATUS_STATE (state) (state << 6)#define KCS_STATUS_STATE_MASK GENMASK(7, 6)
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc) · line 481
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +0
-1@@ -478,19 +476,15 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)spin_lock_init (&priv->lock);mutex_init (&priv->mutex); -
Unrelated change...
Quoted 26 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-7init_waitqueue_head (&priv->queue); priv->client.dev = kcs_bmc; priv->client.ops = &kcs_bmc_ipmi_client_ops; - priv->data_in =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); - priv->data_out =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); - priv->kbuffer =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); priv->miscdev.minor =MISC_DYNAMIC_MINOR ; priv->miscdev.name =devm_kasprintf (kcs_bmc->dev,GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); -if (!priv->data_in || !priv->data_out || !priv->kbuffer || !priv->miscdev.name) +if (!priv->miscdev.name)return -EINVAL ; priv->miscdev.fops = &kcs_bmc_ipmi_fops;@@ -529,9 +523,6 @@ static void kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)misc_deregister (&priv->miscdev);kcs_bmc_disable_device (&priv->client); -devm_kfree (kcs_bmc->dev, priv->kbuffer); -devm_kfree (kcs_bmc->dev, priv->data_out); -devm_kfree (kcs_bmc->dev, priv->data_in);devm_kfree (kcs_bmc->dev, priv); }
Re: [PATCH 07/10] ipmi: kcs_bmc: Disassociate client from device lifetimes
In reply to Andrew JefferyQuoted 12 lines · On Fri, 3 Nov 2023 16:45:19 +1030 Andrew Jeffery…
KCS client modules may be removed by actions unrelated to KCS devices. As usual, removing a KCS client module requires unbinding all client instances from the underlying devices to prevent further use of the code.
Previously, KCS client resource lifetimes were tied to the underlying KCS device instance with the use of
devm_k[mz]alloc()APIs. This requires the use ofdevm_free()as a consequence. It's necessary to scrutinise use of explicitdevm_free()s because they generally indicate there's a concerning corner-case in play, but that's not really the case here. Switch to explicit kmalloc()/kfree() to align expectations with the intent of the code.
Quoted 1 line
Signed-off-by Andrew Jeffery <[email protected]>
Bit more unrelated white space stuff in here that ideally wouldn't be there. Otherwise makes sense to me.
Quoted 126 lines
--- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 28 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_serio.c | 20 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-)drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +19
-9@@ -470,7 +470,7 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)struct kcs_bmc_ipmi *priv;int rc; - priv =devm_kzalloc (kcs_bmc->dev,sizeof (*priv),GFP_KERNEL ); + priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;@@ -482,26 +482,35 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc) priv->client.ops = &kcs_bmc_ipmi_client_ops; priv->miscdev.minor =MISC_DYNAMIC_MINOR ; - priv->miscdev.name =devm_kasprintf (kcs_bmc->dev,GFP_KERNEL ,"%s%u" ,DEVICE_NAME , - kcs_bmc->channel); -if (!priv->miscdev.name) -return -EINVAL ; + priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); +if (!priv->miscdev.name) { + rc = -ENOMEM ; + goto cleanup_priv; + } priv->miscdev.fops = &kcs_bmc_ipmi_fops; rc =misc_register (&priv->miscdev);if (rc) { -dev_err (kcs_bmc->dev,"Unable to register device: %d\n" , rc); -return rc; +pr_err ("Unable to register device: %d\n" , rc); + goto cleanup_miscdev_name; }spin_lock_irq (&kcs_bmc_ipmi_instances_lock);list_add (&priv->entry, &kcs_bmc_ipmi_instances);spin_unlock_irq (&kcs_bmc_ipmi_instances_lock); -dev_info (kcs_bmc->dev,"Initialised IPMI client for channel %d" , kcs_bmc->channel); +pr_info ("Initialised IPMI client for channel %d\n" , kcs_bmc->channel);return 0 ; + +cleanup_miscdev_name: +kfree (priv->miscdev.name); + +cleanup_priv: +kfree (priv); + +return rc; }static void kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc)@@ -523,7 +532,8 @@ static void kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)misc_deregister (&priv->miscdev);kcs_bmc_disable_device (&priv->client); -devm_kfree (kcs_bmc->dev, priv); +kfree (priv->miscdev.name); +kfree (priv); }static const struct kcs_bmc_driver_ops kcs_bmc_ipmi_driver_ops = {drivers/char/ipmi/kcs_bmc_serio.c +13
-7@@ -71,15 +71,18 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_serio *priv;struct serio *port; +int rc; - priv =devm_kzalloc (kcs_bmc->dev,sizeof (*priv),GFP_KERNEL ); + priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;/* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */ port =kzalloc (sizeof (*port),GFP_KERNEL ); -if (!port) -return -ENOMEM ; +if (!port) { + rc = -ENOMEM ; + goto cleanup_priv; + } port->id.type =SERIO_8042 ; port->open = kcs_bmc_serio_open;@@ -98,9 +101,14 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc)serio_register_port (port); -dev_info (kcs_bmc->dev,"Initialised serio client for channel %d" , kcs_bmc->channel); +pr_info ("Initialised serio client for channel %d\n" , kcs_bmc->channel);return 0 ; + +cleanup_priv: +kfree (priv); + +return rc; }static void kcs_bmc_serio_remove_device (struct kcs_bmc_device *kcs_bmc)@@ -122,11 +130,9 @@ static void kcs_bmc_serio_remove_device(struct kcs_bmc_device *kcs_bmc)/* kfree()s priv->port via put_device() */ serio_unregister_port (priv->port); -/* Ensure the IBF IRQ is disabled if we were the active client */ kcs_bmc_disable_device (&priv->client); - -devm_kfree (priv->client.dev->dev, priv); +kfree (priv); }static const struct kcs_bmc_driver_ops kcs_bmc_serio_driver_ops = {
Re: [PATCH 08/10] ipmi: kcs_bmc: Track clients in core
In reply to Andrew JefferyQuoted 10 lines · On Fri, 3 Nov 2023 16:45:20 +1030 Andrew Jeffery…
I ran out of spoons before I could come up with a better client tracking scheme back in the original refactoring series:
https://lore.kernel.org/all/[email protected]/
Jonathan prodded Konstantin about the issue in a review of Konstantin's MCTP patches[1], prompting an attempt to clean it up.
Quoted 8 lines
Prevent client modules from having to track their own instances by requiring they return a pointer to a client object from their
add_device()implementation. We can then track this in the core, and provide it as the argument to theremove_device()implementation to save the client module from further work. The usualcontainer_of()pattern gets the client module access to its private data.Signed-off-by Andrew Jeffery <[email protected]>
Hi Andrew,
A few comments inline.
More generally, whilst this is definitely an improvement I'd have been tempted to make more use of the linux device model for this with the clients added as devices with a parent of the kcs_bmc_device. That would then allow for simple dependency tracking, binding of individual drivers and all that.
What you have here feels fine though and is a much less invasive change.
Jonathan
Quoted 8 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +0
-0@@ -71,8 +71,6 @@ enum kcs_ipmi_errors {
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c enum kcs_ipmi_errors { · line 471
Quoted 10 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +3
-1+static struct kcs_bmc_client * +kcs_bmc_ipmi_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_ipmi *priv;int rc; priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );
As below. I thought it took negatives..
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +0
-0spin_lock_init (&priv->lock);mutex_init (&priv->mutex);init_waitqueue_head (&priv->queue);
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static const struct file_operations kcs_bmc_ipmi_fops = { · line 482
Quoted 10 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +2
-3- priv->client.dev = kcs_bmc; - priv->client.ops = &kcs_bmc_ipmi_client_ops; +kcs_bmc_client_init (&priv->client, &kcs_bmc_ipmi_client_ops, drv, dev); priv->miscdev.minor =MISC_DYNAMIC_MINOR ; - priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); + priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , dev->channel);if (!priv->miscdev.name) { rc = -ENOMEM ;
ERR_PTR
Quoted 1 line
goto
cleanup_priv;
...
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_serio.c +0
-0
...
Quoted 7 lines
drivers/char/ipmi/kcs_bmc_serio.c +2
-0+static struct kcs_bmc_client * +kcs_bmc_serio_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_serio *priv;struct serio *port;@@ -75,12 +71,12 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc)
drivers/char/ipmi/kcs_bmc_serio.c static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) · line 79
Quoted 10 lines
drivers/char/ipmi/kcs_bmc_serio.c +2
-2priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );/* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */ port =kzalloc (sizeof (*port),GFP_KERNEL );if (!port) { - rc = -ENOMEM ; + rc =ENOMEM ;
Why positive?
Doesn't ERR_PTR() typically get passed negatives?
Quoted 34 lines
drivers/char/ipmi/kcs_bmc_serio.c +5
-9goto cleanup_priv; }@@ -88,45 +84,28 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) port->open = kcs_bmc_serio_open; port->close = kcs_bmc_serio_close; port->port_data = priv; - port->dev.parent = kcs_bmc->dev; + port->dev.parent = dev->dev;spin_lock_init (&priv->lock); priv->port = port; - priv->client.dev = kcs_bmc; - priv->client.ops = &kcs_bmc_serio_client_ops; -spin_lock_irq (&kcs_bmc_serio_instances_lock); -list_add (&priv->entry, &kcs_bmc_serio_instances); -spin_unlock_irq (&kcs_bmc_serio_instances_lock); +kcs_bmc_client_init (&priv->client, &kcs_bmc_serio_client_ops, drv, dev);serio_register_port (port); -pr_info ("Initialised serio client for channel %d\n" , kcs_bmc->channel); +pr_info ("Initialised serio client for channel %d\n" , dev->channel); -return 0 ; +return &priv->client; cleanup_priv:kfree (priv); -return rc; +return ERR_PTR (rc); }
Re: [PATCH 09/10] ipmi: kcs_bmc: Add module_kcs_bmc_driver()
In reply to Andrew JefferyQuoted 4 lines · On Fri, 3 Nov 2023 16:45:21 +1030 Andrew Jeffery…
Remove some cruft in the client modules by adding the usual module macro for the KCS subsystem.
Signed-off-by Andrew Jeffery <[email protected]>
Good stuff
Re: [PATCH 10/10] ipmi: kcs_bmc: Add subsystem kerneldoc
In reply to Andrew JefferyQuoted 4 lines · On Fri, 3 Nov 2023 16:45:22 +1030 Andrew Jeffery…
Provide kerneldoc describing the relationships between and the behaviours of the structures and functions of the KCS subsystem.
Signed-off-by Andrew Jeffery <[email protected]>
Seems reasonable but I've only a superficial idea of how this all fits together so no tag from me.
There is the fun question of whether function documentation should be next to the implementation or in the header. As long as it's consistent in a given subsystem I don't personally thing it matters that much.
Jonathan
Re: [PATCH 05/10] ipmi: kcs_bmc: Define client actions in terms of kcs_bmc_client
In reply to Andrew JefferyQuoted 19 lines · On Fri, 3 Nov 2023 16:45:17 +1030 Andrew Jeffery…
Operations such as reading and writing from hardware and updating the events of interest are operations in which the client is interested, but are applied to the device. Strengthen the concept of the client in the subsystem and clean up some call-sites by translating between the client and device types in the core of the KCS subsystem.
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/char/ipmi/kcs_bmc.c | 67 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 50 ++++++++++---------- drivers/char/ipmi/kcs_bmc_client.h | 15 +++--- drivers/char/ipmi/kcs_bmc_serio.c | 10 ++-- 4 files changed, 81 insertions(+), 61 deletions(-)drivers/char/ipmi/kcs_bmc.c +0
-0
drivers/char/ipmi/kcs_bmc.c static LIST_HEAD(kcs_bmc_drivers); · line 27
Quoted 8 lines
drivers/char/ipmi/kcs_bmc.c +2
-2@@ -22,33 +22,53 @@ static LIST_HEAD(kcs_bmc_drivers);/* Consumer data access */ -u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_client_validate (struct kcs_bmc_client *client) { -return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); +WARN_ONCE (client !=READ_ONCE (client->dev->client),"KCS client confusion detected" );
Is this intended as runtime validation or to catch bugs? If just catch bugs then fair enough.
With that question answered based on my somewhat vague understanding of the kcs subsystem.
Quoted 1 line
drivers/char/ipmi/kcs_bmc.c +1
-0+}
Re: [PATCH 02/10] ipmi: kcs_bmc: Include spinlock.h
In reply to Jonathan CameronQuoted 10 lines · Jonathan Cameron
Quoted 5 lines · On Fri, 3 Nov 2023 16:45:14 +1030 Andrew Jeffery…
struct
kcs_bmc_devicedefines a spinlock member but the header in which it is defined failed to include the spinlock header. In the spirit of include-what-you-use, do what's necessary.Signed-off-by Andrew Jeffery <[email protected]>This is fine, but whilst checking it I noticed there is no forwards def of struct device or appropriate include.
Ah, I'll fix that too.
clangd automatically added the spinlock include at one point and so I figured I'd capture it.
Andrew
Re: [PATCH 03/10] ipmi: kcs_bmc: Make kcs_bmc_update_event_mask() static
In reply to Jonathan CameronQuoted 7 lines · Jonathan Cameron
Quoted 3 lines · On Fri, 3 Nov 2023 16:45:15 +1030 Andrew Jeffery…
There were no users outside the subsystem core, so let's not expose it.
Signed-off-by Andrew Jeffery <[email protected]>Is it worth having the wrapper?
Perhaps not, though aesthetically I prefer it. Also the diff is at least slightly smaller by not removing it entirely :)
Quoted 5 lines
I guess all the other cases do have wrappers (even if that's because they continue to be exported) so fair enough.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks,
Andrew
Re: [PATCH 04/10] ipmi: kcs_bmc: Make remove_device() callback return void
In reply to Jonathan CameronQuoted 5 lines · Jonathan Cameron
Quoted 2 lines · On Fri, 3 Nov 2023 16:45:16 +1030 Andrew Jeffery…
Don't pretend there's a valid failure path when there's not.
Quoted 7 lines
Quoted 1 line
Signed-off-by Andrew Jeffery <[email protected]>Whilst I agree returning an error code is pointless, it is perhaps useful to make sure there is a
dev_err()or similar in the paths now that you've remove the one at the call site.Minor point and up to you if you want to or not.
No, that's reasonable. I'll address this in v2.
Andrew
Re: [PATCH 06/10] ipmi: kcs_bmc: Integrate buffers into driver struct
In reply to Jonathan CameronQuoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Fri, 3 Nov 2023 16:45:18 +1030 Andrew Jeffery…
Consolidate several necessary allocations into one to reduce the number of possible error paths.
Quoted 6 lines
Quoted 1 line
Signed-off-by Andrew Jeffery <[email protected]>Gets rid of some of the
devm_kfree()fun, so I'm in favor of the change :)One trivial comment inline.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks.
Quoted 6 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc) · line 481
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +0
-1@@ -478,19 +476,15 @@ static int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)spin_lock_init (&priv->lock);mutex_init (&priv->mutex); -Unrelated change...
Ack, will drop in v2.
Re: [PATCH 07/10] ipmi: kcs_bmc: Disassociate client from device lifetimes
In reply to Jonathan CameronQuoted 15 lines · Jonathan Cameron
Quoted 12 lines · On Fri, 3 Nov 2023 16:45:19 +1030 Andrew Jeffery…
KCS client modules may be removed by actions unrelated to KCS devices. As usual, removing a KCS client module requires unbinding all client instances from the underlying devices to prevent further use of the code.
Previously, KCS client resource lifetimes were tied to the underlying KCS device instance with the use of
devm_k[mz]alloc()APIs. This requires the use ofdevm_free()as a consequence. It's necessary to scrutinise use of explicitdevm_free()s because they generally indicate there's a concerning corner-case in play, but that's not really the case here. Switch to explicit kmalloc()/kfree() to align expectations with the intent of the code.
Quoted 2 lines
Quoted 1 line
Signed-off-by Andrew Jeffery <[email protected]>Bit more unrelated white space stuff in here that ideally wouldn't be there.
Ack, I'll address that for v2.
Quoted 3 lines
Otherwise makes sense to me.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks,
Andrew
Re: [PATCH 08/10] ipmi: kcs_bmc: Track clients in core
In reply to Jonathan CameronQuoted 24 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 3 Nov 2023 16:45:20 +1030 Andrew Jeffery…
I ran out of spoons before I could come up with a better client tracking scheme back in the original refactoring series:
https://lore.kernel.org/all/[email protected]/
Jonathan prodded Konstantin about the issue in a review of Konstantin's MCTP patches[1], prompting an attempt to clean it up.
Quoted 8 lines
Prevent client modules from having to track their own instances by requiring they return a pointer to a client object from their
add_device()implementation. We can then track this in the core, and provide it as the argument to theremove_device()implementation to save the client module from further work. The usualcontainer_of()pattern gets the client module access to its private data.Signed-off-by Andrew Jeffery <[email protected]>Hi Andrew,
Quoted 7 lines
A few comments inline.
More generally, whilst this is definitely an improvement I'd have been tempted to make more use of the linux device model for this with the clients added as devices with a parent of thekcs_bmc_device. That would then allow for simple dependency tracking, binding of individual drivers and all that.What you have here feels fine though and is a much less invasive change.
Yeah, I had this debate with myself before posting the patches. My reasoning for the current approach is that the clients don't typically represent a device, rather a protocol implementation that is communicated over a KCS device (maybe more like pairing a line discipline with a UART). It was unclear to me whether associating a struct device with a protocol implementation was stretching the abstraction a bit, or whether I haven't considered some other perspective hard enough - maybe we treat the client as the remote device, similar to e.g. a struct i2c_client?
Quoted 18 lines
Jonathan
Quoted 14 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +2
-0@@ -71,8 +71,6 @@ enum kcs_ipmi_errors { +static struct kcs_bmc_client * +kcs_bmc_ipmi_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_ipmi *priv;int rc;
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static const struct file_operations kcs_bmc_ipmi_fops = { · line 471
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-1priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );As below. I thought it took negatives..
I should have double checked that. It requires negatives. Thanks.
Quoted 9 lines
Quoted 9 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-2spin_lock_init (&priv->lock);mutex_init (&priv->mutex);init_waitqueue_head (&priv->queue); - priv->client.dev = kcs_bmc; - priv->client.ops = &kcs_bmc_ipmi_client_ops; +kcs_bmc_client_init (&priv->client, &kcs_bmc_ipmi_client_ops, drv, dev);
Quoted 6 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static const struct file_operations kcs_bmc_ipmi_fops = { · line 482
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-1priv->miscdev.minor =MISC_DYNAMIC_MINOR ; - priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); + priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , dev->channel);if (!priv->miscdev.name) { rc = -ENOMEM ;
ERR_PTR
I converted it to an ERR_PTR in the return after the cleanup_priv label. Maybe it's preferable I do the conversion immediately? Easy enough to change if you think so.
Quoted 26 lines
Quoted 1 line
goto
cleanup_priv;...
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_serio.c +0
-0...
Quoted 12 lines
drivers/char/ipmi/kcs_bmc_serio.c +3
-1+static struct kcs_bmc_client * +kcs_bmc_serio_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_serio *priv;struct serio *port;@@ -75,12 +71,12 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );
Quoted 7 lines
drivers/char/ipmi/kcs_bmc_serio.c static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) · line 79
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_serio.c +1
-1/* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */ port =kzalloc (sizeof (*port),GFP_KERNEL );if (!port) { - rc = -ENOMEM ; + rc =ENOMEM ;Why positive?
Doesn'tERR_PTR()typically get passed negatives?
Ack, as above.
Thanks for the review,
Andrew
Re: [PATCH 10/10] ipmi: kcs_bmc: Add subsystem kerneldoc
In reply to Jonathan CameronQuoted 9 lines · Jonathan Cameron
Quoted 4 lines · On Fri, 3 Nov 2023 16:45:22 +1030 Andrew Jeffery…
Provide kerneldoc describing the relationships between and the behaviours of the structures and functions of the KCS subsystem.
Signed-off-by Andrew Jeffery <[email protected]>Seems reasonable but I've only a superficial idea of how this all fits together so no tag from me.
Thanks for the reviews so far!
Quoted 5 lines
There is the fun question of whether function documentation should be next to the implementation or in the header. As long as it's consistent in a given subsystem I don't personally thing it matters that much.
Happy to put it where people prefer. I like the consistency of having it all in the one spot, but I appreciate the idea that it might be easier to maintain alongside the implementation.
Andrew
Re: [PATCH 05/10] ipmi: kcs_bmc: Define client actions in terms of kcs_bmc_client
In reply to Jonathan CameronQuoted 25 lines · Jonathan Cameron
Quoted 22 lines · On Fri, 3 Nov 2023 16:45:17 +1030 Andrew Jeffery…
Operations such as reading and writing from hardware and updating the events of interest are operations in which the client is interested, but are applied to the device. Strengthen the concept of the client in the subsystem and clean up some call-sites by translating between the client and device types in the core of the KCS subsystem.
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/char/ipmi/kcs_bmc.c | 67 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 50 ++++++++++---------- drivers/char/ipmi/kcs_bmc_client.h | 15 +++--- drivers/char/ipmi/kcs_bmc_serio.c | 10 ++-- 4 files changed, 81 insertions(+), 61 deletions(-)drivers/char/ipmi/kcs_bmc.c +0
-0@@ -22,33 +22,53 @@ static LIST_HEAD(kcs_bmc_drivers);/* Consumer data access */
Quoted 8 lines
drivers/char/ipmi/kcs_bmc.c static LIST_HEAD(kcs_bmc_drivers); · line 27
Quoted 5 lines
drivers/char/ipmi/kcs_bmc.c +2
-2-u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc) +static void kcs_bmc_client_validate (struct kcs_bmc_client *client) { -return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); +WARN_ONCE (client !=READ_ONCE (client->dev->client),"KCS client confusion detected" );Is this intended as runtime validation or to catch bugs? If just catch bugs then fair enough.
Ah, I think I missed replying here.
So for "runtime validation" I assume you mean "things userspace might do that are not valid - the error condition should be detected and punted back to userspace", vs "catch bugs" meaning "the implementation in the kernel failed to uphold an invariant and now there are Problems".
If that sounds accurate, then it's the latter: The WARN_ONCE() is asserting "don't operate on a client that doesn't own the device". It isn't an error that can be punted back for handling in userspace as it should not be possible for the kernel to get into this state to begin with. If we reach this state it's an error in the programming of the kernel module that's a client of the KCS subsystem.
Quoted 3 lines
With that question answered based on my somewhat vague understanding of the kcs subsystem.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks,
Andrew
Re: [PATCH 08/10] ipmi: kcs_bmc: Track clients in core
In reply to Andrew JefferyQuoted 25 lines · On Mon, 06 Nov 2023 10:26:38 +1030 Andrew Jeffery…
Quoted 24 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 3 Nov 2023 16:45:20 +1030 Andrew Jeffery…
I ran out of spoons before I could come up with a better client tracking scheme back in the original refactoring series:
https://lore.kernel.org/all/[email protected]/
Jonathan prodded Konstantin about the issue in a review of Konstantin's MCTP patches[1], prompting an attempt to clean it up.
Quoted 8 lines
Prevent client modules from having to track their own instances by requiring they return a pointer to a client object from their
add_device()implementation. We can then track this in the core, and provide it as the argument to theremove_device()implementation to save the client module from further work. The usualcontainer_of()pattern gets the client module access to its private data.Signed-off-by Andrew Jeffery <[email protected]>Hi Andrew,
Quoted 8 lines
Quoted 7 lines
A few comments inline.
More generally, whilst this is definitely an improvement I'd have been tempted to make more use of the linux device model for this with the clients added as devices with a parent of thekcs_bmc_device. That would then allow for simple dependency tracking, binding of individual drivers and all that.What you have here feels fine though and is a much less invasive change.
Sorry for slow reply, been traveling.
Quoted 9 lines
Yeah, I had this debate with myself before posting the patches. My reasoning for the current approach is that the clients don't typically represent a device, rather a protocol implementation that is communicated over a KCS device (maybe more like pairing a line discipline with a UART). It was unclear to me whether associating a
struct devicewith a protocol implementation was stretching the abstraction a bit, or whether I haven't considered some other perspective hard enough - maybe we treat the client as the remote device, similar to e.g. astruct i2c_client?
That was my thinking. The protocol is used to talk to someone - the endpoint (similar to i2c_client) so represent that. If that device is handling multiple protocols (no idea if that is possible) - that is fine as well, it just becomes like having multiple i2c_clients in a single package (fairly common for sensors), or the many other cases where we use a struct device to represent just part of a larger device that operates largely independently of other parts (or with well defined boundaries).
Jonathan
Quoted 43 lines
Quoted 19 lines
Jonathan
Quoted 14 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +2
-0@@ -71,8 +71,6 @@ enum kcs_ipmi_errors { +static struct kcs_bmc_client * +kcs_bmc_ipmi_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_ipmi *priv;int rc;Quoted 5 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static const struct file_operations kcs_bmc_ipmi_fops = { · line 471
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-1priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );As below. I thought it took negatives..
I should have double checked that. It requires negatives. Thanks.
Quoted 9 lines
Quoted 9 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-2spin_lock_init (&priv->lock);mutex_init (&priv->mutex);init_waitqueue_head (&priv->queue); - priv->client.dev = kcs_bmc; - priv->client.ops = &kcs_bmc_ipmi_client_ops; +kcs_bmc_client_init (&priv->client, &kcs_bmc_ipmi_client_ops, drv, dev);Quoted 6 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c static const struct file_operations kcs_bmc_ipmi_fops = { · line 482
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +1
-1priv->miscdev.minor =MISC_DYNAMIC_MINOR ; - priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , kcs_bmc->channel); + priv->miscdev.name =kasprintf (GFP_KERNEL ,"%s%u" ,DEVICE_NAME , dev->channel);if (!priv->miscdev.name) { rc = -ENOMEM ;
ERR_PTR
Quoted 3 lines
I converted it to an
ERR_PTRin the return after thecleanup_privlabel. Maybe it's preferable I do the conversion immediately? Easy enough to change if you think so.
I'm not that fussed either way.
Quoted 40 lines
Quoted 27 lines
Quoted 2 lines
goto
cleanup_priv;...
Quoted 4 lines
drivers/char/ipmi/kcs_bmc_serio.c +0
-0...
Quoted 12 lines
drivers/char/ipmi/kcs_bmc_serio.c +3
-1+static struct kcs_bmc_client * +kcs_bmc_serio_add_device (struct kcs_bmc_driver *drv,struct kcs_bmc_device *dev) {struct kcs_bmc_serio *priv;struct serio *port;@@ -75,12 +71,12 @@ static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) priv =kzalloc (sizeof (*priv),GFP_KERNEL );if (!priv) -return -ENOMEM ; +return ERR_PTR (ENOMEM );Quoted 7 lines
drivers/char/ipmi/kcs_bmc_serio.c static int kcs_bmc_serio_add_device(struct kcs_bmc_device *kcs_bmc) · line 79
Quoted 5 lines
drivers/char/ipmi/kcs_bmc_serio.c +1
-1/* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */ port =kzalloc (sizeof (*port),GFP_KERNEL );if (!port) { - rc = -ENOMEM ; + rc =ENOMEM ;Why positive?
Doesn'tERR_PTR()typically get passed negatives?Ack, as above.
Thanks for the review,
Andrew
Re: [PATCH 08/10] ipmi: kcs_bmc: Track clients in core
Quoted 34 lines · Jonathan Cameron
Quoted 31 lines · On Mon, 06 Nov 2023 10:26:38 +1030 Andrew Jeffery…
Quoted 30 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 3 Nov 2023 16:45:20 +1030 Andrew Jeffery…
I ran out of spoons before I could come up with a better client tracking scheme back in the original refactoring series:
https://lore.kernel.org/all/[email protected]/
Jonathan prodded Konstantin about the issue in a review of Konstantin's MCTP patches[1], prompting an attempt to clean it up.
Quoted 8 lines
Prevent client modules from having to track their own instances by requiring they return a pointer to a client object from their
add_device()implementation. We can then track this in the core, and provide it as the argument to theremove_device()implementation to save the client module from further work. The usualcontainer_of()pattern gets the client module access to its private data.Signed-off-by Andrew Jeffery <[email protected]>Hi Andrew,
A few comments inline.
More generally, whilst this is definitely an improvement I'd have been tempted to make more use of the linux device model for this with the clients added as devices with a parent of thekcs_bmc_device. That would then allow for simple dependency tracking, binding of individual drivers and all that.
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
What you have here feels fine though and is a much less invasive change.
Sorry for slow reply, been traveling.
No worries, I've just got back from travel myself :)
Quoted 11 lines
Quoted 10 lines
Yeah, I had this debate with myself before posting the patches. My reasoning for the current approach is that the clients don't typically represent a device, rather a protocol implementation that is communicated over a KCS device (maybe more like pairing a line discipline with a UART). It was unclear to me whether associating a
struct devicewith a protocol implementation was stretching the abstraction a bit, or whether I haven't considered some other perspective hard enough - maybe we treat the client as the remote device, similar to e.g. astruct i2c_client?
Quoted 7 lines
That was my thinking. The protocol is used to talk to someone - the endpoint (similar to
i2c_client) so represent that. If that device is handling multiple protocols (no idea if that is possible) - that is fine as well, it just becomes like having multiplei2c_clientsin a single package (fairly common for sensors), or the many other cases where we use a struct device to represent just part of a larger device that operates largely independently of other parts (or with well defined boundaries).
Alright, let me think about that a bit more.
Thanks for the feedback,
Andrew