Re: [Xen-devel] [PATCH 2/2] xen/blkfront: Add BUG_ON to deal withmisbehaving backends.

From: Konrad Rzeszutek Wilk
Date: Thu Jun 07 2012 - 18:06:51 EST


On Fri, Jun 01, 2012 at 11:16:29AM +0100, Stefano Stabellini wrote:
> On Thu, 31 May 2012, Konrad Rzeszutek Wilk wrote:
> > The blkfront_remove part is .. that is going to take some surgery to do
> > and I don't think I am going to be able to attempt that within the next couple
> > of weeks. So lets put that on the TODO list and just do this one?
>
> OK
>
> > >From 4aabb5b44778fc0c0b8d4f5a2e2cd8e8490064d7 Mon Sep 17 00:00:00 2001
> > From: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
> > Date: Fri, 25 May 2012 17:34:51 -0400
> > Subject: [PATCH] xen/blkfront: Add WARN to deal with misbehaving backends.
> >
> > Part of the ring structure is the 'id' field which is under
> > control of the frontend. The frontend stamps it with "some"
> > value (this some in this implementation being a value less
> > than BLK_RING_SIZE), and when it gets a response expects
> > said value to be in the response structure. We have a check
> > for the id field when spolling new requests but not when
> > de-spolling responses.
> >
> > We also add an extra check in add_id_to_freelist to make
> > sure that the 'struct request' was not NULL - as we cannot
> > pass a NULL to __blk_end_request_all, otherwise that crashes
> > (and all the operations that the response is dealing with
> > end up with __blk_end_request_all).
> >
> > Lastly we also print the name of the operation that failed.
> >
> > [v1: s/BUG/WARN/ suggested by Stefano]
> > [v2: Add extra check in add_id_to_freelist]
> > Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
> > ---
> > drivers/block/xen-blkfront.c | 39 +++++++++++++++++++++++++++++----------
> > 1 files changed, 29 insertions(+), 10 deletions(-)
> >
> > diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
> > index 60eed4b..c7ef8a4 100644
> > --- a/drivers/block/xen-blkfront.c
> > +++ b/drivers/block/xen-blkfront.c
> > @@ -144,11 +144,22 @@ static int get_id_from_freelist(struct blkfront_info *info)
> > static void add_id_to_freelist(struct blkfront_info *info,
> > unsigned long id)
> > {
> > + BUG_ON(info->shadow[id].req.u.rw.id != id);
> > info->shadow[id].req.u.rw.id = info->shadow_free;
> > + BUG_ON(info->shadow[id].request == NULL);
> > info->shadow[id].request = NULL;
> > info->shadow_free = id;
> > }
>
> Like Jan said, it would be best to change the two BUG_ON into WARN_ON
> and return an error.

Yes. I missed that.
>
>
> > +static const char *op_name(int op)
> > +{
> > + const char *names[BLKIF_OP_DISCARD+1] = {
> > + "read" , "write", "barrier", "flush", "reserved", "discard"};
> > +
> > + if (op > BLKIF_OP_DISCARD)
> > + return "unknown";
> > + return names[op];
> > +}
>
> Considering that op is an int, shoudn't we check for negative values
> too?

Yes! I also converted this per Jan's excellent idea.

Please see: