From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA1C8C43387 for ; Wed, 16 Jan 2019 14:09:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 73B7820657 for ; Wed, 16 Jan 2019 14:09:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391090AbfAPOJ2 (ORCPT ); Wed, 16 Jan 2019 09:09:28 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33706 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733035AbfAPOJ2 (ORCPT ); Wed, 16 Jan 2019 09:09:28 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EE727A786F; Wed, 16 Jan 2019 14:09:27 +0000 (UTC) Received: from redhat.com (ovpn-120-67.rdu2.redhat.com [10.10.120.67]) by smtp.corp.redhat.com (Postfix) with SMTP id 6AE091073028; Wed, 16 Jan 2019 14:09:27 +0000 (UTC) Date: Wed, 16 Jan 2019 09:09:26 -0500 From: "Michael S. Tsirkin" To: Cheng Fei Phung Cc: fei phung , "netdev@vger.kernel.org" Subject: Re: Question on ptr_ring linux header Message-ID: <20190116090809-mutt-send-email-mst@kernel.org> References: <20190115130157-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 16 Jan 2019 14:09:28 +0000 (UTC) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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