From patchwork Mon Jul 19 08:34:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiyu Yang X-Patchwork-Id: 12385015 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=-17.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM, 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 F0342C12002 for ; Mon, 19 Jul 2021 08:34:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7F927611AC for ; Mon, 19 Jul 2021 08:34:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7F927611AC Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=fudan.edu.cn Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 182B78D00F4; Mon, 19 Jul 2021 04:34:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1335B8D00EC; Mon, 19 Jul 2021 04:34:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F166E8D00F4; Mon, 19 Jul 2021 04:34:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0178.hostedemail.com [216.40.44.178]) by kanga.kvack.org (Postfix) with ESMTP id CEAB78D00EC for ; Mon, 19 Jul 2021 04:34:46 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 654658249980 for ; Mon, 19 Jul 2021 08:34:45 +0000 (UTC) X-FDA: 78378676530.14.9A791F8 Received: from zg8tmty1ljiyny4xntqumjca.icoremail.net (zg8tmty1ljiyny4xntqumjca.icoremail.net [165.227.154.27]) by imf08.hostedemail.com (Postfix) with SMTP id 7FAB830000B0 for ; Mon, 19 Jul 2021 08:34:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fudan.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-Id; bh=1SQApd3rlDH29Adif2j2lwPetaF+csgR0aAySS4DMfU=; b=o fOwBaFI8bsZNCAMpgF2RWYK6yn5N94V+9QKDkRNbkU4NyLm+RBBM6UdF9s0U4h+D 95hzba3PcOd8Qsr1Eq+jTzvICNAXXXcmDZAv/8HWV+KWSbGMMGReJncWf84T40Na EKko9ksyhzumZXYiRJDGeFyCAce96kkU5SMLIZznSI= Received: from localhost.localdomain (unknown [10.162.86.133]) by app1 (Coremail) with SMTP id XAUFCgC3mWoZOfVgjxqKAA--.3096S3; Mon, 19 Jul 2021 16:34:33 +0800 (CST) From: Xiyu Yang To: Andrew Morton , Feng Tang , Mike Kravetz , Ben Widawsky , Muchun Song , Xiyu Yang , Yanfei Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: yuanxzhang@fudan.edu.cn, Xin Tan Subject: [PATCH] mm/mempolicy: Convert from atomic_t to refcount_t on mempolicy->refcnt Date: Mon, 19 Jul 2021 16:34:28 +0800 Message-Id: <1626683671-64407-1-git-send-email-xiyuyang19@fudan.edu.cn> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: XAUFCgC3mWoZOfVgjxqKAA--.3096S3 X-Coremail-Antispam: 1UD129KBjvJXoWxAFWkXrykZF48Gr48KFW3KFg_yoW5XF47pF s7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ication-Results: imf08.hostedemail.com; dkim=pass header.d=fudan.edu.cn header.s=dkim header.b="o fOwBaF"; spf=pass (imf08.hostedemail.com: domain of xiyuyang19@fudan.edu.cn designates 165.227.154.27 as permitted sender) smtp.mailfrom=xiyuyang19@fudan.edu.cn; dmarc=pass (policy=reject) header.from=fudan.edu.cn X-Rspamd-Server: rspam05 X-Stat-Signature: wurdix553qqcfk5pwbgfarssun3jpnto X-Rspamd-Queue-Id: 7FAB830000B0 X-HE-Tag: 1626683682-314970 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 and corresponding API can protect refcounters from accidental underflow and overflow and further use-after-free situations. Signed-off-by: Xiyu Yang Signed-off-by: Xin Tan Acked-by: Ben Widawsky Reviewed-by: Muchun Song --- include/linux/mempolicy.h | 5 +++-- mm/mempolicy.c | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 0aaf91b496e2..faec94994eb7 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -6,6 +6,7 @@ #ifndef _LINUX_MEMPOLICY_H #define _LINUX_MEMPOLICY_H 1 +#include #include #include #include @@ -43,7 +44,7 @@ struct mm_struct; * to 1, representing the caller of mpol_dup(). */ struct mempolicy { - atomic_t refcnt; + refcount_t refcnt; unsigned short mode; /* See MPOL_* above */ unsigned short flags; /* See set_mempolicy() MPOL_F_* above */ nodemask_t nodes; /* interleave/bind/perfer */ @@ -94,7 +95,7 @@ static inline struct mempolicy *mpol_dup(struct mempolicy *pol) static inline void mpol_get(struct mempolicy *pol) { if (pol) - atomic_inc(&pol->refcnt); + refcount_inc(&pol->refcnt); } extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b); diff --git a/mm/mempolicy.c b/mm/mempolicy.c index e32360e90274..829a14f34b43 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -298,7 +298,7 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); if (!policy) return ERR_PTR(-ENOMEM); - atomic_set(&policy->refcnt, 1); + refcount_set(&policy->refcnt, 1); policy->mode = mode; policy->flags = flags; @@ -308,7 +308,7 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, /* Slow path of a mpol destructor. */ void __mpol_put(struct mempolicy *p) { - if (!atomic_dec_and_test(&p->refcnt)) + if (!refcount_dec_and_test(&p->refcnt)) return; kmem_cache_free(policy_cache, p); } @@ -2290,7 +2290,7 @@ struct mempolicy *__mpol_dup(struct mempolicy *old) nodemask_t mems = cpuset_mems_allowed(current); mpol_rebind_policy(new, &mems); } - atomic_set(&new->refcnt, 1); + refcount_set(&new->refcnt, 1); return new; } @@ -2581,7 +2581,7 @@ static int shared_policy_replace(struct shared_policy *sp, unsigned long start, goto alloc_new; *mpol_new = *n->policy; - atomic_set(&mpol_new->refcnt, 1); + refcount_set(&mpol_new->refcnt, 1); sp_node_init(n_new, end, n->end, mpol_new); n->end = start; sp_insert(sp, n_new);