| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * IP Payload Compression Protocol (IPComp) - RFC3173. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2003 James Morris <jmorris@intercode.com.au> |
|---|
| 5 | 6 | * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 8 | | - * under the terms of the GNU General Public License as published by the Free |
|---|
| 9 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 10 | | - * any later version. |
|---|
| 11 | 7 | * |
|---|
| 12 | 8 | * Todo: |
|---|
| 13 | 9 | * - Tunable compression parameters. |
|---|
| .. | .. |
|---|
| 20 | 16 | #include <linux/list.h> |
|---|
| 21 | 17 | #include <linux/module.h> |
|---|
| 22 | 18 | #include <linux/mutex.h> |
|---|
| 23 | | -#include <linux/locallock.h> |
|---|
| 24 | 19 | #include <linux/percpu.h> |
|---|
| 25 | 20 | #include <linux/slab.h> |
|---|
| 26 | 21 | #include <linux/smp.h> |
|---|
| .. | .. |
|---|
| 37 | 32 | |
|---|
| 38 | 33 | static DEFINE_MUTEX(ipcomp_resource_mutex); |
|---|
| 39 | 34 | static void * __percpu *ipcomp_scratches; |
|---|
| 40 | | -static DEFINE_LOCAL_IRQ_LOCK(ipcomp_scratches_lock); |
|---|
| 41 | 35 | static int ipcomp_scratch_users; |
|---|
| 42 | 36 | static LIST_HEAD(ipcomp_tfms_list); |
|---|
| 43 | 37 | |
|---|
| .. | .. |
|---|
| 47 | 41 | const int plen = skb->len; |
|---|
| 48 | 42 | int dlen = IPCOMP_SCRATCH_SIZE; |
|---|
| 49 | 43 | const u8 *start = skb->data; |
|---|
| 50 | | - u8 *scratch; |
|---|
| 51 | | - struct crypto_comp *tfm; |
|---|
| 52 | | - int err, len; |
|---|
| 44 | + const int cpu = get_cpu(); |
|---|
| 45 | + u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu); |
|---|
| 46 | + struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu); |
|---|
| 47 | + int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen); |
|---|
| 48 | + int len; |
|---|
| 53 | 49 | |
|---|
| 54 | | - local_lock(ipcomp_scratches_lock); |
|---|
| 55 | | - |
|---|
| 56 | | - scratch = *this_cpu_ptr(ipcomp_scratches); |
|---|
| 57 | | - tfm = *this_cpu_ptr(ipcd->tfms); |
|---|
| 58 | | - err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen); |
|---|
| 59 | 50 | if (err) |
|---|
| 60 | 51 | goto out; |
|---|
| 61 | 52 | |
|---|
| .. | .. |
|---|
| 94 | 85 | if (dlen < len) |
|---|
| 95 | 86 | len = dlen; |
|---|
| 96 | 87 | |
|---|
| 97 | | - frag->page_offset = 0; |
|---|
| 88 | + skb_frag_off_set(frag, 0); |
|---|
| 98 | 89 | skb_frag_size_set(frag, len); |
|---|
| 99 | 90 | memcpy(skb_frag_address(frag), scratch, len); |
|---|
| 100 | 91 | |
|---|
| .. | .. |
|---|
| 108 | 99 | err = 0; |
|---|
| 109 | 100 | |
|---|
| 110 | 101 | out: |
|---|
| 111 | | - local_unlock(ipcomp_scratches_lock); |
|---|
| 102 | + put_cpu(); |
|---|
| 112 | 103 | return err; |
|---|
| 113 | 104 | } |
|---|
| 114 | 105 | |
|---|
| .. | .. |
|---|
| 151 | 142 | int err; |
|---|
| 152 | 143 | |
|---|
| 153 | 144 | local_bh_disable(); |
|---|
| 154 | | - local_lock(ipcomp_scratches_lock); |
|---|
| 155 | | - |
|---|
| 156 | 145 | scratch = *this_cpu_ptr(ipcomp_scratches); |
|---|
| 157 | 146 | tfm = *this_cpu_ptr(ipcd->tfms); |
|---|
| 158 | 147 | err = crypto_comp_compress(tfm, start, plen, scratch, &dlen); |
|---|
| .. | .. |
|---|
| 165 | 154 | } |
|---|
| 166 | 155 | |
|---|
| 167 | 156 | memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen); |
|---|
| 168 | | - local_unlock(ipcomp_scratches_lock); |
|---|
| 169 | 157 | local_bh_enable(); |
|---|
| 170 | 158 | |
|---|
| 171 | 159 | pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr)); |
|---|
| 172 | 160 | return 0; |
|---|
| 173 | 161 | |
|---|
| 174 | 162 | out: |
|---|
| 175 | | - local_unlock(ipcomp_scratches_lock); |
|---|
| 176 | 163 | local_bh_enable(); |
|---|
| 177 | 164 | return err; |
|---|
| 178 | 165 | } |
|---|
| .. | .. |
|---|
| 225 | 212 | vfree(*per_cpu_ptr(scratches, i)); |
|---|
| 226 | 213 | |
|---|
| 227 | 214 | free_percpu(scratches); |
|---|
| 215 | + ipcomp_scratches = NULL; |
|---|
| 228 | 216 | } |
|---|
| 229 | 217 | |
|---|
| 230 | 218 | static void * __percpu *ipcomp_alloc_scratches(void) |
|---|
| .. | .. |
|---|
| 263 | 251 | break; |
|---|
| 264 | 252 | } |
|---|
| 265 | 253 | |
|---|
| 266 | | - WARN_ON(!pos); |
|---|
| 254 | + WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list)); |
|---|
| 267 | 255 | |
|---|
| 268 | 256 | if (--pos->users) |
|---|
| 269 | 257 | return; |
|---|