Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8109621imu; Tue, 4 Dec 2018 03:09:53 -0800 (PST) X-Google-Smtp-Source: AFSGD/XQtiN08zSqF29xlHMfwbFo1JIHzaQPo7PE8MjRp03kd568deHIliZRloww95tyZxgr34Ba X-Received: by 2002:a63:8d44:: with SMTP id z65mr16588995pgd.57.1543921793515; Tue, 04 Dec 2018 03:09:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543921793; cv=none; d=google.com; s=arc-20160816; b=rDNLzqYF8sC1748iUozXfv2+mbIPBHHurqX7BvkK3NWXhCsmIgAgqay91Wlem2zB6a Wo6uFCNXbGb0x/rHYdG/FEF2ayoFSCxJrbcETnuOpkdKtRB4Y79sSU09lD6hkfDRX6fI lvRiL40zAjjOfGXYfX8qVzP2CcUNiKGWHfR64hrrWVc0H89NJWmQZTAOivObi+j1JOrt NIVoCHKXjTLKnYKEHZmwPs1p3Fg2/yzlvo/C9Euz/c1Z1NVajSBCjuEICBWytdklZwf3 9nbs3qlGHqQoiF/SxULNfdvUnRv+QdKnpoQgQiC/zRPeSqRnMKQm6nNl4Viya4mGE99g yfxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VMv30cIoFcvVRzoKDhJSKi0d95ePI7T7ZORX2sokx7w=; b=PQhZ+bt2OrO6NZgOIlhjDlgSwaCfnk37ySjJL6pD+jEB7OZY9UkGvAifG8P2pMl89y iz95+B60BhoQrAnBjqLxxNFZSkQtm3TqcGC4r69gnE+I2NLK/lHsDMQpWD4OaiIku6W+ iOpFvtlc0XlCnP5FXMS56f65oW/lC4l29vtpK0ivCX2QbiGv/nkDUnMx3XrKGvF5Ib6j CaBI5DZ1OVi4ZbMQZDXf/0fNyfq7oawnmBjhnjz9Jix832PWN2N782NF1w9ySsZNNaC2 d29qx+qhyZ67YUq/E2cpE37uOC7XrQOEIBqQT3ZOpU0yKA6qnS5NsNCsunZg48GaECfR 55mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XfiwWAxy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n137si17995168pfd.216.2018.12.04.03.09.38; Tue, 04 Dec 2018 03:09:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XfiwWAxy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728737AbeLDLJH (ORCPT + 99 others); Tue, 4 Dec 2018 06:09:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:58752 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728725AbeLDLJE (ORCPT ); Tue, 4 Dec 2018 06:09:04 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 48749214E0; Tue, 4 Dec 2018 11:09:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921743; bh=7zTJffmiEQ2EAuUnQvFCV9mTNxtH1DopvUUFTj291vs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XfiwWAxyB1fto1ePIZba+4GhaWxz2uap3iMqAz7Gz414vHj6nDY+Km/2m8W+zKve6 dD0ZTbmWHw2jtIFqU4BtV3VM6JijEch7DZ2Dd7CV8k4zKIuwyEhqN2oAAM3m68T0z6 oUwb0nG/bSOfsd6FGM/YNb773JmSFvSuq9bZXhDs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Rapoport , "Kirill A. Shutemov" , Andrea Arcangeli , Hillf Danton , Hugh Dickins , Pavel Emelyanov , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 07/50] shmem: introduce shmem_inode_acct_block Date: Tue, 4 Dec 2018 11:50:02 +0100 Message-Id: <20181204103714.874823429@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103714.485546262@linuxfoundation.org> References: <20181204103714.485546262@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ commit 0f0796945614b7523987f7eea32407421af4b1ee upstream. The shmem_acct_block and the update of used_blocks are following one another in all the places they are used. Combine these two into a helper function. Link: http://lkml.kernel.org/r/1497939652-16528-3-git-send-email-rppt@linux.vnet.ibm.com Signed-off-by: Mike Rapoport Cc: "Kirill A. Shutemov" Cc: Andrea Arcangeli Cc: Hillf Danton Cc: Hugh Dickins Cc: Pavel Emelyanov Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/shmem.c | 82 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b26f11221ea8..e30ffaa065a4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -181,6 +181,38 @@ static inline void shmem_unacct_blocks(unsigned long flags, long pages) vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); } +static inline bool shmem_inode_acct_block(struct inode *inode, long pages) +{ + struct shmem_inode_info *info = SHMEM_I(inode); + struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + + if (shmem_acct_block(info->flags, pages)) + return false; + + if (sbinfo->max_blocks) { + if (percpu_counter_compare(&sbinfo->used_blocks, + sbinfo->max_blocks - pages) > 0) + goto unacct; + percpu_counter_add(&sbinfo->used_blocks, pages); + } + + return true; + +unacct: + shmem_unacct_blocks(info->flags, pages); + return false; +} + +static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) +{ + struct shmem_inode_info *info = SHMEM_I(inode); + struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + + if (sbinfo->max_blocks) + percpu_counter_sub(&sbinfo->used_blocks, pages); + shmem_unacct_blocks(info->flags, pages); +} + static const struct super_operations shmem_ops; static const struct address_space_operations shmem_aops; static const struct file_operations shmem_file_operations; @@ -237,31 +269,20 @@ static void shmem_recalc_inode(struct inode *inode) freed = info->alloced - info->swapped - inode->i_mapping->nrpages; if (freed > 0) { - struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); - if (sbinfo->max_blocks) - percpu_counter_add(&sbinfo->used_blocks, -freed); info->alloced -= freed; inode->i_blocks -= freed * BLOCKS_PER_PAGE; - shmem_unacct_blocks(info->flags, freed); + shmem_inode_unacct_blocks(inode, freed); } } bool shmem_charge(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); - struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); unsigned long flags; - if (shmem_acct_block(info->flags, pages)) + if (!shmem_inode_acct_block(inode, pages)) return false; - if (sbinfo->max_blocks) { - if (percpu_counter_compare(&sbinfo->used_blocks, - sbinfo->max_blocks - pages) > 0) - goto unacct; - percpu_counter_add(&sbinfo->used_blocks, pages); - } - spin_lock_irqsave(&info->lock, flags); info->alloced += pages; inode->i_blocks += pages * BLOCKS_PER_PAGE; @@ -270,16 +291,11 @@ bool shmem_charge(struct inode *inode, long pages) inode->i_mapping->nrpages += pages; return true; - -unacct: - shmem_unacct_blocks(info->flags, pages); - return false; } void shmem_uncharge(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); - struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); unsigned long flags; spin_lock_irqsave(&info->lock, flags); @@ -288,9 +304,7 @@ void shmem_uncharge(struct inode *inode, long pages) shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); - if (sbinfo->max_blocks) - percpu_counter_sub(&sbinfo->used_blocks, pages); - shmem_unacct_blocks(info->flags, pages); + shmem_inode_unacct_blocks(inode, pages); } /* @@ -1423,9 +1437,10 @@ static struct page *shmem_alloc_page(gfp_t gfp, } static struct page *shmem_alloc_and_acct_page(gfp_t gfp, - struct shmem_inode_info *info, struct shmem_sb_info *sbinfo, + struct inode *inode, pgoff_t index, bool huge) { + struct shmem_inode_info *info = SHMEM_I(inode); struct page *page; int nr; int err = -ENOSPC; @@ -1434,14 +1449,8 @@ static struct page *shmem_alloc_and_acct_page(gfp_t gfp, huge = false; nr = huge ? HPAGE_PMD_NR : 1; - if (shmem_acct_block(info->flags, nr)) + if (!shmem_inode_acct_block(inode, nr)) goto failed; - if (sbinfo->max_blocks) { - if (percpu_counter_compare(&sbinfo->used_blocks, - sbinfo->max_blocks - nr) > 0) - goto unacct; - percpu_counter_add(&sbinfo->used_blocks, nr); - } if (huge) page = shmem_alloc_hugepage(gfp, info, index); @@ -1454,10 +1463,7 @@ static struct page *shmem_alloc_and_acct_page(gfp_t gfp, } err = -ENOMEM; - if (sbinfo->max_blocks) - percpu_counter_add(&sbinfo->used_blocks, -nr); -unacct: - shmem_unacct_blocks(info->flags, nr); + shmem_inode_unacct_blocks(inode, nr); failed: return ERR_PTR(err); } @@ -1717,10 +1723,9 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, } alloc_huge: - page = shmem_alloc_and_acct_page(gfp, info, sbinfo, - index, true); + page = shmem_alloc_and_acct_page(gfp, inode, index, true); if (IS_ERR(page)) { -alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo, +alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, index, false); } if (IS_ERR(page)) { @@ -1842,10 +1847,7 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo, * Error recovery. */ unacct: - if (sbinfo->max_blocks) - percpu_counter_sub(&sbinfo->used_blocks, - 1 << compound_order(page)); - shmem_unacct_blocks(info->flags, 1 << compound_order(page)); + shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); if (PageTransHuge(page)) { unlock_page(page); -- 2.17.1