Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3192880rdb; Tue, 6 Feb 2024 09:40:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8OSIZVTHLj9AM1wrA8Z3uVuoJOkT5RN+PgBlIpSV+kqj+wwNwpg8Y0dUE+BzoqwjfM8qq X-Received: by 2002:a17:907:f9a:b0:a37:2675:76a3 with SMTP id kb26-20020a1709070f9a00b00a37267576a3mr2187642ejc.74.1707241214932; Tue, 06 Feb 2024 09:40:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707241214; cv=pass; d=google.com; s=arc-20160816; b=nwgNv7P2Dvin0C8g4KqhFek+uQea4FUL25EDimQFM1GK1MIbmeUmZOJaVzCZ1EiKc7 MRb+ad9shB2SAMQwgEqSOql1xixXWuFJ4RrdESbNc7RNeYAR1by/8POLuf4z4OmTiHSu SUOw6Sj8xpA3WryqUm7Ac7kNr6f27bF2fS3ZrlCnCGPxsTSFAnRU2KO8HkiCfhv5eUfR kh9a+E/YVRUuKdEDWNqxeAD0wHjx1I8He0ibGTmdLpXdSufsuGnqa9C1uSpMWsKZ19qt KXeVnWGPn62CSrZYUZYNQV/f/DDgDOGGvupczjofFlFQ7qXcS+J1IkNcqSrcKpCWPeNR ndoQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=9eKVsLWwsKPgnzmmex/I3IzcymtKT43xKlS15bvI8Q8=; fh=RlSQxHFA93TflCd1pPmrMRoXoh3eaSlnY2+vLMSUugs=; b=RnC2h4zXN95AseiyUImI21hGY6ivHkdoocdoXs+CQAtFAEECjkGatbIgOu8YAVdWJ5 juS8XcTxlayk3i8NLiHIaMnNaQXnBMxrC2tLYZOdtSoCrFv4uON7J/vt9GC3xDrdKqao LfnbpgBlwftiI6cHbnrSH6SL8sIyfLAlyVlRWWHPlyekDBijh2UCF8nSDKV/725a+DMd Jqw/WljOusqYwFnjkeshtSXQ2RvnKZa7S/sbkfAU66dy3VSZe3lA4+3aCjKSWj8XR84Q 3V7YrLKJq0eIpdDSIm1Eg6mrNCYKbgsCYioLzqea2eBedM8TUeZVpoMZighMWOO769FQ EcLQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b2YLMP+i; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCUwjEqMDBegGoR2vW4TbKAGVvsw5t7BaAZ3vB+zllytGUa7vka+WCErHPS/wgHSlUEg1kT6AGrXUdPdlGh4XeTE4uofYEgW2M/kUht7TQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id t17-20020a17090616d100b00a38273c1419si909346ejd.699.2024.02.06.09.40.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 09:40:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b2YLMP+i; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 am.mirrors.kernel.org (Postfix) with ESMTPS id AADD51F253BA for ; Tue, 6 Feb 2024 17:40:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 979FB14274; Tue, 6 Feb 2024 17:38:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b2YLMP+i" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 2AA7014017 for ; Tue, 6 Feb 2024 17:38:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707241128; cv=none; b=IVZGF/WLZo12+9XxE+Wm1deCfE6XT73HBATmUX9I/W5A/kDG62NIlFe5fc+XBZ/R4sJkrzswUpOAWdJoS/M/FpWw3qYJreTN14G+oauHqEcWVT1ThAluuFSDo5jm5VAd02/43I0gTGkIpJ30A99TbS4QeEL3Fs2/0w+D2liFTiE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707241128; c=relaxed/simple; bh=yQUOCOFxQ1trgskQ6RJ5rgvYAiOivGJSqWUAZbzNQlI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LTiv4qIUO+bNNDD/SHgvsal0qeCeaYamliyngJu1CUSSUubzCFVCtfVwpAE7zRJ+eSpRTG2Ly3hsNkb2DPRMwWWpSOVUzGzasRmxhiTfB269Cj/ZIeYeNvTiYpWIakeAFMxrSmKLyhxj3DutiCzeJtJdJgGL3Fb12s/WsnVuwnU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=b2YLMP+i; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707241126; 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: in-reply-to:in-reply-to:references:references; bh=9eKVsLWwsKPgnzmmex/I3IzcymtKT43xKlS15bvI8Q8=; b=b2YLMP+i/sfWlLKggjJ5fSkHJ9l8/LVd3iMbrW6dMe+jt+AJ5tT8ZR0kQrr4ElJ3WUY3e4 gDTGkYos3HqMwjgXuVEcwdTkKfK8WiQag92usRtLkkYSh3PgSjgTqUmlK/AldVRhuL8ERW E1AGEzXciehbBROr8I3rYQVBWzosqkQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-480-rJ6TbiRxOUynthcBAac81A-1; Tue, 06 Feb 2024 12:38:41 -0500 X-MC-Unique: rJ6TbiRxOUynthcBAac81A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2D605879842; Tue, 6 Feb 2024 17:38:41 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.224.46]) by smtp.corp.redhat.com (Postfix) with SMTP id A2383111F9; Tue, 6 Feb 2024 17:38:39 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 6 Feb 2024 18:37:25 +0100 (CET) Date: Tue, 6 Feb 2024 18:37:22 +0100 From: Oleg Nesterov To: Tycho Andersen Cc: Christian Brauner , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen Subject: Re: [PATCH] pidfd: getfd should always report ESRCH if a task is exiting Message-ID: <20240206173722.GA3593@redhat.com> References: <20240206164308.62620-1-tycho@tycho.pizza> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240206164308.62620-1-tycho@tycho.pizza> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 On 02/06, Tycho Andersen wrote: > > From: Tycho Andersen > > We can get EBADF from __pidfd_fget() if a task is currently exiting, which > might be confusing. agreed, because EBADF looks as if the "fd" argument was wrong, > Let's check PF_EXITING, and just report ESRCH if so. agreed, we can pretend that the task has already exited, But: > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd) > int ret; > > task = get_pid_task(pid, PIDTYPE_PID); > - if (!task) > + if (!task || task->flags & PF_EXITING) > return -ESRCH; This looks racy. Suppose that pidfd_getfd() races with the exiting task. It is possible that this task sets PF_EXITING and does exit_files() after the "task->flags & PF_EXITING" check above and before pidfd_getfd() does __pidfd_fget(), in this case pidfd_getfd() still returns the same EBADF we want to avoid. Perhaps we can change pidfd_getfd() to do if (IS_ERR(file)) return (task->flags & PF_EXITING) ? -ESRCH : PTR_ERR(file); instead? This needs a comment to explain the PF_EXITING check. And perhaps another comment to explain that we can't miss PF_EXITING if the target task has already passed exit_files, both exit_files() and fget_task() take the same task_lock(task). What do you think? Oleg.