Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754373AbdLFJWH (ORCPT ); Wed, 6 Dec 2017 04:22:07 -0500 Received: from mail-co1nam03on0077.outbound.protection.outlook.com ([104.47.40.77]:34624 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753656AbdLFJWA (ORCPT ); Wed, 6 Dec 2017 04:22:00 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=George.Cherian@cavium.com; Subject: Re: [PATCH] ptr_ring: add barriers To: "Michael S. Tsirkin" , linux-kernel@vger.kernel.org Cc: George Cherian , Jason Wang , 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> From: George Cherian Message-ID: <7d1ce1b5-edba-b017-3131-37405f1b0c24@caviumnetworks.com> Date: Wed, 6 Dec 2017 14:51:41 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <1512501990-30029-1-git-send-email-mst@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [119.82.125.132] X-ClientProxiedBy: DM5PR21CA0011.namprd21.prod.outlook.com (10.173.176.149) To CY1PR0701MB1710.namprd07.prod.outlook.com (10.163.21.12) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d0b5ac62-5d1d-428f-6036-08d53c8acc52 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603286);SRVR:CY1PR0701MB1710; X-Microsoft-Exchange-Diagnostics: 1;CY1PR0701MB1710;3:ZkAdnaDEcX+ajAV+tRk9HLQe2syjfqHcyLROCS/DypHD7bowzhjEmU0GkVu1hoTlCiYvv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7R2ft1AQUprjLRxlWtNfuYs+ozCtHPCiz8k9okE2XN96idUPYmiR6VC80dn9CUI8T/h/nINSJjAtqXrECVh4tPh6dB2pA4fntfGgX69Jfn0D5sp24hLdrRDTUK0f7MundjnCjpmb1vu+oyRhl08snwF83JnxdeXm+mgI01Meu6kAK31A+I2bjL1/8gHi+czN5Wjj6afyGv5ueRJuQl5xcL65ObOFqEMi/m5nUpi5QASvz6G6y5zCw9ZgbtGw7Da1XuOIS8oTXm+1bUCL5jYLLDwf04hnKopWK8j0hdlzTPpfOuiy4T4Zh7/0PXB2Y6i8/ntLz8B7k+8hxBOP11vgg3qKKS+Ogre4OGBMoQ0xejPzR1wjZsUiudBdJvSLzUg=;4:ZuxB7Zeyg3OEUddThXL292NUFpWIxoKeOIPbBXsZo1VNEj4xUrqTefeKWtiftRy8hVMo5AUF1vj8JeIE9Kb8U+IlYjeFwQFgtZYpvPEsp08ChUFemxnpcAxSlYlsF0hiFqFnOOBRQeUTVGJeHkUDm4Cbq99I6vi9x4oiOucVKCkDuw1FhSX32zVyfNV4KH+uHEOUPP6DKEDmmbK4WB2kXOYQ2hAsaSwVqIQrUTJHR9dkSjmnIrOYcJS6DFBvh3oK9vqOh9yZDtHH0+/r7559Ig== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(8121501046)(5005006)(3231022)(93006095)(10201501046)(3002001)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123560025)(20161123564025)(20161123562025)(20161123555025)(6072148)(201708071742011);SRVR:CY1PR0701MB1710;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:CY1PR0701MB1710; X-Forefront-PRVS: 05134F8B4F X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6049001)(346002)(376002)(366004)(199004)(189003)(24454002)(305945005)(47776003)(31696002)(6246003)(64126003)(7736002)(6116002)(52146003)(3846002)(229853002)(2486003)(23676004)(76176011)(36756003)(230700001)(4326008)(25786009)(53936002)(16526018)(8676002)(6666003)(81166006)(72206003)(81156014)(52116002)(31686004)(66066001)(2950100002)(5660300001)(65956001)(42882006)(50466002)(54906003)(478600001)(106356001)(65826007)(97736004)(65806001)(67846002)(101416001)(105586002)(33646002)(6486002)(58126008)(68736007)(16576012)(83506002)(77096006)(316002)(8936002)(90366009)(53546010)(2906002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY1PR0701MB1710;H:[10.167.103.249];FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTFQUjA3MDFNQjE3MTA7MjM6dkhkdXEwSUovbllHYUt0SnZsWnNnaEZl?= =?utf-8?B?c0V0RGFUZ3BFaGJ5SEkyVkVtK1FYcVVzdVVCc1ZOaENLcWkzTHJMWW9sQXlB?= =?utf-8?B?YlNiYlpkL2MxMlphYkY4dUI1bmVJYUFUK3FsMmRGV3BQR0tGSjA5cm12SGN2?= =?utf-8?B?bjZLekxwZUo2WnVWZnpaSTBwcmxJVjBqcGg1cnZBN0xnUmY5c1hkT3NIODRQ?= =?utf-8?B?WFdjUDZGMGFNTGpXZDBWd283N2o0UlhzaUN6S29Xak12ZlpUZXBzaU1ZUGhB?= =?utf-8?B?a0k1MHVuRHRGRHZKTkNyREJYYTlRSkdTZno2cnFET2hTNlA2RGVpZHU1cXlt?= =?utf-8?B?SER2Y2N1eE9CVGVYSkdMZTYrTFgyRytUMm9waWhGSjkrNjloQUdZRDJmTElT?= =?utf-8?B?ZzF0c1hCcVZSdUFYcHVsVUxaOEltclhVbTg3blZCNkJPRXZDLzdCQ0ZEcnZL?= =?utf-8?B?SnkxcEw0UnU0YUwrTm94dDRLakgrL2NMRGpCUTRncm9qV2pwbE9yZzhGY1Rq?= =?utf-8?B?L3BCZW9MY3k0OCtUcUdWSkNsTnlPWUtrTG9pM3piR1hKcFdjaEFId3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72bDVKG5/5Ylo=;24:LHCfOZuPjusGgpI9T4QMfUoJF0gNOG579TiFLGA7lBLaN1bRPIxrvpyRJCHiwBf9m30IjGBxOyW41npvVn32s3IANI8t2rDZADYaxtlEj6c=;7:9oGsWrRUFEBvOQzsWqpm+bN3o35Yp7hQxrlBZGYudvlkn8nutz5c/Ol9wlChw3kyWZ6OlvV2jUh+rgsexIBiwIFfmZZnI/fciigKDKBBxX83XPOroyKvo6pTTaaxP3x/d/CpUZ7nobpnSp345COdUBoKPSCR6aS9Cj2N/4L1/0z7Q0Nr4sPBuKiEs2dOGC49c2DcokPSc34W1GbNwbEfVKe7ox49CBLRX83LWm5IePginftbmOY7L5hh/aDvjA0E SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2017 09:21:53.0302 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d0b5ac62-5d1d-428f-6036-08d53c8acc52 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0701MB1710 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3299 Lines: 92 Hi Michael, On 12/06/2017 12:59 AM, 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. This is not the exact situation we are seeing. Let me try to explain the situation Affected on ARM64 platform. 1) tun_net_xmit calls skb_array_produce, which pushes the skb to the ptr_ring, this push is protected by a producer_lock. 2)Prior to this call the tun_net_xmit calls skb_orphan which calls the skb->destructor and sets skb->destructor and skb->sk as NULL. 2.a) These 2 writes are getting reordered 3) At the same time in the receive side (tun_ring_recv), which gets executed in another core calls skb_array_consume which pulls the skb from ptr ring, this pull is protected by a consumer lock. 4) eventually calling the skb->destructor (sock_wfree) with stale values. Also note that this issue is reproducible in a long run and doesn't happen immediately after the launch of multiple VM's (infact the particular test cases launches 56 VM's which does iperf back and forth) > > 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. It is not the case that producer is reading the value, but the consumer reading stale value. So we need to have a strict rmb in place . > > 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; > } > >