Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp119610pxf; Tue, 6 Apr 2021 16:52:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOdQDfaW7TqKwVrKEroxUrs2gBo54hEMP1Ws/3k86lsSZWeQr7xxnMd1ozdmFUrA7/Aprg X-Received: by 2002:a6b:4e01:: with SMTP id c1mr306056iob.61.1617753127323; Tue, 06 Apr 2021 16:52:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617753127; cv=none; d=google.com; s=arc-20160816; b=qB8oJ918lpN2jYwPHND65jd77Ko66x5XZVKxP9Gf2lUtQeF67RKvNNnIBXVWo7hP7Y rJtcuYwxY1BUccoTIkvvCIwqcNVLoYXeWrRZxOe6qsz5FeMJ19D/TlpPT6tNUG7j+//u ai0QpHl+rAr3XgT14xSaRiKbVzgdCLO9F46U8LGkgERNaX0osz+HIFgwOGRngFBSgN/w A/kolN1k/nngC3YRnAHfELq5SeojIEctNNRy8DJv2vDya159VF6e2R22aWqtRDcEdaiP NaBCeIkxsCNipJqTN2hOevL+e7TTcmDJDdKTmM31UXEgno82MTlRkHsVtJNYwmBXn/HF Ay3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=zZRy7Ahm1dIY9quUbT5nraE60pIYYK50PUe8OP42REc=; b=tMqkZcQdVb4bbny8rBAMUjriyJ9rQQOz/ytYbg/F+1xlHloP9gGF/25nllhocoi280 NXpzubg88O2ejkHhvD1gMGlLkCWiyNIQ4kibiE6/hwR2NKgg7blHUgkMKsfns38Iv2bj Ti6ooFI3vgJ8EK/s4xrPnh9aQoPCtS7XSgcL2uTv9gTmjJJkTeAZ1yPMgvjOvW2m3Qt4 uB3CuIIK5wj/LBWtn1KgBeQpcXxvqb3Xu9QuB3bY/upyoWz4o38D8KYX76xo7Y5KK5kS zDGjU6p9xiilliUHQpvGTRCZ07DK6ZrQ0dXHT0y4elFJSQa7RFWpAOxSib8kO0tolGxU vGFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d2si19540817ios.97.2021.04.06.16.51.55; Tue, 06 Apr 2021 16:52:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245244AbhDFK6J (ORCPT + 99 others); Tue, 6 Apr 2021 06:58:09 -0400 Received: from mx2.suse.de ([195.135.220.15]:39320 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245237AbhDFK6E (ORCPT ); Tue, 6 Apr 2021 06:58:04 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BEFFAB14C; Tue, 6 Apr 2021 10:57:55 +0000 (UTC) To: Marco Elver , Daniel Latypov Cc: glittao@gmail.com, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Linux Kernel Mailing List , Linux Memory Management List , KUnit Development , Brendan Higgins References: <20210331085156.5028-1-glittao@gmail.com> From: Vlastimil Babka Subject: Re: [PATCH v3 1/2] kunit: add a KUnit test for SLUB debugging functionality Message-ID: <11886d4f-8826-0cd6-b5fd-defc65470ed5@suse.cz> Date: Tue, 6 Apr 2021 12:57:54 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/1/21 11:24 PM, Marco Elver wrote: > On Thu, 1 Apr 2021 at 21:04, Daniel Latypov wrote: >> > } >> > #else >> > static inline bool slab_add_kunit_errors(void) { return false; } >> > #endif >> > >> > And anywhere you want to increase the error count, you'd call >> > slab_add_kunit_errors(). >> > >> > Another benefit of this approach is that if KUnit is disabled, there is >> > zero overhead and no additional code generated (vs. the current >> > approach). >> >> The resource approach looks really good, but... >> You'd be picking up a dependency on >> https://lore.kernel.org/linux-kselftest/20210311152314.3814916-2-dlatypov@google.com/ >> current->kunit_test will always be NULL unless CONFIG_KASAN=y && >> CONFIG_KUNIT=y at the moment. >> My patch drops the CONFIG_KASAN requirement and opens it up to all tests. > > Oh, that's a shame, but hopefully it'll be in -next soon. > >> At the moment, it's just waiting another look over from Brendan or David. >> Any ETA on that, folks? :) >> >> So if you don't want to get blocked on that for now, I think it's fine to add: >> #ifdef CONFIG_SLUB_KUNIT_TEST >> int errors; >> #endif > > Until kunit fixes setting current->kunit_test, a cleaner workaround > that would allow to do the patch with kunit_resource, is to just have > an .init/.exit function that sets it ("current->kunit_test = test;"). > And then perhaps add a note ("FIXME: ...") to remove it once the above > patch has landed. > > At least that way we get the least intrusive change for mm/slub.c, and > the test is the only thing that needs a 2-line patch to clean up > later. So when testing internally Oliver's new version with your suggestions (thanks again for those), I got lockdep splats because slab_add_kunit_errors is called also from irq disabled contexts, and kunit_find_named_resource will call spin_lock(&test->lock) that's not irq safe. Can we make the lock irq safe? I tried the change below and it makde the problem go away. If you agree, the question is how to proceed - make it part of Oliver's patch series and let Andrew pick it all with eventually kunit team's acks on this patch, or whatnot. ----8<---- commit ab28505477892e9824c57ac338c88aec2ec0abce Author: Vlastimil Babka Date: Tue Apr 6 12:28:07 2021 +0200 kunit: make test->lock irq safe diff --git a/include/kunit/test.h b/include/kunit/test.h index 49601c4b98b8..524d4789af22 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -515,8 +515,9 @@ kunit_find_resource(struct kunit *test, void *match_data) { struct kunit_resource *res, *found = NULL; + unsigned long flags; - spin_lock(&test->lock); + spin_lock_irqsave(&test->lock, flags); list_for_each_entry_reverse(res, &test->resources, node) { if (match(test, res, (void *)match_data)) { @@ -526,7 +527,7 @@ kunit_find_resource(struct kunit *test, } } - spin_unlock(&test->lock); + spin_unlock_irqrestore(&test->lock, flags); return found; } diff --git a/lib/kunit/test.c b/lib/kunit/test.c index ec9494e914ef..2c62eeb45b82 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -442,6 +442,7 @@ int kunit_add_resource(struct kunit *test, void *data) { int ret = 0; + unsigned long flags; res->free = free; kref_init(&res->refcount); @@ -454,10 +455,10 @@ int kunit_add_resource(struct kunit *test, res->data = data; } - spin_lock(&test->lock); + spin_lock_irqsave(&test->lock, flags); list_add_tail(&res->node, &test->resources); /* refcount for list is established by kref_init() */ - spin_unlock(&test->lock); + spin_unlock_irqrestore(&test->lock, flags); return ret; } @@ -515,9 +516,11 @@ EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource); void kunit_remove_resource(struct kunit *test, struct kunit_resource *res) { - spin_lock(&test->lock); + unsigned long flags; + + spin_lock_irqsave(&test->lock, flags); list_del(&res->node); - spin_unlock(&test->lock); + spin_unlock_irqrestore(&test->lock, flags); kunit_put_resource(res); } EXPORT_SYMBOL_GPL(kunit_remove_resource); @@ -597,6 +600,7 @@ EXPORT_SYMBOL_GPL(kunit_kfree); void kunit_cleanup(struct kunit *test) { struct kunit_resource *res; + unsigned long flags; /* * test->resources is a stack - each allocation must be freed in the @@ -608,9 +612,9 @@ void kunit_cleanup(struct kunit *test) * protect against the current node being deleted, not the next. */ while (true) { - spin_lock(&test->lock); + spin_lock_irqsave(&test->lock, flags); if (list_empty(&test->resources)) { - spin_unlock(&test->lock); + spin_unlock_irqrestore(&test->lock, flags); break; } res = list_last_entry(&test->resources, @@ -621,7 +625,7 @@ void kunit_cleanup(struct kunit *test) * resource, and this can't happen if the test->lock * is held. */ - spin_unlock(&test->lock); + spin_unlock_irqrestore(&test->lock, flags); kunit_remove_resource(test, res); } #if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))