Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp613642yba; Thu, 18 Apr 2019 06:52:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqwVsHWO5kcBkXHvuNJXMZU1WIOWxwb4egTnb4mgLqb5i9TKIdj16V4fGBhDLYkXks2UX7RZ X-Received: by 2002:a65:47c6:: with SMTP id f6mr88408349pgs.173.1555595549568; Thu, 18 Apr 2019 06:52:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555595549; cv=none; d=google.com; s=arc-20160816; b=dbQT88WKAO2nQbBDMpP52Onh++giisBm3Ayj7Dnu7zw82G8Zi4DwyjxCtcA4Vvgisi F3b3TVVhcWk2S1vJEM3J18rYZJPOMkHU0TI9/EUn85d71SXZPM3DXkP7juCG5KuIzQi3 HORTJL74iURO+AS2TG75DRP0hjws5gvn347XbFvxA9OEYVtmRgn/i4Lht2Vr80k5UIgJ SdUWbY5vUqCry1efmRo2Q8eNFGDbhSGw8ZTin/WuFJva/gE5cWxGX0HeIe2HLxm903rm XgAtwCUUEVH6bVWiV3sh9HvrQLvdNvu7rTmTEzmOEtnKnJJaFdWVT97sgnjEdTzrX0yc Omrg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=j13ppKnA5XOevfzHj/veY7J+stZTddhKz4lu9/wYA5o=; b=GVNboHZ9v0VHF2Ge23UfvGP5gI9T1aU5E9VqT8rRxZauqES4/FPlvasgLWjzCwblJC XYRe5kDCjXSbZzS3WfnS+lgE3RzOBa46SgkSWB2qPctGcOK6izHhoYIi0L7X6A4hYvJe NgoRcVm7IotQa5u4bc5k7uiqBeygPV2ZjYUG3IpdQYcfTgGenJwXu2wZKmCZuaf+3SoD 3Dixbct/xXSkiaqiACmyGXfRV5YSi05C4Hv3BSTTXb89YNnNJf84GRlw2w6E9QbfqLLh 4HazVGgtK1N1JFUcsyryDjSwBdPVqEJuqggPn9celNZLGWHMeOUlmT75BUcCvtDJkmLt 2mLg== ARC-Authentication-Results: i=1; mx.google.com; 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 h29si2440217pfd.180.2019.04.18.06.52.14; Thu, 18 Apr 2019 06:52:29 -0700 (PDT) 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; 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 S2389103AbfDRNu4 (ORCPT + 99 others); Thu, 18 Apr 2019 09:50:56 -0400 Received: from mx2.suse.de ([195.135.220.15]:46238 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731317AbfDRNuz (ORCPT ); Thu, 18 Apr 2019 09:50:55 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id A41E2B6A0; Thu, 18 Apr 2019 13:50:54 +0000 (UTC) From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: Cyrill Gorcunov Cc: mhocko@kernel.org, akpm@linux-foundation.org, arunks@codeaurora.org, brgl@bgdev.pl, geert+renesas@glider.be, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mguzik@redhat.com, mkoutny@suse.com, rppt@linux.ibm.com, vbabka@suse.cz, Laurent Dufour Subject: [PATCH] prctl_set_mm: downgrade mmap_sem to read lock Date: Thu, 18 Apr 2019 15:50:39 +0200 Message-Id: <20190418135039.19987-1-mkoutny@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190417145548.GN5878@dhcp22.suse.cz> References: <20190417145548.GN5878@dhcp22.suse.cz> 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 I learnt, it's, alas, too late to drop the non PRCTL_SET_MM_MAP calls [1], so at least downgrade the write acquisition of mmap_sem as in the patch below (that should be stacked on the previous one or squashed). Cyrill, you mentioned lock changes in [1] but the link seems empty. Is it supposed to be [2]? That could be an alternative to this patch after some refreshments and clarifications. [1] https://lore.kernel.org/lkml/20190417165632.GC3040@uranus.lan/ [2] https://lore.kernel.org/lkml/20180507075606.870903028@gmail.com/ ======== Since commit 88aa7cc688d4 ("mm: introduce arg_lock to protect arg_start|end and env_start|end in mm_struct") we use arg_lock for boundaries modifications. Synchronize prctl_set_mm with this lock and keep mmap_sem for reading only (analogous to what we already do in prctl_set_mm_map). Also, save few cycles by looking up VMA only after performing basic arguments validation. Signed-off-by: Michal Koutný --- kernel/sys.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/sys.c b/kernel/sys.c index 12df0e5434b8..bbce0f26d707 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2125,8 +2125,12 @@ static int prctl_set_mm(int opt, unsigned long addr, error = -EINVAL; - down_write(&mm->mmap_sem); - vma = find_vma(mm, addr); + /* + * arg_lock protects concurent updates of arg boundaries, we need mmap_sem for + * a) concurrent sys_brk, b) finding VMA for addr validation. + */ + down_read(&mm->mmap_sem); + spin_lock(&mm->arg_lock); prctl_map.start_code = mm->start_code; prctl_map.end_code = mm->end_code; @@ -2185,6 +2189,7 @@ static int prctl_set_mm(int opt, unsigned long addr, if (error) goto out; + vma = find_vma(mm, addr); switch (opt) { /* * If command line arguments and environment @@ -2218,7 +2223,8 @@ static int prctl_set_mm(int opt, unsigned long addr, error = 0; out: - up_write(&mm->mmap_sem); + spin_unlock(&mm->arg_lock); + up_read(&mm->mmap_sem); return error; } -- 2.16.4