Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6342741ybx; Mon, 11 Nov 2019 07:42:51 -0800 (PST) X-Google-Smtp-Source: APXvYqziMeqOxCZony0KSp/zIfWB4pi6axQYXn3GhtkLuWG6+iZf8Wh0lsTHZh5vdc+CA4EtWVAo X-Received: by 2002:a05:6402:7cd:: with SMTP id u13mr26666225edy.246.1573486971071; Mon, 11 Nov 2019 07:42:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573486971; cv=none; d=google.com; s=arc-20160816; b=kRNooVDm8LyUzJXZ3Tt4n4jsuABARnZN8PBDFGj6WaPxN0HZXF9RRSZdEmiUh6YzKq VgGSRVu+CYORCp0Ikm0YXUeuC/mKDHdM9Fgbwu8k7Lg7bsuA8mWzWs0Rs7Mk1dKAAIzH 4q9T8WdSgtFzDbh3UjW4iTkB4LWYujxEo7Z0ElAAHl5CFzZ7M6dHE6YukvXRVydtADTe 1a5+oEfvvd9r4CG8JxUsouUqZyAay0zmda/yYpJQTS0KB6hfQH8BO8O3U1bPayX6QzC+ MVKAv+gayADa9+fHAb9HAxYc10J3xHa09Nh/ePWDl3Jaos258TY64uV673dsLRCEPEnP 9fVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:organization:in-reply-to :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=WE2qSoaU6rMv77NUdEk+fYAcqsHv/qRqfECvV2K4njU=; b=dSMhdUtYBSTR/Y3bFY0jXRPWPEMEp3FcMI15fPYTVLD8cCTrq9czkEMoU9y3oSfpjP t6igcXSPIKbQoIE48O6WJzbj3U3m/0A0WXB8kjsK1MFbs0uk07YFx4lyt36tX4u/wQ/W lFbuSYdxortp3v4vFMBLgsySc1GTrCh+ADzgUUwUFCedrCvi9MZe/hWM8/z+vxIQYWr7 qGEBXwbclMCkOA05gxcvrq0vNgX8G0v4weLr6z3EVp2/Logqh3lbJiDinKhbnVBNUhlj udFSMctbpsnXi9+G9sRJMuSMV0WcHeHpgPDU1sA/5gvykZ+8uJTknTXifij5SI5dzYZ6 AAog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GLDB6akg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n27si10864572ejl.272.2019.11.11.07.42.26; Mon, 11 Nov 2019 07:42:51 -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=@redhat.com header.s=mimecast20190719 header.b=GLDB6akg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727059AbfKKPkl (ORCPT + 99 others); Mon, 11 Nov 2019 10:40:41 -0500 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:34153 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726845AbfKKPkk (ORCPT ); Mon, 11 Nov 2019 10:40:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573486840; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WE2qSoaU6rMv77NUdEk+fYAcqsHv/qRqfECvV2K4njU=; b=GLDB6akgbe16cbkcoKbuYnH4Es9+CnrzaoDoT3Tq2+AVu/ntTqE9lbVRn8zmJ28+fLiShS 0MOhlAB7F2tmlRt7hkYfpq0NuzkXwYB8NeowOr0xZJEMXcmqqH5P0LQLz7CC4KgDN5N6x2 qFaCFkjbwGWyu6y+PfCz0raK3RrKm90= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-231-lTx-VP4UPU2sPnhSdZ3KzQ-1; Mon, 11 Nov 2019 10:40:37 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A2E9ADB60; Mon, 11 Nov 2019 15:40:35 +0000 (UTC) Received: from dcbz.redhat.com (ovpn-116-65.ams2.redhat.com [10.36.116.65]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 88B9449AF; Mon, 11 Nov 2019 15:40:30 +0000 (UTC) Date: Mon, 11 Nov 2019 16:40:28 +0100 From: Adrian Reber To: Oleg Nesterov Cc: Christian Brauner , Eric Biederman , Pavel Emelyanov , Jann Horn , Dmitry Safonov <0x7f454c46@gmail.com>, linux-kernel@vger.kernel.org, Andrei Vagin , Mike Rapoport , Radostin Stoyanov Subject: Re: [PATCH v7 1/2] fork: extend clone3() to support setting a PID Message-ID: <20191111154028.GF514519@dcbz.redhat.com> References: <20191111131704.656169-1-areber@redhat.com> <20191111152514.GA11389@redhat.com> MIME-Version: 1.0 In-Reply-To: <20191111152514.GA11389@redhat.com> X-Operating-System: Linux (5.3.8-300.fc31.x86_64) X-Load-Average: 1.08 1.12 1.23 X-Unexpected: The Spanish Inquisition X-GnuPG-Key: gpg --recv-keys D3C4906A Organization: Red Hat User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: lTx-VP4UPU2sPnhSdZ3KzQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 11, 2019 at 04:25:15PM +0100, Oleg Nesterov wrote: > On 11/11, Adrian Reber wrote: > > > > v7: > > - changed set_tid to be an array to set the PID of a process > > in multiple nested PID namespaces at the same time as discussed > > at LPC 2019 (container MC) >=20 > cough... iirc you convinced me this is not needed when we discussed > the previous version ;) Nevermind, probably my memory fools me. You are right. You suggested the same thing and we didn't listen ;) > So far I only have some cosmetic nits, Thanks for the quick review. I will try to apply your suggestions. > > @@ -175,6 +187,18 @@ struct pid *alloc_pid(struct pid_namespace *ns) > > > > =09for (i =3D ns->level; i >=3D 0; i--) { > > =09=09int pid_min =3D 1; > > +=09=09int t_pos =3D 0; > ^^^^^ >=20 > I won't insist, but I'd suggest to cache set_tid[t_pos] instead to make > the code a bit more simple. >=20 > > @@ -186,12 +210,24 @@ struct pid *alloc_pid(struct pid_namespace *ns) > > =09=09if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) > > =09=09=09pid_min =3D RESERVED_PIDS; >=20 > You can probably move this code into the "else" branch below. >=20 > IOW, something like >=20 >=20 > =09for (i =3D ns->level; i >=3D 0; i--) { > =09=09int xxx =3D 0; >=20 > =09=09if (set_tid_size) { > =09=09=09int pos =3D ns->level - i; >=20 > =09=09=09xxx =3D set_tid[pos]; > =09=09=09if (xxx < 1 || xxx >=3D pid_max) > =09=09=09=09return ERR_PTR(-EINVAL); > =09=09=09/* Also fail if a PID !=3D 1 is requested and no PID 1 exists */ > =09=09=09if (xxx !=3D 1 && !tmp->child_reaper) > =09=09=09=09return ERR_PTR(-EINVAL); > =09=09=09if (!ns_capable(tmp->user_ns, CAP_SYS_ADMIN)) > =09=09=09=09return ERR_PTR(-EPERM); > =09=09=09set_tid_size--; > =09=09} >=20 > =09=09idr_preload(GFP_KERNEL); > =09=09spin_lock_irq(&pidmap_lock); >=20 > =09=09if (xxx) { > =09=09=09nr =3D idr_alloc(&tmp->idr, NULL, xxx, xxx + 1, > =09=09=09=09=09GFP_ATOMIC); > =09=09=09/* > =09=09=09 * If ENOSPC is returned it means that the PID is > =09=09=09 * alreay in use. Return EEXIST in that case. > =09=09=09 */ > =09=09=09if (nr =3D=3D -ENOSPC) > =09=09=09=09nr =3D -EEXIST; > =09=09} else { > =09=09=09int pid_min =3D 1; > =09=09=09/* > =09=09=09 * init really needs pid 1, but after reaching the > =09=09=09 * maximum wrap back to RESERVED_PIDS > =09=09=09 */ > =09=09=09if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) > =09=09=09=09pid_min =3D RESERVED_PIDS; > =09=09=09/* > =09=09=09 * Store a null pointer so find_pid_ns does not find > =09=09=09 * a partially initialized PID (see below). > =09=09=09 */ > =09=09=09nr =3D idr_alloc_cyclic(&tmp->idr, NULL, pid_min, > =09=09=09=09=09 pid_max, GFP_ATOMIC); > =09=09} >=20 > =09=09... >=20 > This way only the "if (set_tid_size)" block has to play with set_tid_size= /set_tid. >=20 > note also that this way we can easily allow set_tid[some_level] =3D=3D 0,= we can > simply do >=20 > =09-=09if (xxx < 1 || xxx >=3D pid_max) > =09+=09if (xxx < 0 || xxx >=3D pid_max) >=20 > although I don't think this is really useful. Yes. I explicitly didn't allow 0 as a PID as I didn't thought it would be useful (or maybe even valid). =09=09Adrian