From patchwork Tue Feb 22 05:40:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12754555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0A9EC433EF for ; Tue, 22 Feb 2022 05:40:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DBC98D0003; Tue, 22 Feb 2022 00:40:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 18AB38D0001; Tue, 22 Feb 2022 00:40:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 053758D0003; Tue, 22 Feb 2022 00:40:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0231.hostedemail.com [216.40.44.231]) by kanga.kvack.org (Postfix) with ESMTP id E93618D0001 for ; Tue, 22 Feb 2022 00:40:33 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9C976181CAEE0 for ; Tue, 22 Feb 2022 05:40:33 +0000 (UTC) X-FDA: 79169315946.13.E123F00 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf26.hostedemail.com (Postfix) with ESMTP id 34B08140005 for ; Tue, 22 Feb 2022 05:40:33 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id w1-20020a05690204e100b006244315a721so11647732ybs.0 for ; Mon, 21 Feb 2022 21:40:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EUVVGZ+sfFkAlbWcLcOfWelo8aSG6wtK/MR4mUfnNyc=; b=fpKWCGjam3h+oTc0b3WTir63RzOkNxXZgU5vtYBVjVlxzP5vP/UzSd57jCvC6qB9QY wnW7RUHe1MmvyANTL3zgrIrab9nIWp9DhoBpbeeL8c1uF0/9Oh6Q2wRSBgXSpcue+JVq rUzEGcNR0RVhA+BN4ZOd2DlPj1XYJBw4WnotnP3QgboCwWf3osqEqMWixUmN//eQx9jr O2rDmJeaQcpGM0+aP2JGf5YVs1XKpozN2vXj1EKQ/icvgRL1sGoWkuX8qm/EsMHDe0pj U9PuDoDRtQLLQGx/aXAe/1kKHlALYnkO3Qt2O/eQnn0ydTcdIUKdkDsP39r5Qg7MuYY/ CNbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EUVVGZ+sfFkAlbWcLcOfWelo8aSG6wtK/MR4mUfnNyc=; b=nb2heG8Pu38nJGfcpYzY/alWmPvIHzfwjgXUsqrOYL9TXplr/3mxF53s9a/HtAd73T 6doZAw0zb/dNIJfXdFS/W5G/v5DrNGEn/HDP6/N7PryMRRj606dFoW5mTLsmZRXu0P4t 6WwoPcGMtmGwxBwOn+EoGTQe0kG4gc6hcUSvxM61sVfOo0EcHpkRkAPZ1hoN+XhBrE+P g0OB2A3O51UzpsDQew1X+GB8nuetWbjkbe1vFhzayDaY8pOY4222hiwd1Nf7lIsQ8A/4 aAY8BwZUezuWK9yqNxIFnGYigEzRkZ+Ke9P5yMf8R+JSyV/4h9FxKgsyXKHNIVg/np1A BFUw== X-Gm-Message-State: AOAM531huu6T3zsK9adpwrfAy139Phm4CgTPNU3wN+chhnTAwutqr08p RxDUwcGw34so3s2u7diuC7NfJ+ukWi0= X-Google-Smtp-Source: ABdhPJwVOasBVfLeQeogd5ubt0z+Z2Jo+Med/RLmizMF6Vlq5T8zcqx5UBl7TcdJuZhVwz7vc9q5T1juSek= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:79c1:8a4c:5d17:670f]) (user=surenb job=sendgmr) by 2002:a81:6184:0:b0:2d6:c22c:2a66 with SMTP id v126-20020a816184000000b002d6c22c2a66mr16578937ywb.311.1645508432422; Mon, 21 Feb 2022 21:40:32 -0800 (PST) Date: Mon, 21 Feb 2022 21:40:24 -0800 In-Reply-To: <20220222054025.3412898-1-surenb@google.com> Message-Id: <20220222054025.3412898-2-surenb@google.com> Mime-Version: 1.0 References: <20220222054025.3412898-1-surenb@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH 2/3] mm: prevent vm_area_struct::anon_name refcount saturation From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: ccross@google.com, sumit.semwal@linaro.org, mhocko@suse.com, dave.hansen@intel.com, keescook@chromium.org, willy@infradead.org, kirill.shutemov@linux.intel.com, vbabka@suse.cz, hannes@cmpxchg.org, ebiederm@xmission.com, brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, chris.hyser@oracle.com, dave@stgolabs.net, pcc@google.com, caoxiaofeng@yulong.com, david@redhat.com, gorcunov@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=fpKWCGja; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3UHcUYgYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UHcUYgYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 34B08140005 X-Stat-Signature: w5d9oa6jdkqqyc8rysptzukg51stt3ae X-HE-Tag: 1645508433-849484 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: A deep process chain with many vmas could grow really high. kref refcounting interface used in anon_vma_name structure will detect a counter overflow when it reaches REFCOUNT_SATURATED value but will only generate a warning about broken refcounting. To ensure anon_vma_name refcount does not overflow, stop anon_vma_name sharing when the refcount reaches INT_MAX, which still leaves INT_MAX/2 values before the counter reaches REFCOUNT_SATURATED. This should provide enough headroom for raising the refcounts temporarily. Suggested-by: Michal Hocko Signed-off-by: Suren Baghdasaryan --- include/linux/mm_inline.h | 18 ++++++++++++++---- mm/madvise.c | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 70b619442d56..b189e2638843 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -156,15 +156,25 @@ static inline void anon_vma_name_get(struct anon_vma_name *anon_name) extern void anon_vma_name_put(struct anon_vma_name *anon_name); +static inline +struct anon_vma_name *anon_vma_name_reuse(struct anon_vma_name *anon_name) +{ + /* Prevent anon_name refcount saturation early on */ + if (kref_read(&anon_name->kref) < INT_MAX) { + anon_vma_name_get(anon_name); + return anon_name; + + } + return anon_vma_name_alloc(anon_name->name); +} + static inline void dup_vma_anon_name(struct vm_area_struct *orig_vma, struct vm_area_struct *new_vma) { struct anon_vma_name *anon_name = vma_anon_name(orig_vma); - if (anon_name) { - anon_vma_name_get(anon_name); - new_vma->anon_name = anon_name; - } + if (anon_name) + new_vma->anon_name = anon_vma_name_reuse(anon_name); } static inline void free_vma_anon_name(struct vm_area_struct *vma) diff --git a/mm/madvise.c b/mm/madvise.c index f81d62d8ce9b..a395884aeecb 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -122,8 +122,7 @@ static int replace_vma_anon_name(struct vm_area_struct *vma, if (anon_vma_name_eq(orig_name, anon_name)) return 0; - anon_vma_name_get(anon_name); - vma->anon_name = anon_name; + vma->anon_name = anon_vma_name_reuse(anon_name); anon_vma_name_put(orig_name); return 0;