From patchwork Wed Jan 10 08:46:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 186719 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp660018dyi; Wed, 10 Jan 2024 00:47:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuh3OCz2mrmrG68jXxg8EVuZZU8gyLcCbW5avw3W/pIvaMZvQMI4Ita1w+GpRJEGtP2W+i X-Received: by 2002:a05:6358:7215:b0:175:73aa:c4dc with SMTP id h21-20020a056358721500b0017573aac4dcmr758394rwa.64.1704876427409; Wed, 10 Jan 2024 00:47:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704876427; cv=none; d=google.com; s=arc-20160816; b=AcROBj2RReyqh6y4WWLZ5l3uKJCY0mD2tI5QfXoxW+ZQV4hVwi39Ipf3U/BcSqzWwq WU2gx3vveXpq3nd+tNs1iOGgd1vulXFEc0GoHbUVcsrbRLD7WEEernp035JtVdcx1IwH esNwsF9MTtubJMpnb9Z1ud0bUD2ikuu4qJ7GXjacXKRCZsFPOoI5wblnOF0v/wFYQmUt kFuBfiNNRTPbgny8D7h1fO1fVzdngDdt77wU7otLTZpXSrQKpvuRHpgHr9HWMe+hqtrJ T+t6uR8ZMDomIGFgQc1xRR/PTxJtlLxGWdZlfQFt7UeVSWtZkkhvDe9VL9CIGPdJiW12 khBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=HORRhNn8cQhlCzoO7/tuR5hnshhn8S+KOZbQ0BIusdU=; fh=KPJjYJHLC+uLVMTRQa7GdG2CYCH/otyIF0MYaE50dhE=; b=jUCCH6MYLmM+Sd6MOZq5k0bDI+2omiuJqeRWrbUK7fSF2/Fl6hzcd5WZP2H6jtqvbE ODtMsnhWe8yvPLeKp0i/tuhy+/TmSkFMr2tj9+lkrCl+IMyDqRbaLyHZCySBJ4MJ4YXy YY6Jj1iRrtpsCqSueqcciVYSuVuWWz+wT5fiOAFbmA9DbMyqoiLIavVC8m5blhaze5sM CjIoCw+Ebye1roglNOXMXep+i3GQPH8Juziw8LO2FiEkzz6iB18AaMRF5F6gTrkhU5K4 3Wp2s1E6rwT/rUOCJz6eDrfjK9vMbQduegj70OpUbTmUgI27zRwRrqPx5QHPZ6w0260N LfzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=QATBXoTT; spf=pass (google.com: domain of linux-kernel+bounces-21859-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21859-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c13-20020a65420d000000b005cd8cb67fe9si3201026pgq.583.2024.01.10.00.47.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 00:47:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21859-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=QATBXoTT; spf=pass (google.com: domain of linux-kernel+bounces-21859-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21859-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 4350FB25E10 for ; Wed, 10 Jan 2024 08:47:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 37CA43B189; Wed, 10 Jan 2024 08:46:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="QATBXoTT" Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9702039859 for ; Wed, 10 Jan 2024 08:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1704876396; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HORRhNn8cQhlCzoO7/tuR5hnshhn8S+KOZbQ0BIusdU=; b=QATBXoTTJxGOORB8isMLBsltDJ0gIKHF/Qn2rl6uVul7pIKSixCFQYSo0rcgNV0arMm2Vv p4nfI6Y7vOFIZJGhBsDy6t3uFuZ+bWNOpM+3+LaP1C4Fm+5MvlL+lSeHvvTAmrXRp1JwO9 E8Fw4yUlgMQZfmTD38KQfXtwXh6URNc= From: Yajun Deng To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yajun Deng Subject: [PATCH] mm/mmap: simplify vma link and unlink Date: Wed, 10 Jan 2024 16:46:22 +0800 Message-Id: <20240110084622.2425927-1-yajun.deng@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787692504803658441 X-GMAIL-MSGID: 1787692504803658441 The file parameter in the __remove_shared_vm_struct is no longer used, remove it. These functions vma_link() and mmap_region() have some of the same code, introduce vma_link_file() helper function to simplify the code. Signed-off-by: Yajun Deng --- mm/mmap.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index b78e83d351d2..06f1f3e88598 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -105,7 +105,7 @@ void vma_set_page_prot(struct vm_area_struct *vma) * Requires inode->i_mapping->i_mmap_rwsem */ static void __remove_shared_vm_struct(struct vm_area_struct *vma, - struct file *file, struct address_space *mapping) + struct address_space *mapping) { if (vma_is_shared_maywrite(vma)) mapping_unmap_writable(mapping); @@ -126,7 +126,7 @@ void unlink_file_vma(struct vm_area_struct *vma) if (file) { struct address_space *mapping = file->f_mapping; i_mmap_lock_write(mapping); - __remove_shared_vm_struct(vma, file, mapping); + __remove_shared_vm_struct(vma, mapping); i_mmap_unlock_write(mapping); } } @@ -392,26 +392,30 @@ static void __vma_link_file(struct vm_area_struct *vma, flush_dcache_mmap_unlock(mapping); } +static void vma_link_file(struct vm_area_struct *vma) +{ + struct file *file = vma->vm_file; + struct address_space *mapping; + + if (file) { + mapping = file->f_mapping; + i_mmap_lock_write(mapping); + __vma_link_file(vma, mapping); + i_mmap_unlock_write(mapping); + } +} + static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) { VMA_ITERATOR(vmi, mm, 0); - struct address_space *mapping = NULL; vma_iter_config(&vmi, vma->vm_start, vma->vm_end); if (vma_iter_prealloc(&vmi, vma)) return -ENOMEM; vma_start_write(vma); - vma_iter_store(&vmi, vma); - - if (vma->vm_file) { - mapping = vma->vm_file->f_mapping; - i_mmap_lock_write(mapping); - __vma_link_file(vma, mapping); - i_mmap_unlock_write(mapping); - } - + vma_link_file(vma); mm->map_count++; validate_mm(mm); return 0; @@ -519,10 +523,9 @@ static inline void vma_complete(struct vma_prepare *vp, } if (vp->remove && vp->file) { - __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); + __remove_shared_vm_struct(vp->remove, vp->mapping); if (vp->remove2) - __remove_shared_vm_struct(vp->remove2, vp->file, - vp->mapping); + __remove_shared_vm_struct(vp->remove2, vp->mapping); } else if (vp->insert) { /* * split_vma has split insert from vma, and needs @@ -2889,16 +2892,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_start_write(vma); vma_iter_store(&vmi, vma); mm->map_count++; - if (vma->vm_file) { - i_mmap_lock_write(vma->vm_file->f_mapping); - if (vma_is_shared_maywrite(vma)) - mapping_allow_writable(vma->vm_file->f_mapping); - - flush_dcache_mmap_lock(vma->vm_file->f_mapping); - vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); - flush_dcache_mmap_unlock(vma->vm_file->f_mapping); - i_mmap_unlock_write(vma->vm_file->f_mapping); - } + vma_link_file(vma); /* * vma_merge() calls khugepaged_enter_vma() either, the below