Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751540AbcLEIXb (ORCPT ); Mon, 5 Dec 2016 03:23:31 -0500 Received: from m12-17.163.com ([220.181.12.17]:46282 "EHLO m12-17.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750998AbcLEIXa (ORCPT ); Mon, 5 Dec 2016 03:23:30 -0500 From: Pan Bian To: Juergen Gross , David Vrabel , Boris Ostrovsky , xen-devel@lists.xenproject.org Cc: linux-kernel@vger.kernel.org, Pan Bian Subject: [PATCH 1/1 v2] xen: set error code on failures Date: Mon, 5 Dec 2016 16:23:05 +0800 Message-Id: <1480926185-19926-1-git-send-email-bianpan2016@163.com> X-Mailer: git-send-email 1.9.1 X-CM-TRANSID: EcCowABnt__sI0VYz6L5Dw--.305S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF4ftF1fWFW7trWxAF4rAFb_yoW8Xr18pF s3G34jyFyrJr17GF43Jw1kAF43Ww47KFW5Cr92qw1DZa13Jr10gF98ta40gFn7ZF95uFWY va1kKr9xZFWDCa7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07bFGQhUUUUU= X-Originating-IP: [106.120.213.17] X-CM-SenderInfo: held01tdqsiiqw6rljoofrz/xtbBZxA3cletlBCoBAAAsg Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1453 Lines: 46 Variable rc is reset in the loop, and its value will be non-negative during the second and after repeat of the loop. If it fails to allocate memory then, it may return a non-negative integer, which indicates no error. This patch fixes the bug, assigning "-ENOMEM" to rc when kzalloc() or alloc_page() returns NULL, and removing the initialization of rc outside of the loop. v1 is reviewed by: Juergen Gross Signed-off-by: Pan Bian --- drivers/xen/gntalloc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/xen/gntalloc.c b/drivers/xen/gntalloc.c index 7a47c4c..1bf55a3 100644 --- a/drivers/xen/gntalloc.c +++ b/drivers/xen/gntalloc.c @@ -127,18 +127,21 @@ static int add_grefs(struct ioctl_gntalloc_alloc_gref *op, struct gntalloc_gref *gref, *next; readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE); - rc = -ENOMEM; for (i = 0; i < op->count; i++) { gref = kzalloc(sizeof(*gref), GFP_KERNEL); - if (!gref) + if (!gref) { + rc = -ENOMEM; goto undo; + } list_add_tail(&gref->next_gref, &queue_gref); list_add_tail(&gref->next_file, &queue_file); gref->users = 1; gref->file_index = op->index + i * PAGE_SIZE; gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO); - if (!gref->page) + if (!gref->page) { + rc = -ENOMEM; goto undo; + } /* Grant foreign access to the page. */ rc = gnttab_grant_foreign_access(op->domid, -- 1.9.1