Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1718868 > unrolled thread
| Started by | Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> |
|---|---|
| First post | 2017-08-24 08:40 +0200 |
| Last post | 2017-08-25 11:40 +0200 |
| Articles | 2 — 2 participants |
Back to article view | Back to linux.kernel
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
[PATCH v7 06/12] powerpc/vas: Define helpers to alloc/free windows Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> - 2017-08-24 08:40 +0200
Re: [PATCH v7 06/12] powerpc/vas: Define helpers to alloc/free windows Michael Ellerman <mpe@ellerman.id.au> - 2017-08-25 11:40 +0200
| From | Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> |
|---|---|
| Date | 2017-08-24 08:40 +0200 |
| Subject | [PATCH v7 06/12] powerpc/vas: Define helpers to alloc/free windows |
| Message-ID | <uhTY5-2SA-3@gated-at.bofh.it> |
Define helpers to allocate/free VAS window objects. These will
be used in follow-on patches when opening/closing windows.
Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
---
arch/powerpc/platforms/powernv/vas-window.c | 70 +++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/arch/powerpc/platforms/powernv/vas-window.c b/arch/powerpc/platforms/powernv/vas-window.c
index 3a50d6a..9c12919 100644
--- a/arch/powerpc/platforms/powernv/vas-window.c
+++ b/arch/powerpc/platforms/powernv/vas-window.c
@@ -490,6 +490,76 @@ int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
return 0;
}
+DEFINE_SPINLOCK(vas_ida_lock);
+
+void vas_release_window_id(struct ida *ida, int winid)
+{
+ spin_lock(&vas_ida_lock);
+ ida_remove(ida, winid);
+ spin_unlock(&vas_ida_lock);
+}
+
+int vas_assign_window_id(struct ida *ida)
+{
+ int rc, winid;
+
+ rc = ida_pre_get(ida, GFP_KERNEL);
+ if (!rc)
+ return -EAGAIN;
+
+ spin_lock(&vas_ida_lock);
+ rc = ida_get_new_above(ida, 0, &winid);
+ spin_unlock(&vas_ida_lock);
+
+ if (rc)
+ return rc;
+
+ if (winid > VAS_WINDOWS_PER_CHIP) {
+ pr_err("VAS: Too many (%d) open windows\n", winid);
+ vas_release_window_id(ida, winid);
+ return -EAGAIN;
+ }
+
+ return winid;
+}
+
+void vas_window_free(struct vas_window *window)
+{
+ int winid = window->winid;
+ struct vas_instance *vinst = window->vinst;
+
+ unmap_winctx_mmio_bars(window);
+ kfree(window);
+
+ vas_release_window_id(&vinst->ida, winid);
+}
+
+struct vas_window *vas_window_alloc(struct vas_instance *vinst)
+{
+ int winid;
+ struct vas_window *window;
+
+ winid = vas_assign_window_id(&vinst->ida);
+ if (winid < 0)
+ return ERR_PTR(winid);
+
+ window = kzalloc(sizeof(*window), GFP_KERNEL);
+ if (!window)
+ return ERR_PTR(-ENOMEM);
+
+ window->vinst = vinst;
+ window->winid = winid;
+
+ if (map_winctx_mmio_bars(window))
+ goto out_free;
+
+ return window;
+
+out_free:
+ kfree(window);
+ return ERR_PTR(-ENOMEM);
+}
+
/* stub for now */
int vas_win_close(struct vas_window *window)
{
--
2.7.4
[toc] | [next] | [standalone]
| From | Michael Ellerman <mpe@ellerman.id.au> |
|---|---|
| Date | 2017-08-25 11:40 +0200 |
| Message-ID | <uijfQ-2gM-23@gated-at.bofh.it> |
| In reply to | #1718868 |
Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> writes:
> diff --git a/arch/powerpc/platforms/powernv/vas-window.c b/arch/powerpc/platforms/powernv/vas-window.c
> index 3a50d6a..9c12919 100644
> --- a/arch/powerpc/platforms/powernv/vas-window.c
> +++ b/arch/powerpc/platforms/powernv/vas-window.c
> @@ -490,6 +490,76 @@ int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
> return 0;
> }
>
> +DEFINE_SPINLOCK(vas_ida_lock);
static.
> +
> +void vas_release_window_id(struct ida *ida, int winid)
static.
> +{
> + spin_lock(&vas_ida_lock);
> + ida_remove(ida, winid);
> + spin_unlock(&vas_ida_lock);
> +}
> +
> +int vas_assign_window_id(struct ida *ida)
static.
> +{
> + int rc, winid;
> +
> + rc = ida_pre_get(ida, GFP_KERNEL);
> + if (!rc)
> + return -EAGAIN;
> +
> + spin_lock(&vas_ida_lock);
> + rc = ida_get_new_above(ida, 0, &winid);
If you're passing 0 you can just use ida_get_new().
Or did you actually want to exclude 0? In which case you should pass 1.
> + spin_unlock(&vas_ida_lock);
> +
> + if (rc)
> + return rc;
You're supposed to handle EAGAIN I thought.
> +
> + if (winid > VAS_WINDOWS_PER_CHIP) {
> + pr_err("VAS: Too many (%d) open windows\n", winid);
> + vas_release_window_id(ida, winid);
> + return -EAGAIN;
> + }
> +
> + return winid;
> +}
> +
> +void vas_window_free(struct vas_window *window)
static.
> +{
> + int winid = window->winid;
> + struct vas_instance *vinst = window->vinst;
> +
> + unmap_winctx_mmio_bars(window);
> + kfree(window);
> +
> + vas_release_window_id(&vinst->ida, winid);
> +}
> +
> +struct vas_window *vas_window_alloc(struct vas_instance *vinst)
> +{
> + int winid;
> + struct vas_window *window;
> +
> + winid = vas_assign_window_id(&vinst->ida);
> + if (winid < 0)
> + return ERR_PTR(winid);
> +
> + window = kzalloc(sizeof(*window), GFP_KERNEL);
> + if (!window)
> + return ERR_PTR(-ENOMEM);
You leak an id here.
The error handling would be easier in here if the caller did the alloc,
or if you split alloc and init, and alloc just did the kzalloc().
One of the callers even prints "unable to allocate memory" if this
function fails, but that's not accurate, there's several failure modes.
> +
> + window->vinst = vinst;
> + window->winid = winid;
> +
> + if (map_winctx_mmio_bars(window))
> + goto out_free;
> +
> + return window;
> +
> +out_free:
> + kfree(window);
Leak an id here.
> + return ERR_PTR(-ENOMEM);
> +}
> +
> /* stub for now */
> int vas_win_close(struct vas_window *window)
> {
> --
> 2.7.4
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web