This fixes scatterlist corruptions added by
	commit 
68e3f5dd4db62619fdbe520d36c9ebf62e672256
	[CRYPTO] users: Fix up scatterlist conversion errors
The issue is that the code calls sg_mark_end() which clobbers the
sg_page() pointer of the final scatterlist entry.
The first part fo the fix makes skb_to_sgvec() do __sg_mark_end().
After considering all skb_to_sgvec() call sites the most correct
solution is to call __sg_mark_end() in skb_to_sgvec() since that is
what all of the callers would end up doing anyways.
I suspect this might have fixed some problems in virtio_net which is
the sole non-crypto user of skb_to_sgvec().
Other similar sg_mark_end() cases were converted over to
__sg_mark_end() as well.
Arguably sg_mark_end() is a poorly named function because it doesn't
just "mark", it clears out the page pointer as a side effect, which is
what led to these bugs in the first place.
The one remaining plain sg_mark_end() call is in scsi_alloc_sgtable()
and arguably it could be converted to __sg_mark_end() if only so that
we can delete this confusing interface from linux/scatterlist.h
Signed-off-by: David S. Miller <davem@davemloft.net>
 
  *     Fill the specified scatter-gather list with mappings/pointers into a
  *     region of the buffer space attached to a socket buffer.
  */
-int
-skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
+static int
+__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
 {
        int start = skb_headlen(skb);
        int i, copy = start - offset;
                        if ((copy = end - offset) > 0) {
                                if (copy > len)
                                        copy = len;
-                               elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
+                               elt += __skb_to_sgvec(list, sg+elt, offset - start,
+                                                     copy);
                                if ((len -= copy) == 0)
                                        return elt;
                                offset += copy;
        return elt;
 }
 
+int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
+{
+       int nsg = __skb_to_sgvec(skb, sg, offset, len);
+
+       __sg_mark_end(&sg[nsg - 1]);
+
+       return nsg;
+}
+
 /**
  *     skb_cow_data - Check that a socket buffer's data buffers are writable
  *     @skb: The socket buffer to check.
 
                                goto unlock;
                }
                sg_init_table(sg, nfrags);
-               sg_mark_end(sg, skb_to_sgvec(skb, sg, esph->enc_data +
-                                                     esp->conf.ivlen -
-                                                     skb->data, clen));
+               skb_to_sgvec(skb, sg,
+                            esph->enc_data +
+                            esp->conf.ivlen -
+                            skb->data, clen);
                err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
                if (unlikely(sg != &esp->sgbuf[0]))
                        kfree(sg);
                        goto out;
        }
        sg_init_table(sg, nfrags);
-       sg_mark_end(sg, skb_to_sgvec(skb, sg, sizeof(*esph) + esp->conf.ivlen,
-                                    elen));
+       skb_to_sgvec(skb, sg,
+                    sizeof(*esph) + esp->conf.ivlen,
+                    elen);
        err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
        if (unlikely(sg != &esp->sgbuf[0]))
                kfree(sg);
 
        sg_set_buf(&sg[block++], key->key, key->keylen);
        nbytes += key->keylen;
 
-       sg_mark_end(sg, block);
+       __sg_mark_end(&sg[block - 1]);
 
        /* Now store the Hash into the packet */
        err = crypto_hash_init(desc);
 
                                goto unlock;
                }
                sg_init_table(sg, nfrags);
-               sg_mark_end(sg, skb_to_sgvec(skb, sg, esph->enc_data +
-                                                     esp->conf.ivlen -
-                                                     skb->data, clen));
+               skb_to_sgvec(skb, sg,
+                            esph->enc_data +
+                            esp->conf.ivlen -
+                            skb->data, clen);
                err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
                if (unlikely(sg != &esp->sgbuf[0]))
                        kfree(sg);
                        }
                }
                sg_init_table(sg, nfrags);
-               sg_mark_end(sg, skb_to_sgvec(skb, sg,
-                                            sizeof(*esph) + esp->conf.ivlen,
-                                            elen));
+               skb_to_sgvec(skb, sg,
+                            sizeof(*esph) + esp->conf.ivlen,
+                            elen);
                ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
                if (unlikely(sg != &esp->sgbuf[0]))
                        kfree(sg);
 
        sg_set_buf(&sg[block++], key->key, key->keylen);
        nbytes += key->keylen;
 
-       sg_mark_end(sg, block);
+       __sg_mark_end(&sg[block - 1]);
 
        /* Now store the hash into the packet */
        err = crypto_hash_init(desc);
 
        len = data_size + call->conn->size_align - 1;
        len &= ~(call->conn->size_align - 1);
 
-       sg_init_table(sg, skb_to_sgvec(skb, sg, 0, len));
+       sg_init_table(sg, nsg);
+       skb_to_sgvec(skb, sg, 0, len);
        crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
 
        _leave(" = 0");
                goto nomem;
 
        sg_init_table(sg, nsg);
-       sg_mark_end(sg, skb_to_sgvec(skb, sg, 0, 8));
+       skb_to_sgvec(skb, sg, 0, 8);
 
        /* start the decryption afresh */
        memset(&iv, 0, sizeof(iv));
        }
 
        sg_init_table(sg, nsg);
-       sg_mark_end(sg, skb_to_sgvec(skb, sg, 0, skb->len));
+       skb_to_sgvec(skb, sg, 0, skb->len);
 
        /* decrypt from the session key */
        payload = call->conn->key->payload.data;
                nsg++;
        }
 
-       sg_mark_end(sg, nsg);
+       __sg_mark_end(&sg[nsg - 1]);
 
        ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
 }
 
        if (thislen == 0)
                return 0;
 
-       sg_mark_end(desc->infrags, desc->fragno);
-       sg_mark_end(desc->outfrags, desc->fragno);
+       __sg_mark_end(&desc->infrags[desc->fragno - 1]);
+       __sg_mark_end(&desc->outfrags[desc->fragno - 1]);
 
        ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
                                          desc->infrags, thislen);
        if (thislen == 0)
                return 0;
 
-       sg_mark_end(desc->frags, desc->fragno);
+       __sg_mark_end(&desc->frags[desc->fragno - 1]);
 
        ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
                                          desc->frags, thislen);