* [dpdk-dev] [PATCH] ivshmem: fix race condition
@ 2016-04-01 8:16 Mauricio Vasquez B
2016-04-01 9:01 ` Burakov, Anatoly
2016-04-01 11:22 ` [dpdk-dev] [PATCH v2] " Mauricio Vasquez B
0 siblings, 2 replies; 6+ messages in thread
From: Mauricio Vasquez B @ 2016-04-01 8:16 UTC (permalink / raw)
To: Anatoly Burakov; +Cc: dev
The memory zone could be freed just after adding it to the metadata
file and just before marking it as not freeable.
This patch changes the locking logic in order to prevent it.
Fixes: cd10c42eb5bc ("mem: fix ivshmem freeing")
Signed-off-by: Mauricio Vasquez B <mauricio.vasquezbernal@studenti.polito.it>
---
lib/librte_ivshmem/rte_ivshmem.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/lib/librte_ivshmem/rte_ivshmem.c b/lib/librte_ivshmem/rte_ivshmem.c
index 8fc4b57..ed2c276 100644
--- a/lib/librte_ivshmem/rte_ivshmem.c
+++ b/lib/librte_ivshmem/rte_ivshmem.c
@@ -471,10 +471,16 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
struct ivshmem_config * config)
{
struct rte_ivshmem_metadata_entry * entry;
- unsigned i;
+ unsigned i, idx;
+ struct rte_mem_config *mcfg;
rte_spinlock_lock(&config->sl);
+ mcfg = rte_eal_get_configuration()->mem_config;
+
+ /* it prevents the memzone being freed while we add it to the metadata */
+ rte_rwlock_write_lock(&mcfg->mlock);
+
/* find free slot in this config */
for (i = 0; i < RTE_DIM(config->metadata->entry); i++) {
entry = &config->metadata->entry[i];
@@ -504,13 +510,6 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
config->metadata->name);
goto fail;
}
-#ifdef RTE_LIBRTE_IVSHMEM
- struct rte_mem_config *mcfg;
- unsigned int idx;
-
- mcfg = rte_eal_get_configuration()->mem_config;
-
- rte_rwlock_write_lock(&mcfg->mlock);
idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
idx = idx / sizeof(struct rte_memzone);
@@ -519,10 +518,10 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
mcfg->memzone[idx].ioremap_addr = mz->phys_addr;
rte_rwlock_write_unlock(&mcfg->mlock);
-#endif
rte_spinlock_unlock(&config->sl);
return 0;
fail:
+ rte_rwlock_write_unlock(&mcfg->mlock);
rte_spinlock_unlock(&config->sl);
return -1;
}
--
1.9.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH] ivshmem: fix race condition
2016-04-01 8:16 [dpdk-dev] [PATCH] ivshmem: fix race condition Mauricio Vasquez B
@ 2016-04-01 9:01 ` Burakov, Anatoly
2016-04-01 11:17 ` Mauricio Vásquez
2016-04-01 11:22 ` [dpdk-dev] [PATCH v2] " Mauricio Vasquez B
1 sibling, 1 reply; 6+ messages in thread
From: Burakov, Anatoly @ 2016-04-01 9:01 UTC (permalink / raw)
To: Mauricio Vasquez B; +Cc: dev
Hi Mauricio,
> The memory zone could be freed just after adding it to the metadata
> file and just before marking it as not freeable.
> This patch changes the locking logic in order to prevent it.
Just a nit: if the stated purpose of the patch is to prevent the memzone free
before adding it to metadata, then it probably should check if the memzone
wasn't freed before we got the lock (or if it's a valid memzone in the first place).
Probably just checking if the virtual address of the memzone isn't 0 would be
enough. Otherwise looks fine to me.
Thanks,
Anatoly
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH] ivshmem: fix race condition
2016-04-01 9:01 ` Burakov, Anatoly
@ 2016-04-01 11:17 ` Mauricio Vásquez
0 siblings, 0 replies; 6+ messages in thread
From: Mauricio Vásquez @ 2016-04-01 11:17 UTC (permalink / raw)
To: Burakov, Anatoly; +Cc: dev
Hi Anatoly,
On Fri, Apr 1, 2016 at 11:01 AM, Burakov, Anatoly <anatoly.burakov@intel.com
> wrote:
> Hi Mauricio,
>
> > The memory zone could be freed just after adding it to the metadata
> > file and just before marking it as not freeable.
> > This patch changes the locking logic in order to prevent it.
>
> Just a nit: if the stated purpose of the patch is to prevent the memzone
> free
> before adding it to metadata, then it probably should check if the memzone
> wasn't freed before we got the lock (or if it's a valid memzone in the
> first place).
> Probably just checking if the virtual address of the memzone isn't 0 would
> be
> enough. Otherwise looks fine to me.
>
>
The purpose of this patch is to avoid a memzone be freed after it has been
added to the metadata but
before it is marked as not freeable. (that small time interval where it is
possible).
Avoid adding an already freed memzone is another case, I will send a v2
that addresses it.
I used the length of the memzone to check whether the memzone is valid or
not.
> Thanks,
> Anatoly
>
Mauricio Vasquez,
^ permalink raw reply [flat|nested] 6+ messages in thread
* [dpdk-dev] [PATCH v2] ivshmem: fix race condition
2016-04-01 8:16 [dpdk-dev] [PATCH] ivshmem: fix race condition Mauricio Vasquez B
2016-04-01 9:01 ` Burakov, Anatoly
@ 2016-04-01 11:22 ` Mauricio Vasquez B
2016-04-01 11:30 ` Burakov, Anatoly
1 sibling, 1 reply; 6+ messages in thread
From: Mauricio Vasquez B @ 2016-04-01 11:22 UTC (permalink / raw)
To: Anatoly Burakov; +Cc: dev
The memory zone could be freed just after adding it to the metadata
file and just before marking it as not freeable.
This patch changes the locking logic in order to prevent it.
Fixes: cd10c42eb5bc ("mem: fix ivshmem freeing")
Signed-off-by: Mauricio Vasquez B <mauricio.vasquezbernal@studenti.polito.it>
---
lib/librte_ivshmem/rte_ivshmem.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/lib/librte_ivshmem/rte_ivshmem.c b/lib/librte_ivshmem/rte_ivshmem.c
index 8fc4b57..013c3eb 100644
--- a/lib/librte_ivshmem/rte_ivshmem.c
+++ b/lib/librte_ivshmem/rte_ivshmem.c
@@ -471,10 +471,21 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
struct ivshmem_config * config)
{
struct rte_ivshmem_metadata_entry * entry;
- unsigned i;
+ unsigned i, idx;
+ struct rte_mem_config *mcfg;
+
+ if(mz->len == 0) {
+ RTE_LOG(ERR, EAL, "Trying to add an empty memzone\n");
+ return -1;
+ }
rte_spinlock_lock(&config->sl);
+ mcfg = rte_eal_get_configuration()->mem_config;
+
+ /* it prevents the memzone being freed while we add it to the metadata */
+ rte_rwlock_write_lock(&mcfg->mlock);
+
/* find free slot in this config */
for (i = 0; i < RTE_DIM(config->metadata->entry); i++) {
entry = &config->metadata->entry[i];
@@ -504,13 +515,6 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
config->metadata->name);
goto fail;
}
-#ifdef RTE_LIBRTE_IVSHMEM
- struct rte_mem_config *mcfg;
- unsigned int idx;
-
- mcfg = rte_eal_get_configuration()->mem_config;
-
- rte_rwlock_write_lock(&mcfg->mlock);
idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
idx = idx / sizeof(struct rte_memzone);
@@ -519,10 +523,10 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
mcfg->memzone[idx].ioremap_addr = mz->phys_addr;
rte_rwlock_write_unlock(&mcfg->mlock);
-#endif
rte_spinlock_unlock(&config->sl);
return 0;
fail:
+ rte_rwlock_write_unlock(&mcfg->mlock);
rte_spinlock_unlock(&config->sl);
return -1;
}
--
1.9.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH v2] ivshmem: fix race condition
2016-04-01 11:22 ` [dpdk-dev] [PATCH v2] " Mauricio Vasquez B
@ 2016-04-01 11:30 ` Burakov, Anatoly
2016-04-01 13:35 ` Thomas Monjalon
0 siblings, 1 reply; 6+ messages in thread
From: Burakov, Anatoly @ 2016-04-01 11:30 UTC (permalink / raw)
To: Mauricio Vasquez B; +Cc: dev
> The memory zone could be freed just after adding it to the metadata
> file and just before marking it as not freeable.
> This patch changes the locking logic in order to prevent it.
>
> Fixes: cd10c42eb5bc ("mem: fix ivshmem freeing")
>
> Signed-off-by: Mauricio Vasquez B
> <mauricio.vasquezbernal@studenti.polito.it>
> ---
> lib/librte_ivshmem/rte_ivshmem.c | 22 +++++++++++++---------
> 1 file changed, 13 insertions(+), 9 deletions(-)
>
> diff --git a/lib/librte_ivshmem/rte_ivshmem.c
> b/lib/librte_ivshmem/rte_ivshmem.c
> index 8fc4b57..013c3eb 100644
> --- a/lib/librte_ivshmem/rte_ivshmem.c
> +++ b/lib/librte_ivshmem/rte_ivshmem.c
> @@ -471,10 +471,21 @@ add_memzone_to_metadata(const struct
> rte_memzone * mz,
> struct ivshmem_config * config)
> {
> struct rte_ivshmem_metadata_entry * entry;
> - unsigned i;
> + unsigned i, idx;
> + struct rte_mem_config *mcfg;
> +
> + if(mz->len == 0) {
> + RTE_LOG(ERR, EAL, "Trying to add an empty memzone\n");
> + return -1;
> + }
>
> rte_spinlock_lock(&config->sl);
>
> + mcfg = rte_eal_get_configuration()->mem_config;
> +
> + /* it prevents the memzone being freed while we add it to the
> metadata */
> + rte_rwlock_write_lock(&mcfg->mlock);
> +
> /* find free slot in this config */
> for (i = 0; i < RTE_DIM(config->metadata->entry); i++) {
> entry = &config->metadata->entry[i];
> @@ -504,13 +515,6 @@ add_memzone_to_metadata(const struct
> rte_memzone * mz,
> config->metadata->name);
> goto fail;
> }
> -#ifdef RTE_LIBRTE_IVSHMEM
> - struct rte_mem_config *mcfg;
> - unsigned int idx;
> -
> - mcfg = rte_eal_get_configuration()->mem_config;
> -
> - rte_rwlock_write_lock(&mcfg->mlock);
>
> idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
> idx = idx / sizeof(struct rte_memzone);
> @@ -519,10 +523,10 @@ add_memzone_to_metadata(const struct
> rte_memzone * mz,
> mcfg->memzone[idx].ioremap_addr = mz->phys_addr;
>
> rte_rwlock_write_unlock(&mcfg->mlock);
> -#endif
> rte_spinlock_unlock(&config->sl);
> return 0;
> fail:
> + rte_rwlock_write_unlock(&mcfg->mlock);
> rte_spinlock_unlock(&config->sl);
> return -1;
> }
> --
> 1.9.1
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [dpdk-dev] [PATCH v2] ivshmem: fix race condition
2016-04-01 11:30 ` Burakov, Anatoly
@ 2016-04-01 13:35 ` Thomas Monjalon
0 siblings, 0 replies; 6+ messages in thread
From: Thomas Monjalon @ 2016-04-01 13:35 UTC (permalink / raw)
To: Mauricio Vasquez B; +Cc: dev, Burakov, Anatoly
> > The memory zone could be freed just after adding it to the metadata
> > file and just before marking it as not freeable.
> > This patch changes the locking logic in order to prevent it.
> >
> > Fixes: cd10c42eb5bc ("mem: fix ivshmem freeing")
> >
> > Signed-off-by: Mauricio Vasquez B
> > <mauricio.vasquezbernal@studenti.polito.it>
>
> Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
Applied, thanks
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2016-04-01 13:37 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-01 8:16 [dpdk-dev] [PATCH] ivshmem: fix race condition Mauricio Vasquez B
2016-04-01 9:01 ` Burakov, Anatoly
2016-04-01 11:17 ` Mauricio Vásquez
2016-04-01 11:22 ` [dpdk-dev] [PATCH v2] " Mauricio Vasquez B
2016-04-01 11:30 ` Burakov, Anatoly
2016-04-01 13:35 ` Thomas Monjalon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).