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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24460C3DA64 for ; Fri, 26 Jul 2024 00:44:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A4C576B009F; Thu, 25 Jul 2024 20:44:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D5616B00A0; Thu, 25 Jul 2024 20:44:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84ED56B00A1; Thu, 25 Jul 2024 20:44:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5DC566B009F for ; Thu, 25 Jul 2024 20:44:08 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1C432C14D2 for ; Fri, 26 Jul 2024 00:44:08 +0000 (UTC) X-FDA: 82380056976.07.B18F64B Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by imf02.hostedemail.com (Postfix) with ESMTP id 392F180007 for ; Fri, 26 Jul 2024 00:44:05 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SbsODR9s; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721954609; a=rsa-sha256; cv=none; b=Y3WGQ8oI2cL/MydMY4zJZJS4IxKnJ+Kr56oYKE9hLCj0wvv5BR8Bvb1YuYexR1GkXfwrO5 orN+A+/dX9fJbGL6ZSsZeiBOssptqfogSC9CkrRi9f/j4Y+VmdLNmVjiJXM/9Wh6cv95eT CbU8VS0qNEXfqBVPw+iihGVH3Mj6EFw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SbsODR9s; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721954609; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qWLLE86qSUx/Olt4aDlaYWvMRcyjVLxgebJsy4G4sv8=; b=QtuP0ZPEtsmLUGjFuhkqnrxP2y99KzCOWWVVpdsBR6udnP/nsH03PHxM26QNx0FRSjtXp2 oPlR67iUC4fr3Rw5dZ9PNpatk5U9/KExktNDtKYrgFL0nWEMS1TzDCIYPYUNNLAj5d86pe M/FX9nqBvHbnp6DqyDghg2nn7p2cDWM= Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4257d5fc9b7so15009775e9.2 for ; Thu, 25 Jul 2024 17:44:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721954645; x=1722559445; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=qWLLE86qSUx/Olt4aDlaYWvMRcyjVLxgebJsy4G4sv8=; b=SbsODR9sq1h3Ps6xuBvRqwnzxISwZJND0PbzfGDY22J7X+hNEynTQlvMJ00lk/XWPe eGd4kfORJ4kqe1mgF4ENXHbqJHShIEKXLPx9aZycDJssKs3biy0ETCSCg9C9laEgwZYO Czczva3j0haApM8NU/R5WFbmWzbItlhqmLrNgrqwp+SUPW+c4Dakfiwii6JDyjlmEAfN kwk1rBRboADy3WXyDCczHWoZBoESJK7DOhBAXGMXbaJ427zDv1H5snVh/a9wrwJGvbJI QNAcMstPZycAhprV6e3teF+R6wInsw7c8tMRxD8r755rXGK7FKyK+9Lmf8bVd3w/5G0s qkXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721954645; x=1722559445; 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=qWLLE86qSUx/Olt4aDlaYWvMRcyjVLxgebJsy4G4sv8=; b=YtcZn4llBhpZPgqyeXHKBK1IwPVnLZsZUlOmfFhB6ogxiqD0tE9TjMU8iB1+sSmNDh KUV1cykYRfzvEHUilrRbI/lOzMCSCIvfdLcNQBa9oTDGzuLHPigwsdU05QM+bZ1umMwX GqLbWxcsXjVs1NCKT94pOd2P5QAStFhxPTNORbj/Qdmc+QojlQRBXkyq4dxzZNYyX/MT 4lT7hTOblTXE4IgxHUft2UFDdDNpI6To3qQhb/ZTS3UJdubdq+V6ClocZaf7QPENRLHU g+4Rr9GH2sWDMrHs3Or6LQXpbr4Y47VMFbJ1cifLR4TbhZ1j5LpaNULf0G6pDsqW0Fhy pJeg== X-Forwarded-Encrypted: i=1; AJvYcCXT0D1KRaI5zL0HJSKD96vGRAsH1GmLCl7tcBXffy4PGkJxxEmd6ZkqL9S/hKrWGTuB+cAWWpqL7Xe6N/P1FqN7euA= X-Gm-Message-State: AOJu0YzH/FYPczv6/xYbpXzL1tpOczRofGyWwVMJZNR2Aav3rPj34YFW N+3V8/Zj+MZLXE78BVa/30yAaMSPU81ImoYIEd1tlGg+IFO6H2nDYq/CGI1eG5I5aUJOpmeAvUq AhgITmMNAuAqYOWh0W/6lL0kD7+I= X-Google-Smtp-Source: AGHT+IHcafIQ5teDASs1UNLISNfxx6zvEfFKVdysTmE0/SFNJKndkMStbPu7X3ImBd6XnDc0QWAYU5xyuU+ZECnSCfc= X-Received: by 2002:adf:f18b:0:b0:367:f059:4c55 with SMTP id ffacd0b85a97d-36b363d54demr3001273f8f.26.1721954644724; Thu, 25 Jul 2024 17:44:04 -0700 (PDT) MIME-Version: 1.0 References: <20240725-kasan-tsbrcu-v3-0-51c92f8f1101@google.com> <20240725-kasan-tsbrcu-v3-2-51c92f8f1101@google.com> In-Reply-To: <20240725-kasan-tsbrcu-v3-2-51c92f8f1101@google.com> From: Andrey Konovalov Date: Fri, 26 Jul 2024 02:43:53 +0200 Message-ID: Subject: Re: [PATCH v3 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG To: Jann Horn Cc: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Marco Elver , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 392F180007 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tpj6q3dscezkyymjh46zde36rbpekx1g X-HE-Tag: 1721954645-924281 X-HE-Meta: U2FsdGVkX19kvqk2qe00FpIr2DAxjoQn7sfOxK4c2KNVdNJeWNCccIwJdjF8aV/YSyg9Gg/0k4j1dRYjb6LA1q6uU9AZFxk4VIzRzBz9GSmd5ZnlTyxj5IhtOZ6HryFTcTzHLaZOBJUcJYRDKOBrcKX+Ff370K8a37BXl7F/8j+nuHOvKoJtcCMlFIsOdxG6LC9MEKo0n9GwlsMlkRZzCGs4ZhArnlK1ii9kujHJDdllOiCIgQIDSMeUPDDabxZRq/Pex9xxFFfK5fdUHiTeHsHJ8PNOk5mzQoPRgSImhxr+ZwaCO7D4F9z85xBk8YrgzrxSL7IikEVuk4S/hYHcbiA5m5RF5mIH3X5dkMDEf01z5r/daBZKnia1YdcV/hG+6jyxQX/zX0zPD5xS04d5WWGxRetEMLFAjVokazMA4x7Sh6xkUlhHIdT0A1UiYGavbcLBtyiM1LJqsQyDiiep2NldVnafGftDc5UISODBeMKkxNKoVO5mJt+eAu/Cl7BHqzpTfzxTR8hXAQa0LrVKO1twu4rJMJJScPa8wL7tfL/Z5s39OU0l6DK5Di1IUyFQ6cbsKOvgsihkwgI/2x2ZUu9VIdPGkcvDvVeHJrx65akN7LfN6ODpgk3Nci31orealvv+FkJaTjZrZbEfKtPHm47+K3xOEMj3AlkyRnt4ZfA5WNJ6ggq+925TXfPjHNIlM0HeOi2IxeFaMonfK6gxmbDYHovXB2TAza2Y9Czv8k83afNUDdV4M+6xvKllFFhp4TPMN6urT3LnejJJLybVCKT2YBq+hDmcHdyaOi1WsbNp0hsGTR6FA/xfG2w55MdxL9DNUh9rGFZbMq0/zI3J7SSRR/HVsgRtLdRSc4k/X4kjIk/mbvjnyllXrXdttfsdu4kzvrS30iSc1tbP+NorVX3ErywAaEJhYjj5b3sWYUH0KRdEtrE0JvhJTiTW5kzOOeLxx3kAPveqtYXpW+9 3sZy2bYo vnWW3VXz9rzYjXlXtkTPp/cl6jDrdX/mQQrLV/1sF12TUC3ZTV5oWmzZlN5FrRG6MePhefXNyCvNlMyPOnUAEPp3dBr141eKd70X/rwXaSJBeDwpJE708CD/YGdqW6nrmbl0SFyLqiNxbUdnslmI3Cn13QvaJfGJmpfnHozsShuVjfIw14GS7iReO739njlGRQHiFuA7aFh9r1Gkq0YOM6rop8b8n8AjLDCxpKLZeGFk5M2Lo+veLXSBCTzvKT7S0G5dMZTSa7CgXB5mlpNlRRRK4hRmIjKxcIa6iKQbArHL6D3/ancZ9Gs/MOunVAZH1jGWV03DG+7GSvSeIpM0cb9gtkFd3b91vUJViRgUojO+FN+sS5VnqLBhM5IS1SyE5bYhDjmgDF+aLiOlRWVtqHwNKwGFZZrUM8R7uoGUAq908pkkGhhdZEx4lGMODjzSzPQXhLDm7KFdblqQ= 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: List-Subscribe: List-Unsubscribe: On Thu, Jul 25, 2024 at 5:32=E2=80=AFPM Jann Horn wrote: > > Currently, KASAN is unable to catch use-after-free in SLAB_TYPESAFE_BY_RC= U > slabs because use-after-free is allowed within the RCU grace period by > design. > > Add a SLUB debugging feature which RCU-delays every individual > kmem_cache_free() before either actually freeing the object or handing it > off to KASAN, and change KASAN to poison freed objects as normal when thi= s > option is enabled. > > For now I've configured Kconfig.debug to default-enable this feature in t= he > KASAN GENERIC and SW_TAGS modes; I'm not enabling it by default in HW_TAG= S > mode because I'm not sure if it might have unwanted performance degradati= on > effects there. > > Note that this is mostly useful with KASAN in the quarantine-based GENERI= C > mode; SLAB_TYPESAFE_BY_RCU slabs are basically always also slabs with a > ->ctor, and KASAN's assign_tag() currently has to assign fixed tags for > those, reducing the effectiveness of SW_TAGS/HW_TAGS mode. > (A possible future extension of this work would be to also let SLUB call > the ->ctor() on every allocation instead of only when the slab page is > allocated; then tag-based modes would be able to assign new tags on every > reallocation.) > > Signed-off-by: Jann Horn Acked-by: Andrey Konovalov But see some nits below. Thank you! > --- > include/linux/kasan.h | 14 ++++++---- > mm/Kconfig.debug | 29 ++++++++++++++++++++ > mm/kasan/common.c | 13 +++++---- > mm/kasan/kasan_test.c | 44 +++++++++++++++++++++++++++++ > mm/slab_common.c | 12 ++++++++ > mm/slub.c | 76 +++++++++++++++++++++++++++++++++++++++++++++= ------ > 6 files changed, 170 insertions(+), 18 deletions(-) > > diff --git a/include/linux/kasan.h b/include/linux/kasan.h > index ebd93c843e78..c64483d3e2bd 100644 > --- a/include/linux/kasan.h > +++ b/include/linux/kasan.h > @@ -186,12 +186,15 @@ static __always_inline bool kasan_slab_pre_free(str= uct kmem_cache *s, > } > > bool __kasan_slab_free(struct kmem_cache *s, void *object, > - unsigned long ip, bool init); > + unsigned long ip, bool init, bool after_rcu_delay= ); > static __always_inline bool kasan_slab_free(struct kmem_cache *s, > - void *object, bool init) > + void *object, bool init, > + bool after_rcu_delay) > { > - if (kasan_enabled()) > - return __kasan_slab_free(s, object, _RET_IP_, init); > + if (kasan_enabled()) { > + return __kasan_slab_free(s, object, _RET_IP_, init, > + after_rcu_delay); > + } > return false; > } > > @@ -387,7 +390,8 @@ static inline bool kasan_slab_pre_free(struct kmem_ca= che *s, void *object) > return false; > } > > -static inline bool kasan_slab_free(struct kmem_cache *s, void *object, b= ool init) > +static inline bool kasan_slab_free(struct kmem_cache *s, void *object, > + bool init, bool after_rcu_delay) > { > return false; > } > diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug > index afc72fde0f03..0c088532f5a7 100644 > --- a/mm/Kconfig.debug > +++ b/mm/Kconfig.debug > @@ -70,6 +70,35 @@ config SLUB_DEBUG_ON > off in a kernel built with CONFIG_SLUB_DEBUG_ON by specifying > "slab_debug=3D-". > > +config SLUB_RCU_DEBUG > + bool "Make use-after-free detection possible in TYPESAFE_BY_RCU c= aches" Perhaps, it makes sense to point out that is related to KASAN's use-after-free detection in the option description. > + depends on SLUB_DEBUG Do we need depends on KASAN? > + default KASAN_GENERIC || KASAN_SW_TAGS > + help > + Make SLAB_TYPESAFE_BY_RCU caches behave approximately as if the= cache > + was not marked as SLAB_TYPESAFE_BY_RCU and every caller used > + kfree_rcu() instead. > + > + This is intended for use in combination with KASAN, to enable K= ASAN to > + detect use-after-free accesses in such caches. > + (KFENCE is able to do that independent of this flag.) > + > + This might degrade performance. > + Unfortunately this also prevents a very specific bug pattern fr= om > + triggering (insufficient checks against an object being recycle= d > + within the RCU grace period); so this option can be turned off = even on > + KASAN builds, in case you want to test for such a bug. > + > + If you're using this for testing bugs / fuzzing and care about > + catching all the bugs WAY more than performance, you might want= to > + also turn on CONFIG_RCU_STRICT_GRACE_PERIOD. > + > + WARNING: > + This is designed as a debugging feature, not a security feature= . > + Objects are sometimes recycled without RCU delay under memory p= ressure. > + > + If unsure, say N. > + > config PAGE_OWNER > bool "Track page owner" > depends on DEBUG_KERNEL && STACKTRACE_SUPPORT > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 7c7fc6ce7eb7..d92cb2e9189d 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -238,7 +238,8 @@ static enum free_validation_result check_slab_free(st= ruct kmem_cache *cache, > } > > static inline bool poison_slab_object(struct kmem_cache *cache, void *ob= ject, > - unsigned long ip, bool init) > + unsigned long ip, bool init, > + bool after_rcu_delay) > { > void *tagged_object =3D object; > enum free_validation_result valid =3D check_slab_free(cache, obje= ct, ip); > @@ -251,7 +252,8 @@ static inline bool poison_slab_object(struct kmem_cac= he *cache, void *object, > object =3D kasan_reset_tag(object); > > /* RCU slabs could be legally used after free within the RCU peri= od. */ > - if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) > + if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU) && > + !after_rcu_delay) This can be kept on the same line. > return false; > > kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_S= IZE), > @@ -270,7 +272,8 @@ bool __kasan_slab_pre_free(struct kmem_cache *cache, = void *object, > } > > bool __kasan_slab_free(struct kmem_cache *cache, void *object, > - unsigned long ip, bool init) > + unsigned long ip, bool init, > + bool after_rcu_delay) > { > if (is_kfence_address(object)) > return false; > @@ -280,7 +283,7 @@ bool __kasan_slab_free(struct kmem_cache *cache, void= *object, > * freelist. The object will thus never be allocated again and it= s > * metadata will never get released. > */ > - if (poison_slab_object(cache, object, ip, init)) > + if (poison_slab_object(cache, object, ip, init, after_rcu_delay)) > return true; > > /* > @@ -535,7 +538,7 @@ bool __kasan_mempool_poison_object(void *ptr, unsigne= d long ip) > return false; > > slab =3D folio_slab(folio); > - return !poison_slab_object(slab->slab_cache, ptr, ip, false); > + return !poison_slab_object(slab->slab_cache, ptr, ip, false, fals= e); > } > > void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned lo= ng ip) > diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c > index 7b32be2a3cf0..cba782a4b072 100644 > --- a/mm/kasan/kasan_test.c > +++ b/mm/kasan/kasan_test.c > @@ -996,6 +996,49 @@ static void kmem_cache_invalid_free(struct kunit *te= st) > kmem_cache_destroy(cache); > } > > +static void kmem_cache_rcu_uaf(struct kunit *test) > +{ > + char *p; > + size_t size =3D 200; > + struct kmem_cache *cache; > + > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB_RCU_DEBUG); > + > + cache =3D kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_= BY_RCU, > + NULL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); > + > + p =3D kmem_cache_alloc(cache, GFP_KERNEL); > + if (!p) { > + kunit_err(test, "Allocation failed: %s\n", __func__); > + kmem_cache_destroy(cache); > + return; > + } > + *p =3D 1; > + > + rcu_read_lock(); > + > + /* Free the object - this will internally schedule an RCU callbac= k. */ > + kmem_cache_free(cache, p); > + > + /* We should still be allowed to access the object at this point = because Empty line after /* here and below. > + * the cache is SLAB_TYPESAFE_BY_RCU and we've been in an RCU rea= d-side > + * critical section since before the kmem_cache_free(). > + */ > + READ_ONCE(*p); > + > + rcu_read_unlock(); > + > + /* Wait for the RCU callback to execute; after this, the object s= hould > + * have actually been freed from KASAN's perspective. > + */ > + rcu_barrier(); > + > + KUNIT_EXPECT_KASAN_FAIL(test, READ_ONCE(*p)); > + > + kmem_cache_destroy(cache); > +} > + > static void empty_cache_ctor(void *object) { } > > static void kmem_cache_double_destroy(struct kunit *test) > @@ -1937,6 +1980,7 @@ static struct kunit_case kasan_kunit_test_cases[] = =3D { > KUNIT_CASE(kmem_cache_oob), > KUNIT_CASE(kmem_cache_double_free), > KUNIT_CASE(kmem_cache_invalid_free), > + KUNIT_CASE(kmem_cache_rcu_uaf), > KUNIT_CASE(kmem_cache_double_destroy), > KUNIT_CASE(kmem_cache_accounted), > KUNIT_CASE(kmem_cache_bulk), > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 1560a1546bb1..19511e34017b 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -450,6 +450,18 @@ static void slab_caches_to_rcu_destroy_workfn(struct= work_struct *work) > > static int shutdown_cache(struct kmem_cache *s) > { > + if (IS_ENABLED(CONFIG_SLUB_RCU_DEBUG) && > + (s->flags & SLAB_TYPESAFE_BY_RCU)) { > + /* > + * Under CONFIG_SLUB_RCU_DEBUG, when objects in a > + * SLAB_TYPESAFE_BY_RCU slab are freed, SLUB will interna= lly > + * defer their freeing with call_rcu(). > + * Wait for such call_rcu() invocations here before actua= lly > + * destroying the cache. > + */ > + rcu_barrier(); > + } > + > /* free asan quarantined objects */ > kasan_cache_shutdown(s); > > diff --git a/mm/slub.c b/mm/slub.c > index 34724704c52d..f44eec209e3e 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -2144,15 +2144,26 @@ static inline void memcg_slab_free_hook(struct km= em_cache *s, struct slab *slab, > } > #endif /* CONFIG_MEMCG_KMEM */ > > +#ifdef CONFIG_SLUB_RCU_DEBUG > +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head); > + > +struct rcu_delayed_free { > + struct rcu_head head; > + void *object; > +}; > +#endif > + > /* > * Hooks for other subsystems that check memory allocations. In a typica= l > * production configuration these hooks all should produce no code at al= l. > * > * Returns true if freeing of the object can proceed, false if its reuse > - * was delayed by KASAN quarantine, or it was returned to KFENCE. > + * was delayed by CONFIG_SLUB_RCU_DEBUG or KASAN quarantine, or it was r= eturned > + * to KFENCE. > */ > static __always_inline > -bool slab_free_hook(struct kmem_cache *s, void *x, bool init) > +bool slab_free_hook(struct kmem_cache *s, void *x, bool init, > + bool after_rcu_delay) > { > kmemleak_free_recursive(x, s->flags); > kmsan_slab_free(s, x); > @@ -2163,7 +2174,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, = bool init) > debug_check_no_obj_freed(x, s->object_size); > > /* Use KCSAN to help debug racy use-after-free. */ > - if (!(s->flags & SLAB_TYPESAFE_BY_RCU)) > + if (!(s->flags & SLAB_TYPESAFE_BY_RCU) || after_rcu_delay) > __kcsan_check_access(x, s->object_size, > KCSAN_ACCESS_WRITE | KCSAN_ACCESS_AS= SERT); > > @@ -2177,6 +2188,28 @@ bool slab_free_hook(struct kmem_cache *s, void *x,= bool init) > if (kasan_slab_pre_free(s, x)) > return false; > > +#ifdef CONFIG_SLUB_RCU_DEBUG > + if ((s->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu_delay) { > + struct rcu_delayed_free *delayed_free; > + > + delayed_free =3D kmalloc(sizeof(*delayed_free), GFP_NOWAI= T); > + if (delayed_free) { > + /* > + * Let KASAN track our call stack as a "related w= ork > + * creation", just like if the object had been fr= eed > + * normally via kfree_rcu(). > + * We have to do this manually because the rcu_he= ad is > + * not located inside the object. > + */ > + kasan_record_aux_stack_noalloc(x); > + > + delayed_free->object =3D x; > + call_rcu(&delayed_free->head, slab_free_after_rcu= _debug); > + return false; > + } > + } > +#endif /* CONFIG_SLUB_RCU_DEBUG */ > + > /* > * As memory initialization might be integrated into KASAN, > * kasan_slab_free and initialization memset's must be > @@ -2200,7 +2233,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, = bool init) > s->size - inuse - rsize); > } > /* KASAN might put x into memory quarantine, delaying its reuse. = */ > - return !kasan_slab_free(s, x, init); > + return !kasan_slab_free(s, x, init, after_rcu_delay); > } > > static __fastpath_inline > @@ -2214,7 +2247,7 @@ bool slab_free_freelist_hook(struct kmem_cache *s, = void **head, void **tail, > bool init; > > if (is_kfence_address(next)) { > - slab_free_hook(s, next, false); > + slab_free_hook(s, next, false, false); > return false; > } > > @@ -2229,7 +2262,7 @@ bool slab_free_freelist_hook(struct kmem_cache *s, = void **head, void **tail, > next =3D get_freepointer(s, object); > > /* If object's reuse doesn't have to be delayed */ > - if (likely(slab_free_hook(s, object, init))) { > + if (likely(slab_free_hook(s, object, init, false))) { > /* Move object to the new freelist */ > set_freepointer(s, object, *head); > *head =3D object; > @@ -4442,7 +4475,7 @@ void slab_free(struct kmem_cache *s, struct slab *s= lab, void *object, > memcg_slab_free_hook(s, slab, &object, 1); > alloc_tagging_slab_free_hook(s, slab, &object, 1); > > - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) > + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), f= alse))) > do_slab_free(s, slab, object, object, 1, addr); > } > > @@ -4451,7 +4484,7 @@ void slab_free(struct kmem_cache *s, struct slab *s= lab, void *object, > static noinline > void memcg_alloc_abort_single(struct kmem_cache *s, void *object) > { > - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) > + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), f= alse))) > do_slab_free(s, virt_to_slab(object), object, object, 1, = _RET_IP_); > } > #endif > @@ -4470,6 +4503,33 @@ void slab_free_bulk(struct kmem_cache *s, struct s= lab *slab, void *head, > do_slab_free(s, slab, head, tail, cnt, addr); > } > > +#ifdef CONFIG_SLUB_RCU_DEBUG > +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head) > +{ > + struct rcu_delayed_free *delayed_free =3D > + container_of(rcu_head, struct rcu_delayed_free, h= ead); > + void *object =3D delayed_free->object; > + struct slab *slab =3D virt_to_slab(object); > + struct kmem_cache *s; > + > + if (WARN_ON(is_kfence_address(rcu_head))) > + return; > + > + /* find the object and the cache again */ > + if (WARN_ON(!slab)) > + return; > + s =3D slab->slab_cache; > + if (WARN_ON(!(s->flags & SLAB_TYPESAFE_BY_RCU))) > + return; > + > + /* resume freeing */ > + if (!slab_free_hook(s, object, slab_want_init_on_free(s), true)) > + return; > + do_slab_free(s, slab, object, NULL, 1, _THIS_IP_); > + kfree(delayed_free); > +} > +#endif /* CONFIG_SLUB_RCU_DEBUG */ > + > #ifdef CONFIG_KASAN_GENERIC > void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr= ) > { > > -- > 2.45.2.1089.g2a221341d9-goog >