mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-21 09:24:37 +07:00
6da2ec5605
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This patch replaces cases of: kmalloc(a * b, gfp) with: kmalloc_array(a * b, gfp) as well as handling cases of: kmalloc(a * b * c, gfp) with: kmalloc(array3_size(a, b, c), gfp) as it's slightly less ugly than: kmalloc_array(array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: kmalloc(4 * 1024, gfp) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. The tools/ directory was manually excluded, since it has its own implementation of kmalloc(). The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ type TYPE; expression THING, E; @@ ( kmalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | kmalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( kmalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(char) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(u8) * COUNT + COUNT , ...) | kmalloc( - sizeof(__u8) * COUNT + COUNT , ...) | kmalloc( - sizeof(char) * COUNT + COUNT , ...) | kmalloc( - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - kmalloc + kmalloc_array ( - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ - kmalloc + kmalloc_array ( - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( kmalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( kmalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kmalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kmalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kmalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kmalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | kmalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ identifier STRIDE, SIZE, COUNT; @@ ( kmalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products, // when they're not all constants... @@ expression E1, E2, E3; constant C1, C2, C3; @@ ( kmalloc(C1 * C2 * C3, ...) | kmalloc( - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | kmalloc( - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | kmalloc( - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | kmalloc( - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants, // keeping sizeof() as the second factor argument. @@ expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( kmalloc(sizeof(THING) * C2, ...) | kmalloc(sizeof(TYPE) * C2, ...) | kmalloc(C1 * C2 * C3, ...) | kmalloc(C1 * C2, ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - (E1) * E2 + E1, E2 , ...) | - kmalloc + kmalloc_array ( - (E1) * (E2) + E1, E2 , ...) | - kmalloc + kmalloc_array ( - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
306 lines
7.5 KiB
C
306 lines
7.5 KiB
C
/*
|
|
* Copyright (c) 2004 Topspin Communications. All rights reserved.
|
|
*
|
|
* This software is available to you under a choice of one of two
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
* General Public License (GPL) Version 2, available from the file
|
|
* COPYING in the main directory of this source tree, or the
|
|
* OpenIB.org BSD license below:
|
|
*
|
|
* Redistribution and use in source and binary forms, with or
|
|
* without modification, are permitted provided that the following
|
|
* conditions are met:
|
|
*
|
|
* - Redistributions of source code must retain the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer.
|
|
*
|
|
* - Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer in the documentation and/or other materials
|
|
* provided with the distribution.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include <linux/errno.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/bitmap.h>
|
|
|
|
#include "mthca_dev.h"
|
|
|
|
/* Trivial bitmap-based allocator */
|
|
u32 mthca_alloc(struct mthca_alloc *alloc)
|
|
{
|
|
unsigned long flags;
|
|
u32 obj;
|
|
|
|
spin_lock_irqsave(&alloc->lock, flags);
|
|
|
|
obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
|
|
if (obj >= alloc->max) {
|
|
alloc->top = (alloc->top + alloc->max) & alloc->mask;
|
|
obj = find_first_zero_bit(alloc->table, alloc->max);
|
|
}
|
|
|
|
if (obj < alloc->max) {
|
|
set_bit(obj, alloc->table);
|
|
obj |= alloc->top;
|
|
} else
|
|
obj = -1;
|
|
|
|
spin_unlock_irqrestore(&alloc->lock, flags);
|
|
|
|
return obj;
|
|
}
|
|
|
|
void mthca_free(struct mthca_alloc *alloc, u32 obj)
|
|
{
|
|
unsigned long flags;
|
|
|
|
obj &= alloc->max - 1;
|
|
|
|
spin_lock_irqsave(&alloc->lock, flags);
|
|
|
|
clear_bit(obj, alloc->table);
|
|
alloc->last = min(alloc->last, obj);
|
|
alloc->top = (alloc->top + alloc->max) & alloc->mask;
|
|
|
|
spin_unlock_irqrestore(&alloc->lock, flags);
|
|
}
|
|
|
|
int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,
|
|
u32 reserved)
|
|
{
|
|
int i;
|
|
|
|
/* num must be a power of 2 */
|
|
if (num != 1 << (ffs(num) - 1))
|
|
return -EINVAL;
|
|
|
|
alloc->last = 0;
|
|
alloc->top = 0;
|
|
alloc->max = num;
|
|
alloc->mask = mask;
|
|
spin_lock_init(&alloc->lock);
|
|
alloc->table = kmalloc_array(BITS_TO_LONGS(num), sizeof(long),
|
|
GFP_KERNEL);
|
|
if (!alloc->table)
|
|
return -ENOMEM;
|
|
|
|
bitmap_zero(alloc->table, num);
|
|
for (i = 0; i < reserved; ++i)
|
|
set_bit(i, alloc->table);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void mthca_alloc_cleanup(struct mthca_alloc *alloc)
|
|
{
|
|
kfree(alloc->table);
|
|
}
|
|
|
|
/*
|
|
* Array of pointers with lazy allocation of leaf pages. Callers of
|
|
* _get, _set and _clear methods must use a lock or otherwise
|
|
* serialize access to the array.
|
|
*/
|
|
|
|
#define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)
|
|
|
|
void *mthca_array_get(struct mthca_array *array, int index)
|
|
{
|
|
int p = (index * sizeof (void *)) >> PAGE_SHIFT;
|
|
|
|
if (array->page_list[p].page)
|
|
return array->page_list[p].page[index & MTHCA_ARRAY_MASK];
|
|
else
|
|
return NULL;
|
|
}
|
|
|
|
int mthca_array_set(struct mthca_array *array, int index, void *value)
|
|
{
|
|
int p = (index * sizeof (void *)) >> PAGE_SHIFT;
|
|
|
|
/* Allocate with GFP_ATOMIC because we'll be called with locks held. */
|
|
if (!array->page_list[p].page)
|
|
array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
|
|
|
|
if (!array->page_list[p].page)
|
|
return -ENOMEM;
|
|
|
|
array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;
|
|
++array->page_list[p].used;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void mthca_array_clear(struct mthca_array *array, int index)
|
|
{
|
|
int p = (index * sizeof (void *)) >> PAGE_SHIFT;
|
|
|
|
if (--array->page_list[p].used == 0) {
|
|
free_page((unsigned long) array->page_list[p].page);
|
|
array->page_list[p].page = NULL;
|
|
} else
|
|
array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
|
|
|
|
if (array->page_list[p].used < 0)
|
|
pr_debug("Array %p index %d page %d with ref count %d < 0\n",
|
|
array, index, p, array->page_list[p].used);
|
|
}
|
|
|
|
int mthca_array_init(struct mthca_array *array, int nent)
|
|
{
|
|
int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;
|
|
int i;
|
|
|
|
array->page_list = kmalloc_array(npage, sizeof(*array->page_list),
|
|
GFP_KERNEL);
|
|
if (!array->page_list)
|
|
return -ENOMEM;
|
|
|
|
for (i = 0; i < npage; ++i) {
|
|
array->page_list[i].page = NULL;
|
|
array->page_list[i].used = 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void mthca_array_cleanup(struct mthca_array *array, int nent)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
|
|
free_page((unsigned long) array->page_list[i].page);
|
|
|
|
kfree(array->page_list);
|
|
}
|
|
|
|
/*
|
|
* Handling for queue buffers -- we allocate a bunch of memory and
|
|
* register it in a memory region at HCA virtual address 0. If the
|
|
* requested size is > max_direct, we split the allocation into
|
|
* multiple pages, so we don't require too much contiguous memory.
|
|
*/
|
|
|
|
int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,
|
|
union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,
|
|
int hca_write, struct mthca_mr *mr)
|
|
{
|
|
int err = -ENOMEM;
|
|
int npages, shift;
|
|
u64 *dma_list = NULL;
|
|
dma_addr_t t;
|
|
int i;
|
|
|
|
if (size <= max_direct) {
|
|
*is_direct = 1;
|
|
npages = 1;
|
|
shift = get_order(size) + PAGE_SHIFT;
|
|
|
|
buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
|
|
size, &t, GFP_KERNEL);
|
|
if (!buf->direct.buf)
|
|
return -ENOMEM;
|
|
|
|
dma_unmap_addr_set(&buf->direct, mapping, t);
|
|
|
|
memset(buf->direct.buf, 0, size);
|
|
|
|
while (t & ((1 << shift) - 1)) {
|
|
--shift;
|
|
npages *= 2;
|
|
}
|
|
|
|
dma_list = kmalloc_array(npages, sizeof(*dma_list),
|
|
GFP_KERNEL);
|
|
if (!dma_list)
|
|
goto err_free;
|
|
|
|
for (i = 0; i < npages; ++i)
|
|
dma_list[i] = t + i * (1 << shift);
|
|
} else {
|
|
*is_direct = 0;
|
|
npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
|
|
shift = PAGE_SHIFT;
|
|
|
|
dma_list = kmalloc_array(npages, sizeof(*dma_list),
|
|
GFP_KERNEL);
|
|
if (!dma_list)
|
|
return -ENOMEM;
|
|
|
|
buf->page_list = kmalloc_array(npages,
|
|
sizeof(*buf->page_list),
|
|
GFP_KERNEL);
|
|
if (!buf->page_list)
|
|
goto err_out;
|
|
|
|
for (i = 0; i < npages; ++i)
|
|
buf->page_list[i].buf = NULL;
|
|
|
|
for (i = 0; i < npages; ++i) {
|
|
buf->page_list[i].buf =
|
|
dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
|
|
&t, GFP_KERNEL);
|
|
if (!buf->page_list[i].buf)
|
|
goto err_free;
|
|
|
|
dma_list[i] = t;
|
|
dma_unmap_addr_set(&buf->page_list[i], mapping, t);
|
|
|
|
clear_page(buf->page_list[i].buf);
|
|
}
|
|
}
|
|
|
|
err = mthca_mr_alloc_phys(dev, pd->pd_num,
|
|
dma_list, shift, npages,
|
|
0, size,
|
|
MTHCA_MPT_FLAG_LOCAL_READ |
|
|
(hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),
|
|
mr);
|
|
if (err)
|
|
goto err_free;
|
|
|
|
kfree(dma_list);
|
|
|
|
return 0;
|
|
|
|
err_free:
|
|
mthca_buf_free(dev, size, buf, *is_direct, NULL);
|
|
|
|
err_out:
|
|
kfree(dma_list);
|
|
|
|
return err;
|
|
}
|
|
|
|
void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,
|
|
int is_direct, struct mthca_mr *mr)
|
|
{
|
|
int i;
|
|
|
|
if (mr)
|
|
mthca_free_mr(dev, mr);
|
|
|
|
if (is_direct)
|
|
dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
|
|
dma_unmap_addr(&buf->direct, mapping));
|
|
else {
|
|
for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
|
|
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
|
|
buf->page_list[i].buf,
|
|
dma_unmap_addr(&buf->page_list[i],
|
|
mapping));
|
|
kfree(buf->page_list);
|
|
}
|
|
}
|