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 F378FC43334 for ; Mon, 13 Jun 2022 03:19:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 818D08D0144; Sun, 12 Jun 2022 23:19:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C7958D0142; Sun, 12 Jun 2022 23:19:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 668AA8D0144; Sun, 12 Jun 2022 23:19:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 56F128D0142 for ; Sun, 12 Jun 2022 23:19:50 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 379B160D0A for ; Mon, 13 Jun 2022 03:19:50 +0000 (UTC) X-FDA: 79571758140.16.85B32ED Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf16.hostedemail.com (Postfix) with ESMTP id 25549180083 for ; Mon, 13 Jun 2022 03:19:49 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d5so1407372plo.12 for ; Sun, 12 Jun 2022 20:19:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=egrU8RsjHIRm43M/aC907vcBMj4PwC5iUGqpoMehifQ=; b=U5cVALfpRPrBSMeTCRobBDq50nUnnH+QOaW6MukQugDqXhIx/0UupubDPK1E3y3iNL pfp/V+sUHxfXexmZUMwf6HLqx3jqPGRAFpHV3OiH+ICKTw86UPGhqB35hzAgTB4GD3b5 5w8+cSuC8CRemyHZWtmV3Juvicd/adKr7v0GZr6aYmbl3Eda4guq87yE6n1VdDADSXAy CgiP/Fh7AMSBh3KvpsAoae7x7bcifYY6Bw+HJOClmUo8hSUM9v8tpvALWmzI3emZ3T1i TtGGg23536uSPr87O14KzOY8iiDpMXEoj53BXLBrUvRqV9lNfHdxskR2POJhlxUZINyh 3rkA== 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:message-id:references :mime-version:content-disposition:in-reply-to; bh=egrU8RsjHIRm43M/aC907vcBMj4PwC5iUGqpoMehifQ=; b=rV8JqiEB0UuhlMrgAn66urc0ZgO8khrl+c3R5F5yAYrIYZqM6vUeOc4U8XchnkaQvo yKzKDl2QmFteZEPdw2ucQgbiTizZ6MbdRkSxPCg5PV5m6VPqn+KACar83jHXQqEmgssD 28omDVcYWnv8ritdaY0hMoSAYA8AneGN7c7IiejQwSK6dTUsAJdulhCk3lhnOcvnvN1J F+I0r0+SpxGhoJY9S3NEicUMF2O9m8V5Nl0KrAE8V7AhIVt7rsvG14Tq8bJEIIIO2tZm AuPQwV//iQ1Sa/BPmCDHCRr5ZvPug2c+q47ib12dUDuluABDPjId6MhifAw/4MRkXDFU gIaQ== X-Gm-Message-State: AOAM531BFaYB8EE1zhRqOOAEvd9o0J7ra6fF4ScKVi/w8Z1kAP3vc+ly JSKzKYZkbN4mXRQI+4oO1a+a8A== X-Google-Smtp-Source: ABdhPJyTo54edL8cKqlW6GL2iq0Dn0gjG6KDpcfbJZgLCfVVENlJn4NULWF5+ZUsh3GANFVnkX4FCA== X-Received: by 2002:a17:90a:778c:b0:1df:56a5:8474 with SMTP id v12-20020a17090a778c00b001df56a58474mr13414903pjk.63.1655090387796; Sun, 12 Jun 2022 20:19:47 -0700 (PDT) Received: from localhost ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id e11-20020a170902ed8b00b00163f8ddf160sm3780111plj.161.2022.06.12.20.19.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 20:19:47 -0700 (PDT) Date: Mon, 13 Jun 2022 11:19:44 +0800 From: Muchun Song To: Jann Horn Cc: Christoph Lameter , Pekka Enberg , David Rientjes , 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 Message-ID: References: <20220608182205.2945720-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220608182205.2945720-1-jannh@google.com> ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655090389; a=rsa-sha256; cv=none; b=CwxhSESu847EEDI4U2UD2WVrcXm6yjJNLYzzViRZFPXeVZWGiQ6PlSp5E6h/ZcmiIXCYKY kRvV8pJSD760zLavNGDpoqUdhoCgLQzDPd4Y9BQMVUu2AlbFtPGltRLeAkeG9ysjMSiSkW 1jAbGCOInB7E+rIuilUk4sF5JhUn5T0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=U5cVALfp; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655090389; 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=egrU8RsjHIRm43M/aC907vcBMj4PwC5iUGqpoMehifQ=; b=LnVVAstiweRzuVV0ZCkptsov66mWtWf5tFLMAKTLNqlAgtjUNoPQOdg1mCWnLkno1Y98YC K/HJWcoc3sl94koL4AuMDttoIrN0ifWQ3y11IAPkt2JYMwHwB0nrmPX58O7nH+Itjb5ze6 q1auBLNYTE+e05bhwn/t7cpHxZRzz50= X-Stat-Signature: 63rk1nt8enthirgdmunj7xiscpq6oapn X-Rspamd-Queue-Id: 25549180083 X-Rspam-User: X-Rspamd-Server: rspam10 Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=U5cVALfp; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-HE-Tag: 1655090389-502865 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, Jun 08, 2022 at 08:22:05PM +0200, 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 Reviewed-by: Muchun Song Thanks.