Re: [PATCH] scsi: libcxgbi: use kvzalloc instead of opencoded kzalloc/vzalloc
From: Denis Efremov
Date: Sat Aug 01 2020 - 04:01:10 EST
On 8/1/20 10:51 AM, Denis Efremov wrote:
>
>
> On 8/1/20 1:24 AM, Joe Perches wrote:
>> On Sat, 2020-08-01 at 01:10 +0300, Denis Efremov wrote:
>>>
>>> On 8/1/20 12:58 AM, Joe Perches wrote:
>>>> On Sat, 2020-08-01 at 00:55 +0300, Denis Efremov wrote:
>>>>> Remove cxgbi_alloc_big_mem(), cxgbi_free_big_mem() functions
>>>>> and use kvzalloc/kvfree instead.
>>>>
>>>> Sensible, thanks.
>>>>
>>>>> diff --git a/drivers/scsi/cxgbi/libcxgbi.c b/drivers/scsi/cxgbi/libcxgbi.c
>>>> []
>>>>> @@ -77,9 +77,9 @@ int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
>>>>> {
>>>>> struct cxgbi_ports_map *pmap = &cdev->pmap;
>>>>>
>>>>> - pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
>>>>> - sizeof(struct cxgbi_sock *),
>>>>> - GFP_KERNEL);
>>>>> + pmap->port_csk = kvzalloc(array_size(max_conn,
>>>>> + sizeof(struct cxgbi_sock *)),
>>>>> + GFP_KERNEL);
>>>>
>>>> missing __GFP_NOWARN
>>>>
>>>
>>> kvmalloc_node adds __GFP_NOWARN internally to kmalloc call
>>> https://elixir.bootlin.com/linux/v5.8-rc4/source/mm/util.c#L568
>>
>> Only when there's a fallback, and the fallback does not.
>>
>
> Sorry, Joe, I don't understand why do we need to add __GFP_NOWARN here.
>
> We use GFP_KERNEL for allocation. cxgbi_alloc_big_mem adds __GFP_NOWARN only to kzalloc().
> kvzalloc is: kvmalloc(size, flags | __GFP_ZERO)
> kvmalloc is: kvmalloc_node(size, flags, NUMA_NO_NODE)
> kvmalloc_node:
> if ((flags & GFP_KERNEL) != GFP_KERNEL) // false, flags == GFP_KERNEL|__GFP_ZERO
> return kmalloc_node(size, flags, node);
>
> if (size > PAGE_SIZE) {
> kmalloc_flags |= __GFP_NOWARN; // add __GFP_NOWARN for "big" allocations
>
> if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
> kmalloc_flags |= __GFP_NORETRY;
> }
>
> // kmalloc_flags == GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN if size > PAGE_SIZE
> ret = kmalloc_node(size, kmalloc_flags, node);
>
> if (ret || size <= PAGE_SIZE)
> return ret;
>
> // flags == GFP_KERNEL|__GFP_ZERO
> return __vmalloc_node(size, 1, flags, node,
> __builtin_return_address(0));
>
> So, to my understanding the difference is only that cxgbi_alloc_big_mem adds __GFP_NOWARN
> to kzalloc unconditionally, kvzalloc adds __GFP_NOWARN to kmalloc_node if size > PAGE_SIZE.
> We use: max_conn * sizeof(struct cxgbi_sock *) in allocation. max_conn can be either
> CXGBI_MAX_CONN or CXGB4I_MAX_CONN. CXGBI_MAX_CONN == 16384, CXGB4I_MAX_CONN == 16384.
> Thus the allocation is bigger than PAGE_SIZE and kvmalloc_node adds __GFP_NOWARN to the
> kmalloc_node call. Maybe I missed something?
>
Of course max_conn can be smaller than CXGBI_MAX_CONN and CXGB4I_MAX_CONN and size < PAGE_SIZE.
However, it's by design not to warn on "small" allocations in kvmalloc_node and it will not even
try to call __vmalloc_node if "small" allocation will fail with kmalloc_node. I think that this
behavior fits the cxgbi_device_portmap_create() context call.