Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2240373ybv; Fri, 14 Feb 2020 14:24:54 -0800 (PST) X-Google-Smtp-Source: APXvYqwiwnG5oRlyvJV1OHjSFciWZ5PCtmnPGiKRCcQw4ua2+XwpuKR9Mp2fM1jGWghfxsQnFE92 X-Received: by 2002:aca:5fc6:: with SMTP id t189mr3549791oib.166.1581719094399; Fri, 14 Feb 2020 14:24:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581719094; cv=none; d=google.com; s=arc-20160816; b=I+XtnVhOuMegxHFnAwV5sCFoksDrijf14jkXdz9lmoBBZPbjlKKsUkJXbUpatvvOZF YJoANj24QJ5b8D+MlhYsouqoAnl4sd4G0PuLmYePi/ESAJBi8PtkU733GQESUo54VZhk TOZ2TlZbCOtP0jcNwZZmpfoEd8vIL+xdO4bnla/pBGEpNVGbbvTrvi729sSW5kwu8pxF Ib6nAO0udWRWVr2Zu/h3S4jJHb6Mu8jXT5PzFQlf7fwhLhVKvJBe4wxO3OIeF7nSvjA7 GaE3+v3G9jNx7WL4Wf3GVHEGUKYPyJUc2GSvA3d2s97cajzaArEmcHkI8THfa7oQrzou zWLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=gqPN6rca6tQbB2wbaK3mUEwUJIuY7eBrM+y+4z2O/UE=; b=pCkWuvutODt6f7fqokk1hdqy/r4MR2O5p/wrIhFUBJyUtlIsQsanSR1yMringeQkn1 vC7YAllH7uQ5orbwroXJD15KuYI4prloVHMPyJzovFqBlZt8mwKKSVqhKMtWr7F+hVxG wFp8zmbNHYK6VB8CchbH93gxJ8sDuAgTa+ZmIeRsNVKbupRUw5ipdOZAWdTG0ztjFGuE eyYAnO4uXW5RCjSGP7NRxNX6ttS/15ymMuxpmbdv9O1w4iyNak+Hl5nBMhlCH6OFpbap FmiB0Q6GeGEB7pF8VfQ7RGDjMmf7XYMeRaLGab6z2EWsDnRSx3C/magW3pNuxMv+MIiG 0D7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IBiQ45Su; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v21si3269822otj.282.2020.02.14.14.24.42; Fri, 14 Feb 2020 14:24:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IBiQ45Su; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727649AbgBNWYe (ORCPT + 99 others); Fri, 14 Feb 2020 17:24:34 -0500 Received: from mail-pl1-f201.google.com ([209.85.214.201]:35092 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727529AbgBNWYe (ORCPT ); Fri, 14 Feb 2020 17:24:34 -0500 Received: by mail-pl1-f201.google.com with SMTP id v24so5972045plo.2 for ; Fri, 14 Feb 2020 14:24:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=gqPN6rca6tQbB2wbaK3mUEwUJIuY7eBrM+y+4z2O/UE=; b=IBiQ45SugGiYgT2pAGmSYKd2rySIEgvA5tF8RFPtcJAYQzPcRnZZQr24PUCzcN/nOy fuH+tT2GGPHZ/JMiRmG1u9zClkRghNwblFj7VdyFHxNkqGOW7qmcfVMZSDe0HIS0nK0R fk0Ye33LfaW/FfzbJvH+2/9aeoq0Ew1e2MkVST/thn104F3pBTElosxodPQEzWRLm1SW T0qx/k/kdqkGZ/0scvEn7yc1YnSSuFmlsfYPk6xlcBLpGJVFHI7EPaMWi01x19xcxP9c 8Wesjy05hYcLRq6fCWOiqpxwsjtTmDR5fDLTKl2BYzX6v59fF2uCPKJFma+ZaAmJmMj8 7HZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=gqPN6rca6tQbB2wbaK3mUEwUJIuY7eBrM+y+4z2O/UE=; b=M9WdmLxP3R8wOIgLSOtHKq0V1k0HkDeERBwCvsOofkUJtu2kmeN/eN9fqd4RyQnnBJ dNgRBJwIUwM34E22tKos3oQGUuHGfJSRVAT8qmeh/vBVYMEQpsJmVMZznK8dKZp3xPJN sQYqYOYNwt1wzkjv2m0P/4lxRJLNj2uX1ulxRyaKIXQW/KJqYQGvS5s3CHqEsEvlN19/ nAoGPQHX5lh/igeueEloFA/EHy3LqUrMWYBA5vgddeOv6Z+E7Gzp1unSSXv4I9uZ1GXR pfEi7WFyHlwrLzs/cwLPbx+L13Sum20IxtHnd0MU46Z5XPop3/zQos3M+VLg5byHlFz1 iIeg== X-Gm-Message-State: APjAAAUrCuMJaohUbiBiHfg1piX5cbR04uJC15VwPYGJsz7syo2jrEdW jUk30mmjXW4BrpjQs6G8xy6X0Ix21tkIQg== X-Received: by 2002:a63:778c:: with SMTP id s134mr5559980pgc.451.1581719071824; Fri, 14 Feb 2020 14:24:31 -0800 (PST) Date: Fri, 14 Feb 2020 14:24:15 -0800 Message-Id: <20200214222415.181467-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH v2] cgroup: memcg: net: do not associate sock with unrelated cgroup From: Shakeel Butt To: Johannes Weiner , Eric Dumazet Cc: Tejun Heo , Greg Thelen , Michal Hocko , Vladimir Davydov , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, Roman Gushchin , linux-kernel@vger.kernel.org, Shakeel Butt 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 We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1. mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup. Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt. Fixes: 2d7580738345 ("mm: memcontrol: consolidate cgroup socket tracking") Fixes: d979a39d7242 ("cgroup: duplicate cgroup reference when cloning sockets") Signed-off-by: Shakeel Butt --- Changes since v1: - Fix cgroup_sk_alloc() too. kernel/cgroup/cgroup.c | 4 ++++ mm/memcontrol.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 9a8a5ded3c48..46e5f5518fba 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -6449,6 +6449,10 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd) return; } + /* Do not associate the sock with unrelated interrupted task's memcg. */ + if (in_interrupt()) + return; + rcu_read_lock(); while (true) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 63bb6a2aab81..f500da82bfe8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6697,6 +6697,10 @@ void mem_cgroup_sk_alloc(struct sock *sk) return; } + /* Do not associate the sock with unrelated interrupted task's memcg. */ + if (in_interrupt()) + return; + rcu_read_lock(); memcg = mem_cgroup_from_task(current); if (memcg == root_mem_cgroup) -- 2.25.0.265.gbab2e86ba0-goog