From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754632AbdLFDWG (ORCPT ); Tue, 5 Dec 2017 22:22:06 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40028 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754406AbdLFDWD (ORCPT ); Tue, 5 Dec 2017 22:22:03 -0500 Subject: Re: [PATCH] ptr_ring: add barriers To: "Michael S. Tsirkin" Cc: linux-kernel@vger.kernel.org, George Cherian , davem@davemloft.net, edumazet@google.com, netdev@vger.kernel.org, virtualization@lists.linux-foundation.org References: <1512501990-30029-1-git-send-email-mst@redhat.com> <96e8cec4-37db-b41d-b02b-767a21d9efba@redhat.com> <20171206045247-mutt-send-email-mst@kernel.org> From: Jason Wang Message-ID: <4de6d61a-6c49-9298-ca11-9b53aa5254ba@redhat.com> Date: Wed, 6 Dec 2017 11:21:54 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20171206045247-mutt-send-email-mst@kernel.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 06 Dec 2017 03:22:03 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2017年12月06日 10:53, Michael S. Tsirkin wrote: > On Wed, Dec 06, 2017 at 10:31:39AM +0800, Jason Wang wrote: >> >> On 2017年12月06日 03:29, Michael S. Tsirkin wrote: >>> Users of ptr_ring expect that it's safe to give the >>> data structure a pointer and have it be available >>> to consumers, but that actually requires an smb_wmb >>> or a stronger barrier. >>> >>> In absence of such barriers and on architectures that reorder writes, >>> consumer might read an un=initialized value from an skb pointer stored >>> in the skb array. This was observed causing crashes. >>> >>> To fix, add memory barriers. The barrier we use is a wmb, the >>> assumption being that producers do not need to read the value so we do >>> not need to order these reads. >>> >>> Reported-by: George Cherian >>> Suggested-by: Jason Wang >>> Signed-off-by: Michael S. Tsirkin >>> --- >>> >>> George, could you pls report whether this patch fixes >>> the issue for you? >>> >>> This seems to be needed in stable as well. >>> >>> >>> >>> >>> include/linux/ptr_ring.h | 9 +++++++++ >>> 1 file changed, 9 insertions(+) >>> >>> diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h >>> index 37b4bb2..6866df4 100644 >>> --- a/include/linux/ptr_ring.h >>> +++ b/include/linux/ptr_ring.h >>> @@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r) >>> /* Note: callers invoking this in a loop must use a compiler barrier, >>> * for example cpu_relax(). Callers must hold producer_lock. >>> + * Callers are responsible for making sure pointer that is being queued >>> + * points to a valid data. >>> */ >>> static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr) >>> { >>> if (unlikely(!r->size) || r->queue[r->producer]) >>> return -ENOSPC; >>> + /* Make sure the pointer we are storing points to a valid data. */ >>> + /* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */ >>> + smp_wmb(); >>> + >>> r->queue[r->producer++] = ptr; >>> if (unlikely(r->producer >= r->size)) >>> r->producer = 0; >>> @@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r) >>> if (ptr) >>> __ptr_ring_discard_one(r); >>> + /* Make sure anyone accessing data through the pointer is up to date. */ >>> + /* Pairs with smp_wmb in __ptr_ring_produce. */ >>> + smp_read_barrier_depends(); >>> return ptr; >>> } >> I was thinking whether or not it's better to move those to the callers. Then >> we can save lots of barriers in e.g batch consuming. >> >> Thanks > Batch consumers only do smp_read_barrier_depends which is free on > non-alpha. I suggest we do the simple thing for stable and reserve > optimizations for later. > Right. Acked-by: Jason Wang