Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp200573rwj; Sat, 17 Dec 2022 06:31:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXuMAUSHFttZzN/fEYhHqneNd63FMxRWCrP+Zv8LPNlTrfMj6ZJnwAwPOKx5hB6lXuUsaMNR X-Received: by 2002:a17:903:2154:b0:189:5a49:36c3 with SMTP id s20-20020a170903215400b001895a4936c3mr2342161ple.0.1671287496269; Sat, 17 Dec 2022 06:31:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671287496; cv=none; d=google.com; s=arc-20160816; b=liKiPQULYj+1DtjT40C2MDvkln/qgLuQdmEmz55JlFAUoeYLK7E758K/IYpz++d2xJ DCmVQcqmU+Uvr7bqCMq2ZA8PANhS9B9p6NDuuEP6doc+CmpkiImtzdDZZW833KrlYv71 Wpq8YeuuiBMqhlgcgRgXt9Gxk8J/QEdAzRKEVgNr8Fh5hDjje4Nu8MiX2MN6u3Gn90qT RplnNv+7nmDGOu+KolM06+dg52VsW2hj4Tm0SppXkGP1mD/qJtpwpZTFUM6+erKoavz7 BnXp08vbqYfrISsHyU5uLHvvPqGbz1gYP+x4uMcCRsIqvdQdZqexPW3S6NrzEr0W8UxS WYzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=mT2HK5xPf8mLBGBOndWuGbgQvCSHXqFmoPiNWyyq8Nw=; b=WtEi0TTifWZ40tmEN/rlHz4o83hvazsRQDglNj/G2dCtAGkASLyqRIH8aotiQDXXg5 QQN+cJ01YFQheV8nNXFS3AOo8s6xL7yDdnyAFzx9kTDniLwQa3FkivDjatB2WaNMwcre KO21+znXJwH8SqQI7hQDsbwLrREkxLMsjQxPmyv8WYma3rfB7BVIhCaBghYW1scJqyZ2 vVtrOftiAiXUn9ODWyh42XZeYjwbExrnUuqNPETAIXY7KsBW56Qs1uSreaRNxe/FSeDG lXeknOelrCbqBqoY2FIpiXj47bYoF2aisEYYA4w9+orRT2mnT++kXm1oE9evUwjMUu5F cf/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r10-20020a170902e3ca00b0017f75654a27si5133752ple.510.2022.12.17.06.31.25; Sat, 17 Dec 2022 06:31:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230380AbiLQNhb convert rfc822-to-8bit (ORCPT + 70 others); Sat, 17 Dec 2022 08:37:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230110AbiLQNh1 (ORCPT ); Sat, 17 Dec 2022 08:37:27 -0500 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2C40120BE; Sat, 17 Dec 2022 05:37:24 -0800 (PST) Received: by mail-qt1-f170.google.com with SMTP id ay32so4822595qtb.11; Sat, 17 Dec 2022 05:37:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GEQhxkMBO6XWJFe+D6nzXPLObph3tXaHaWCQZey9h9Y=; b=KF8wQm5mje6ABdqvpnOQe0OyWYnoaDIXhpc2Zqd2a6JCIAlNHuO9Tc/utnMD6QYqFO 52InTIIBieZtbCWDoCxpzMvGGtLwA2ujetcPuNcxWoaBjIh4joJZkemiAy0OKUBDaWsX jFWxvAPAjoWguGy1HPFmgJlrBDkYnVkMFdLRLZAmYoNVmBh4pjtkBaAA9IkFNMezXdxV FT0GHwiyFva2DTTJjUsvuj1uyGlhSftKXBOAvBvbulTGIEIf5koK6GRmkHcX5UBr9kpG DvLu6zZCSn8vZWBYHO6UsOeIbGCxJB+Mmp8uLK+U4ajldIsONr/rf8M2eiJWT0NJ38LU dwdw== X-Gm-Message-State: AFqh2krulqa+dJygYldrNBFM1QPvFOjtNtXMlGqZRrN2Q9NQ++EdZb1a 3APfQvQcvB6Jxi7sQMYxgyZ6eVmJdJPw+w== X-Received: by 2002:a05:622a:a05:b0:3a7:e8ab:2ae3 with SMTP id bv5-20020a05622a0a0500b003a7e8ab2ae3mr4857608qtb.25.1671284243072; Sat, 17 Dec 2022 05:37:23 -0800 (PST) Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com. [209.85.219.170]) by smtp.gmail.com with ESMTPSA id v18-20020a05620a441200b006fed2788751sm3592727qkp.76.2022.12.17.05.37.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Dec 2022 05:37:22 -0800 (PST) Received: by mail-yb1-f170.google.com with SMTP id 203so617399yby.10; Sat, 17 Dec 2022 05:37:22 -0800 (PST) X-Received: by 2002:a25:7104:0:b0:702:90b4:2e24 with SMTP id m4-20020a257104000000b0070290b42e24mr14490470ybc.365.1671284241922; Sat, 17 Dec 2022 05:37:21 -0800 (PST) MIME-Version: 1.0 References: <20221115083349.1662-1-thunder.leizhen@huawei.com> <4aaede14-8bd3-6071-f17b-7efcb5f0de42@huawei.com> <66ec4021-b633-09ba-73ee-b24cdb3fa25a@huawei.com> <06345dca-0afb-00a5-c9e9-5ba830d8ad05@huawei.com> <52450ec1da164d6d87587063c3b3d3d2@AcuMS.aculab.com> <592dce7a0de24c62bd31c29f86ce6c1b@AcuMS.aculab.com> <87pmcjidfe.fsf@igel.home> <1ba1fadb39994a4d91edabdfd9d69fa6@AcuMS.aculab.com> <87len7ibtt.fsf@igel.home> <87fsdfib07.fsf@igel.home> <819801284eb745d9a4189759bad297f5@AcuMS.aculab.com> <20221216115314.6120beb7@gandalf.local.home> <20221216121947.7d03b651@gandalf.local.home> <20221216123805.6eba002c@gandalf.local.home> <83701d57c0064c709669c03cecc38356@AcuMS.aculab.com> <066d4a61-3df9-b9c8-81a2-40dfcd3c73ef@huawei.com> In-Reply-To: <066d4a61-3df9-b9c8-81a2-40dfcd3c73ef@huawei.com> From: Geert Uytterhoeven Date: Sat, 17 Dec 2022 14:37:09 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v9] kallsyms: Add self-test facility To: "Leizhen (ThunderTown)" Cc: David Laight , Steven Rostedt , Andreas Schwab , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , "live-patching@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Masahiro Yamada , Alexei Starovoitov , Jiri Olsa , Kees Cook , Andrew Morton , Luis Chamberlain , "linux-modules@vger.kernel.org" , Ingo Molnar , linux-m68k , "Jason A. Donenfeld" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Zhen, On Sat, Dec 17, 2022 at 8:32 AM Leizhen (ThunderTown) wrote: > On 2022/12/17 3:27, David Laight wrote: > > From: Steven Rostedt > >> Sent: 16 December 2022 17:38 > >> > >> On Fri, 16 Dec 2022 12:19:47 -0500 > >> Steven Rostedt wrote: > >> > >>> I assumed that "memory" was for memory unrelated to the input constraints. > >> > >> Well, it looks like you do need a "memory" barrier. > >> > >> https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html > >> > >> "memory" > >> > >> The "memory" clobber tells the compiler that the assembly code > >> performs memory reads or writes to items other than those listed in > >> the input and output operands (for example, accessing the memory > >> pointed to by one of the input parameters). To ensure memory contains > >> correct values, GCC may need to flush specific register values to > >> memory before executing the asm. Further, the compiler does not > >> assume that any values read from memory before an asm remain > >> unchanged after that asm; it reloads them as needed. Using the > >> "memory" clobber effectively forms a read/write memory barrier for > >> the compiler. > >> > >> As the "(for example, accessing the memory pointed to by one of the input > >> parameters)" is exactly this case. > > > > Without the memory clobber code like: > > int f(const char *s) > > { > > char c[4] = "abc"; > > return strcmp(s, c); > > } > > is very like to get optimised so that c[] is never written. > > > > However, in this case, the strings have all existed for ages. > > So that won't be the problem. > > > > It might be obvious what is wrong from the asm output. > > Although the binary-chop lookup is suspect I'd also check > > that the sorted index is plausible - just tracing the first > > 20 entries might be enough. > > No point peering at the search code if the setup is wrong. > > 6.47.2.1 Volatile > GCC’s optimizers sometimes discard asm statements if they determine there is no need for > the output variables. Also, the optimizers may move code out of loops if they believe that > the code will always return the same result (i.e. none of its input values change between > calls). Using the volatile qualifier disables these optimizations. > > So it's quite possible (I didn't disassemble vmlinux, because I didn't learn m68k): You don't have to disassemble, "make kernel/kallsyms.s" creates annotated assembler. > //binary search > while (low <= high) { > ... > ret = compare_symbol_name(name, namebuf); ----> (1) > if (!ret) > break; > } > > low = mid; > while (low) { > ... > if (compare_symbol_name(name, namebuf)) ----> (2) > break; > low--; > } > > The pointer 'name' and 'namebuf' of (1) and (2) are the same, > so the 'if' statement of (2) maybe omitted by compiler. And that is exactly what is happening: there are 3 calls to strcmp() with the exact same parameters, and gcc omits two of them, because it assumes it can predict the outcome, as the parameters haven't changed. Now, why have we never noticed this before? I guess because it is very uncommon for a function calling strcmp() multiple times with the exact same pointer parameters. Common users change the pointers before every call, instead of keeping the pointer, but changing the buffers' contents the pointers point to. > By the way, I tried no volatile but with > + : : "memory"); > It also works well. Indeed, gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04) generates the same code for either adding the volatile or the memory clobber. Note that strcmp() is the only function in arch/m68k/include/asm/string.h using inline asm without the volatile keyword. I guess we would see similar issues with strnlen() (which also doesn't modify memory) when dropping the volatile. Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds