From patchwork Thu Apr 8 14:31:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 12191647 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=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 0F6EEC433B4 for ; Thu, 8 Apr 2021 14:32:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6F02B610CC for ; Thu, 8 Apr 2021 14:31:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6F02B610CC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F167F8E0001; Thu, 8 Apr 2021 10:31:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC69D8D0002; Thu, 8 Apr 2021 10:31:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D67E38E0001; Thu, 8 Apr 2021 10:31:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0216.hostedemail.com [216.40.44.216]) by kanga.kvack.org (Postfix) with ESMTP id BB8368D0002 for ; Thu, 8 Apr 2021 10:31:58 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 7622D181FA8C5 for ; Thu, 8 Apr 2021 14:31:58 +0000 (UTC) X-FDA: 78009439116.11.6858C16 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by imf28.hostedemail.com (Postfix) with ESMTP id 6DBF0200025E for ; Thu, 8 Apr 2021 14:31:58 +0000 (UTC) Received: by mail-qk1-f171.google.com with SMTP id c3so2360789qkc.5 for ; Thu, 08 Apr 2021 07:31:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=btC50RWZIR531OhYeaaK7FrT2NEPi0tjXTwyDrbtV5o=; b=i6e9rr/E1F5vh2gsAqs1AjButTuV2db5PXnLnGsrylcSpVrAPBMDWw7TlkxlPuEcfR 69vzlUrbGFl3mdMfMTrED2E20o/836zklqnPuO8grp+mwVmWtAl9sMJVQGN2R4Dfib4q Osvj9Hy/tqt2ZffewcVDUwMkxVb1ZeXE1xfdOe+xF3NssK0biDNoBBJm2sBSeWzjxNij nr9QqJj/dqmY/PhUHIQC7nJV/X2Y7+GSQPCA2WhHya1WL9wC13mjR7Cy40Od+40yG4kd Csi505C9RBKkqCoYQSty2edESBk7g/mzSEKx6/oZPMUVrtNTtAaw7umqVm4t7n/HereL hQBg== 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:mime-version :content-transfer-encoding; bh=btC50RWZIR531OhYeaaK7FrT2NEPi0tjXTwyDrbtV5o=; b=onY0HUQcqFPqH/NCjfkspGdfVo4hOwWFSWFM8jYNYFJiGL0unePQX8y/AomZ9IjZXP k9Rr1QWc7HGvvi+xE0H4XQ9I+/ljZ9RxzqM1EMRqg+aV93fE1S1OfwaGbj1De8HtAce8 nQ3+QyAqeQXhLBDLDqbJI0rDIwIZL6sjSv3aU/paCedpXpkii4qF14I56blN37sRzmbu obIc02u26KI173VVuvQ5e2nyIGZsOI7AE6cnTDFux9EUjMD0R8jVZzPtWc9k5ZipWU/d dgdaEWQJg0bemoxhMJUpBUGMCsuKa8wlGVBrsKSBvMSU8cuUl2Mj4Cb0umV1Lxzxbz+L ixrg== X-Gm-Message-State: AOAM533h8BD95KcJtz1ZlN7vibiKFKeec9wUoDQmOH30s4XKDveAtrlP Y57ASI9DpCYgwnRQMev1KgdbHA== X-Google-Smtp-Source: ABdhPJw9myrgIvMrLTa+QlhcKS2FfWUGYkStVWOHWX2cPsEm/RDXMQIRxqvmwPYAYvSc9C/Cz2AHcg== X-Received: by 2002:a37:9d53:: with SMTP id g80mr8891089qke.499.1617892317152; Thu, 08 Apr 2021 07:31:57 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::1:cfb7]) by smtp.gmail.com with ESMTPSA id o26sm17463212qko.83.2021.04.08.07.31.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Apr 2021 07:31:56 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Michal Hocko , Hugh Dickins , Shakeel Butt , Roman Gushchin , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH] mm: page_counter: mitigate consequences of a page_counter underflow Date: Thu, 8 Apr 2021 10:31:55 -0400 Message-Id: <20210408143155.2679744-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6DBF0200025E X-Stat-Signature: 5uy1bay1xuzop8s58xibtcowmd7rwsn1 Received-SPF: none (cmpxchg.org>: No applicable sender policy available) receiver=imf28; identity=mailfrom; envelope-from=""; helo=mail-qk1-f171.google.com; client-ip=209.85.222.171 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617892318-225785 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: When the unsigned page_counter underflows, even just by a few pages, a cgroup will not be able to run anything afterwards and trigger the OOM killer in a loop. Underflows shouldn't happen, but when they do in practice, we may just be off by a small amount that doesn't interfere with the normal operation - consequences don't need to be that dire. Reset the page_counter to 0 upon underflow. We'll issue a warning that the accounting will be off and then try to keep limping along. [ We used to do this with the original res_counter, where it was a more straight-forward correction inside the spinlock section. I didn't carry it forward into the lockless page counters for simplicity, but it turns out this is quite useful in practice. ] Signed-off-by: Johannes Weiner Acked-by: Michal Hocko Acked-by: Chris Down Reviewed-by: Shakeel Butt --- mm/page_counter.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/page_counter.c b/mm/page_counter.c index c6860f51b6c6..7d83641eb86b 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -52,9 +52,13 @@ void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages) long new; new = atomic_long_sub_return(nr_pages, &counter->usage); - propagate_protected_usage(counter, new); /* More uncharges than charges? */ - WARN_ON_ONCE(new < 0); + if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n", + new, nr_pages)) { + new = 0; + atomic_long_set(&counter->usage, new); + } + propagate_protected_usage(counter, new); } /**