Received: by 2002:ab2:23c8:0:b0:1f2:fdbc:cb93 with SMTP id a8csp169673lqe; Wed, 27 Mar 2024 02:09:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUEQaEYNBICMrTP0gaumgzOk2EHuq1As17n7xMdAgBP5poK6B2pIac7p0EdfJpAJ9T7EBIAKMVyAJPcUNng3sSLHxKhCwycex1FdimaTA== X-Google-Smtp-Source: AGHT+IHEtZDXqecjCL5s2jLzrbyId1kdwjV/BcQs6g2wXgqwjJw6j0+fWvpTK93atTptagTyNyYd X-Received: by 2002:a17:902:ba88:b0:1e0:b5d4:9f62 with SMTP id k8-20020a170902ba8800b001e0b5d49f62mr439593pls.58.1711530569159; Wed, 27 Mar 2024 02:09:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711530569; cv=pass; d=google.com; s=arc-20160816; b=EBWG0zr4pBtky2N2DSvB54UlmhnGdA20dKz29O4nZuPJZZC173/IUPqkve+sTG6urt NeNx5ASoWo1Ryd085WQPCuLi3M0O1309yVoZbzdSbVeewviTJJYb2KZvBR5+tE2UDC75 Ds2WcvsK9L3pddl0y77AkaSoxjsLxqlpAD6+hcP1BrUuP2yYXw5lVwsNg+m510NjRF37 JVE0s5x8Zvwi5O3pJ4hw+yRC5dOx3D2pQoY4JiqPvb+aj1Og5Y0+U2w4VouxB+kvSkcp 2RyyzWbqqtcOYBd3vV4lDFd1mof8Yq69ep5etGbR8zdk4GZ0mh6NMNwsG5t01EKsAI0R tE/Q== ARC-Message-Signature: i=2; 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; bh=XOJGcSTUE+NHqjyvKIF1rQ7JLjl43/BxOx6otz0ZCvo=; fh=/SyGMKohqcXJogy0RdUG6s/ekG47eULsGjMD9BYjVxI=; b=zSHBE+KsNW5pRiZ53N4UKuCmQJyt2DihH1K/1Yd46rSyEjSiWzu21AS3v4we9ARhyo pi9mdVx6jzX+LUe6j62a19zBLC69YiGm8ImFCRzkwtB0fb7kKi9NR2V3uNbzry1E2ULV l3qYU4uJ3q2hMd8wCJiyxG9drcKg6KS8PNeROE5AEQCezm60oGZzmwhmnczqYy+hhrJp vFqyyzNP28RphU3FKXKTF7KDgiLZ0Ag/9Kb/kpzasBvwLhcM9Ld2TMM55RGsE4BjpXFJ 5MJ0ELbNt3bmFYnL9SEfX1Bs5eRzCq8Wq9xsu46kXaiAqQHtDBQfcxK47dmyOsCNlWmT LZnA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-120589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120589-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id t18-20020a17090340d200b001e01f26868esi9118669pld.245.2024.03.27.02.09.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 02:09:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-120589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-120589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120589-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 36B192966F6 for ; Wed, 27 Mar 2024 09:08:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1287136B1C; Wed, 27 Mar 2024 09:08:52 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (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 7EF603611D for ; Wed, 27 Mar 2024 09:08:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711530531; cv=none; b=GPOr30RBkQMNLSmkLn5Sj+yrlCah8OOXMTiBlfkU0zWPVmUqqJQGWB528/5TSVs+2DS5B+KR38ZJ2x5Ga4dJGr0DtW2tye/+f5PrEzUcDBAfl/iXNCUw8yzXCMMyXK1n1/UhEN98NfRIV+hE2qfdAVscUpcLH1VK0RgauY7SYmU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711530531; c=relaxed/simple; bh=/toiUyRRKpHhrNq1RW8Qw0KewFXk6dt7DT2RMe/GA2U=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ECR/DUBm0ermn/fOUvDCyOt791bXeB5n+gGE/MGDlGCASRw0QiMYUKJ7vEBD3HtIttj1JqBGL0dyh7AxMmLlCXkRIEPTH1iVHQB4RYFhewur4Fsey811pPBuCVkSVlR36sR3ICQcLcEU2IIfWoniTjuulFKqHetGRIseBPj9rfA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4V4LQB0LQgz1xsqX; Wed, 27 Mar 2024 17:06:42 +0800 (CST) Received: from kwepemm600020.china.huawei.com (unknown [7.193.23.147]) by mail.maildlp.com (Postfix) with ESMTPS id 69422180062; Wed, 27 Mar 2024 17:08:42 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 27 Mar 2024 17:08:41 +0800 From: Peng Zhang To: , CC: , , , , , , , , Subject: [PATCH] userfaultfd: early return in dup_userfaultfd() Date: Wed, 27 Mar 2024 17:08:35 +0800 Message-ID: <20240327090835.3232629-1-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600020.china.huawei.com (7.193.23.147) From: ZhangPeng When vma->vm_userfaultfd_ctx.ctx is NULL, vma->vm_flags should have cleared __VM_UFFD_FLAGS. Therefore, there is no need to down_write or clear the flag, which will affect fork performance. Fix this by returning early if octx is NULL in dup_userfaultfd(). By applying this patch we can get a 1.3% performance improvement for lmbench fork_prot. Results are as follows: base early return Process fork+exit: 419.1106 413.4804 Signed-off-by: ZhangPeng --- fs/userfaultfd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 60dcfafdc11a..3e6ddda6f159 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -657,7 +657,10 @@ int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) struct userfaultfd_fork_ctx *fctx; octx = vma->vm_userfaultfd_ctx.ctx; - if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) { + if (!octx) + return 0; + + if (!(octx->features & UFFD_FEATURE_EVENT_FORK)) { vma_start_write(vma); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS); -- 2.25.1