Subject: Re: [PATCH v4 1/9] bgmac: simplify tx ring index handling

From: Felix Fietkau <[email-masked]>
Date: Mon, 13 Apr 2015 17:01:02 +0200

On 2015-04-13 16:21, Rafał Miłecki wrote:
>> @@ -158,13 +157,7 @@ static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
>>                 skb_checksum_help(skb);
>>
>>         nr_frags = skb_shinfo(skb)->nr_frags;
>> -
>> -       if (ring->start <= ring->end)
>> -               free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
>> -       else
>> -               free_slots = ring->start - ring->end;
>> -
>> -       if (free_slots <= nr_frags + 1) {
>> +       if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
>>                 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
>>                 netif_stop_queue(net_dev);
>>                 return NETDEV_TX_BUSY;
> 
> How is this going to work with ring->end lower than ring->start? Let's
> say you have 2 empty slots at the end of ring and 2 empty slots at the
> beginning. In total 4 free slots. Doing ring->end - ring->start will
> give you some big negative number (depending on the ring size), won't
> it?
It won't, because the resulting data type is limited to 32 bit. I'm
pretty sure there are other places in the kernel that rely on the same
behavior.

>> @@ -284,10 +276,8 @@ static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
>>                         slot->skb = NULL;
>>                 }
>>
>> -next:
>>                 slot->dma_addr = 0;
>> -               if (++ring->start >= BGMAC_TX_RING_SLOTS)
>> -                       ring->start = 0;
>> +               ring->start++;
>>                 freed = true;
>>         }
>>
> 
> Do I understand correctly you're using u32 overflow here? Is this
> OK/allowed in kernel to knowingly use overflows?
I think so.

>> diff --git a/drivers/net/ethernet/broadcom/bgmac.h b/drivers/net/ethernet/broadcom/bgmac.h
>> index 3ad965f..5a198d5 100644
>> --- a/drivers/net/ethernet/broadcom/bgmac.h
>> +++ b/drivers/net/ethernet/broadcom/bgmac.h
>> @@ -414,10 +414,10 @@ enum bgmac_dma_ring_type {
>>   * empty.
>>   */
>>  struct bgmac_dma_ring {
>> -       u16 num_slots;
>> -       u16 start;
>> -       u16 end;
>> +       u32 start;
>> +       u32 end;
>>
>> +       u16 num_slots;
>>         u16 mmio_base;
>>         struct bgmac_dma_desc *cpu_base;
>>         dma_addr_t dma_base;
> 
> Any reason for u32 instead of u16?
To avoid writes touching other fields close to it.

- Felix