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=-18.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=ham 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 A7A64C4361B for ; Thu, 17 Dec 2020 05:58:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0B21623741 for ; Thu, 17 Dec 2020 05:58:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0B21623741 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0FC596B006E; Thu, 17 Dec 2020 00:58:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AD1B6B0070; Thu, 17 Dec 2020 00:58:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F05FE6B0071; Thu, 17 Dec 2020 00:58:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0043.hostedemail.com [216.40.44.43]) by kanga.kvack.org (Postfix) with ESMTP id DA2BD6B006E for ; Thu, 17 Dec 2020 00:58:24 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 811C4181AC212 for ; Thu, 17 Dec 2020 05:58:24 +0000 (UTC) X-FDA: 77601719328.29.smash01_230e54b27432 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 5660C180868EC for ; Thu, 17 Dec 2020 05:58:24 +0000 (UTC) X-HE-Tag: smash01_230e54b27432 X-Filterd-Recvd-Size: 13362 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 05:58:23 +0000 (UTC) Received: by mail-ot1-f41.google.com with SMTP id b24so8700431otj.0 for ; Wed, 16 Dec 2020 21:58:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TeXTJPc29FroLzXFFtb7UT4d0aE1W3v3wTsahSpGSlU=; b=N3HlrJ5DPENsWN1NULvrZ3VfWOpkvWWpPWJ0ri8JtAm2+SolST8wH7hQoftY4eWrdh F4Z1v0FVMzflKV4kZ7xwLWOPlz4OhQuXBKm0DGfd9RWtF0T4Mi0/Ct+QbIf+fljYNzFJ Lcwp3k56SDdfsSyPMEDBth8ywYjohCaOcS4PY7sbEsg5qChit6b7g8S574AiicgXLVsP fJQukSN0jJDSBm3EX1u4fIHvL5npFW90MqovAzGrj/sJTkjtxfmnoyqi1mK5FhsOfQsl jYr7iWEtXwEdW23mB+mnfMiN3TYfZFP50445iv8oPCdffpHWKpR28vlw9HFJQ3fd0sip DAWA== 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=TeXTJPc29FroLzXFFtb7UT4d0aE1W3v3wTsahSpGSlU=; b=RUc1AM0YVAgZ/S3/McplY4mwYgLNtuCwNWhOiqqnFy9Z8lLnNOSpw5gN1X8asw6Erm r0ChK4Q8D12tl5GamTFiZh9IpTOiGFfvymuQJlJ3i8SFxJNMKTr9XlqlU1b3oteC9DUY qs3FbPx1ZRpBl+1/pPrffrD9E22qptpu83W4wa49QKFrer3JKwIHPuBffUshGMFgSXoe 69+pE1nvd0mCMh8eXpKgnXGO+FGiL6k2EGLz7m2VNWnD2ZcC4edYbUmgOdhmzcrlthcl 2g/HbZfkU856qZGO7zsUd8Dkr7cR7G6unsjoQNT+zSXpydo2HZpS06dfdl85tpD5b0uE XUrw== X-Gm-Message-State: AOAM531RH0JGVr/OLhEwiPyEF8bJ6+M6yoBlhkPJyijGfiBQvdsMhd01 oskRIc+4ZBEUJ9HmxNFVMfQ3npt7zFqueamSVWywwA== X-Google-Smtp-Source: ABdhPJyQkHMIJkn9oUgok2LRgukYqhiKQw6mcoyNCCQDtRd0sDfvaDjBziqfVaAM+UXIYUT03htDadInhLsnhfe1TiY= X-Received: by 2002:a05:6830:2413:: with SMTP id j19mr19817719ots.251.1608184702786; Wed, 16 Dec 2020 21:58:22 -0800 (PST) MIME-Version: 1.0 References: <20201217013925.GQ67148@shao2-debian> In-Reply-To: <20201217013925.GQ67148@shao2-debian> From: Marco Elver Date: Thu, 17 Dec 2020 06:58:11 +0100 Message-ID: Subject: Re: [linux-next:master 12593/13311] mm/kfence/core.c:250:13: sparse: sparse: context imbalance in 'kfence_guarded_alloc' - wrong count at exit To: kernel test robot Cc: Alexander Potapenko , kbuild-all@lists.01.org, Linux Memory Management List , Dmitry Vyukov , Jann Horn , Andrew Morton Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, 17 Dec 2020 at 02:40, kernel test robot wrote: > > tree: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master > head: 9317f948b0b188b8d2fded75957e6d42c460df1b > commit: e21d96503adda2ccb571d577ad32929383c710ea [12593/13311] x86, kfence: enable KFENCE for x86 > config: x86_64-randconfig-s022-20201216 (attached as .config) > compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 > reproduce: > # apt-get install sparse > # sparse version: v0.6.3-184-g1b896707-dirty > # https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=e21d96503adda2ccb571d577ad32929383c710ea > git remote add linux-next https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git > git fetch --no-tags linux-next master > git checkout e21d96503adda2ccb571d577ad32929383c710ea > # save the attached .config to linux build tree > make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64 > > If you fix the issue, kindly add following tag as appropriate > Reported-by: kernel test robot > > > "sparse warnings: (new ones prefixed by >>)" > >> mm/kfence/core.c:250:13: sparse: sparse: context imbalance in 'kfence_guarded_alloc' - wrong count at exit > >> mm/kfence/core.c:825:9: sparse: sparse: context imbalance in 'kfence_handle_page_fault' - different lock contexts for basic block This is a false positive, and sparse can't seem to follow locking done here. This code has been tested extensively with lockdep. > vim +/kfence_guarded_alloc +250 mm/kfence/core.c > > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 249 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 @250 static void *kfence_guarded_alloc(struct kmem_cache *cache, size_t size, gfp_t gfp) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 251 { > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 252 struct kfence_metadata *meta = NULL; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 253 unsigned long flags; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 254 struct page *page; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 255 void *addr; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 256 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 257 /* Try to obtain a free object. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 258 raw_spin_lock_irqsave(&kfence_freelist_lock, flags); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 259 if (!list_empty(&kfence_freelist)) { > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 260 meta = list_entry(kfence_freelist.next, struct kfence_metadata, list); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 261 list_del_init(&meta->list); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 262 } > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 263 raw_spin_unlock_irqrestore(&kfence_freelist_lock, flags); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 264 if (!meta) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 265 return NULL; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 266 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 267 if (unlikely(!raw_spin_trylock_irqsave(&meta->lock, flags))) { > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 268 /* > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 269 * This is extremely unlikely -- we are reporting on a > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 270 * use-after-free, which locked meta->lock, and the reporting > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 271 * code via printk calls kmalloc() which ends up in > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 272 * kfence_alloc() and tries to grab the same object that we're > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 273 * reporting on. While it has never been observed, lockdep does > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 274 * report that there is a possibility of deadlock. Fix it by > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 275 * using trylock and bailing out gracefully. > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 276 */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 277 raw_spin_lock_irqsave(&kfence_freelist_lock, flags); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 278 /* Put the object back on the freelist. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 279 list_add_tail(&meta->list, &kfence_freelist); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 280 raw_spin_unlock_irqrestore(&kfence_freelist_lock, flags); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 281 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 282 return NULL; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 283 } > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 284 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 285 meta->addr = metadata_to_pageaddr(meta); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 286 /* Unprotect if we're reusing this page. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 287 if (meta->state == KFENCE_OBJECT_FREED) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 288 kfence_unprotect(meta->addr); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 289 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 290 /* > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 291 * Note: for allocations made before RNG initialization, will always > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 292 * return zero. We still benefit from enabling KFENCE as early as > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 293 * possible, even when the RNG is not yet available, as this will allow > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 294 * KFENCE to detect bugs due to earlier allocations. The only downside > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 295 * is that the out-of-bounds accesses detected are deterministic for > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 296 * such allocations. > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 297 */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 298 if (prandom_u32_max(2)) { > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 299 /* Allocate on the "right" side, re-calculate address. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 300 meta->addr += PAGE_SIZE - size; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 301 meta->addr = ALIGN_DOWN(meta->addr, cache->align); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 302 } > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 303 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 304 addr = (void *)meta->addr; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 305 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 306 /* Update remaining metadata. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 307 metadata_update_state(meta, KFENCE_OBJECT_ALLOCATED); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 308 /* Pairs with READ_ONCE() in kfence_shutdown_cache(). */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 309 WRITE_ONCE(meta->cache, cache); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 310 meta->size = size; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 311 for_each_canary(meta, set_canary_byte); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 312 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 313 /* Set required struct page fields. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 314 page = virt_to_page(meta->addr); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 315 page->slab_cache = cache; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 316 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 317 raw_spin_unlock_irqrestore(&meta->lock, flags); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 318 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 319 /* Memory initialization. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 320 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 321 /* > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 322 * We check slab_want_init_on_alloc() ourselves, rather than letting > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 323 * SL*B do the initialization, as otherwise we might overwrite KFENCE's > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 324 * redzone. > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 325 */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 326 if (unlikely(slab_want_init_on_alloc(gfp, cache))) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 327 memzero_explicit(addr, size); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 328 if (cache->ctor) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 329 cache->ctor(addr); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 330 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 331 if (CONFIG_KFENCE_STRESS_TEST_FAULTS && !prandom_u32_max(CONFIG_KFENCE_STRESS_TEST_FAULTS)) > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 332 kfence_protect(meta->addr); /* Random "faults" by protecting the object. */ > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 333 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 334 atomic_long_inc(&counters[KFENCE_COUNTER_ALLOCATED]); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 335 atomic_long_inc(&counters[KFENCE_COUNTER_ALLOCS]); > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 336 > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 337 return addr; > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 338 } > 3b295ea3a66b734 Alexander Potapenko 2020-12-10 339 > > :::::: The code at line 250 was first introduced by commit > :::::: 3b295ea3a66b734a0cd23ae66bae0747a078725a mm: add Kernel Electric-Fence infrastructure > > :::::: TO: Alexander Potapenko > :::::: CC: Stephen Rothwell > > --- > 0-DAY CI Kernel Test Service, Intel Corporation > https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org > _______________________________________________ > kbuild mailing list -- kbuild@lists.01.org > To unsubscribe send an email to kbuild-leave@lists.01.org