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 X-Spam-Level: X-Spam-Status: No, score=-14.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9600DC433E2 for ; Fri, 11 Sep 2020 16:22:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1DFC0208FE for ; Fri, 11 Sep 2020 16:22:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HwKeCNBR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1DFC0208FE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5AEDD6B0003; Fri, 11 Sep 2020 12:22:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 560896B0037; Fri, 11 Sep 2020 12:22:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 475B26B005A; Fri, 11 Sep 2020 12:22:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0101.hostedemail.com [216.40.44.101]) by kanga.kvack.org (Postfix) with ESMTP id 31DB76B0003 for ; Fri, 11 Sep 2020 12:22:04 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E2E19441C for ; Fri, 11 Sep 2020 16:22:03 +0000 (UTC) X-FDA: 77251297326.01.sleet14_3d113b3270f0 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id 6A8A51005000E for ; Fri, 11 Sep 2020 16:22:03 +0000 (UTC) X-HE-Tag: sleet14_3d113b3270f0 X-Filterd-Recvd-Size: 5311 Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com [209.85.208.193]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Fri, 11 Sep 2020 16:22:02 +0000 (UTC) Received: by mail-lj1-f193.google.com with SMTP id b19so12887832lji.11 for ; Fri, 11 Sep 2020 09:22:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6uCs8sK724xgrjgAwvIZNy/g6OhzqOwMIcpbgNs4M6k=; b=HwKeCNBRiGDa4BUCzy8CR2l/UJd4vXSUu0N4tOlCrZdJM49ooJ9gEOmR+/JWAtXIgX 28/Fed29zYW9pTrsc6mchvrgm12+1PUbaBeP/CRvjSCX1nakdfcfUrcqcP/94TKAuNz0 7NF90iZQblfqWgrFqKDY/XRkWCzr21gci103JWnHGS1BNVZ246yj+iP/8LhuddJGkVWK caR0dq8xhLKVqvnXCHYtQ+ECDtGQHEYuhum6p7QTM9s0flV70Fdyu8Zd/FXFjqk8IgDf q73wawMp/zzcEjJnL86qrlYZtkW8VZD4rKNJc8PRGO5uuqY7cik3T8jmbu2r+dxJdwts ttkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6uCs8sK724xgrjgAwvIZNy/g6OhzqOwMIcpbgNs4M6k=; b=gCMqdXbx3cdIFqcmPby347SeUpSk9EDjuOORrl1dYfGbvYnJ1lP4ADcB6GZt4kiYcA pusaAfSi8Ax5z+kYIA8vDpdjBVnpEJ1UXvyq7cCn0pxFx+q507bQlDhSYkPiEH24THHm lE+QxeGn77pjwi/CToFXc3KjhFXrfH5nrqrqbfkilU5QbaOVDn1uWChsLwRWXMDhx+h5 4QxkdSBAYvWeIenYIuIhA6Xozv/JsSNmZmEABbo+8sR0d6g/wTy3EsGDsKP1xIsbRQLO vjVnr46LDadwpzE3sx0IT7pKkwWCtHlB3CLedETVyRKHdEHa67PhyXSbZFErJFN5BiYx XejA== X-Gm-Message-State: AOAM530h6LlgZMIpwEuZWgCfscmfeyeju4QL1snoRC9RI4ufgzdXCwbO f2Q+u+VJEiwnWMb19Fh7MKm2EeAh2i3cdH20pl6EiA== X-Google-Smtp-Source: ABdhPJwtHiPRand28SIuaJYBfKMlytcg/m9BTz0IY4B1SVSLiAwLsVzfY9fjFEFxACXaxPfCIT4kr8D+Ra+DI4y2jgo= X-Received: by 2002:a2e:a202:: with SMTP id h2mr1185600ljm.282.1599841321134; Fri, 11 Sep 2020 09:22:01 -0700 (PDT) MIME-Version: 1.0 References: <20200910022435.2773735-1-guro@fb.com> <20200910224309.GB1307870@carbon.dhcp.thefacebook.com> In-Reply-To: <20200910224309.GB1307870@carbon.dhcp.thefacebook.com> From: Shakeel Butt Date: Fri, 11 Sep 2020 09:21:49 -0700 Message-ID: Subject: Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj() To: Roman Gushchin Cc: Andrew Morton , Linux MM , Johannes Weiner , Michal Hocko , Kernel Team , LKML , Vlastimil Babka , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 6A8A51005000E X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 X-Bogosity: Ham, tests=bogofilter, spamicity=0.111449, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Sep 10, 2020 at 3:43 PM Roman Gushchin wrote: > > Forgot to cc stable@, an updated version is below. > > Thanks! > > -- > > From fe61af45ae570b143ca783ba4d013a0a2b923a15 Mon Sep 17 00:00:00 2001 > From: Roman Gushchin > Date: Wed, 9 Sep 2020 12:19:37 -0700 > Subject: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in > mem_cgroup_from_obj() > > mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup > pointer to determine if the page has an attached obj_cgroup vector > instead of a regular memcg pointer. If it's not set, it simple returns > the page->mem_cgroup value as a struct mem_cgroup pointer. > > The commit 10befea91b61 ("mm: memcg/slab: use a single set of > kmem_caches for all allocations") changed the moment when this bit > is set: if previously it was set on the allocation of the slab page, > now it can be set well after, when the first accounted object is > allocated on this page. > > It opened a race: if page->mem_cgroup is set concurrently after the > first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups > array can be returned as a memory cgroup pointer. > > A simple check for page->mem_cgroup pointer for NULL before the > page_has_obj_cgroups() check fixes the race. Indeed, if the pointer > is not NULL, it's either a simple mem_cgroup pointer or a pointer > to obj_cgroup vector. The pointer can be asynchronously changed > from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed > from a valid memcg pointer to objcg vector or back. > > If the object passed to mem_cgroup_from_obj() is a slab object > and page->mem_cgroup is NULL, it means that the object is not > accounted, so the function must return NULL. > > I've discovered the race looking at the code, so far I haven't seen it > in the wild. > > Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations") > Signed-off-by: Roman Gushchin > Cc: Johannes Weiner > Cc: Vlastimil Babka > Cc: Shakeel Butt > Cc: stable@vger.kernel.org > --- Is the caller list_lru_from_kmem() the concern or is this more about making mem_cgroup_from_obj() more future proof? Also have you taken a look at [1]? I am still trying to figure out how that is possible. [1] https://lore.kernel.org/lkml/20200901075321.GL4299@shao2-debian/