[PATCH v2] scsi: eata: drop VLA in reorder()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Avoid 3 VLAs[1] by using a single dinamically allocated array
and some helper variables: we don't need 3 arrays.

[1] https://lkml.org/lkml/2018/3/7/621

Signed-off-by: Salvatore Mesoraca <s.mesoraca16@xxxxxxxxx>
---
 drivers/scsi/eata.c | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/drivers/scsi/eata.c b/drivers/scsi/eata.c
index 6501c33..8ba44b7 100644
--- a/drivers/scsi/eata.c
+++ b/drivers/scsi/eata.c
@@ -2091,12 +2091,14 @@ static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
 static int reorder(struct hostdata *ha, unsigned long cursec,
 		   unsigned int ihdlr, unsigned int il[], unsigned int n_ready)
 {
+	int overlap;
 	struct scsi_cmnd *SCpnt;
 	struct mscp *cpp;
 	unsigned int k, n;
 	unsigned int rev = 0, s = 1, r = 1;
-	unsigned int input_only = 1, overlap = 0;
-	unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
+	unsigned int input_only = 1;
+	unsigned long *sl;
+	unsigned long sl_zero, sl_cur, sl_prev, ll_cur, ll_prev;
 	unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
 	unsigned long ioseek = 0;
 
@@ -2115,6 +2117,12 @@ static int reorder(struct hostdata *ha, unsigned long cursec,
 	if (n_ready <= 1)
 		return 0;
 
+	overlap = -ENOMEM;
+	sl = kcalloc(n_ready, sizeof(*sl), GFP_ATOMIC);
+	if (!sl)
+		goto out;
+	overlap = 0;
+
 	for (n = 0; n < n_ready; n++) {
 		k = il[n];
 		cpp = &ha->cp[k];
@@ -2164,31 +2172,33 @@ static int reorder(struct hostdata *ha, unsigned long cursec,
 	if (!((rev && r) || (!rev && s)))
 		sort(sl, il, n_ready, rev);
 
+	sl_zero = sl[0];
+
 	if (!input_only)
 		for (n = 0; n < n_ready; n++) {
 			k = il[n];
 			cpp = &ha->cp[k];
 			SCpnt = cpp->SCpnt;
-			ll[n] = blk_rq_sectors(SCpnt->request);
-			pl[n] = SCpnt->serial_number;
+			ll_cur = blk_rq_sectors(SCpnt->request);
+			sl_cur = sl[n];
+			sl[n] = SCpnt->serial_number;
 
-			if (!n)
-				continue;
-
-			if ((sl[n] == sl[n - 1])
-			    || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
-			    || (rev && ((sl[n] + ll[n]) > sl[n - 1])))
+			if (n && ((sl_cur == sl_prev)
+				  || (!rev && ((sl_prev + ll_prev) > sl_cur))
+				  || (rev && ((sl_cur + ll_cur) > sl_prev))))
 				overlap = 1;
+			ll_prev = ll_cur;
+			sl_prev = sl_cur;
 		}
 
 	if (overlap)
-		sort(pl, il, n_ready, 0);
+		sort(sl, il, n_ready, 0);
 
 	if (link_statistics) {
-		if (cursec > sl[0])
-			iseek = cursec - sl[0];
+		if (cursec > sl_zero)
+			iseek = cursec - sl_zero;
 		else
-			iseek = sl[0] - cursec;
+			iseek = sl_zero - cursec;
 		batchcount++;
 		readycount += n_ready;
 		seeknosort += seek / 1024;
@@ -2225,6 +2235,8 @@ static int reorder(struct hostdata *ha, unsigned long cursec,
 			     YESNO(overlap), cpp->din);
 		}
 #endif
+	kfree(sl);
+out:
 	return overlap;
 }
 
-- 
1.9.1




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [SCSI Target Devel]     [Linux SCSI Target Infrastructure]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Linux IIO]     [Samba]     [Device Mapper]

  Powered by Linux