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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E3423C54E60 for ; Sun, 17 Mar 2024 16:51:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YheUyJWRvM9kvVfW+O3x1cKIYzMPeiq4Q+mTwyi6rXQ=; b=kcHkdIb1PhuAZy PjbVQ9Uzd57gStDaioRuQwmc3FZigAvWv+SVWKSMSkfLp23nP8vIerSFnUzGqXM4nq4swb+5oHzzd ofRnJob6CvIjyJsJL4MEHdPWPu/5A32U6zbvyu5Gls15siX6Tklu7U+euNAH7RzJLPlW06L0wbaHz OvtPeWS6NAx+Os79bOzrPN8M/KaJ3bF+kxogeNTRuve4p8PYQd7p1Zsi1XCxVw9SWqFsYswWZv5UA aTi1kQi8FHn0WyVRe8dbch+BSv6bXCNRjOnDbvG2pw+5UOduCXPGBsqJhoQguIQs/M2MOK9pbdVR4 BwVAErsIz/SFKfuzUIqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rltio-00000005odd-3nBx; Sun, 17 Mar 2024 16:50:54 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rltil-00000005ocs-31dZ for linux-arm-kernel@lists.infradead.org; Sun, 17 Mar 2024 16:50:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710694248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wGvhd+CS6miBdqthamxj1yw7gllwSdC3ScsQwTgOyD8=; b=DHOGNBwUEnmpDJ2La26JVqN+y1+4fSeNrX5Sjcb7kmaWmccBUlprWrJubVdggcQ+wct0Mg 8HrrebPa/g2m+rMjyJdOa6iIX50PrZ958rWCC5uwewGH1QMxMS7zfezkKs4qDPt0eTuO2K gaRlUY4NkLEDB9FXwR5RKoAysBBxP+M= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-54-vE4D6VA_OUyWunGbyNBIGQ-1; Sun, 17 Mar 2024 12:50:47 -0400 X-MC-Unique: vE4D6VA_OUyWunGbyNBIGQ-1 Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-568a3be9127so1931136a12.2 for ; Sun, 17 Mar 2024 09:50:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710694246; x=1711299046; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=wGvhd+CS6miBdqthamxj1yw7gllwSdC3ScsQwTgOyD8=; b=TAUNJu7L4EThLI/i+sAJ5CAzcaT4AWm8n9eZLHNhqfAoY9opRU/Qeks95OpA7ZFn6Z lV4UQMTifoV2UTclaR5VsocZFthV/lJuV4onF56ag4is929tvc3ISudliikGSYI8ViPg pqm9+pC1Ns+2IYhhp1DpN43BVDXCoPbaXQ+sYOSkkx++7AKlbqE44RmyUwmD9hCgCZIh zjjORBkv5ZITDLxiMZU3Jb+1d+Y7xiguKhJji0WiMZJlfnJhNH9pciRV8nJAqBfFSBFN whCz4CRVPeY0kd8tOAeKrSDhyE+5ZXrYNgDxoyHizdWKGPQ3bHYRIpa0qRhdltd14S++ P+mQ== X-Forwarded-Encrypted: i=1; AJvYcCU5FhR3sLNHPMFR+kl1jkz3mEC3yYk05g3hLFJeMM9pcdNh2QwejZj/5eyYa7oHlxrYZbS/qNYU5Vzm8GN/n1cqVz8Iieq7Lpelm2YtDOoqH8nTPX4= X-Gm-Message-State: AOJu0YzZs+MLQiM+cH44nXFJXQS0yqwRkessEZvf6qeOY42n7i5M/qtp JaelmNsqvpFAoxow2OcX6VovogOtLhaHc5s8WSf1cSRdNfT56KI2L86Fb81i4r4EywHuEjyffOk wUlAvVfB17iqekQVtqySQ2b5gc0z54+pEdkxXBX7LLvABAiKAom5GS6KJR3hfhNOPu1n1dxpr X-Received: by 2002:a05:6402:538f:b0:568:b815:caa0 with SMTP id ew15-20020a056402538f00b00568b815caa0mr4116080edb.39.1710694245819; Sun, 17 Mar 2024 09:50:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8Nit2FP2MKnbmfP4dsvWppBfe7t0ROYKCk6hvOVVepgry4K32jKa9tHwjSAngIboUrm7XDw== X-Received: by 2002:a05:6402:538f:b0:568:b815:caa0 with SMTP id ew15-20020a056402538f00b00568b815caa0mr4116069edb.39.1710694245384; Sun, 17 Mar 2024 09:50:45 -0700 (PDT) Received: from redhat.com ([2a02:14f:1f9:4d09:1b04:6193:3a0b:7c1]) by smtp.gmail.com with ESMTPSA id b70-20020a509f4c000000b00567c34d8a82sm3950966edf.85.2024.03.17.09.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Mar 2024 09:50:44 -0700 (PDT) Date: Sun, 17 Mar 2024 12:50:39 -0400 From: "Michael S. Tsirkin" To: Gavin Shan Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, yihyu@redhat.com, shan.gavin@gmail.com, Will Deacon , Catalin Marinas , linux-arm-kernel@lists.infradead.org, mochs@nvidia.com Subject: Re: [PATCH] virtio_ring: Fix the stale index in available ring Message-ID: <20240317124214-mutt-send-email-mst@kernel.org> References: <20240314074923.426688-1-gshan@redhat.com> <20240314040443-mutt-send-email-mst@kernel.org> <9b148de7-b687-4d10-b177-5608b8dc7046@redhat.com> <20240314074216-mutt-send-email-mst@kernel.org> <23dc6d00-6a57-4ddf-8611-f3c6f6a8e43c@redhat.com> <20240314085630-mutt-send-email-mst@kernel.org> <63002c24-8117-458f-84c7-fa4f7acd8cc6@redhat.com> <20240315065318-mutt-send-email-mst@kernel.org> <66e12633-b2d6-4b9a-9103-bb79770fcafa@redhat.com> MIME-Version: 1.0 In-Reply-To: <66e12633-b2d6-4b9a-9103-bb79770fcafa@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240317_095051_871670_1EE48DDC X-CRM114-Status: GOOD ( 25.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Mar 15, 2024 at 09:24:36PM +1000, Gavin Shan wrote: > > On 3/15/24 21:05, Michael S. Tsirkin wrote: > > On Fri, Mar 15, 2024 at 08:45:10PM +1000, Gavin Shan wrote: > > > > > Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried > > > to reproduce it with my own driver where one thread writes to the shared buffer > > > and another thread reads from the buffer. I don't hit the out-of-order issue so > > > far. > > > > Make sure the 2 areas you are accessing are in different cache lines. > > > > Yes, I already put those 2 areas to separate cache lines. > > > > > > My driver may be not correct somewhere and I will update if I can reproduce > > > the issue with my driver in the future. > > > > Then maybe your change is just making virtio slower and masks the bug > > that is actually elsewhere? > > > > You don't really need a driver. Here's a simple test: without barriers > > assertion will fail. With barriers it will not. > > (Warning: didn't bother testing too much, could be buggy. > > > > --- > > > > #include > > #include > > #include > > #include > > > > #define FIRST values[0] > > #define SECOND values[64] > > > > volatile int values[100] = {}; > > > > void* writer_thread(void* arg) { > > while (1) { > > FIRST++; > > // NEED smp_wmb here > __asm__ volatile("dmb ishst" : : : "memory"); > > SECOND++; > > } > > } > > > > void* reader_thread(void* arg) { > > while (1) { > > int first = FIRST; > > // NEED smp_rmb here > __asm__ volatile("dmb ishld" : : : "memory"); > > int second = SECOND; > > assert(first - second == 1 || first - second == 0); > > } > > } > > > > int main() { > > pthread_t writer, reader; > > > > pthread_create(&writer, NULL, writer_thread, NULL); > > pthread_create(&reader, NULL, reader_thread, NULL); > > > > pthread_join(writer, NULL); > > pthread_join(reader, NULL); > > > > return 0; > > } > > > > Had a quick test on NVidia's grace-hopper and Ampere's CPUs. I hit > the assert on both of them. After replacing 'dmb' with 'dsb', I can > hit assert on both of them too. I need to look at the code closely. > > [root@virt-mtcollins-02 test]# ./a > a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. > Aborted (core dumped) > > [root@nvidia-grace-hopper-05 test]# ./a > a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. > Aborted (core dumped) > > Thanks, > Gavin Actually this test is broken. No need for ordering it's a simple race. The following works on x86 though (x86 does not need barriers though). #include #include #include #include #if 0 #define x86_rmb() asm volatile("lfence":::"memory") #define x86_mb() asm volatile("mfence":::"memory") #define x86_smb() asm volatile("sfence":::"memory") #else #define x86_rmb() asm volatile("":::"memory") #define x86_mb() asm volatile("":::"memory") #define x86_smb() asm volatile("":::"memory") #endif #define FIRST values[0] #define SECOND values[640] #define FLAG values[1280] volatile unsigned values[2000] = {}; void* writer_thread(void* arg) { while (1) { /* Now synchronize with reader */ while(FLAG); FIRST++; x86_smb(); SECOND++; x86_smb(); FLAG = 1; } } void* reader_thread(void* arg) { while (1) { /* Now synchronize with writer */ while(!FLAG); x86_rmb(); unsigned first = FIRST; x86_rmb(); unsigned second = SECOND; assert(first - second == 1 || first - second == 0); FLAG = 0; if (!(first %1000000)) printf("%d\n", first); } } int main() { pthread_t writer, reader; pthread_create(&writer, NULL, writer_thread, NULL); pthread_create(&reader, NULL, reader_thread, NULL); pthread_join(writer, NULL); pthread_join(reader, NULL); return 0; } _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel