Re: [Cluster-devel] [RFC PATCH 3/5] gfs2: Add a dynamic buffer backed by a vector of pages

From: Bob Peterson
Date: Fri Jul 25 2014 - 14:42:53 EST


----- Original Message -----
> +static int vp_alloc_pages(struct vp_ctx *vpx, int start, int end)
> +{
> + int i;
> +
> + for (i = start; i < end; i++) {
> + vpx->vp_pages[i] = alloc_page(GFP_KERNEL | GFP_NOFS);
> + if (vpx->vp_pages[i] == NULL)
> + goto free;
> + }
> + return 0;
> +free:
> + for (i = start; i < end; i++)
> + if (vpx->vp_pages[i]) {
> + __free_page(vpx->vp_pages[i]);
> + vpx->vp_pages[i] = NULL;
> + }
> + return -ENOMEM;
> +}

I'm concerned that if we have a value for vpx->vp_pages[i] that is
nonzero (uninitialized) _after_ an element in which we couldn't
allocate a page, we could try to free the nonzero value. For that
reason, I always prefer to do something like this instead:

free:
while (i > start) {
i--;
__free_page(vpx->vp_pages[i]);
vpx->vp_pages[i] = NULL;
}
return -ENOMEM;

(snip)
> +struct vp_ctx* vp_get_vpx(struct vbuf *vb)
> +{
> + struct vp_ctx *vpx = NULL;
> +
> + if (!vb || !vb->v_opaque)
> + goto out;
> +
> + vpx = vb->v_opaque;
> + if (vpx->vp_magic != VP_MAGIC) {
> + vpx = NULL;
> + goto out;

This goto is unnecessary, and if you eliminate it, you don't need the brackets.

> + }
> +out:
> + return vpx;
> +}

Regards,

Bob Peterson
Red Hat File Systems
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/