linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nicholas A. Bellinger" <nab@linux-iscsi.org>
To: linux-scsi <linux-scsi@vger.kernel.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	James Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Christoph Hellwig <hch@lst.de>,
	Mike Christie <michaelc@cs.wisc.edu>,
	Hannes Reinecke <hare@suse.de>,
	FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>,
	Boaz Harrosh <bharrosh@panasas.com>,
	Joel Becker <jlbec@evilplan.org>,
	Stephen Rothwell <sfr@canb.auug.org.au>,
	Douglas Gilbert <dgilbert@interlog.com>,
	Jesper Juhl <jj@chaosbits.net>,
	"Nicholas A. Bellinger" <nab@linux-iscsi.org>
Subject: [PATCH 11/21] target: Avoid mem leak and needless work in transport_generic_get_mem
Date: Mon, 14 Mar 2011 04:06:05 -0700	[thread overview]
Message-ID: <1300100775-10012-12-git-send-email-nab@linux-iscsi.org> (raw)
In-Reply-To: <1300100775-10012-1-git-send-email-nab@linux-iscsi.org>

From: Jesper Juhl <jj@chaosbits.net>

In drivers/target/target_core_transport.c::transport_generic_get_mem()
there are a few potential memory leaks in the error paths. This patch
makes sure that we free previously allocated memory when other allocations
fail.  It also moves some work (INIT_LIST_HEAD() and assignment to
se_mem->se_len) below all the allocations so that if something fails we
don't do the work at all.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Nicholas A. Bellinger <nab@linux-iscsi.org>
---
 drivers/target/target_core_transport.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c
index 30c1223..af56ce2 100644
--- a/drivers/target/target_core_transport.c
+++ b/drivers/target/target_core_transport.c
@@ -4358,11 +4358,9 @@ transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
 			printk(KERN_ERR "Unable to allocate struct se_mem\n");
 			goto out;
 		}
-		INIT_LIST_HEAD(&se_mem->se_list);
-		se_mem->se_len = (length > dma_size) ? dma_size : length;
 
 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
-		se_mem->se_page = (struct page *) alloc_pages(GFP_KERNEL, 0);
+		se_mem->se_page = alloc_pages(GFP_KERNEL, 0);
 		if (!(se_mem->se_page)) {
 			printk(KERN_ERR "alloc_pages() failed\n");
 			goto out;
@@ -4373,6 +4371,8 @@ transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
 			printk(KERN_ERR "kmap_atomic() failed\n");
 			goto out;
 		}
+		INIT_LIST_HEAD(&se_mem->se_list);
+		se_mem->se_len = (length > dma_size) ? dma_size : length;
 		memset(buf, 0, se_mem->se_len);
 		kunmap_atomic(buf, KM_IRQ0);
 
@@ -4391,6 +4391,9 @@ transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
 
 	return 0;
 out:
+	if (se_mem)
+		__free_pages(se_mem->se_page, 0);
+	kmem_cache_free(se_mem_cache, se_mem);
 	return -1;
 }
 
-- 
1.7.4.1

  parent reply	other threads:[~2011-03-14 11:06 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-14 11:05 [PATCH 00/21] target patches for .39 scsi-misc (v3) Nicholas A. Bellinger
2011-03-14 11:05 ` [PATCH 01/21] target: Fix memory leak on error path in pscsi_alloc_task Nicholas A. Bellinger
2011-03-14 11:05 ` [PATCH 02/21] target: Fix match_strdup() memory leaks Nicholas A. Bellinger
2011-03-14 11:05 ` [PATCH 03/21] target: tcm_mod_builder.py generated Makefile cleanups Nicholas A. Bellinger
2011-03-14 11:05 ` [PATCH 04/21] target: remove EXTRA_CFLAGS Nicholas A. Bellinger
2011-03-14 11:05 ` [PATCH 05/21] target: Convert backend ->create_virtdevice() call to return ERR_PTR Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 06/21] target: Remove unnecessary hba_dev_list walk and se_clear_dev_ports legacy code Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 07/21] target: Minor sparse warning fixes and annotations Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 08/21] target: Remove spurious double cast from structure macro accessors Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 09/21] target: Convert rd_build_device_space() to use errno Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 10/21] target: Convert TMR REQ/RSP definitions to target namespace Nicholas A. Bellinger
2011-03-14 11:06 ` Nicholas A. Bellinger [this message]
2011-03-14 11:06 ` [PATCH 12/21] target: Fix bogus return in transport_add_device_to_core_hba failure path Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 13/21] target: Fix generated *_drop_nodeacl() handler in tcm_mod_builder.py Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 14/21] target: Fix FILEIO fd_buffered_io= token typo Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 15/21] target: Add __init/__exit annotation for target_core_[init,exit]_configfs Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 16/21] target core v4.0.0-rc7-ml Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 17/21] target: add initial device backend context target_core_stat.c code Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 18/21] target: add initial fabric port " Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 19/21] target: add initial fabric MappedLUN " Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 20/21] target: Add fabric_stat_group for fabric module statistics default_group Nicholas A. Bellinger
2011-03-14 11:06 ` [PATCH 21/21] tcm_loop: Add multi-fabric Linux/SCSI LLD fabric module Nicholas A. Bellinger
2011-03-14 14:58   ` Christoph Hellwig
2011-03-14 22:02     ` Nicholas A. Bellinger
2011-03-15 10:18       ` Christoph Hellwig
2011-03-17  0:50         ` Nicholas A. Bellinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1300100775-10012-12-git-send-email-nab@linux-iscsi.org \
    --to=nab@linux-iscsi.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=bharrosh@panasas.com \
    --cc=dgilbert@interlog.com \
    --cc=fujita.tomonori@lab.ntt.co.jp \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=jj@chaosbits.net \
    --cc=jlbec@evilplan.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=michaelc@cs.wisc.edu \
    --cc=sfr@canb.auug.org.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).