From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 547673858C2C for ; Tue, 21 Dec 2021 15:40:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 547673858C2C Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-653-LHR9FDeCNw-wYXGoytyFxQ-1; Tue, 21 Dec 2021 10:40:12 -0500 X-MC-Unique: LHR9FDeCNw-wYXGoytyFxQ-1 Received: by mail-qv1-f71.google.com with SMTP id o14-20020ad45c8e000000b004112b52fc2cso6721523qvh.6 for ; Tue, 21 Dec 2021 07:40:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:from:subject; bh=r1c9tPjT+JFWMSh01o8l5VsD5J2ZLzVnON8Th29RZfk=; b=HJ0sqNL21Jy7FoAYVJVxJ8KBTEeDvJUGaQwpgTc2TOSOmZh1AvrdxgR7/WMMRw4Vib JMyu9t73AYgxaq270SxhadUFAasx3Mk7habAG4YKo0CzdJq5Xsh4GLf8V1rxuj3/jiIJ Z1pQ7tq+l4ZWSN8TGANI6KxgJieRJcGnGmHrnC75nSB3gHaaEHPUSxDzH/jmlUxV1fEE AzoJwfJETqIy9dQmwJgvFm1JfdXB7IkaroNfqB4omIviYRt6i2z97JadPZznl5c314rT Zsl0WLE7k4LW4ATNLxe81k03OEsK1MXO87fTYlNkv1WE6U3SNdvmzKIYsKq00fHASgzo FOxw== X-Gm-Message-State: AOAM532Pacmy++EtQ8JbUzWhnlHLancv2vScusdJtrMFk6QAhBT+eWqb cClEpKcZn/2SFYQw646uvNfpdJRWlXxdV72Ek9VtwFghYWS5bHBA5MGKeXQjYIoM3XAYsjKDhL6 Klzz5a6lFEf4lKs8uNoPmvqWZQXMlU7uUFltk/iSCtR5pMpxopfvkAlntfhu84B+PRg== X-Received: by 2002:a05:620a:454d:: with SMTP id u13mr2385091qkp.221.1640101211026; Tue, 21 Dec 2021 07:40:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMW/7fIYb+KAzPC7M/NfMGB77zGQhoW8w0/qG0ipu+q39UG9fFZUMcCHCP4FjWzuc+qI2wJA== X-Received: by 2002:a05:620a:454d:: with SMTP id u13mr2385064qkp.221.1640101210632; Tue, 21 Dec 2021 07:40:10 -0800 (PST) Received: from [192.168.0.129] ([81.0.47.68]) by smtp.gmail.com with ESMTPSA id e15sm49534qtq.83.2021.12.21.07.40.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Dec 2021 07:40:09 -0800 (PST) Message-ID: Date: Tue, 21 Dec 2021 16:40:07 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.3.0 To: gcc-help@gcc.gnu.org From: Adrian Moreno Subject: broken code only when optimized "-O2" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="------------jqzrgiJ2QlvITUCZ1m9zY0Zf" Content-Language: en-US X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Dec 2021 15:40:24 -0000 This is a multi-part message in MIME format. --------------jqzrgiJ2QlvITUCZ1m9zY0Zf Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hello, I need some help understanding what might be wrong with a piece of code from the openvswitch project. By ${subject} I'm not suggesting there's a problem in gcc, clang also shows the same behavior so it's likely our code is broken. I am kindly asking for help to understand/troubleshoot the problem. Summary: It seems that certain interaction between two main openvswitch data structures, when optimized ("-O2 -flto=auto") is broken. The two data structures are: hmap: https://github.com/openvswitch/ovs/blob/master/include/openvswitch/hmap.h list: https://github.com/openvswitch/ovs/blob/master/include/openvswitch/list.h I've reproduced the problem outside of openvswitch daemon using a short C program (attached) Code snippet: struct bond { struct hmap members; }; struct member { struct hmap_node hmap_node; int order; struct ovs_list elem; }; int main() { int ret = 0; struct member *member, *member1, *member2; struct bond *bond; struct ovs_list start = {0}; bond = malloc(sizeof *bond); memset(bond, 0, sizeof (struct bond)); hmap_init(&bond->members); member1 = malloc(sizeof *member1); member2 = malloc(sizeof *member2); memset(member1, 0, sizeof (struct member)); memset(member2, 0, sizeof (struct member)); member1->order = 3; member2->order = 2; hmap_insert(&bond->members, &member1->hmap_node, (uint32_t)(uintptr_t)member1); hmap_insert(&bond->members, &member2->hmap_node, (uint32_t)(uintptr_t)member2); ovs_list_init(&start); HMAP_FOR_EACH (member, hmap_node, &bond->members) { /* * Insert member in start (sorted) * */ struct member *pos; LIST_FOR_EACH (pos, elem, &start) { if (member->order > pos->order) { break; } } // TESTED: If I add this printf, the problem disappears //printf("Inserting member: %p\n", member); ovs_list_insert(&pos->elem, &member->elem); } /* I've inserted two members into the 'start' list. * first and last have to be either member1 or member2 * */ if ((first != member1 && first != member2) || (last != member1 && last != member2)) { printf("list is broken!\n"); } } What I know for now: * -fno-strict-aliasing does not fix it * Only happens with "-O2 -flto=auto" * If I define 'ovs_list *start' and change the code to use the pointer directly and not '&start' the problem disappears. It seems that the LIST_FOR_EACH macros prefer an lvalue rather than "&" but I don't get why. * I'm not able to reproduce without using hmap _and_ ovs_list. * If I add a compiler barrier (or a call to an external function) after the loop, the problem disappears (e.g printf), the problem disappears. I'd really appreciate any hint or idea to try to understand this problem. Thanks in advanced. -- Adrián Moreno --------------jqzrgiJ2QlvITUCZ1m9zY0Zf Content-Type: text/x-csrc; charset=UTF-8; name="example.c" Content-Disposition: attachment; filename="example.c" Content-Transfer-Encoding: base64 Ly8gUmVwcm9kdWNlciBvZiBodHRwczovL2J1Z3ppbGxhLnJlZGhhdC5jb20vc2hvd19idWcuY2dp P2lkPTIwMTQ5NDIKLy8gSG93IHRvIGJ1aWxkOgovLyAgIEJ1aWxkICYgaW5zdGFsbCBvcGVudnN3 aXRjaAovLyAgICAgIGdpdCBjbG9uZSBodHRwczovL2dpdGh1Yi5jb20vb3BlbnZzd2l0Y2gvb3Zz Ci8vICAgICAgY2Qgb3ZzCi8vICAgICAgLi9ib290LnNoICYmIC4vY29uZmlndXJlICYmIG1ha2Ug JiYgc3VkbyBtYWtlIGluc3RhbGwKLy8gICBCdWlsZCB0aGlzIHByb2dyYW0gYXM6Ci8vICAgICAg ZXhwb3J0IFBLR19DT05GSUdfUEFUSD0vdXNyL2xvY2FsL2xpYi9wa2djb25maWcKLy8gICAgICBn Y2MgYHBrZy1jb25maWcgLS1saWJzIGxpYm9wZW52c3dpdGNoIC0tc3RhdGljYCAkQ0ZMQUdTIC1s c3NsIC1sY3J5cHRvIC1sY2FwLW5nIC1vIGV4YW1wbGUgZXhhbXBsZS5jIC91c3IvbG9jYWwvbGli L2xpYm9wZW52c3dpdGNoLmEKLy8KLy8gICBUZXN0OgovLyAgIGV4cG9ydCBDRkxBR1M9Ii1PMCAt ZyIKLy8gICBUaGUgcHJvZ3JhbSBydW5zIHN1Y2Nlc3NmdWxseQovLwovLyAgIGV4cG9ydCBDRkxB R1M9Ii1PMiAtZmx0bz1hdXRvIC1nIgovLyAgIF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