Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2178996pxj; Thu, 20 May 2021 01:49:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycvN/f6hQzZD2hXbT7BzRSAnALsorPK+n/8WQots5tJWIlXPDRU/UeFg+CQvOsdceJsg2s X-Received: by 2002:a02:cac6:: with SMTP id f6mr4703685jap.142.1621500566471; Thu, 20 May 2021 01:49:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621500566; cv=none; d=google.com; s=arc-20160816; b=LQOTtu161UgCNy/CpkK02/kx3E3kb2pEqs9nl6LWiLZGYgjLYZkpX7AWW9Wb7heaHl veqiUZqEmixxRROrrdWLfAObB07gvTEbFQ4YquIqXBGMDVJL01EylX608LmEmAEhmna1 yP9lehsOjGnTQziEOjeLSGYcfKUjWe6lie/2polGozUWnItOPbYWhRLIxJdZZr+TKLOk tspXhITeEifJje6kANHEspl6ZMY5LOCnNP4cYEOMi6SiP1XLAKzSkLqNp7FalO7NPFF0 dCSINowE30BsDzFwQy6336B5yjNxhCBM4ASBdyxAidgHFioCgnBtj2vfTiv3imJPDDlG flRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:ironport-sdr:ironport-sdr; bh=42du0jBVFTaOe1Yb0th/fdtivui3aAX12I8uLPKJ9pU=; b=b0AIZsVMd2jHs4GwDkRZmr2W9efg/JFg3vhHXmn7Bh0YZb6/EUVQnv5MDbZoYs6o23 cXvzc80huOh3Fk18P1ICYkEOdLFw2u4loxLMVrsYoFSWGTddnI+3wZYIi5b3WGidesMa VjYTOhmz0f11Rp+q4pTT9zgg2hbQM1j7hp8L601Q5LRL6Q3PXJ80eVEqlB5QFgWFH2p9 CGl9BxoU2GxuFBH0SfIz0HNvNvjlfSb2s8nZPGYWBZb5d79G+UGlpg1XZ9I1nPtCBPa/ ktMUzDTfy+28MJ78JuL8toDElmP8SmX4mbugR1Up1fWbCVKUkw5g0qvtYpIkuA09G8W3 HzmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si2545098ilv.63.2021.05.20.01.49.09; Thu, 20 May 2021 01:49:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231246AbhETIcB (ORCPT + 99 others); Thu, 20 May 2021 04:32:01 -0400 Received: from mga11.intel.com ([192.55.52.93]:61362 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbhETIb6 (ORCPT ); Thu, 20 May 2021 04:31:58 -0400 IronPort-SDR: x7omwfNs6jVVXQranMsTPm+ZMFmO9Owf3CVkd024MWa9zknPM2pVfoRlrgbUkp1P5GTW/6My/B HIehGl6OCCjw== X-IronPort-AV: E=McAfee;i="6200,9189,9989"; a="198088177" X-IronPort-AV: E=Sophos;i="5.82,313,1613462400"; d="scan'208";a="198088177" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2021 01:30:19 -0700 IronPort-SDR: d/xtzwRVWrQjMJ24low5iOQu84fAtGYY0Mt7lk4Nz5Gyq5OK7ikeHhBhVMP0UNo1SSNsxw/lph koUNTsCRD74Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,313,1613462400"; d="scan'208";a="473899333" Received: from shbuild999.sh.intel.com ([10.239.147.94]) by orsmga001.jf.intel.com with ESMTP; 20 May 2021 01:30:15 -0700 From: Feng Tang To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Michal Hocko Cc: Andrea Arcangeli , David Rientjes , Mel Gorman , Mike Kravetz , Randy Dunlap , Vlastimil Babka , Dave Hansen , Ben Widawsky , Andi Kleen , Dan Williams , ying.huang@intel.com, Feng Tang Subject: [RFC Patch v2 2/4] mm/mempolicy: unify the preprocessing for mbind and set_mempolicy Date: Thu, 20 May 2021 16:30:02 +0800 Message-Id: <1621499404-67756-3-git-send-email-feng.tang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1621499404-67756-1-git-send-email-feng.tang@intel.com> References: <1621499404-67756-1-git-send-email-feng.tang@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently the kernel_mbind() and kernel_set_mempolicy() do almost the same operation for parameter sanity check and preprocessing. Add a macro to unify the code to reduce the redundancy, and make it easier for changing the pre-processing code in future. Signed-off-by: Feng Tang --- mm/mempolicy.c | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 1964cca..0f5bf60 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1460,25 +1460,29 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; } +#define MPOL_PRE_PROCESS() \ + \ + nodemask_t nodes; \ + int err; \ + unsigned short mode_flags; \ + mode_flags = mode & MPOL_MODE_FLAGS; \ + mode &= ~MPOL_MODE_FLAGS; \ + if (mode >= MPOL_MAX) \ + return -EINVAL; \ + if ((mode_flags & MPOL_F_STATIC_NODES) && \ + (mode_flags & MPOL_F_RELATIVE_NODES)) \ + return -EINVAL; \ + err = get_nodes(&nodes, nmask, maxnode); \ + if (err) \ + return err; + static long kernel_mbind(unsigned long start, unsigned long len, unsigned long mode, const unsigned long __user *nmask, unsigned long maxnode, unsigned int flags) { - nodemask_t nodes; - int err; - unsigned short mode_flags; + MPOL_PRE_PROCESS(); start = untagged_addr(start); - mode_flags = mode & MPOL_MODE_FLAGS; - mode &= ~MPOL_MODE_FLAGS; - if (mode >= MPOL_MAX) - return -EINVAL; - if ((mode_flags & MPOL_F_STATIC_NODES) && - (mode_flags & MPOL_F_RELATIVE_NODES)) - return -EINVAL; - err = get_nodes(&nodes, nmask, maxnode); - if (err) - return err; return do_mbind(start, len, mode, mode_flags, &nodes, flags); } @@ -1493,20 +1497,8 @@ SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len, static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask, unsigned long maxnode) { - int err; - nodemask_t nodes; - unsigned short flags; - - flags = mode & MPOL_MODE_FLAGS; - mode &= ~MPOL_MODE_FLAGS; - if ((unsigned int)mode >= MPOL_MAX) - return -EINVAL; - if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES)) - return -EINVAL; - err = get_nodes(&nodes, nmask, maxnode); - if (err) - return err; - return do_set_mempolicy(mode, flags, &nodes); + MPOL_PRE_PROCESS(); + return do_set_mempolicy(mode, mode_flags, &nodes); } SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask, -- 2.7.4