From patchwork Tue Oct 27 08:02:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11859629 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 5C69C61C for ; Tue, 27 Oct 2020 08:03:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 05FAD207BB for ; Tue, 27 Oct 2020 08:03:33 +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="0mmq9zyn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 05FAD207BB 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 1498E6B006C; Tue, 27 Oct 2020 04:03:33 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1203A6B006E; Tue, 27 Oct 2020 04:03:33 -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 037496B0070; Tue, 27 Oct 2020 04:03:32 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0031.hostedemail.com [216.40.44.31]) by kanga.kvack.org (Postfix) with ESMTP id CC0EC6B006C for ; Tue, 27 Oct 2020 04:03:32 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 71EFC180AD806 for ; Tue, 27 Oct 2020 08:03:32 +0000 (UTC) X-FDA: 77416965864.09.voice61_1405e802727a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 59A66180AD802 for ; Tue, 27 Oct 2020 08:03:32 +0000 (UTC) X-Spam-Summary: 1,0,0,e0c406d29acba01c,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:1540:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2693:3138:3139:3140:3141:3142:3352:3865:3866:3867:3872:3874:4385:4390:4395:5007:6261:6653:6737:8603:9036:10004:11026:11658:11914:12048:12296:12297:12517:12519:12555:12679:12895:13069:13161:13229:13311:13357:13894:14096:14181:14384:14394:14721:21080:21444:21451:21627:30054:30070,0,RBL:209.85.215.196:@bytedance.com:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04yrpns3ysz6k4nkpdj57ug6bzf7fopxjrhttu45t6os1jqrbhqcs93gybyresq.xiw6hqi9b3yq749o9pa6osb84twsdyx57duikzeqrasxmm66gwyptex8k5gqemp.o-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:34,LUA_SUMMARY:none X-HE-Tag: voice61_1405e802727a X-Filterd-Recvd-Size: 4291 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Tue, 27 Oct 2020 08:03:31 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id o7so321762pgv.6 for ; Tue, 27 Oct 2020 01:03:31 -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=YWIAYpPs9+svXY8PEtGZ5WZ0CNEgBLTQowd7Fmk6Ks0=; b=0mmq9zynkfizT4Ql1IxpOGnGDMmXfmaRO/jTjmX5y5xzgLSWldW1D0OmZmiaqfd3Jj uTnV/2ojetEMYoqEy6o95NADBO7FFfWXq+J8lRdeuLuCL+w6GFggpi1WoXcq7Ai0Ouln kMd+2CMAl09mrojtd/edMDGXtdwUoq2EPfkdkJl8IJ42pR927Gy1+02gSJnULXNN7/AR Tin3noEy1hSSySrqkHSsnkchL+m1SAd/NAdjV9jF1hVqVfziOhHfoalcTD5MZA+F0CcN Wsvz53aafL+3Nhw88pYQat+6bd/CjXTZhks3P7F+G145xKZfz8Ji/eNFy8hAV5ap0QgC lJjQ== 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=YWIAYpPs9+svXY8PEtGZ5WZ0CNEgBLTQowd7Fmk6Ks0=; b=HWQKLqgc2r7Dgq9C4xgz3HpmKkfrcRO44FHMRJo1/F8wkjnOIng2FRATPM23KALty/ UCfLzJn/4BF+8EkMvwGBdDUPeRkAr8Qeb5XhRnSQ5Pjyer3/RhKRcG3zSmRjmALZP91e rmPKOIFzX+EXfXZdAyxxKkfL/UFxEbOj2akUjfdwE+sEQI3pEs7oxkaVg7+taAKIWP3x Zi6nP1+PMPEljSeBaRMQqlHYbpNFhsRzZwtGyERsm5epQ8zfekj0AsDoWWM0qkMNVdRg JGSCALho98cESac8RKKBnQbBbzMwJIxIgzqfa87bC1x38106mKHv+LFhzN/gk45eS+wM BQfQ== X-Gm-Message-State: AOAM533SSipr1EepnyPgF3I7AyAPozcOv7H3D1DlmOF/4bMPwZNsaITp /xZEFQSXKWjZTb1994Ha0eRnAw== X-Google-Smtp-Source: ABdhPJz1hfXJG1lmVYIhQsXjHZGILbCTSJSIyuvmPZNDlDiLd4f3AkFLLlZK7QOTjOFm2stL4K/P9g== X-Received: by 2002:a63:5421:: with SMTP id i33mr938048pgb.316.1603785810689; Tue, 27 Oct 2020 01:03:30 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.220.89]) by smtp.gmail.com with ESMTPSA id p8sm1039580pgs.34.2020.10.27.01.03.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Oct 2020 01:03:30 -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 2/5] mm: memcg/slab: Fix use after free in obj_cgroup_charge Date: Tue, 27 Oct 2020 16:02:53 +0800 Message-Id: <20201027080256.76497-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201027080256.76497-1-songmuchun@bytedance.com> References: <20201027080256.76497-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: 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. This can be happened when we reparent the memcg. So using css_tryget instead of css_get. Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Muchun Song Acked-by: Roman Gushchin --- mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index fcbd79c5023e..c0c27bee23ad 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3223,8 +3223,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 (!css_tryget(&memcg->css)) + goto retry; rcu_read_unlock(); nr_pages = size >> PAGE_SHIFT;