All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: fei phung <feiphung27@gmail.com>
Cc: netdev@vger.kernel.org, feiphung@hotmail.com
Subject: Re: Question on ptr_ring linux header
Date: Tue, 15 Jan 2019 13:13:48 -0500	[thread overview]
Message-ID: <20190115130157-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <CAF8QhUiX_ba1gavf1yODAyvFyBr6vJ2mgZWsnH53BkKzXY4u2g@mail.gmail.com>

On Wed, Jan 16, 2019 at 01:10:31AM +0800, fei phung wrote:
> Hi,
> 
> inline int pop_circ_queue(struct ptr_ring * buffer, struct item * item_pop)
> {
>         if (!ptr_ring_empty_any(buffer)) // if (not empty)
>         {
>                 DEBUG_MSG(KERN_INFO "Before pop, head = %u , tail =
> %u\n", buffer->consumer_head, buffer->consumer_tail);
> 
>                 /* extract one item struct containing two unsigned
> integers from the buffer */
>                 *item_pop = *((struct item *)ptr_ring_consume_any(buffer));
> 
>                 DEBUG_MSG(KERN_INFO "val1 = %u , val2 = %u\n",
> item_pop->val1, item_pop->val2);
> 
>                 DEBUG_MSG(KERN_INFO "After pop, head = %u , tail =
> %u\n", buffer->consumer_head, buffer->consumer_tail);
> 
>                 return 0;
>         }
> 
>         else return 1; // empty, nothing to pop from the ring
> }
> 
> > https://gist.github.com/promach/65e9331d55a43a2815239430a28e29c6#file-circ_ring-c-L44
> > racy if there are multiple consumers.
> > just call ptr_ring_consume_any.
> 
> > And it seems to leak the memory the pointer to which you
> > have consumed - although it's possible it's freed elsewhere -
> 
> I will definitely just call ptr_ring_consume_any() without ptr_ring_empty_any()
> 
> Which exact line has memory leak ?
> Are you referring to   struct item * item_pop   ?

yes:

                  *item_pop = *((struct item *)ptr_ring_consume_any(buffer));

seems to discard the pointer returned.

> 
> 
> // TX (PC receive) scatter gather buffer is read.
> if (vect & (1<<((5*i)+1))) {
>         recv = 1;
> 
>         item_recv_push[sc->recv[chnl]->msgs->producer].val1 = EVENT_SG_BUF_READ;
>         item_recv_push[sc->recv[chnl]->msgs->producer].val2 = 0;
> 
>         // Keep track so the thread can handle this.
>         if (push_circ_queue(sc->recv[chnl]->msgs,
> &item_recv_push[sc->recv[chnl]->msgs->producer])) {
>                 printk(KERN_ERR "riffa: fpga:%d chnl:%d, recv sg buf
> read msg queue full\n", sc->id, chnl);
>         }
>         DEBUG_MSG(KERN_INFO "riffa: fpga:%d chnl:%d, recv sg buf
> read\n", sc->id, chnl);
> }
> 
> // TX (PC receive) transaction done.
> if (vect & (1<<((5*i)+2))) {
>         recv = 1;
> 
>         item_recv_push[sc->recv[chnl]->msgs->producer].val1 = EVENT_TXN_DONE;
>         item_recv_push[sc->recv[chnl]->msgs->producer].val2 = len;
> 
>         // Read the transferred amount.
>         len = read_reg(sc, CHNL_REG(chnl, TX_TNFR_LEN_REG_OFF));
>         // Notify the thread.
>         if (push_circ_queue(sc->recv[chnl]->msgs,
> &item_recv_push[sc->recv[chnl]->msgs->producer])) {
>                 printk(KERN_ERR "riffa: fpga:%d chnl:%d, recv txn done
> msg queue full\n", sc->id, chnl);
>         }
>         DEBUG_MSG(KERN_INFO "riffa: fpga:%d chnl:%d, recv txn done\n",
> sc->id, chnl);
> }
> 
> > https://gist.github.com/promach/7716ee8addcaa33fda140d74d1ad94d6#file-riffa_driver-c-L663
> > this one seems to poke at the internal producer index for its own
> > purposes. That's not something I expected when I built ptr_ring
> 
> if I do not use ->producer in the above multiple (due to multiple if()
> clause) sequential
> push_circ_queue() operations,  what else I can use other than
> ->producer to store the data
> for item_recv_push   ? Probably a simple integer indexing will do.


maybe

> 
> 
> 
> > https://i.imgur.com/xWJOH1G.png
> > I am having problem getting proper ptr_ring operation where one
> > ptr_ring entry (val1=2 for item_recv_pop) is missing from the void ** queue
> 
> This "ptr_ring packet" drop does not make any sense to me.
> 
> Regards,
> Phung


i suspect that you overwrite an entry in your data structure
due to race wrt producer index access.

  reply	other threads:[~2019-01-15 18:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-15 17:10 Question on ptr_ring linux header fei phung
2019-01-15 18:13 ` Michael S. Tsirkin [this message]
     [not found]   ` <SG2PR06MB3176FA4C12888C84990F109BC1820@SG2PR06MB3176.apcprd06.prod.outlook.com>
2019-01-16 14:09     ` Michael S. Tsirkin
  -- strict thread matches above, loose matches on Subject: below --
2019-01-17  3:51 fei phung
2019-01-31  5:16 ` fei phung
2019-01-31 14:39   ` Michael S. Tsirkin
2019-02-01  8:12     ` fei phung
2019-02-01 15:36       ` Michael S. Tsirkin
2019-02-15  3:03         ` fei phung
2019-03-01  3:20           ` fei phung
2019-01-16  7:00 fei phung
2019-01-15  4:33 fei phung
2019-01-15 12:48 ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190115130157-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=feiphung27@gmail.com \
    --cc=feiphung@hotmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.