From 171753db0f9430e41907323b7d98aaeaf32c4b68 Mon Sep 17 00:00:00 2001 From: Yuji Mano Date: Thu, 19 Mar 2009 13:54:17 -0700 Subject: base: Context negative num mpus segfault This patch fixes a bug that would result in a segfault if a negative number of mpus are requested when creating a MARS context. Also the internal function mars_mpu_max() now returns 0 as a successful result. The caller is responsible for handling a return of 0 appropriately. MARS_ERROR_INTERNAL is only returned if there was some error to retrieve the count. Signed-off-by: Yuji Mano Acked-by: Kazunori Asayama --- base/include/host/mars/context.h | 5 +++-- base/src/host/lib/context.c | 6 ++++-- base/src/host/lib/context_internal.h | 2 +- base/src/host/lib/mpu_cell.c | 12 ++++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/base/include/host/mars/context.h b/base/include/host/mars/context.h index 7888320..db39470 100644 --- a/base/include/host/mars/context.h +++ b/base/include/host/mars/context.h @@ -88,12 +88,13 @@ extern "C" { * each MARS context will suffer the large over head of MPU context switches. * * \param[out] mars - address of pointer to MARS context - * \param[in] num_mpus - number of mpus utilized by MARS context + * \param[in] num_mpus - number of mpus requested by MARS context * \param[in] shared - specifies if context is shared or not * \return * MARS_SUCCESS - successfully created MARS context * \n MARS_ERROR_NULL - null pointer specified - * \n MARS_ERROR_PARAMS - bad MARS params specified + * \n MARS_ERROR_LIMIT - no more available MPUs + * \n MARS_ERROR_PARAMS - more MPUs requested than there are available * \n MARS_ERROR_MEMORY - not enough memory * \n MARS_ERROR_INTERNAL - some internal error occurred */ diff --git a/base/src/host/lib/context.c b/base/src/host/lib/context.c index d6c7ad5..22dbbc9 100644 --- a/base/src/host/lib/context.c +++ b/base/src/host/lib/context.c @@ -183,7 +183,7 @@ int mars_context_create(struct mars_context **mars_ret, uint32_t num_mpus, uint8_t shared) { int ret; - int num_mpus_max; + uint32_t num_mpus_max; struct mars_context *mars = NULL; /* check function params */ @@ -194,7 +194,9 @@ int mars_context_create(struct mars_context **mars_ret, uint32_t num_mpus, ret = mars_mpu_max(&num_mpus_max); if (ret != MARS_SUCCESS) return ret; - if (num_mpus_max < (int)num_mpus) + if (!num_mpus_max) + return MARS_ERROR_LIMIT; + if (num_mpus > num_mpus_max) return MARS_ERROR_PARAMS; if (!num_mpus) num_mpus = num_mpus_max; diff --git a/base/src/host/lib/context_internal.h b/base/src/host/lib/context_internal.h index a5ca963..ef8e0e3 100644 --- a/base/src/host/lib/context_internal.h +++ b/base/src/host/lib/context_internal.h @@ -72,7 +72,7 @@ struct mars_context { mars_callback_t callback_handler; }; -int mars_mpu_max(int *num); +int mars_mpu_max(uint32_t *num); int mars_mpu_run(mars_mpu_context_t *mpu, uint64_t params_ea); int mars_mpu_wait(mars_mpu_context_t *mpu); diff --git a/base/src/host/lib/mpu_cell.c b/base/src/host/lib/mpu_cell.c index cff9233..49744fd 100644 --- a/base/src/host/lib/mpu_cell.c +++ b/base/src/host/lib/mpu_cell.c @@ -89,16 +89,20 @@ static int numa_mpu_max(void) extern const unsigned char mars_kernel_entry[]; -int mars_mpu_max(int *num) +int mars_mpu_max(uint32_t *num) { + int mpu_max; + if (mars_numa_enabled()) - *num = numa_mpu_max(); + mpu_max = numa_mpu_max(); else - *num = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1); + mpu_max = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1); - if (*num <= 0) + if (mpu_max < 0) return MARS_ERROR_INTERNAL; + *num = mpu_max; + return MARS_SUCCESS; } -- cgit 1.2.3-korg