From patchwork Fri Dec 2 17:16:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Foster X-Patchwork-Id: 13063002 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 415A9C4321E for ; Fri, 2 Dec 2022 17:16:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 970958E0002; Fri, 2 Dec 2022 12:16:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80DD38E0001; Fri, 2 Dec 2022 12:16:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63B038E0002; Fri, 2 Dec 2022 12:16:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 461378E0001 for ; Fri, 2 Dec 2022 12:16:22 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E7CDC1C6C34 for ; Fri, 2 Dec 2022 17:16:21 +0000 (UTC) X-FDA: 80198019762.20.8A52D7D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 8F1E480017 for ; Fri, 2 Dec 2022 17:16:21 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=APwBGI3E; spf=pass (imf02.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670001381; a=rsa-sha256; cv=none; b=IKw/720+v3G+igq5e2F4Ylaclx2s4yqC1TVMYVNdnffu10pdtcY3KGTUFWNU5pqVWrsRvU sU3aneps/wSuXr5jcYBYyuAmSNxqb12BmCAa0OPYoVoSwvwktunSJVH1i5LXGAoHERiGjC 8EhHM54nb6aYoDreZUEFjY6ln2tbQmU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=APwBGI3E; spf=pass (imf02.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670001381; h=from:from:sender: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:dkim-signature; bh=E+1/kxt1BcrGWlRNRbifBPUUK2viMqVoDBsQbTYSfG4=; b=O/JAPO1hSBAmxV9BX/lK55Zw2CHIOC3UfUg+vr5PyCBRhSBCqMU64MlI80jmPP014OZtOo 4KYytIN3fHRNBLSyzdFzQkd/f0v2+TO1BTos0T0e87WWpRWlWTXCOn71tEY+ajMKnQuebP GI/V+bvCTNunHse4FS43RRRkkDfDpAU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670001381; 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=E+1/kxt1BcrGWlRNRbifBPUUK2viMqVoDBsQbTYSfG4=; b=APwBGI3EpSM2YHc4BfSDksqF8KElryuI9aNPrThQNR2g3lMGcv0JGCmE0xF7bH8HhOD+7p Ftdk7qDSzgGgS5cW5Gu5mxDWKFVr6WYWKRZ4Y3QqT0/7MP0wNuSosU6fnbBiwPEmzlmzQ5 troSnadU8FDAwiqIDtBh8H9SPGGw3ws= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-210-p9TcCX5SMzOk8TRsi3DIrA-1; Fri, 02 Dec 2022 12:16:19 -0500 X-MC-Unique: p9TcCX5SMzOk8TRsi3DIrA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9DCE23C0F7F6; Fri, 2 Dec 2022 17:16:15 +0000 (UTC) Received: from bfoster.redhat.com (unknown [10.22.8.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B0DD40C947B; Fri, 2 Dec 2022 17:16:15 +0000 (UTC) From: Brian Foster To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ikent@redhat.com, onestero@redhat.com, willy@infradead.org, ebiederm@redhat.com Subject: [PATCH v3 2/5] pid: split cyclic id allocation cursor from idr Date: Fri, 2 Dec 2022 12:16:17 -0500 Message-Id: <20221202171620.509140-3-bfoster@redhat.com> In-Reply-To: <20221202171620.509140-1-bfoster@redhat.com> References: <20221202171620.509140-1-bfoster@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spamd-Result: default: False [-3.40 / 9.00]; BAYES_HAM(-6.00)[99.99%]; R_MISSING_CHARSET(2.50)[]; MID_CONTAINS_FROM(1.00)[]; DMARC_POLICY_ALLOW(-0.50)[redhat.com,none]; R_DKIM_ALLOW(-0.20)[redhat.com:s=mimecast20190719]; R_SPF_ALLOW(-0.20)[+ip4:170.10.133.0/24]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_SEVEN(0.00)[7]; FROM_EQ_ENVFROM(0.00)[]; DKIM_TRACE(0.00)[redhat.com:+]; RCVD_COUNT_THREE(0.00)[4]; MIME_TRACE(0.00)[0:+]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; TO_DN_NONE(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; ARC_NA(0.00)[] X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8F1E480017 X-Stat-Signature: cuzb6414mj1tfznn5r4cigw4i4asb3rm X-HE-Tag: 1670001381-155272 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: As a next step in separating pid allocation from the idr, split off the cyclic pid allocation cursor from the idr. Lift the cursor value into the struct pid_namespace. Note that this involves temporarily open-coding the cursor increment on allocation, but this is cleaned up in the subsequent patch. Signed-off-by: Matthew Wilcox Signed-off-by: Brian Foster Reviewed-by: Ian Kent --- arch/powerpc/platforms/cell/spufs/sched.c | 2 +- fs/proc/loadavg.c | 2 +- include/linux/pid_namespace.h | 1 + kernel/pid.c | 6 ++++-- kernel/pid_namespace.c | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/platforms/cell/spufs/sched.c b/arch/powerpc/platforms/cell/spufs/sched.c index 99bd027a7f7c..a2ed928d7658 100644 --- a/arch/powerpc/platforms/cell/spufs/sched.c +++ b/arch/powerpc/platforms/cell/spufs/sched.c @@ -1072,7 +1072,7 @@ static int show_spu_loadavg(struct seq_file *s, void *private) LOAD_INT(c), LOAD_FRAC(c), count_active_contexts(), atomic_read(&nr_spu_contexts), - idr_get_cursor(&task_active_pid_ns(current)->idr) - 1); + READ_ONCE(task_active_pid_ns(current)->pid_next) - 1); return 0; } #endif diff --git a/fs/proc/loadavg.c b/fs/proc/loadavg.c index 817981e57223..2740b31b6461 100644 --- a/fs/proc/loadavg.c +++ b/fs/proc/loadavg.c @@ -22,7 +22,7 @@ static int loadavg_proc_show(struct seq_file *m, void *v) LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]), LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]), nr_running(), nr_threads, - idr_get_cursor(&task_active_pid_ns(current)->idr) - 1); + READ_ONCE(task_active_pid_ns(current)->pid_next) - 1); return 0; } diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index 07481bb87d4e..82c72482019d 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -18,6 +18,7 @@ struct fs_pin; struct pid_namespace { struct idr idr; + unsigned int pid_next; struct rcu_head rcu; unsigned int pid_allocated; struct task_struct *child_reaper; diff --git a/kernel/pid.c b/kernel/pid.c index 3622f8b13143..2e2d33273c8e 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -75,6 +75,7 @@ int pid_max_max = PID_MAX_LIMIT; struct pid_namespace init_pid_ns = { .ns.count = REFCOUNT_INIT(2), .idr = IDR_INIT(init_pid_ns.idr), + .pid_next = 0, .pid_allocated = PIDNS_ADDING, .level = 0, .child_reaper = &init_task, @@ -208,7 +209,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, * init really needs pid 1, but after reaching the * maximum wrap back to RESERVED_PIDS */ - if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) + if (tmp->pid_next > RESERVED_PIDS) pid_min = RESERVED_PIDS; /* @@ -217,6 +218,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, */ nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min, pid_max, GFP_ATOMIC); + tmp->pid_next = nr + 1; } xa_unlock_irq(&tmp->idr.idr_rt); idr_preload_end(); @@ -278,7 +280,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, /* On failure to allocate the first pid, reset the state */ if (tmp == ns && tmp->pid_allocated == PIDNS_ADDING) - idr_set_cursor(&ns->idr, 0); + ns->pid_next = 0; idr_remove(&tmp->idr, upid->nr); xa_unlock_irq(&tmp->idr.idr_rt); diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index f4f8cb0435b4..a53d20c5c85e 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -272,12 +272,12 @@ static int pid_ns_ctl_handler(struct ctl_table *table, int write, * it should synchronize its usage with external means. */ - next = idr_get_cursor(&pid_ns->idr) - 1; + next = READ_ONCE(pid_ns->pid_next) - 1; tmp.data = &next; ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); if (!ret && write) - idr_set_cursor(&pid_ns->idr, next + 1); + WRITE_ONCE(pid_ns->pid_next, next + 1); return ret; }