Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3033743pxb; Fri, 12 Feb 2021 07:37:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxtcOjp1Cw4K7M1p0rFpf+Rsi0xQ2CQoAbq5hoMMlgk5+SSAUNiGVjD4m3aTvzpX0vLV3z X-Received: by 2002:a17:906:3656:: with SMTP id r22mr3581509ejb.14.1613144269928; Fri, 12 Feb 2021 07:37:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613144269; cv=none; d=google.com; s=arc-20160816; b=J/HmaddPoaMWJ7kwI+u8vO6aTW9714G0H7eJHhc4kR+8CAbR16vAMv5/VMr1b0ug7+ mc65JBKf+maPSlPe4mD5jJ+zyxGLuGUT+y+yzKWD4iXgN4l+InU4OKuMBRLiqQVcd5WX MIs5MAztJgUrMrYwFLCIbHNiSZGVEvesakTxWJ42srS76BvJSGf4iv/Te4pferDy2FPf aFwjkGpNljFS1Y7+ACrE/7UMWRnHGGMG24kimNW1On12nBtWDj9VpYfoZZDmnyjwufoD bk15wfKYwhWnnHFujhkkAKW4qT/4fLjE7mSLa7s3jho1UkQhhswxTMU1k0S0ZgOBoXGd 2nIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=Jbzn9PTKpWYF/8TPV/kOGW0XZ4Y2LoKOXT+5rADtviM=; b=C1zOrwrfIyK8H5CBIITlrkBz42hFKLd8DrWEgwDUTt+bFFGxkcnM1buLPmNe9adAVX trXNsdeJeW+wAGBWqQJ43YUTwYw0Wef4hnrJ6k+Uqh39PMOXfdUzE1s3XUrRQpeDilgq E9MVx/ET/g/EXZPp7Y6bBs4bF+QR06whRX2w4Y76+1J3/MgVH6i5l3RMuGrk0q0jhW3X /0zpRGbZG6zcqSmGIqtoELGIhpWGVOkC+Oa/ihqSzuVgVnNxAD8EoLpdi1O4tm/xgFsT zgqJRI7iXGllWK+oWNSpypjX47WWaSF7FlBZGOuoJ+9+N+aK+H+JTOdYCt8iNSfFPY0h 5Pmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=HJ3PSRo1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f16si6621480ejw.361.2021.02.12.07.37.25; Fri, 12 Feb 2021 07:37:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=HJ3PSRo1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232106AbhBLPgQ (ORCPT + 99 others); Fri, 12 Feb 2021 10:36:16 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:52962 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbhBLPfV (ORCPT ); Fri, 12 Feb 2021 10:35:21 -0500 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 11CFXxvf001536; Fri, 12 Feb 2021 10:34:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=Jbzn9PTKpWYF/8TPV/kOGW0XZ4Y2LoKOXT+5rADtviM=; b=HJ3PSRo1YoBrxsCj/045G9nAk4Jtp/vcJ6/0Emm/70ersrCziI8/yVGToncuai1ROqwZ ojq+oPnB8sSpIn67Dx1doUlRepqJnXt7MClll6GLp4Ze/zBQjnpGsIe964Zv2Ff3+Tar OxeJ1RGo+MIvxIW34yy6MfdOLsdNumP+8V+rXiTudtr6UnZbAZZkG/93YK4lQ7upAkBz y8UFThIX77o1LwjE3mK9AG20lcZNOSeRkOQrA9WLEAW33vmX1c4Dmm70D+qyOzBdI57I yqo9VOz8itEwPpcdEeggCVtmSz7BZ0yHIB9v8Tq0jBHVAlQYDIgUE/OAqmKDEZvVG00U YA== Received: from ppma04fra.de.ibm.com (6a.4a.5195.ip4.static.sl-reverse.com [149.81.74.106]) by mx0a-001b2d01.pphosted.com with ESMTP id 36nv9vr7sv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 12 Feb 2021 10:34:28 -0500 Received: from pps.filterd (ppma04fra.de.ibm.com [127.0.0.1]) by ppma04fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 11CFI1Db010169; Fri, 12 Feb 2021 15:33:36 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04fra.de.ibm.com with ESMTP id 36hjr8bjek-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 12 Feb 2021 15:33:36 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 11CFXNmK36634944 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Feb 2021 15:33:23 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E17CEA4051; Fri, 12 Feb 2021 15:33:33 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CF69AA4040; Fri, 12 Feb 2021 15:33:32 +0000 (GMT) Received: from [9.199.62.96] (unknown [9.199.62.96]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 12 Feb 2021 15:33:32 +0000 (GMT) Subject: Re: Memory keys and io_uring. To: Jens Axboe , Dave Hansen , Michael Ellerman Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <877dndzs8c.fsf@linux.ibm.com> From: "Aneesh Kumar K.V" Message-ID: <4ed6cbf6-b850-dac5-88c6-03e58dfc6631@linux.ibm.com> Date: Fri, 12 Feb 2021 21:03:32 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.737 definitions=2021-02-12_05:2021-02-12,2021-02-12 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 bulkscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 spamscore=0 adultscore=0 phishscore=0 mlxlogscore=999 impostorscore=0 mlxscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102120121 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/12/21 8:45 PM, Jens Axboe wrote: > On 2/11/21 11:59 PM, Aneesh Kumar K.V wrote: >> >> Hi, >> >> I am trying to estabilish the behaviour we should expect when passing a >> buffer with memory keys attached to io_uring syscalls. As show in the >> blow test >> >> /* >> * gcc -Wall -O2 -D_GNU_SOURCE -o pkey_uring pkey_uring.c -luring >> */ >> #include >> #include >> #include >> #include >> #include >> #include >> #include "liburing.h" >> >> #define PAGE_SIZE (64 << 10) >> >> int main(int argc, char *argv[]) >> { >> int fd, ret, pkey; >> struct io_uring ring; >> struct io_uring_sqe *sqe; >> struct io_uring_cqe *cqe; >> struct iovec iovec; >> void *buf; >> >> if (argc < 2) { >> printf("%s: file\n", argv[0]); >> return 1; >> } >> >> ret = io_uring_queue_init(1, &ring, IORING_SETUP_SQPOLL); >> if (ret < 0) { >> fprintf(stderr, "queue_init: %s\n", strerror(-ret)); >> return 1; >> } >> >> fd = open(argv[1], O_RDONLY | O_DIRECT); >> if (fd < 0) { >> perror("open"); >> return 1; >> } >> >> if (posix_memalign(&buf, PAGE_SIZE, PAGE_SIZE)) >> return 1; >> iovec.iov_base = buf; >> iovec.iov_len = PAGE_SIZE; >> >> //mprotect(buf, PAGE_SIZE, PROT_NONE); >> pkey = pkey_alloc(0, PKEY_DISABLE_WRITE); >> pkey_mprotect(buf, PAGE_SIZE, PROT_READ | PROT_WRITE, pkey); >> >> >> sqe = io_uring_get_sqe(&ring); >> if (!sqe) { >> perror("io_uring_get_sqe"); >> return 1; >> } >> io_uring_prep_readv(sqe, fd, &iovec, 1, 0); >> >> ret = io_uring_submit(&ring); >> if (ret != 1) { >> fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret)); >> return 1; >> } >> >> ret = io_uring_wait_cqe(&ring, &cqe); >> >> if (cqe->res < 0) >> fprintf(stderr, "iouring submit failed %s\n", strerror(-cqe->res)); >> else >> fprintf(stderr, "iouring submit success\n"); >> >> io_uring_cqe_seen(&ring, cqe); >> >> /* >> * let's access this via a read syscall >> */ >> ret = read(fd, buf, PAGE_SIZE); >> if (ret < 0) >> fprintf(stderr, "read failed : %s\n", strerror(errno)); >> >> close(fd); >> io_uring_queue_exit(&ring); >> >> return 0; >> } >> >> A read syscall do fail with EFAULT. But we allow read via io_uring >> syscalls. Is that ok? Considering memory keys are thread-specific we >> could debate that kernel thread can be considered to be the one that got all access >> allowed via keys or we could update that access is denied via kernel >> thread for any key value other than default key (key 0). Other option >> is to inherit the memory key restrictions when doing >> io_uring_submit() and use the same when accessing the userspace from >> kernel thread. >> >> Any thoughts here with respect to what should be behaviour? > > It this a powerpc thing? I get -EFAULT on x86 for both reads, io_uring > and regular syscall. That includes SQPOLL, not using SQPOLL, or > explicitly setting IOSQE_ASYNC on the sqe. > Interesting, I didn't check x86 because i don't have hardware that supports memory keys. I am trying to make ppc64 behavior compatible with other archs here. IIUC, in your test io_wqe/sqe kernel thread did hit access fault when touching the buffer on x86? That is different from what Dave explained earlier. With the patch 8c511eff1827 ("powerpc/kuap: Allow kernel thread to access userspace after kthread_use_mm") I now have key 0 access allowed but all other keys denied with ppc64. I was planning to change that to allow all key access based on reply from Dave. I would be curious to understand what made x86 deny the access and how did kthread inherit the key details. -aneesh