linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Gabbasov <andrew_gabbasov-nmGgyN9QBj3QT0dZR+AlfA@public.gmane.org>
To: <linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: [PATCH] spi: Fix per-page mapping of unaligned vmalloc-ed buffer
Date: Mon, 1 Jun 2015 05:14:36 -0500	[thread overview]
Message-ID: <1433153676-18229-1-git-send-email-andrew_gabbasov@mentor.com> (raw)

spi_map_buf() processes mapping of vmalloc-ed buffers in a special way,
making mapping of every page separately. However, if the buffer is not
aligned to page boundary (e.g. sub-array in a vmalloc-ed array), it
fills the scatter table with page-size unaligned pieces, that cross
page boundaries. This is incorrect and can, for example, cause memory
corruption and various crashes when working with ubifs on spi-nor chips.

Fix this by using proper scatter table size and intra-page buffer lengths,
so that the whole buffer splits into separate scatter table entries on
page boundaries.

Signed-off-by: Andrew Gabbasov <andrew_gabbasov-nmGgyN9QBj3QT0dZR+AlfA@public.gmane.org>
---
 drivers/spi/spi.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 50910d8..14016dc 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -477,7 +477,9 @@ static int spi_map_buf(struct spi_master *master, struct device *dev,
 {
 	const bool vmalloced_buf = is_vmalloc_addr(buf);
 	const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;
-	const int sgs = DIV_ROUND_UP(len, desc_len);
+	const int sgs = DIV_ROUND_UP(vmalloced_buf ?
+					len + offset_in_page(buf) : len,
+				     desc_len);
 	struct page *vm_page;
 	void *sg_buf;
 	size_t min;
@@ -488,9 +490,10 @@ static int spi_map_buf(struct spi_master *master, struct device *dev,
 		return ret;
 
 	for (i = 0; i < sgs; i++) {
-		min = min_t(size_t, len, desc_len);
 
 		if (vmalloced_buf) {
+			min = min_t(size_t,
+				    len, desc_len - offset_in_page(buf));
 			vm_page = vmalloc_to_page(buf);
 			if (!vm_page) {
 				sg_free_table(sgt);
@@ -499,6 +502,7 @@ static int spi_map_buf(struct spi_master *master, struct device *dev,
 			sg_set_page(&sgt->sgl[i], vm_page,
 				    min, offset_in_page(buf));
 		} else {
+			min = min_t(size_t, len, desc_len);
 			sg_buf = buf;
 			sg_set_buf(&sgt->sgl[i], sg_buf, min);
 		}
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2015-06-01 10:14 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-01 10:14 Andrew Gabbasov [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-06-15  6:59 [PATCH] spi: Fix per-page mapping of unaligned vmalloc-ed buffer Andrew Gabbasov
     [not found] ` <1434351560-29463-1-git-send-email-andrew_gabbasov-nmGgyN9QBj3QT0dZR+AlfA@public.gmane.org>
2015-06-25 11:27   ` Andrew Gabbasov
2015-06-25 14:00     ` Mark Brown
     [not found]       ` <20150625140054.GM14071-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-06-25 15:57         ` Gabbasov, Andrew
     [not found]           ` <6C5EA58090A5ED459815C4D04C2B466FF82082B4-Codg+kbhZ4/nlEkxMdpx1dQH9K4/4qFeAL8bYrjMMd8@public.gmane.org>
2015-06-25 16:12             ` Mark Brown
2015-06-25 16:08 Andrew Gabbasov
2015-06-25 16:40 Andrew Gabbasov
     [not found] ` <1435250415-9147-1-git-send-email-andrew_gabbasov-nmGgyN9QBj3QT0dZR+AlfA@public.gmane.org>
2015-06-26 11:46   ` Mark Brown
     [not found]     ` <20150626114622.GW14071-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-06-30 15:37       ` Andrew Gabbasov
2015-06-30 16:02         ` Mark Brown
     [not found]           ` <20150630160237.GR11162-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-06-30 16:21             ` Andrew Gabbasov

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=1433153676-18229-1-git-send-email-andrew_gabbasov@mentor.com \
    --to=andrew_gabbasov-nmggyn9qbj3qt0dzr+alfa@public.gmane.org \
    --cc=linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    /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).