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 7319EC433E2 for ; Fri, 11 Sep 2020 22:41:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2B8CE22207 for ; Fri, 11 Sep 2020 22:41:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H1dQIbWK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725930AbgIKWlr (ORCPT ); Fri, 11 Sep 2020 18:41:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725864AbgIKWln (ORCPT ); Fri, 11 Sep 2020 18:41:43 -0400 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E59FDC061757 for ; Fri, 11 Sep 2020 15:41:42 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id k25so13705065ljg.9 for ; Fri, 11 Sep 2020 15:41:42 -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=naSKnnliw5Mpgkr2F3L5QROMJpfjAY9uHH+kOIOM738=; b=H1dQIbWKaMmL+aZc7U2mstufXvwtiANpXmTZnMm92/rw2KJWyLz/QU83Bt4N+zqKKa sm6W1Vm1HGStoH5YocmrMJPTFOTwLtdgXallj0p/IV+V5ichue/yRoZxEDobDnlyYotz g+OI9rxfJtVW1bP9RX+ZfomLlXUdA58+Q8IhjI4mo/pP7YsBJBpnyLsLNk1Xg9iGRECh cMGsbTGXWu+0vtGpwBtRG+eK3HbgrtCp8GxFvdtcmYFoEIC7FxeZ9lx1+Z9dLfQHW6nv 0TB+JZO/cExgP/lnq+hAiYivGXu9Na077PEDjZuOfif3yjrXPVgz9L1BMxI04BWBYWvP ZBbA== 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=naSKnnliw5Mpgkr2F3L5QROMJpfjAY9uHH+kOIOM738=; b=ReYo0r+ABlCO/IDEC/B94VIuargt0yrIHnawkCPq2VgyQ+ZQ6VW4wvMMe2qaOP74GV FhKihkiIbzH1YM3GTIKpnkTqYOHJrNbljBBSRcynofAD84WJqn/1NwYcTJ8u3Cm/lJRQ 9zHSmsNaDCUWitwPnvNm/64DUMAQhsfk6dMv+OttKVZU4fH6k1rZ1hszklJL8tHXf/2O 4bc2yeyP4Tshc4mN43sly9d0m2QzVcNw7F3sKS/bX+VjKsw1sgR6nWqDr4UalTl7Jn8I PHqzBfRYSjF2G5TBg8uEeNfmWuZxK8dUfqhB7vzYoBhVhIfY0g9inWgzFXZBtjAxgPqz Ai5w== X-Gm-Message-State: AOAM533r/y7lT3tKcTfEORwC3Ks4ar57Qcg+h/DXIWZvuS90Ie2n/Mvm hHX03O8DlZ1g670w2EwmFO2bYZLUuG44ezXDAfQaOg== X-Google-Smtp-Source: ABdhPJxixpRuERtNUdFVdh1FadJSasbxEi9YKCOjBuG1pUZrm0Kydbw0hEXHgdqwha99ZluySJyDdfc8rGsqDTUY8cE= X-Received: by 2002:a2e:3511:: with SMTP id z17mr1459032ljz.58.1599864100989; Fri, 11 Sep 2020 15:41:40 -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 15:41:30 -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" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 I think this patch is good to have as it will make mem_cgroup_from_obj() more future proof. Reviewed-by: Shakeel Butt