Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5909227ybc; Wed, 27 Nov 2019 11:27:54 -0800 (PST) X-Google-Smtp-Source: APXvYqyEknaa1wE3Qnwvs7nGMUBq2jSAJSv83PCCWFwcftQ4CBIVZMhl4PT2yvJ6JYd53EAp8cxL X-Received: by 2002:a17:906:3919:: with SMTP id f25mr51502572eje.210.1574882874062; Wed, 27 Nov 2019 11:27:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574882874; cv=none; d=google.com; s=arc-20160816; b=QpuGl/LOrp7DpqmF0JQlC4hPAaqZ4I8y9yOcI4ap8EvR9A4yS5uvKA4bTJfKkyhWYW /8p6L8rbGr9VemqSnAEbAS/GVHupAlHUAh4kFd0x6m04u3U4X/VskCvlsiZG0bw4k65d mib/fplpt8vh6/BodDTPjc/0hq76Yu6CFz826zSeBbiG9LmPaI/eCMvSLQkVtqfnA3MB BG622LLWHpOkX/MddnUmR6JM0xArAZvJsaEaFoxjCi5rC0CzqdlgucLS66ceXVlO0iT0 I2vB/RGmozzkcqTumKvhScZiLaRYFpENEq9XbckeF3Ez7J8ACOe3S7OA85I+z96ttguc nkKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=3Hw0kz+i9Mk8GNpNzBnmR7s/yayYdDJKPB0j3/83kv4=; b=a/6eROJxFy9aUjZm1LAvysZE368DPoAgzYSjJzpvpc6jrfg8Hqcyu8znTzTa5i40if 39rvxB46TJ09AP0wWwpNpa1gdYws7Uvbq5+jUOCEHYzNWVT3cm/Edi+2yuLyH1V6F6QX 8gIiKPTI9pVS9GQ2eeI4f5t7u5yiFZ8nQS2U4FYq+uTmi21GvQJszsPofqI4WH3fnMOn bAg/KpHeC0evYCPF5x2GiW6RvdnMPEN7xApKoTJzQ6rN+5NbyfqY39BBat++N1Y9Re7t y5pcEnnBpFMnnv1IQCPYovea8CdeTjLABvLYDi5etRb9lPjqh5QZXyuQ+P8fDSKXVXaJ sCLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nyxjcgHA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id sa8si4026575ejb.279.2019.11.27.11.27.29; Wed, 27 Nov 2019 11:27:54 -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=@google.com header.s=20161025 header.b=nyxjcgHA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbfK0TXk (ORCPT + 99 others); Wed, 27 Nov 2019 14:23:40 -0500 Received: from mail-oi1-f193.google.com ([209.85.167.193]:37770 "EHLO mail-oi1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbfK0TXk (ORCPT ); Wed, 27 Nov 2019 14:23:40 -0500 Received: by mail-oi1-f193.google.com with SMTP id 128so13064542oih.4 for ; Wed, 27 Nov 2019 11:23:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3Hw0kz+i9Mk8GNpNzBnmR7s/yayYdDJKPB0j3/83kv4=; b=nyxjcgHAyOOTk376Ad6oVXa6b6/uZGiWdROp4ZAu14ImXIP2lRabEaWnZzN1qeKSre RAiFxTIXlri7abDsc5Dr2ZEX7wc2VRyfE+rWwForgEue8rC+W0hnI/bDQD0TU3IQvJaI ++Avuw3+7IxY6zxB6OIB+vmow7A3jVLN4vjELvLgvr1TbbGWLbE4CZ9r8G0toKPwwFXK 6cmPoeN8RcupeDZ3H2NWHcpSt8hivxj9+TLUW841txs8kM/v3D0lwTHyM1PoRD842YlA cYGZP7o2CxIqOrmKDk0GGeV7i4hcfFya+YObWK3/+jtaHbDElKCM3a7XUma18MQw8pBl fQ0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3Hw0kz+i9Mk8GNpNzBnmR7s/yayYdDJKPB0j3/83kv4=; b=lIhqZdM6shhQNfXCD0Qiefq4dI7m7NEPjoXjyOZSXDwtC6MS1Do0/AIpnwITvAqBBf z/8lAMXt7mmri3QkSte3pERcXNYx2BI/VqDREdLeUY2eoX59KEWUntQlOnzHKBZcDISO dx5eWTF70qHYLkN9fYCJukCca6quVi8NHKaNvehFT6X0cbsoQRbAhmBkY5iwH37LFjkg bo1YvRC9Uv1MKiXE3dlVh64WFQXK4kACrXNnsNKpnhauigH/yNqqsN1kFrMVSmjcvJr6 94pqgrSnqv+J9QTzrKLuVutC1R5aubfRJjKuBp1Aft/Oi1ThoFAGa1uO5tmD5O+cnFmq 8HZw== X-Gm-Message-State: APjAAAVibgwMShwUK324vglVqw6bGxu78kRVAfACPXGFJh33cUQgFZ4q XLK2+NRAEvUpxoqrbZl7Se1EkQ5vwky7RTJhp5/Phw== X-Received: by 2002:aca:782:: with SMTP id 124mr5318377oih.47.1574882618639; Wed, 27 Nov 2019 11:23:38 -0800 (PST) MIME-Version: 1.0 References: <254505c9-2b76-ebeb-306c-02aaf1704b88@kernel.dk> In-Reply-To: <254505c9-2b76-ebeb-306c-02aaf1704b88@kernel.dk> From: Jann Horn Date: Wed, 27 Nov 2019 20:23:12 +0100 Message-ID: Subject: Re: [PATCH RFC] signalfd: add support for SFD_TASK To: Jens Axboe Cc: io-uring , "linux-kernel@vger.kernel.org" , linux-fsdevel Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 27, 2019 at 6:11 AM Jens Axboe wrote: > I posted this a few weeks back, took another look at it and refined it a > bit. I'd like some input on the viability of this approach. > > A new signalfd setup flag is added, SFD_TASK. This is only valid if used > with SFD_CLOEXEC. If set, the task setting up the signalfd descriptor is > remembered in the signalfd context, and will be the one we use for > checking signals in the poll/read handlers in signalfd. > > This is needed to make signalfd useful with io_uring and aio, of which > the former in particular has my interest. > > I _think_ this is sane. To prevent the case of a task clearing O_CLOEXEC > on the signalfd descriptor, forking, and then exiting, we grab a > reference to the task when we assign it. If that original task exits, we > catch it in signalfd_flush() and ensure waiters are woken up. Mh... that's not really reliable, because you only get ->flush() from the last exiting thread (or more precisely, the last exiting task that shares the files_struct). What is your goal here? To have a reference to a task without keeping the entire task_struct around in memory if someone leaks the signalfd to another process - basically like a weak pointer? If so, you could store a refcounted reference to "struct pid" instead of a refcounted reference to the task_struct, and then do the lookup of the task_struct on ->poll and ->read (similar to what procfs does). In other words: > diff --git a/fs/signalfd.c b/fs/signalfd.c > index 44b6845b071c..4bbdab9438c1 100644 > --- a/fs/signalfd.c > +++ b/fs/signalfd.c > @@ -50,28 +50,62 @@ void signalfd_cleanup(struct sighand_struct *sighand) > > struct signalfd_ctx { > sigset_t sigmask; > + struct task_struct *task; Turn this into "struct pid *task_pid". > +static int signalfd_flush(struct file *file, void *data) > +{ > + struct signalfd_ctx *ctx = file->private_data; > + struct task_struct *tsk = ctx->task; > + > + if (tsk == current) { > + ctx->task = NULL; > + wake_up(&tsk->sighand->signalfd_wqh); > + put_task_struct(tsk); > + } > + > + return 0; > +} Get rid of this. > +static struct task_struct *signalfd_get_task(struct signalfd_ctx *ctx) > +{ > + struct task_struct *tsk = ctx->task ?: current; > + > + get_task_struct(tsk); > + return tsk; > +} Replace this with something like: if (ctx->task_pid) return get_pid_task(ctx->task_pid, PIDTYPE_PID); /* will return NULL if the task is gone */ else return get_task_struct(current); and add NULL checks to the places that call this. > @@ -167,10 +201,11 @@ static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info > int nonblock) > { > ssize_t ret; > + struct task_struct *tsk = signalfd_get_task(ctx); (Here we could even optimize away the refcounting using RCU if we wanted to, since unlike in the ->poll handler, we don't need to be able to block.) > if (ufd == -1) { > - ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); > + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); > if (!ctx) > return -ENOMEM; > > ctx->sigmask = *mask; > + if (flags & SFD_TASK) { > + ctx->task = current; > + get_task_struct(ctx->task); > + } and here do "ctx->task_pid = get_task_pid(current, PIDTYPE_PID)"