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 98B5BC433EF for ; Sun, 12 Jun 2022 22:45:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D32158D0135; Sun, 12 Jun 2022 18:45:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE1756B01F3; Sun, 12 Jun 2022 18:45:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA85C8D0135; Sun, 12 Jun 2022 18:45:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AAFF76B01F2 for ; Sun, 12 Jun 2022 18:45:04 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 73DB334CAE for ; Sun, 12 Jun 2022 22:45:04 +0000 (UTC) X-FDA: 79571065728.21.64B29BA Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf10.hostedemail.com (Postfix) with ESMTP id 00F23C006E for ; Sun, 12 Jun 2022 22:45:03 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id z17so4261999pff.7 for ; Sun, 12 Jun 2022 15:45:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=1oIWI4/pMxhVpnGR8mWwnEUQwkLULe/4XPICVKx3jDo=; b=UOW0B2+g9veBuaJJesGAmSzK1rGUugRhWrD5RW7QzMjs15uB/vabAAaa+1ArjtXI5+ wCYhNwe/NeGYXSF+DCsvGYFedmwdBuhv5XJsnOadbqloQBNC5oMOgdTuovwGUIvQdb/O Ve4m1lKhYyHDx6pkuMI6BUOWdxHnAE5ZmVQnZyhPBoSWVzqR3L/7Ywyt2mbMDx4Es4Vi 6MO9PZj1YzqUY4BVoHNrGUQY9D8KevxZ6ZNB3l5YcY6KBE1AZccufF7wl4TSc5xLGf8k dJaTpMxuK1moJei5r0lcOcD5I7ackAqK91hbUpELC0RTnWmzijD2pmlA5UiGo/nWJSdp /MIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=1oIWI4/pMxhVpnGR8mWwnEUQwkLULe/4XPICVKx3jDo=; b=TUsC6erEGjhknVllzRT2NGQVRHwVy1LzRQecDK01aGS6b1mh0vI51xr+MriA7kYTY1 yStJpEyYF2mClIwIldtWdyN9wEdiMEUwAxJZ4A20Sy6A0m/lJcsu14zj+sJ97zYO+NNp LXtslvuPo0NrKo7KOr9cs0kfmsGrLWrGh9rYaliMKfFCkheIMqMtd5YxrDv3MkI+krh+ yi2ogqtFYUr/Ezxwhh2gU7Q+9MXvhGIu3MmcPQUPLoItNa/KyCUJxDJ1gAl3bGuNb/SF XleT4pQmGY+0kjDxzzCLV3Fj1dCayanrYx+L3dDaX5JYFj8mJLVqhg1D6Sa2j5xuHEDn iNrA== X-Gm-Message-State: AOAM532lLWGYKzPytgEFFEc6ssKUkuwNSLEg98UqthNiyHk05GFnYHJA DBjLLDfCC2MGixZx+46c4nCR5w== X-Google-Smtp-Source: ABdhPJyAP8xw16XX2GSBGtQvZt9bLabQmLWafEoHqcf2Pz+n5g0fWR/Cp0oESoC3dkMDpfmQImnIDQ== X-Received: by 2002:a62:cd08:0:b0:51b:c569:cbb8 with SMTP id o8-20020a62cd08000000b0051bc569cbb8mr55896645pfg.71.1655073902690; Sun, 12 Jun 2022 15:45:02 -0700 (PDT) Received: from [2620:15c:29:204:c274:b6da:d4b1:cf53] ([2620:15c:29:204:c274:b6da:d4b1:cf53]) by smtp.gmail.com with ESMTPSA id 19-20020a170902e9d300b001677841e9c2sm3548275plk.119.2022.06.12.15.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 15:45:02 -0700 (PDT) Date: Sun, 12 Jun 2022 15:45:01 -0700 (PDT) From: David Rientjes To: Jann Horn cc: Christoph Lameter , Pekka Enberg , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/slub: add missing TID updates on slab deactivation In-Reply-To: <20220608182205.2945720-1-jannh@google.com> Message-ID: References: <20220608182205.2945720-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655073904; a=rsa-sha256; cv=none; b=Njyv/wcFjc35LGHOcN5fq72LC7j+SRsa/3puAu9T8+dO1tbLtszSTL0puoWoDoFNHdQDk2 8egzyOump+H+yQTK/eCQMfeI6++EHbArmW/1mijF4ZEcqS1gF/Te9AGhJ1uPQIuMUxnrpb eliaxY9b6/PZjpiJO+znsmQtY6J2L7g= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UOW0B2+g; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of rientjes@google.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=rientjes@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655073904; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1oIWI4/pMxhVpnGR8mWwnEUQwkLULe/4XPICVKx3jDo=; b=ogDx1yMGrb1o8mXJEVBv8Vsc+UGFw4hqhMDsuownIKb5tIn/BXy1tdXDyTBwpty2/Sp6+m Wc0Oh8AjIkkkEigQR3PWF0adRGEP9Dz/W/z/SAyR2QSIa6G5Ec2A8pUBRepXisFKYPfvnM Pi4JC/F3lJ9jwV2fQhjEJjxErdeYcBk= X-Stat-Signature: thacp1s9hxdm5na7h5jm1f6eaqmp3bzz X-Rspamd-Queue-Id: 00F23C006E X-Rspam-User: X-Rspamd-Server: rspam10 Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UOW0B2+g; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of rientjes@google.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=rientjes@google.com X-HE-Tag: 1655073903-484891 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 Wed, 8 Jun 2022, Jann Horn wrote: > The fastpath in slab_alloc_node() assumes that c->slab is stable as long as > the TID stays the same. However, two places in __slab_alloc() currently > don't update the TID when deactivating the CPU slab. > > If multiple operations race the right way, this could lead to an object > getting lost; or, in an even more unlikely situation, it could even lead to > an object being freed onto the wrong slab's freelist, messing up the > `inuse` counter and eventually causing a page to be freed to the page > allocator while it still contains slab objects. > > (I haven't actually tested these cases though, this is just based on > looking at the code. Writing testcases for this stuff seems like it'd be > a pain...) > > The race leading to state inconsistency is (all operations on the same CPU > and kmem_cache): > > - task A: begin do_slab_free(): > - read TID > - read pcpu freelist (==NULL) > - check `slab == c->slab` (true) > - [PREEMPT A->B] > - task B: begin slab_alloc_node(): > - fastpath fails (`c->freelist` is NULL) > - enter __slab_alloc() > - slub_get_cpu_ptr() (disables preemption) > - enter ___slab_alloc() > - take local_lock_irqsave() > - read c->freelist as NULL > - get_freelist() returns NULL > - write `c->slab = NULL` > - drop local_unlock_irqrestore() > - goto new_slab > - slub_percpu_partial() is NULL > - get_partial() returns NULL > - slub_put_cpu_ptr() (enables preemption) > - [PREEMPT B->A] > - task A: finish do_slab_free(): > - this_cpu_cmpxchg_double() succeeds() > - [CORRUPT STATE: c->slab==NULL, c->freelist!=NULL] > > > From there, the object on c->freelist will get lost if task B is allowed to > continue from here: It will proceed to the retry_load_slab label, > set c->slab, then jump to load_freelist, which clobbers c->freelist. > > > But if we instead continue as follows, we get worse corruption: > > - task A: run __slab_free() on object from other struct slab: > - CPU_PARTIAL_FREE case (slab was on no list, is now on pcpu partial) > - task A: run slab_alloc_node() with NUMA node constraint: > - fastpath fails (c->slab is NULL) > - call __slab_alloc() > - slub_get_cpu_ptr() (disables preemption) > - enter ___slab_alloc() > - c->slab is NULL: goto new_slab > - slub_percpu_partial() is non-NULL > - set c->slab to slub_percpu_partial(c) > - [CORRUPT STATE: c->slab points to slab-1, c->freelist has objects > from slab-2] > - goto redo > - node_match() fails > - goto deactivate_slab > - existing c->freelist is passed into deactivate_slab() > - inuse count of slab-1 is decremented to account for object from > slab-2 > > At this point, the inuse count of slab-1 is 1 lower than it should be. > This means that if we free all allocated objects in slab-1 except for one, > SLUB will think that slab-1 is completely unused, and may free its page, > leading to use-after-free. > > Fixes: c17dda40a6a4e ("slub: Separate out kmem_cache_cpu processing from deactivate_slab") > Fixes: 03e404af26dc2 ("slub: fast release on full slab") > Cc: stable@vger.kernel.org > Signed-off-by: Jann Horn Acked-by: David Rientjes