Improvements to PCIe Endpoint Core
Subsystems (2)
cover Improvements to PCIe Endpoint Core
This series adds miscellaneous improvements to PCIe endpoint core.
1) Protect concurrent access to memory allocation in pci-epc-mem
2) Replace spinlock with mutex in pci-epc-core and also use notification chain mechanism to notify EPC events to EPF driver.
3) Since endpoint function device can be created by multiple mechanisms (configfs, devicetree, etc..), allowing each of these mechanisms to assign a function number would result in mutliple endpoint function devices having the same function number. In order to avoid this, let EPC core assign a function number to the endpoint device.
This series is created on top of [1] and [2]
[1] -> http://lore.kernel.org/r/[email protected]
[2] -> http://lore.kernel.org/r/[email protected]
Kishon Vijay Abraham I (5):
PCI: endpoint: Use notification chain mechanism to notify EPC events
to EPF
PCI: endpoint: Replace spinlock with mutex
PCI: endpoint: Protect concurrent access to memory allocation with
mutex
PCI: endpoint: Protect concurrent access to pci_epf_ops with mutex
PCI: endpoint: Assign function number for each PF in EPC core
drivers/pci/endpoint/functions/pci-epf-test.c | 13 +- drivers/pci/endpoint/pci-ep-cfs.c | 27 +---- drivers/pci/endpoint/pci-epc-core.c | 113 ++++++++---------- drivers/pci/endpoint/pci-epc-mem.c | 10 +- drivers/pci/endpoint/pci-epf-core.c | 33 ++--- include/linux/pci-epc.h | 19 ++- include/linux/pci-epf.h | 9 +- 7 files changed, 108 insertions(+), 116 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 PCI: endpoint: Use notification chain mechanism to notify EPC events to EPF
Use atomic_notifier_call_chain() to notify EPC events like linkup to EPF driver instead of using linkup ops in EPF driver. This is in preparation for adding proper locking mechanism to EPF ops. This will also enable to add more events (in addition to linkup) in the future.
--- drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++++++++--- drivers/pci/endpoint/pci-epc-core.c | 9 ++------ drivers/pci/endpoint/pci-epf-core.c | 22 +------------------ include/linux/pci-epc.h | 8 +++++++ include/linux/pci-epf.h | 6 ++--- 5 files changed, 23 insertions(+), 35 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +10 -3
@@ -361,12 +361,16 @@ static void pci_epf_test_cmd_handler(struct work_struct *work) msecs_to_jiffies(1)); } -static void pci_epf_test_linkup (struct pci_epf *epf) +static int pci_epf_test_notifier (struct notifier_block *nb,unsigned long val, +void *data) { + struct pci_epf *epf = container_of(nb, struct pci_epf, nb);struct pci_epf_test *epf_test =epf_get_drvdata (epf);queue_delayed_work (kpcitest_workqueue, &epf_test->cmd_handler,msecs_to_jiffies (1 )); + +return NOTIFY_OK ; }static void pci_epf_test_unbind (struct pci_epf *epf)
@@ -565,8 +569,12 @@ static int pci_epf_test_bind(struct pci_epf *epf) } } -if (!linkup_notifier) +if (linkup_notifier) { + epf->nb.notifier_call = pci_epf_test_notifier; +pci_epc_register_notifier (epc, &epf->nb); + }else {queue_work (kpcitest_workqueue, &epf_test->cmd_handler.work); + }return 0 ; }
@@ -599,7 +607,6 @@ static int pci_epf_test_probe(struct pci_epf *epf)static struct pci_epf_ops ops = { .unbind = pci_epf_test_unbind, .bind = pci_epf_test_bind, - .linkup = pci_epf_test_linkup, };static struct pci_epf_driver test_driver = {
drivers/pci/endpoint/pci-epc-core.c +2 -7
@@ -542,16 +542,10 @@ EXPORT_SYMBOL_GPL(pci_epc_remove_epf);*/ void pci_epc_linkup (struct pci_epc *epc) { -unsigned long flags; -struct pci_epf *epf; -if (!epc ||IS_ERR (epc))return ; -spin_lock_irqsave (&epc->lock, flags); -list_for_each_entry (epf, &epc->pci_epf, list) -pci_epf_linkup (epf); -spin_unlock_irqrestore (&epc->lock, flags); +atomic_notifier_call_chain (&epc->notifier,0 ,NULL ); }EXPORT_SYMBOL_GPL (pci_epc_linkup);
@@ -615,6 +609,7 @@ __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,spin_lock_init (&epc->lock);INIT_LIST_HEAD (&epc->pci_epf); +ATOMIC_INIT_NOTIFIER_HEAD (&epc->notifier);device_initialize (&epc->dev); epc->dev.class = pci_epc_class;
drivers/pci/endpoint/pci-epf-core.c +1 -21
@@ -20,26 +20,6 @@ static DEFINE_MUTEX(pci_epf_mutex);static struct bus_type pci_epf_bus_type;static const struct device_type pci_epf_type; -/** -* pci_epf_linkup() - Notify the function driver that EPC device has -* established a connection with the Root Complex. -* @epf: the EPF device bound to the EPC device which has established -* the connection with the host -* -* Invoke to notify the function driver that EPC device has established -* a connection with the Root Complex. -*/ -void pci_epf_linkup (struct pci_epf *epf) -{ -if (!epf->driver) { -dev_WARN (&epf->dev,"epf device not bound to driver\n" ); -return ; - } - - epf->driver->ops->linkup (epf); -} -EXPORT_SYMBOL_GPL (pci_epf_linkup); -/** * pci_epf_unbind() - Notify the function driver that the binding between the * EPF device and EPC device has been lost
@@ -216,7 +196,7 @@ int __pci_epf_register_driver(struct pci_epf_driver *driver,if (!driver->ops)return -EINVAL ; -if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup) +if (!driver->ops->bind || !driver->ops->unbind)return -EINVAL ; driver->driver.bus = &pci_epf_bus_type;
include/linux/pci-epc.h +8 -0
@@ -90,6 +90,7 @@ struct pci_epc_mem {* @max_functions: max number of functions that can be configured in this EPC * @group: configfs group representing the PCI EPC device * @lock: spinlock to protect pci_epc ops +* @notifier: used to notify EPF of any EPC events (like linkup) */ struct pci_epc {struct device dev;
@@ -100,6 +101,7 @@ struct pci_epc {struct config_group *group;/* spinlock to protect against concurrent access of EP controller */ spinlock_t lock; +struct atomic_notifier_head notifier; };/**
@@ -142,6 +144,12 @@ static inline void *epc_get_drvdata(struct pci_epc *epc)return dev_get_drvdata (&epc->dev); } +static inline int +pci_epc_register_notifier (struct pci_epc *epc,struct notifier_block *nb) +{ +return atomic_notifier_chain_register (&epc->notifier, nb); +} +struct pci_epc *__devm_pci_epc_create (struct device *dev,const struct pci_epc_ops *ops,struct module *owner);
include/linux/pci-epf.h +2 -4
@@ -55,13 +55,10 @@ struct pci_epf_header {* @bind: ops to perform when a EPC device has been bound to EPF device * @unbind: ops to perform when a binding has been lost between a EPC device * and EPF device -* @linkup: ops to perform when the EPC device has established a connection with -* a host system */ struct pci_epf_ops {int (*bind)(struct pci_epf *epf);void (*unbind)(struct pci_epf *epf); -void (*linkup)(struct pci_epf *epf); };/**
@@ -114,6 +111,7 @@ struct pci_epf_bar {* @epc: the EPC device to which this EPF device is bound * @driver: the EPF driver to which this EPF device is bound * @list: to add pci_epf as a list of PCI endpoint functions to pci_epc +* @nb: notifier block to notify EPF of any EPC events (like linkup) */ struct pci_epf {struct device dev;
@@ -127,6 +125,7 @@ struct pci_epf {struct pci_epc *epc;struct pci_epf_driver *driver;struct list_head list; +struct notifier_block nb; };/**
@@ -169,5 +168,4 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,void pci_epf_free_space (struct pci_epf *epf,void *addr,enum pci_barno bar);int pci_epf_bind (struct pci_epf *epf);void pci_epf_unbind (struct pci_epf *epf); -void pci_epf_linkup (struct pci_epf *epf);#endif /* __LINUX_PCI_EPF_H */
Signature · 1 line
2.17.1
PATCH 2/5 PCI: endpoint: Replace spinlock with mutex
In reply to Kishon Vijay Abraham IThe pci_epc_ops is not intended to be invoked from interrupt context. Hence replace spin_lock_irqsave and spin_unlock_irqrestore with mutex_lock and mutex_unlock respectively.
--- drivers/pci/endpoint/pci-epc-core.c | 82 +++++++++++------------------ include/linux/pci-epc.h | 6 +-- 2 files changed, 34 insertions(+), 54 deletions(-)
drivers/pci/endpoint/pci-epc-core.c +31 -51
@@ -120,7 +120,6 @@ const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, u8 func_no) {const struct pci_epc_features *epc_features; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return NULL ;
@@ -128,9 +127,9 @@ const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,if (!epc->ops->get_features)return NULL ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); epc_features = epc->ops->get_features (epc, func_no); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return epc_features; }
@@ -144,14 +143,12 @@ EXPORT_SYMBOL_GPL(pci_epc_get_features);*/ void pci_epc_stop (struct pci_epc *epc) { -unsigned long flags; -if (IS_ERR (epc) || !epc->ops->stop)return ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); epc->ops->stop (epc); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock); }EXPORT_SYMBOL_GPL (pci_epc_stop);
@@ -164,7 +161,6 @@ EXPORT_SYMBOL_GPL(pci_epc_stop);int pci_epc_start (struct pci_epc *epc) {int ret; -unsigned long flags;if (IS_ERR (epc))return -EINVAL ;
@@ -172,9 +168,9 @@ int pci_epc_start(struct pci_epc *epc)if (!epc->ops->start)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->start (epc); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -193,7 +189,6 @@ int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, enum pci_epc_irq_type type, u16 interrupt_num) { int ret; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return -EINVAL ;
@@ -201,9 +196,9 @@ int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,if (!epc->ops->raise_irq)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->raise_irq (epc, func_no, type, interrupt_num); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -219,7 +214,6 @@ EXPORT_SYMBOL_GPL(pci_epc_raise_irq);int pci_epc_get_msi (struct pci_epc *epc,u8 func_no) {int interrupt; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return 0 ;
@@ -227,9 +221,9 @@ int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)if (!epc->ops->get_msi)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); interrupt = epc->ops->get_msi (epc, func_no); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);if (interrupt <0 )return 0 ;
@@ -252,7 +246,6 @@ int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts) {int ret;u8 encode_int; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions || interrupts >32 )
@@ -263,9 +256,9 @@ int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts) encode_int =order_base_2 (interrupts); -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->set_msi (epc, func_no, encode_int); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -281,7 +274,6 @@ EXPORT_SYMBOL_GPL(pci_epc_set_msi);int pci_epc_get_msix (struct pci_epc *epc,u8 func_no) {int interrupt; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return 0 ;
@@ -289,9 +281,9 @@ int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)if (!epc->ops->get_msix)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); interrupt = epc->ops->get_msix (epc, func_no); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);if (interrupt <0 )return 0 ;
@@ -314,7 +306,6 @@ int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts, enum pci_barno bir, u32 offset) { int ret; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions || interrupts <1 || interrupts >2048 )
@@ -323,9 +314,9 @@ int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,if (!epc->ops->set_msix)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->set_msix (epc, func_no, interrupts -1 , bir, offset); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -342,17 +333,15 @@ EXPORT_SYMBOL_GPL(pci_epc_set_msix);void pci_epc_unmap_addr (struct pci_epc *epc,u8 func_no,phys_addr_t phys_addr) { -unsigned long flags; -if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return ;if (!epc->ops->unmap_addr)return ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); epc->ops->unmap_addr (epc, func_no, phys_addr); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock); }EXPORT_SYMBOL_GPL (pci_epc_unmap_addr);
@@ -370,7 +359,6 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, phys_addr_t phys_addr, u64 pci_addr, size_t size) { int ret; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return -EINVAL ;
@@ -378,9 +366,9 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,if (!epc->ops->map_addr)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->map_addr (epc, func_no, phys_addr, pci_addr, size); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -397,8 +385,6 @@ EXPORT_SYMBOL_GPL(pci_epc_map_addr);void pci_epc_clear_bar (struct pci_epc *epc,u8 func_no,struct pci_epf_bar *epf_bar) { -unsigned long flags; -if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions || (epf_bar->barno ==BAR_5 && epf_bar->flags &PCI_BASE_ADDRESS_MEM_TYPE_64 ))
@@ -407,9 +393,9 @@ void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,if (!epc->ops->clear_bar)return ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); epc->ops->clear_bar (epc, func_no, epf_bar); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock); }EXPORT_SYMBOL_GPL (pci_epc_clear_bar);
@@ -425,7 +411,6 @@ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, struct pci_epf_bar *epf_bar) {int ret; -unsigned long irq_flags;int flags = epf_bar->flags; if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
@@ -440,9 +425,9 @@ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,if (!epc->ops->set_bar)return 0 ; -spin_lock_irqsave (&epc->lock, irq_flags); +mutex_lock (&epc->lock); ret = epc->ops->set_bar (epc, func_no, epf_bar); -spin_unlock_irqrestore (&epc->lock, irq_flags); +mutex_unlock (&epc->lock);return ret; }
@@ -463,7 +448,6 @@ int pci_epc_write_header(struct pci_epc *epc, u8 func_no, struct pci_epf_header *header) {int ret; -unsigned long flags;if (IS_ERR_OR_NULL (epc) || func_no >= epc->max_functions)return -EINVAL ;
@@ -471,9 +455,9 @@ int pci_epc_write_header(struct pci_epc *epc, u8 func_no,if (!epc->ops->write_header)return 0 ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock); ret = epc->ops->write_header (epc, func_no, header); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return ret; }
@@ -490,8 +474,6 @@ EXPORT_SYMBOL_GPL(pci_epc_write_header);*/ int pci_epc_add_epf (struct pci_epc *epc,struct pci_epf *epf) { -unsigned long flags; -if (epf->epc)return -EBUSY ;
@@ -503,9 +485,9 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf) epf->epc = epc; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock);list_add_tail (&epf->list, &epc->pci_epf); -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock);return 0 ; }
@@ -520,15 +502,13 @@ EXPORT_SYMBOL_GPL(pci_epc_add_epf);*/ void pci_epc_remove_epf (struct pci_epc *epc,struct pci_epf *epf) { -unsigned long flags; -if (!epc ||IS_ERR (epc) || !epf)return ; -spin_lock_irqsave (&epc->lock, flags); +mutex_lock (&epc->lock);list_del (&epf->list); epf->epc =NULL ; -spin_unlock_irqrestore (&epc->lock, flags); +mutex_unlock (&epc->lock); }EXPORT_SYMBOL_GPL (pci_epc_remove_epf);
@@ -607,7 +587,7 @@ __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, goto err_ret; } -spin_lock_init (&epc->lock); +mutex_init (&epc->lock);INIT_LIST_HEAD (&epc->pci_epf);ATOMIC_INIT_NOTIFIER_HEAD (&epc->notifier);
include/linux/pci-epc.h +3 -3
@@ -89,7 +89,7 @@ struct pci_epc_mem {* @mem: address space of the endpoint controller * @max_functions: max number of functions that can be configured in this EPC * @group: configfs group representing the PCI EPC device -* @lock: spinlock to protect pci_epc ops +* @lock: mutex to protect pci_epc ops * @notifier: used to notify EPF of any EPC events (like linkup) */ struct pci_epc {
@@ -99,8 +99,8 @@ struct pci_epc {struct pci_epc_mem *mem;u8 max_functions;struct config_group *group; -/* spinlock to protect against concurrent access of EP controller */ -spinlock_t lock; +/* mutex to protect against concurrent access of EP controller */ +struct mutex lock;struct atomic_notifier_head notifier; };
Signature · 1 line
2.17.1
PATCH 4/5 PCI: endpoint: Protect concurrent access to pci_epf_ops with mutex
In reply to Kishon Vijay Abraham IProtect concurrent access to pci_epf_ops with mutex.
--- drivers/pci/endpoint/pci-epf-core.c | 11 ++++++++++- include/linux/pci-epf.h | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/pci/endpoint/pci-epf-core.c +10 -1
@@ -35,7 +35,9 @@ void pci_epf_unbind(struct pci_epf *epf)return ; } +mutex_lock (&epf->lock); epf->driver->ops->unbind (epf); +mutex_unlock (&epf->lock);module_put (epf->driver->owner); }EXPORT_SYMBOL_GPL (pci_epf_unbind);
@@ -49,6 +51,8 @@ EXPORT_SYMBOL_GPL(pci_epf_unbind);*/ int pci_epf_bind (struct pci_epf *epf) { +int ret; +if (!epf->driver) {dev_WARN (&epf->dev,"epf device not bound to driver\n" );return -EINVAL ;
@@ -57,7 +61,11 @@ int pci_epf_bind(struct pci_epf *epf)if (!try_module_get (epf->driver->owner))return -EAGAIN ; -return epf->driver->ops->bind (epf); +mutex_lock (&epf->lock); + ret = epf->driver->ops->bind (epf); +mutex_unlock (&epf->lock); + +return ret; }EXPORT_SYMBOL_GPL (pci_epf_bind);
@@ -254,6 +262,7 @@ struct pci_epf *pci_epf_create(const char *name)device_initialize (dev); dev->bus = &pci_epf_bus_type; dev->type = &pci_epf_type; +mutex_init (&epf->lock); ret =dev_set_name (dev,"%s" , name);if (ret) {
include/linux/pci-epf.h +3 -0
@@ -112,6 +112,7 @@ struct pci_epf_bar {* @driver: the EPF driver to which this EPF device is bound * @list: to add pci_epf as a list of PCI endpoint functions to pci_epc * @nb: notifier block to notify EPF of any EPC events (like linkup) +* @lock: mutex to protect pci_epf_ops */ struct pci_epf {struct device dev;
@@ -126,6 +127,8 @@ struct pci_epf {struct pci_epf_driver *driver;struct list_head list;struct notifier_block nb; +/* mutex to protect against concurrent access of pci_epf_ops */ +struct mutex lock; };/**
Signature · 1 line
2.17.1
PATCH 5/5 PCI: endpoint: Assign function number for each PF in EPC core
In reply to Kishon Vijay Abraham IThe PCIe endpoint core relies on the drivers that invoke the pci_epc_add_epf() API to allocate and assign a function number to each physical function (PF). Since endpoint function device can be created by multiple mechanisms (configfs, devicetree, etc..), allowing each of these mechanisms to assign a function number would result in mutliple endpoint function devices having the same function number. In order to avoid this, let EPC core assign a function number to the endpoint device.
--- drivers/pci/endpoint/pci-ep-cfs.c | 27 +++++---------------------- drivers/pci/endpoint/pci-epc-core.c | 26 ++++++++++++++++++++++---- include/linux/pci-epc.h | 2 ++ 3 files changed, 29 insertions(+), 26 deletions(-)
drivers/pci/endpoint/pci-ep-cfs.c +5 -22
@@ -29,7 +29,6 @@ struct pci_epc_group {struct config_group group;struct pci_epc *epc;bool start; -unsigned long function_num_map; };static inline struct pci_epf_group *to_pci_epf_group (struct config_item *item)
@@ -89,37 +88,22 @@ static int pci_epc_epf_link(struct config_item *epc_item, struct config_item *epf_item) {int ret; -u32 func_no =0 ;struct pci_epf_group *epf_group =to_pci_epf_group (epf_item);struct pci_epc_group *epc_group =to_pci_epc_group (epc_item);struct pci_epc *epc = epc_group->epc;struct pci_epf *epf = epf_group->epf; - func_no =find_first_zero_bit (&epc_group->function_num_map, -BITS_PER_LONG ); -if (func_no >=BITS_PER_LONG ) -return -EINVAL ; - -set_bit (func_no, &epc_group->function_num_map); - epf->func_no = func_no; - ret =pci_epc_add_epf (epc, epf);if (ret) - goto err_add_epf; +return ret; ret =pci_epf_bind (epf); -if (ret) - goto err_epf_bind; +if (ret) { +pci_epc_remove_epf (epc, epf); +return ret; + }return 0 ; - -err_epf_bind: -pci_epc_remove_epf (epc, epf); - -err_add_epf: -clear_bit (func_no, &epc_group->function_num_map); - -return ret; } static void pci_epc_epf_unlink(struct config_item *epc_item,
@@ -134,7 +118,6 @@ static void pci_epc_epf_unlink(struct config_item *epc_item, epc = epc_group->epc; epf = epf_group->epf; -clear_bit (epf->func_no, &epc_group->function_num_map);pci_epf_unbind (epf);pci_epc_remove_epf (epc, epf); }
drivers/pci/endpoint/pci-epc-core.c +22 -4
@@ -474,22 +474,39 @@ EXPORT_SYMBOL_GPL(pci_epc_write_header);*/ int pci_epc_add_epf (struct pci_epc *epc,struct pci_epf *epf) { +u32 func_no; +int ret =0 ; +if (epf->epc)return -EBUSY ;if (IS_ERR (epc))return -EINVAL ; -if (epf->func_no > epc->max_functions -1 ) -return -EINVAL ; +mutex_lock (&epc->lock); + func_no =find_first_zero_bit (&epc->function_num_map, +BITS_PER_LONG ); +if (func_no >=BITS_PER_LONG ) { + ret = -EINVAL ; + goto ret; + } + +if (func_no > epc->max_functions -1 ) { +dev_err (&epc->dev,"Exceeding max supported Function Number\n" ); + ret = -EINVAL ; + goto ret; + } +set_bit (func_no, &epc->function_num_map); + epf->func_no = func_no; epf->epc = epc; -mutex_lock (&epc->lock);list_add_tail (&epf->list, &epc->pci_epf); + +ret:mutex_unlock (&epc->lock); -return 0 ; +return ret; }EXPORT_SYMBOL_GPL (pci_epc_add_epf);
@@ -506,6 +523,7 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)return ;mutex_lock (&epc->lock); +clear_bit (epf->func_no, &epc->function_num_map);list_del (&epf->list); epf->epc =NULL ;mutex_unlock (&epc->lock);
include/linux/pci-epc.h +2 -0
@@ -93,6 +93,7 @@ struct pci_epc_mem {* @max_functions: max number of functions that can be configured in this EPC * @group: configfs group representing the PCI EPC device * @lock: mutex to protect pci_epc ops +* @function_num_map: bitmap to manage physical function number * @notifier: used to notify EPF of any EPC events (like linkup) */ struct pci_epc {
@@ -104,6 +105,7 @@ struct pci_epc {struct config_group *group;/* mutex to protect against concurrent access of EP controller */ struct mutex lock; +unsigned long function_num_map;struct atomic_notifier_head notifier; };
Signature · 1 line
2.17.1
PATCH 3/5 PCI: endpoint: Protect concurrent access to memory allocation with mutex
In reply to Kishon Vijay Abraham Ipci-epc-mem uses bitmap to manage the PCI address region. This address region will be shared by multiple endpoint functions (in the case of multi function endpoint) and it has to be protected from concurrent access to avoid updating inconsistent state. Use mutex to protect while updating bitmap.
--- drivers/pci/endpoint/pci-epc-mem.c | 10 ++++++++-- include/linux/pci-epc.h | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-)
drivers/pci/endpoint/pci-epc-mem.c +8 -2
@@ -79,6 +79,7 @@ int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size, mem->page_size = page_size; mem->pages = pages; mem->size = size; +mutex_init (&mem->lock); epc->mem = mem;
@@ -122,7 +123,7 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, phys_addr_t *phys_addr, size_t size) { int pageno; -void __iomem *virt_addr; +void __iomem *virt_addr =NULL ;struct pci_epc_mem *mem = epc->mem;unsigned int page_shift =ilog2 (mem->page_size);int order;
@@ -130,15 +131,18 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, size =ALIGN (size, mem->page_size); order =pci_epc_mem_get_order (mem, size); +mutex_lock (&mem->lock); pageno =bitmap_find_free_region (mem->bitmap, mem->pages, order);if (pageno <0 ) -return NULL ; + goto ret; *phys_addr = mem->phys_base + ((phys_addr_t )pageno << page_shift); virt_addr =ioremap (*phys_addr, size);if (!virt_addr)bitmap_release_region (mem->bitmap, pageno, order); +ret: +mutex_unlock (&mem->lock);return virt_addr; }EXPORT_SYMBOL_GPL (pci_epc_mem_alloc_addr);
@@ -164,7 +168,9 @@ void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr, pageno = (phys_addr - mem->phys_base) >> page_shift; size =ALIGN (size, mem->page_size); order =pci_epc_mem_get_order (mem, size); +mutex_lock (&mem->lock);bitmap_release_region (mem->bitmap, pageno, order); +mutex_unlock (&mem->lock); }EXPORT_SYMBOL_GPL (pci_epc_mem_free_addr);
include/linux/pci-epc.h +3 -0
@@ -72,6 +72,7 @@ struct pci_epc_ops {* @bitmap: bitmap to manage the PCI address space * @pages: number of bits representing the address region * @page_size: size of each page +* @lock: mutex to protect bitmap */ struct pci_epc_mem {phys_addr_t phys_base;
@@ -79,6 +80,8 @@ struct pci_epc_mem {unsigned long *bitmap;size_t page_size;int pages; +/* mutex to protect against concurrent access for memory allocation*/ +struct mutex lock; };/**
Signature · 1 line
2.17.1