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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable 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 51B93C433EF for ; Sun, 5 Sep 2021 22:40:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2A03560F4A for ; Sun, 5 Sep 2021 22:40:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233561AbhIEWlS (ORCPT ); Sun, 5 Sep 2021 18:41:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbhIEWlR (ORCPT ); Sun, 5 Sep 2021 18:41:17 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5842CC061757 for ; Sun, 5 Sep 2021 15:40:13 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id q3so6841992edt.5 for ; Sun, 05 Sep 2021 15:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BoHJHDDUQvDuxt5VzrZQ7aDSXlW3dvl3n4+BEi9wJZM=; b=hmwVZv0IehSDHdKr/5gEMvaA4SxOUAKSho+Jh+NgZ1YtJJgB87/nqAiVmkq66RdbcM m+ku3jJOCWZwOt2J5SoOy3JSSPHsPHq/HNv3OST+Lj4JPBXdJbI5bWnRVPkzOHJ/bgyL hDAAmJUDK7Yf/504RbkVBTMeqTlIYtS5Axr6ED6mLvfljtZRZRHq8umoerOAbOYfvHvo 7SK0Ll3OUpn2XZdNw35HWbMzVovHYKntMZKX4Am3V5qDrrBvCQrmM20NtEc7zgpd8+pX 6CCKzma4s/8LIWTRLRNTjmG0ylRPBY8z5WgYsZzuIZ0MSe31xdzN2Y6fxwEfSybJiBgY krOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BoHJHDDUQvDuxt5VzrZQ7aDSXlW3dvl3n4+BEi9wJZM=; b=sJHh3HamqVHwibwpyx0xhOwviuJag6VInkqgRCntRAMih9FKEARXBnqfyXQKbsLhNi HkJKlgP5DSSTM46YuhY8jGaDhTPkgISBANRbQ4IJ4FPWsldjObfGu2nArYnYlCeCUvzr tEVBFfFYJkeqFDTICrLj026XhuyB3CBT8/IoyNf2DH/U8YC/MPOEPnNaUtpwZX3TDrtD +ypqEN9QXS16ByS7S/9/tPAkZ0KJVdPNTO8lbf3HCgkav12HVp+oyEsvIop6/J+5vd9G duOgq1KbMrJzcVrhEaz94iPpi0TRLMBPEZd8DAsHmsL5kBFFuC53wc+pNhnWC7YsmN0/ K+pg== X-Gm-Message-State: AOAM5306R3svNCxfc/8Tei4Fwd3+ExZHZRqrG+/IRLvnu/XvAEH+b0W+ 21iGjLv75G7stdM48LxFyXVQiSA+SnfDVQUqz555ug== X-Google-Smtp-Source: ABdhPJxaX8EKh1++EIPdXEy9vz+JqwIHdkgVBY0k1neyuVfIClkmj6c97zfxXYhjxFQKVzDWdG2PNPtf63cT4sN/knE= X-Received: by 2002:aa7:de92:: with SMTP id j18mr10392290edv.141.1630881611812; Sun, 05 Sep 2021 15:40:11 -0700 (PDT) MIME-Version: 1.0 References: <20210826012626.1163705-1-isabellabdoamaral@usp.br> <20210826012626.1163705-3-isabellabdoamaral@usp.br> In-Reply-To: From: Isabella B do Amaral Date: Sun, 5 Sep 2021 19:40:00 -0300 Message-ID: Subject: Re: [PATCH 2/6] test_hash.c: move common definitions to top of file To: Marco Elver Cc: geert@linux-m68k.org, Enzo Ferreira , =?UTF-8?Q?Augusto_Dur=C3=A3es_Camargo?= , Brendan Higgins , Daniel Latypov , David Gow , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, ~lkcamp/patches@lists.sr.ht, rodrigosiqueiramelo@gmail.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, Marco, On Thu, Aug 26, 2021 at 11:36 AM Marco Elver wrote: > > On Thu, 26 Aug 2021 at 03:26, 'Isabella Basso' via KUnit Development > wrote: > > Keep function signatures minimal by making common definitions static. > > This does not change any behavior. > > This seems like an odd change; if I read it right it's changing the > out-param passed to test_int_hash() to simply be static globals. > > For one, it makes the code harder to read because now test_int_hash() > is no longer "pure" (no global side-effects ... modulo printfs), and > what was previously an out-param, is now a global. > > Unfortunately this is poor style and likely to lead to hard-to-debug > problems. One such problem is if suddenly you have multiple threads > involved. While this is just a test and unlikely to be a problem, I > would recommend not introducing global state carelessly. I see. My peers at LKCamp and I talked over the thread-safety problems for a while but we concluded it wasn't a big deal (precisely because this is a test). Though being stylistically poor seems a huge heads up, so I'm really thankful for your thorough explanation(, and review)! Noted! :) > An alternative common idiom, where a set of variables are always > passed around to other functions, is to introduce a struct and pass a > pointer to it along. > > > Signed-off-by: Isabella Basso > > --- > > lib/test_hash.c | 13 ++++++++----- > > 1 file changed, 8 insertions(+), 5 deletions(-) > > > > diff --git a/lib/test_hash.c b/lib/test_hash.c > > index d4b0cfdb0377..8bcc645a7294 100644 > > --- a/lib/test_hash.c > > +++ b/lib/test_hash.c > > @@ -23,6 +23,11 @@ > > #include > > #include > > > > +#define SIZE 256 /* Run time is cubic in SIZE */ > > + > > +static u32 string_or; /* stores or-ed string output */ > > +static u32 hash_or[2][33] = { { 0, } }; /* stores or-ed hash output */ > > These now use up memory for as long as this module is loaded, vs. > before where it would only use up stack space. (For a test that's not > a problem, but in non-test code it might.) > > > /* 32-bit XORSHIFT generator. Seed must not be zero. */ > > static u32 __init __attribute_const__ > > xorshift(u32 seed) > > @@ -66,7 +71,7 @@ fill_buf(char *buf, size_t len, u32 seed) > > * recompile and re-test the module without rebooting. > > */ > > static bool __init > > -test_int_hash(unsigned long long h64, u32 hash_or[2][33]) > > +test_int_hash(unsigned long long h64) > > { > > int k; > > u32 h0 = (u32)h64, h1, h2; > > @@ -123,17 +128,15 @@ test_int_hash(unsigned long long h64, u32 hash_or[2][33]) > > return true; > > } > > > > -#define SIZE 256 /* Run time is cubic in SIZE */ > > - > > static int __init > > test_hash_init(void) > > { > > char buf[SIZE+1]; > > - u32 string_or = 0, hash_or[2][33] = { { 0, } }; > > unsigned tests = 0; > > unsigned long long h64 = 0; > > int i, j; > > > > + string_or = 0; > > That's another problem with changes like this; now the compiler has no > chance to warn you in case the variable is not initialized correctly. > > Also, I don't see string_or used anywhere else. Why make it global? > If a later change would require that, it should say so in the commit > message. But my guess is you can avoid all that by bundling everything > up in a struct. > > > fill_buf(buf, SIZE, 1); > > > > /* Test every possible non-empty substring in the buffer. */ > > @@ -161,7 +164,7 @@ test_hash_init(void) > > > > string_or |= h0; > > h64 = h64 << 32 | h0; /* For use with hash_64 */ > > - if (!test_int_hash(h64, hash_or)) > > + if (!test_int_hash(h64)) > > return -EINVAL; > > tests++; > > } /* i */ > > -- > > 2.33.0 Thanks, -- Isabella Basso