linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hugh Dickins <hugh@veritas.com>
To: Roland Dreier <rdreier@cisco.com>
Cc: Andrew Morton <akpm@osdl.org>,
	"Michael S. Tsirkin" <mst@mellanox.co.il>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] ib: don't doublefree pages from scatterlist
Date: Fri, 3 Feb 2006 19:51:18 +0000 (GMT)	[thread overview]
Message-ID: <Pine.LNX.4.61.0602031948100.14829@goblin.wat.veritas.com> (raw)
In-Reply-To: <Pine.LNX.4.61.0602031842290.14065@goblin.wat.veritas.com>

On some architectures, mapping the scatterlist may coalesce entries:
if that coalesced list is then used for freeing the pages afterwards,
there's a danger that pages may be doubly freed (and others leaked).

Fix Infiniband's __ib_umem_release by freeing from a separate array
beyond the scatterlist: IB_UMEM_MAX_PAGE_CHUNK lowered to fit one page.

Signed-off-by: Hugh Dickins <hugh@veritas.com>
---
Warning: untested!  And please double-check the adjusted definition of
IB_UMEM_MAX_PAGE_CHUNK - the old definition was avoiding "sizeof"s, but
I don't understand why.

 drivers/infiniband/core/uverbs_mem.c |   22 ++++++++++++++++------
 include/rdma/ib_verbs.h              |    3 +--
 2 files changed, 17 insertions(+), 8 deletions(-)

--- 2.6.16-rc2/drivers/infiniband/core/uverbs_mem.c	2005-10-28 01:02:08.000000000 +0100
+++ linux/drivers/infiniband/core/uverbs_mem.c	2006-02-03 09:59:37.000000000 +0000
@@ -49,15 +49,18 @@ struct ib_umem_account_work {
 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
 {
 	struct ib_umem_chunk *chunk, *tmp;
+	struct page **sg_pages;
 	int i;
 
 	list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
 		dma_unmap_sg(dev->dma_device, chunk->page_list,
 			     chunk->nents, DMA_BIDIRECTIONAL);
+		/* Scatterlist may have been coalesced: free saved pagelist */
+		sg_pages = (struct page **) (chunk->page_list + chunk->nents);
 		for (i = 0; i < chunk->nents; ++i) {
 			if (umem->writable && dirty)
-				set_page_dirty_lock(chunk->page_list[i].page);
-			put_page(chunk->page_list[i].page);
+				set_page_dirty_lock(sg_pages[i]);
+			put_page(sg_pages[i]);
 		}
 
 		kfree(chunk);
@@ -69,11 +72,13 @@ int ib_umem_get(struct ib_device *dev, s
 {
 	struct page **page_list;
 	struct ib_umem_chunk *chunk;
+	struct page **sg_pages;
 	unsigned long locked;
 	unsigned long lock_limit;
 	unsigned long cur_base;
 	unsigned long npages;
 	int ret = 0;
+	int nents;
 	int off;
 	int i;
 
@@ -121,16 +126,21 @@ int ib_umem_get(struct ib_device *dev, s
 		off = 0;
 
 		while (ret) {
-			chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
-					min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
+			nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
+			chunk = kmalloc(sizeof *chunk +
+					sizeof (struct scatterlist) * nents +
+					sizeof (struct page *) * nents,
 					GFP_KERNEL);
 			if (!chunk) {
 				ret = -ENOMEM;
 				goto out;
 			}
+			/* Save pages to be freed in array beyond scatterlist */
+			sg_pages = (struct page **) (chunk->page_list + nents);
 
-			chunk->nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
+			chunk->nents = nents;
 			for (i = 0; i < chunk->nents; ++i) {
+				sg_pages[i] =
 				chunk->page_list[i].page   = page_list[i + off];
 				chunk->page_list[i].offset = 0;
 				chunk->page_list[i].length = PAGE_SIZE;
@@ -142,7 +152,7 @@ int ib_umem_get(struct ib_device *dev, s
 						 DMA_BIDIRECTIONAL);
 			if (chunk->nmap <= 0) {
 				for (i = 0; i < chunk->nents; ++i)
-					put_page(chunk->page_list[i].page);
+					put_page(sg_pages[i]);
 				kfree(chunk);
 
 				ret = -ENOMEM;
--- 2.6.16-rc2/include/rdma/ib_verbs.h	2006-02-03 09:32:50.000000000 +0000
+++ linux/include/rdma/ib_verbs.h	2006-02-03 09:59:37.000000000 +0000
@@ -696,8 +696,7 @@ struct ib_udata {
 
 #define IB_UMEM_MAX_PAGE_CHUNK						\
 	((PAGE_SIZE - offsetof(struct ib_umem_chunk, page_list)) /	\
-	 ((void *) &((struct ib_umem_chunk *) 0)->page_list[1] -	\
-	  (void *) &((struct ib_umem_chunk *) 0)->page_list[0]))
+	 (sizeof(struct scatterlist) + sizeof(struct page *)))
 
 struct ib_umem_object {
 	struct ib_uobject	uobject;

  reply	other threads:[~2006-02-03 19:50 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20051129092432.0f5742f0.akpm@osdl.org>
2005-11-29 18:34 ` Fw: crash on x86_64 - mm related? Ryan Richter
     [not found] ` <Pine.LNX.4.63.0511292147120.5739@kai.makisara.local>
2005-11-29 20:31   ` Ryan Richter
2005-11-29 20:48     ` Kai Makisara
2005-11-29 20:58       ` Ryan Richter
2005-11-29 21:36         ` Kai Makisara
2005-11-30  5:12       ` Kai Makisara
2005-12-01 19:18 ` Kai Makisara
2005-12-01 19:38   ` Linus Torvalds
2005-12-01 19:56     ` Ryan Richter
2005-12-01 20:21       ` Hugh Dickins
2005-12-01 21:44         ` Kai Makisara
2005-12-02 18:03         ` Ryan Richter
2005-12-02 18:43           ` Jesper Juhl
2005-12-02 19:12           ` Hugh Dickins
2005-12-02 19:44             ` Ryan Richter
2005-12-02 20:40               ` Hugh Dickins
2005-12-03 17:29                 ` Ryan Richter
2005-12-06 16:08                 ` Ryan Richter
2005-12-06 20:31                   ` Hugh Dickins
2005-12-06 20:43                     ` Ryan Richter
2005-12-07 18:37                       ` Hugh Dickins
2005-12-08  2:26                         ` Ryan Richter
2005-12-12 16:54                         ` Ryan Richter
2005-12-12 17:40                           ` Linus Torvalds
2005-12-12 17:45                             ` James Bottomley
2005-12-12 18:04                               ` Ryan Richter
2005-12-12 18:09                               ` Linus Torvalds
2005-12-12 18:24                                 ` James Bottomley
2005-12-15 19:09                                   ` Ryan Richter
2005-12-16  4:01                                     ` James Bottomley
2005-12-17  3:31                                       ` Ryan Richter
2005-12-26 23:42                                       ` Ryan Richter
2005-12-27 16:21                                         ` Kai Makisara
2006-01-03 19:03                                           ` Ryan Richter
2006-01-04 17:27                                           ` Ryan Richter
2006-01-04 21:48                                             ` Kai Makisara
2006-01-05  5:40                                               ` Ryan Richter
2006-01-05 20:12                                               ` Ryan Richter
2006-01-05 21:18                                                 ` Linus Torvalds
2006-01-08 22:36                                                   ` Ryan Richter
2006-01-09  3:31                                                   ` Ryan Richter
2006-01-09  4:07                                                     ` Linus Torvalds
2006-01-09  5:13                                                       ` Andrew Morton
2006-01-09  5:45                                                         ` Ryan Richter
2006-01-09  5:57                                                           ` Andrew Morton
2006-01-09  9:44                                                       ` Hugh Dickins
2006-01-09 18:53                                                         ` Ryan Richter
2006-01-09 19:31                                                           ` Hugh Dickins
2006-01-09 20:05                                                             ` Ryan Richter
2006-01-18  0:12                                                             ` Ryan Richter
2006-01-18 16:00                                                               ` Hugh Dickins
2006-02-03 19:46                                                                 ` Hugh Dickins
2006-02-03 19:51                                                                   ` Hugh Dickins [this message]
2006-02-03 23:13                                                                     ` [PATCH] ib: don't doublefree pages from scatterlist Roland Dreier
2006-02-03 19:53                                                                   ` [PATCH] st: " Hugh Dickins
2006-02-03 20:38                                                                     ` Mike Christie
2006-02-03 21:16                                                                       ` Hugh Dickins
2006-02-04 12:10                                                                         ` Kai Makisara
2006-02-04 15:01                                                                           ` Hugh Dickins
2006-02-03 19:55                                                                   ` [PATCH] ipr: " Hugh Dickins
2006-02-03 22:06                                                                     ` Brian King
2006-02-04  0:26                                                                       ` Hugh Dickins
2006-02-05 21:35                                                                         ` Brian King
2006-02-06  9:32                                                                           ` Hugh Dickins
2006-02-06  9:46                                                                             ` David S. Miller
2006-02-06 14:46                                                                               ` Brian King
2006-02-06 16:45                                                                                 ` Hugh Dickins
2006-02-06 17:38                                                                                   ` James Bottomley
2006-02-06 19:15                                                                                     ` Brian King
2006-02-06 21:11                                                                                   ` Andi Kleen
2006-02-06 21:49                                                                                     ` David S. Miller
2006-02-06 22:11                                                                                     ` Hugh Dickins
2006-02-06 22:13                                                                                       ` Andi Kleen
2006-02-07  3:09                                                                                       ` Ryan Richter
2006-02-11 22:38                                                                                       ` Ryan Richter
2006-02-12 18:57                                                                                         ` Hugh Dickins
2006-02-12 21:29                                                                                           ` Andi Kleen
2006-02-13 17:21                                                                                             ` Hugh Dickins
2006-02-06 15:02                                                                               ` James Bottomley
2006-02-06 17:01                                                                                 ` Hugh Dickins
2006-02-03 19:56                                                                   ` [PATCH] osst: " Hugh Dickins
2006-02-03 21:10                                                                   ` Fw: crash on x86_64 - mm related? Ryan Richter
2006-02-04 11:58                                                                   ` Kai Makisara
2006-02-04 14:46                                                                     ` Hugh Dickins
2006-01-05 22:09                                                 ` Kai Makisara
2006-01-04 18:26                                           ` Ryan Richter
2005-12-07 18:30                     ` Ryan Richter
2005-12-07 18:56                       ` Hugh Dickins
2005-12-07 19:06                         ` Ryan Richter
2005-12-06 17:57                 ` Ryan Richter
2005-12-01 20:28     ` James Bottomley
2005-12-01 21:17       ` Kai Makisara
2005-12-02 13:45         ` Hugh Dickins
2005-12-02 17:59           ` Kai Makisara
2005-12-02 18:55             ` Hugh Dickins
2005-12-02 19:46               ` Kai Makisara
2005-12-02 20:47                 ` Hugh Dickins
2005-12-04  9:29                   ` Kai Makisara
2005-12-01 19:53   ` Ryan Richter

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=Pine.LNX.4.61.0602031948100.14829@goblin.wat.veritas.com \
    --to=hugh@veritas.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@mellanox.co.il \
    --cc=rdreier@cisco.com \
    /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).