From patchwork Wed Oct 28 03:50:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11862429 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E2C4114B4 for ; Wed, 28 Oct 2020 03:51:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 801E322456 for ; Wed, 28 Oct 2020 03:51:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="JRG9qhqW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 801E322456 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AEDD06B005D; Tue, 27 Oct 2020 23:50:59 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A9E656B0062; Tue, 27 Oct 2020 23:50:59 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 967856B0068; Tue, 27 Oct 2020 23:50:59 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id 691B36B005D for ; Tue, 27 Oct 2020 23:50:59 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1A3008249980 for ; Wed, 28 Oct 2020 03:50:59 +0000 (UTC) X-FDA: 77419958238.10.man03_4500f7f27281 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id EF50A16A0C3 for ; Wed, 28 Oct 2020 03:50:58 +0000 (UTC) X-Spam-Summary: 1,0,0,ff195a44976d8357,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:541:800:960:965:966:973:988:989:1260:1311:1314:1345:1359:1437:1515:1534:1541:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2693:2895:3138:3139:3140:3141:3142:3352:3865:3866:3867:3868:3870:3871:3872:3874:4385:4390:4395:5007:6119:6261:6653:6737:7903:8603:9036:10004:11026:11658:11914:12043:12048:12296:12297:12517:12519:12555:12679:12895:13069:13161:13229:13311:13357:13894:14096:14181:14384:14394:14721:21080:21444:21451:21627:30054:30064:30070,0,RBL:209.85.214.193:@bytedance.com:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04ygbhmnxp3dcdyc85yg4pr5yyob6oc9gom5mpn5cxkrjdmjgttnhtzukkkt5n1.dije7nioje4ncdd3r5qyumitdphmrztd4joafhmy4o1off9ma4g5rhueet6zbqq.6-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:68,LUA_SUMMARY:none X-HE-Tag: man03_4500f7f27281 X-Filterd-Recvd-Size: 4658 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Wed, 28 Oct 2020 03:50:58 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id r3so1840514plo.1 for ; Tue, 27 Oct 2020 20:50:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=f42zNGuydbxsMprn7/o4rTiW5Mh/YrWQVTbGkrcpGAU=; b=JRG9qhqWVdv+qAPGEyd6hAeN94upYYLiFE/8ziCyK2IfmG0KH0vcgx4BU3gjZFhSCf OEaj1GD1WXVm1jmeXl1DAWeAzX9Lob3XEKoPsdIGbnDQ1EZlxhf90ONNi7wNxTQIRxa9 JYfxxPzpE73/OCT2b00zuaCJtatE4mhFQZTM1zuzRudG7zsEUnH4gtecsO/0Msaf4j7w CHNZUDlEGkeM+dWh/h54KR874ZVAd/8idZH6gFDdGIdh0J1HKh15Z6JSbBjt3nBo17Ag izJQJtcjN0Ho2qCL3Miw31aUHdSVubCbCTwrcvEvzj7CbUF9eK85qoW+4YUKLq/JBNAg 8XsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=f42zNGuydbxsMprn7/o4rTiW5Mh/YrWQVTbGkrcpGAU=; b=kjgUC9C3mlN6Mj+HEffhMTn7VZQXjpGlzRcEoBruv5MMzV0psjOWZU4d/1UnchGT+p gyx+0eNcuPmM6aHUJ+2Sfq6haQeDVwK/Ymf9+rafvTb1/BtuAa19gmO5nIh9+BnRDqXx 6DjPgPnl3Zgb0p7CcJd2mGFagzYzNaCSRECzC3b1s9IeRt72MLF1qjRgo8MMBhSIxC7x t7WqjDHDpAHGa5oGkkEfgke3dzd7xk1wZEpbdJvaBy6OXXOL0t1DrVzXOve6WdYJcFiH DQNb9zAiXoJ8ee/8iOtj8BegZgyR8lOde1TK6/1b8R2rHI/62FUL9WR0eFEmVQvFO8et Lflw== X-Gm-Message-State: AOAM530ssvl+6VGKtevDDAxRpuT3Y5Kq6lr0RckK9H+GgnFmhHxc++JX gT9Jxt48zc4fOb+VE91UN5wFPA== X-Google-Smtp-Source: ABdhPJyMlM5OFcaQWKTSVzpQLorRg0eUkIPGImJyauZgOd1B4JNiA5dI9gI84a4KFGgTpJ2ovwTXOA== X-Received: by 2002:a17:90b:1392:: with SMTP id hr18mr4756699pjb.182.1603857057592; Tue, 27 Oct 2020 20:50:57 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.220.89]) by smtp.gmail.com with ESMTPSA id s8sm3412273pjn.46.2020.10.27.20.50.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Oct 2020 20:50:56 -0700 (PDT) From: Muchun Song To: hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org, shakeelb@google.com, guro@fb.com, iamjoonsoo.kim@lge.com, laoar.shao@gmail.com, chris@chrisdown.name, christian.brauner@ubuntu.com, peterz@infradead.org, mingo@kernel.org, keescook@chromium.org, tglx@linutronix.de, esyr@redhat.com, surenb@google.com, areber@redhat.com, elver@google.com Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Muchun Song Subject: [PATCH v2] mm: memcg/slab: Fix use after free in obj_cgroup_charge Date: Wed, 28 Oct 2020 11:50:11 +0800 Message-Id: <20201028035013.99711-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201028035013.99711-1-songmuchun@bytedance.com> References: <20201028035013.99711-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000032, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of css_get to memcg. If the whole process of a cgroup offlining is completed between reading a objcg->memcg pointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to the obj_cgroup_charge() then?), css_get() can change the ref counter from 0 back to 1. Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Muchun Song Acked-by: Roman Gushchin Reviewed-by: Shakeel Butt --- changelog in v2: 1. Add unlikely and update the commit log suggested by Roman. mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 8c8b4c3ed5a0..d9cdf899c6fc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3221,8 +3221,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size) * independently later. */ rcu_read_lock(); +retry: memcg = obj_cgroup_memcg(objcg); - css_get(&memcg->css); + if (unlikely(!css_tryget(&memcg->css))) + goto retry; rcu_read_unlock(); nr_pages = size >> PAGE_SHIFT;