netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Cheng Fei Phung <feiphung@hotmail.com>
Cc: fei phung <feiphung27@gmail.com>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: Re: Question on ptr_ring linux header
Date: Wed, 16 Jan 2019 09:09:26 -0500	[thread overview]
Message-ID: <20190116090809-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <SG2PR06MB3176FA4C12888C84990F109BC1820@SG2PR06MB3176.apcprd06.prod.outlook.com>

On Wed, Jan 16, 2019 at 06:48:41AM +0000, Cheng Fei Phung wrote:
> Hi,
> 
> > https://gist.github.com/promach/65e9331d55a43a2815239430a28e29c6#file-circ_ring-c-L62
> > racy if there are multiple consumers.
> > just call ptr_ring_consume_any.
> 
> If I modify pop_circ_queue() below to directly use ptr_ring_consume_any() without
> ptr_ring_empty_any() , I am afraid I am running into system crash due to NULL pointer
> deferencing for *item_pop:   
> 
>           *item_pop = *((struct item *)ptr_ring_consume_any(buffer));

Yes - you want to check the returned pointer before dereferencing.


> Just one silly beginner question, why will ptr_ring_empty_any() lead to data race problem ?

because ring can become empty between the check and the call to consume?

> 
> inline int pop_circ_queue(struct ptr_ring * buffer, struct item * item_pop)
> {
> 	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));
> 
> 	if((item_pop != NULL) && (item_pop->val1 > 0)) 
> 	{ 
> 	// val1 will never be zero since the event number starts from 1 (so, val1 in push_circ_queue() will not be zero, same case after pop_circ_queue()), and 0 is only possible during initialization, not during pop_circ_queue()
> 
> 		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 {
> 		DEBUG_MSG(KERN_INFO "empty, nothing to pop from the ring\n");
> 
> 		return 1;
> 	}
> }
> 
> 
> 
> Regards,
> Phung

  parent reply	other threads:[~2019-01-16 14:09 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
     [not found]   ` <SG2PR06MB3176FA4C12888C84990F109BC1820@SG2PR06MB3176.apcprd06.prod.outlook.com>
2019-01-16 14:09     ` Michael S. Tsirkin [this message]
  -- 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=20190116090809-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).