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=-0.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no 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 07C0FC33CAC for ; Thu, 6 Feb 2020 18:43:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D3D8E214AF for ; Thu, 6 Feb 2020 18:43:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="smx53BRL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727910AbgBFSno (ORCPT ); Thu, 6 Feb 2020 13:43:44 -0500 Received: from frisell.zx2c4.com ([192.95.5.64]:60369 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726990AbgBFSnn (ORCPT ); Thu, 6 Feb 2020 13:43:43 -0500 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 0f9268e8; Thu, 6 Feb 2020 18:42:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=date:from:to :cc:subject:message-id:references:mime-version:content-type :content-transfer-encoding:in-reply-to; s=mail; bh=k/OVyrULSF/E2 mtzHs8ojPxS/l0=; b=smx53BRLhnJ1EyJookJhS44+2t0uSJ4wEYlvj2YwBBsoM SMeW9CdQDf7OmnPqOkl9r0TJPdJabZrWaXJIpMyr7RKEng9oT6vCj1qftxPlu74C rNe714SS2dUkVJZ7tN4OMajjejerh2/CZHi0//PUYcxrhF7xKoFv1Xlm0a/sgbN8 9SNlPq8Tsmo1+d2+H8h2h9p0IK9UBpdJe7E7T6wNIF/ByApfn5wWx6IFyitVU0Ki 49HVara6Qygt2QitzJAJI8mjnsfOiqE0+k2oehpKJJlmws28fw8zL8bShG6yXs7j VbO9vg06qq0gy1s8b1sWNj4FYsQ7GjBVMhsiJlMfw== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 86c03cff (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Thu, 6 Feb 2020 18:42:36 +0000 (UTC) Date: Thu, 6 Feb 2020 19:43:40 +0100 From: "Jason A. Donenfeld" To: Eric Dumazet Cc: cai@lca.pw, Netdev , LKML , Marco Elver , Dmitry Vyukov Subject: Re: [PATCH v3] skbuff: fix a data race in skb_queue_len() Message-ID: <20200206184340.GA494766@zx2c4.com> References: <1580841629-7102-1-git-send-email-cai@lca.pw> <20200206163844.GA432041@zx2c4.com> <453212cf-8987-9f05-ceae-42a4fc3b0876@gmail.com> <495f79f5-ae27-478a-2a1d-6d3fba2d4334@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <495f79f5-ae27-478a-2a1d-6d3fba2d4334@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 06, 2020 at 10:22:02AM -0800, Eric Dumazet wrote: > On 2/6/20 10:12 AM, Jason A. Donenfeld wrote: > > On Thu, Feb 6, 2020 at 6:10 PM Eric Dumazet wrote: > >> Unfortunately we do not have ADD_ONCE() or something like that. > > > > I guess normally this is called "atomic_add", unless you're thinking > > instead about something like this, which generates the same > > inefficient code as WRITE_ONCE: > > > > #define ADD_ONCE(d, s) *(volatile typeof(d) *)&(d) += (s) > > > > Dmitry Vyukov had a nice suggestion few months back how to implement this. > > https://lkml.org/lkml/2019/10/5/6 That trick appears to work well in clang but not gcc: #define ADD_ONCE(d, i) ({ \        typeof(d) *__p = &(d); \        __atomic_store_n(__p, (i) + __atomic_load_n(__p, __ATOMIC_RELAXED), __ATOMIC_RELAXED); \ }) gcc 9.2 gives:  0:   8b 47 10                mov    0x10(%rdi),%eax   3:   83 e8 01                sub    $0x1,%eax   6:   89 47 10                mov    %eax,0x10(%rdi) clang 9.0.1 gives:    0:   81 47 10 ff ff ff ff    addl   $0xffffffff,0x10(%rdi) But actually, clang does equally as well with: #define ADD_ONCE(d, i) *(volatile typeof(d) *)&(d) += (i) And testing further back, it generates the same code with your original WRITE_ONCE. If clang's optimization here is technically correct, maybe we should go talk to the gcc people about catching this case?