From patchwork Thu Mar 11 05:56:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yejune Deng X-Patchwork-Id: 12130329 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 C38E1C433E0 for ; Thu, 11 Mar 2021 05:56:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1BF9364FC4 for ; Thu, 11 Mar 2021 05:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1BF9364FC4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 743DF8D0271; Thu, 11 Mar 2021 00:56:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F31B8D0250; Thu, 11 Mar 2021 00:56:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56CE98D0271; Thu, 11 Mar 2021 00:56:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0022.hostedemail.com [216.40.44.22]) by kanga.kvack.org (Postfix) with ESMTP id 397B88D0250 for ; Thu, 11 Mar 2021 00:56:25 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id F20C88249980 for ; Thu, 11 Mar 2021 05:56:24 +0000 (UTC) X-FDA: 77906533488.22.DE904C0 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf27.hostedemail.com (Postfix) with ESMTP id C1D36801914D for ; Thu, 11 Mar 2021 05:56:20 +0000 (UTC) Received: by mail-pg1-f177.google.com with SMTP id 16so6554642pgo.13 for ; Wed, 10 Mar 2021 21:56:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=yJ6Pl8QgifadwfIXaa4NP4BZgjS0vadvYmpwZmuwkkk=; b=hwuzbPlcBfwq1+hstCB+gQzlay6IauvYpbDDnT09v2bz4UP4aBhG0JAQAA8/6Abbsx uhVPHYxk+ZwRrsuI6ctUxQsECDppJzeV+UwjtVTMBTBRtJsLmZGjD0X18VxvcBVe96// gZYVew1yux4yDHcp6vy9R4tDITPwFMCLvOFeu6qk2TRuELyuGc+Vk4eo88t6RMO1O70W 3IZbbv/hmqjGFPgtdSkN5+3470X8u4ljSRuyX8XpNfPUv9Auo50X+6YBa9r4sWGCpi8g SWvxP2cnd/WwiIjrVAlm2IZRVGLch917LAzvi0wLHXfgynHuf0zVC9Yl/rWVhlHph+5N zeNg== 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=yJ6Pl8QgifadwfIXaa4NP4BZgjS0vadvYmpwZmuwkkk=; b=P2DJpdvn1pa+Ubpojq2wuLLTckU0NOiGk5fBeM9Lc+d+Ee+KCvcxkRxID8jHAmPxDE 49N/T0wlRgZcAGJ6d7SBMNXvtOJ4j7g2FfYfplcabCLGaTOee7F4aWs/qt/MSnQtWPjl TqgX/GimwXPvahooPZtsozT3obA415+Fu636GZRw/bJrd915wsDSbXZA+XbHisGHBRhU afkk5k7DiJ3rw5dPkYjdtEOwr7JVqwbnmlzM7S5s3ZuHsF8Bbs0BLiWkeXODQXijnNU2 PA0EZtoRHNz0R4Ut64Xo5mrwIEJkuT7OHAsOCVGbAR1ncgDQP/TXExzsNADMO8Q+D5eM eyWg== X-Gm-Message-State: AOAM531ax9HQyN3Cod7tdPUMoyw4SFAuFvhkLHQjtcrdVrb/QIOBj6Am IkFAz3mA3Pm1zebVrLa3A/o= X-Google-Smtp-Source: ABdhPJy6Z2oKCwiM/qJEc5mJBJyjZNkVynLEh3xDp//sx94oaahUvVNpo6mGLukXWE52HUDRWETL2A== X-Received: by 2002:aa7:956d:0:b029:1f1:5ba6:2a58 with SMTP id x13-20020aa7956d0000b02901f15ba62a58mr6381996pfq.63.1615442183475; Wed, 10 Mar 2021 21:56:23 -0800 (PST) Received: from localhost.localdomain ([122.10.161.207]) by smtp.gmail.com with ESMTPSA id k10sm1138982pfk.49.2021.03.10.21.56.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Mar 2021 21:56:23 -0800 (PST) From: Yejune Deng To: akpm@linux-foundation.org, walken@google.com, hannes@cmpxchg.org, louhongxiang@huawei.com, linmiaohe@huawei.com, shakeelb@google.com, yejune.deng@gmail.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm/rmap: convert anon_vma.refcount from atomic_t to refcount_t Date: Thu, 11 Mar 2021 13:56:15 +0800 Message-Id: <20210311055615.4461-1-yejune.deng@gmail.com> X-Mailer: git-send-email 2.29.0 MIME-Version: 1.0 X-Stat-Signature: foz1uggbx1ueo54fpru8p5i1qegd97n8 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C1D36801914D Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf27; identity=mailfrom; envelope-from=""; helo=mail-pg1-f177.google.com; client-ip=209.85.215.177 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615442180-774925 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: refcount_t type should be used instead of atomic_t when the variable is used as a reference counter. This is because the implementation of refcount_t can prevent overflows and detect possible use-after-free. Signed-off-by: Yejune Deng --- include/linux/rmap.h | 7 ++++--- mm/rmap.c | 14 +++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index def5c62c93b3..0d6dfea7afa6 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -11,6 +11,7 @@ #include #include #include +#include /* * The anon_vma heads a list of private "related" vmas, to scan if @@ -36,7 +37,7 @@ struct anon_vma { * the reference is responsible for clearing up the * anon_vma if they are the last user on release */ - atomic_t refcount; + refcount_t refcount; /* * Count of child anon_vmas and VMAs which points to this anon_vma. @@ -103,14 +104,14 @@ enum ttu_flags { #ifdef CONFIG_MMU static inline void get_anon_vma(struct anon_vma *anon_vma) { - atomic_inc(&anon_vma->refcount); + refcount_inc(&anon_vma->refcount); } void __put_anon_vma(struct anon_vma *anon_vma); static inline void put_anon_vma(struct anon_vma *anon_vma) { - if (atomic_dec_and_test(&anon_vma->refcount)) + if (refcount_dec_and_test(&anon_vma->refcount)) __put_anon_vma(anon_vma); } diff --git a/mm/rmap.c b/mm/rmap.c index b0fc27e77d6d..6aea12f3ac5c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -88,7 +88,7 @@ static inline struct anon_vma *anon_vma_alloc(void) anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); if (anon_vma) { - atomic_set(&anon_vma->refcount, 1); + refcount_set(&anon_vma->refcount, 1); anon_vma->degree = 1; /* Reference for first vma */ anon_vma->parent = anon_vma; /* @@ -103,7 +103,7 @@ static inline struct anon_vma *anon_vma_alloc(void) static inline void anon_vma_free(struct anon_vma *anon_vma) { - VM_BUG_ON(atomic_read(&anon_vma->refcount)); + VM_BUG_ON(refcount_read(&anon_vma->refcount)); /* * Synchronize against page_lock_anon_vma_read() such that @@ -445,7 +445,7 @@ static void anon_vma_ctor(void *data) struct anon_vma *anon_vma = data; init_rwsem(&anon_vma->rwsem); - atomic_set(&anon_vma->refcount, 0); + refcount_set(&anon_vma->refcount, 0); anon_vma->rb_root = RB_ROOT_CACHED; } @@ -495,7 +495,7 @@ struct anon_vma *page_get_anon_vma(struct page *page) goto out; anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); - if (!atomic_inc_not_zero(&anon_vma->refcount)) { + if (!refcount_inc_not_zero(&anon_vma->refcount)) { anon_vma = NULL; goto out; } @@ -554,7 +554,7 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page) } /* trylock failed, we got to sleep */ - if (!atomic_inc_not_zero(&anon_vma->refcount)) { + if (!refcount_inc_not_zero(&anon_vma->refcount)) { anon_vma = NULL; goto out; } @@ -569,7 +569,7 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page) rcu_read_unlock(); anon_vma_lock_read(anon_vma); - if (atomic_dec_and_test(&anon_vma->refcount)) { + if (refcount_dec_and_test(&anon_vma->refcount)) { /* * Oops, we held the last refcount, release the lock * and bail -- can't simply use put_anon_vma() because @@ -1810,7 +1810,7 @@ void __put_anon_vma(struct anon_vma *anon_vma) struct anon_vma *root = anon_vma->root; anon_vma_free(anon_vma); - if (root != anon_vma && atomic_dec_and_test(&root->refcount)) + if (root != anon_vma && refcount_dec_and_test(&root->refcount)) anon_vma_free(root); }