Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751114AbdFTGVH (ORCPT ); Tue, 20 Jun 2017 02:21:07 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:53080 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751007AbdFTGVG (ORCPT ); Tue, 20 Jun 2017 02:21:06 -0400 From: Mike Rapoport To: Andrew Morton Cc: Andrea Arcangeli , Hugh Dickins , "Kirill A. Shutemov" , Hillf Danton , Pavel Emelyanov , linux mm , lkml , Mike Rapoport Subject: [PATCH 1/7] shmem: shmem_charge: verify max_block is not exceeded before inode update Date: Tue, 20 Jun 2017 09:20:46 +0300 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1497939652-16528-1-git-send-email-rppt@linux.vnet.ibm.com> References: <1497939652-16528-1-git-send-email-rppt@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17062006-0040-0000-0000-000003CE1B1C X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17062006-0041-0000-0000-00002067B920 Message-Id: <1497939652-16528-2-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-06-20_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1706200115 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1712 Lines: 56 Currently we update inode and shmem_inode_info before verifying that used_blocks will not exceed max_blocks. In case it will, we undo the update. Let's switch the order and move the verification of the blocks count before the inode and shmem_inode_info update. Signed-off-by: Mike Rapoport --- mm/shmem.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e67d6ba..40a43ae 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -265,6 +265,14 @@ bool shmem_charge(struct inode *inode, long pages) 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); + } + spin_lock_irqsave(&info->lock, flags); info->alloced += pages; inode->i_blocks += pages * BLOCKS_PER_PAGE; @@ -272,20 +280,11 @@ bool shmem_charge(struct inode *inode, long pages) spin_unlock_irqrestore(&info->lock, flags); inode->i_mapping->nrpages += pages; - if (!sbinfo->max_blocks) - return true; - if (percpu_counter_compare(&sbinfo->used_blocks, - sbinfo->max_blocks - pages) > 0) { - inode->i_mapping->nrpages -= pages; - spin_lock_irqsave(&info->lock, flags); - info->alloced -= pages; - shmem_recalc_inode(inode); - spin_unlock_irqrestore(&info->lock, flags); - shmem_unacct_blocks(info->flags, pages); - return false; - } - percpu_counter_add(&sbinfo->used_blocks, pages); return true; + +unacct: + shmem_unacct_blocks(info->flags, pages); + return false; } void shmem_uncharge(struct inode *inode, long pages) -- 2.7.4