Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1694842pxb; Wed, 9 Feb 2022 02:24:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxb66PuNes24mluKJx1Pz17Nr8VUZWU/p4A2bhtVwzo9goeEwHCZVAcZiV+OXnP8vcJ7uby X-Received: by 2002:a63:6982:: with SMTP id e124mr1353523pgc.528.1644402262521; Wed, 09 Feb 2022 02:24:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644402262; cv=none; d=google.com; s=arc-20160816; b=rZwZfOAoPKMjmNkBzu2hu8KEgK6DpK5sB0+CJdjemihpqC2mS+KUZwU/u7L10YxH9K 9p96tCdkHUCgdP2DvblqsC9xnOl5Q6qxog/ib8GOGLhiBV3IGgrzeZAMouWg4mwobsy4 8oRprQq7avJtoKStPZJRkqRbSLhwdkXVUOSlH1Z3rnH3KqqBlujIWaAlW3/B0hirXs0y iiFe2DGS0xo1Sp1C9aAskxo6KTqyNma2t9q0WOXya4WyBYmDcrwvJC4iqPqBa4r+pHQ9 ZzLCenOxgkDQ3jHEBBlyYV6OiBkt8M6xPWC2qbQHLiVaPatd9lgQJzv4xbXlOSxkNAyn cGPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=cGAUHhHkpAzrJb5ixZgckyR9+lpUn48KaDM46WHxkUQ=; b=CulY34Hip/uAcrEoOTMK58jeTkL4VDG44LUEay3c+drtLGqYprz82kITHh/3n0/Ycj oVn+EcnP3PEO53LL2E0I8er1YOix72ZN9pJZijm4eCghqRz7wkmxNNnzJfcL5dhsUrK6 ldAEZdA/ihhmvKc8v/Gz9DJoFaCovkRO43djrPJRqL5pnRT5lxF9qThNeS/ozE7jQoOm Xt7M2WbRc5GKYlQJOJ3pQ24dhvrIugODM2/TRuo7+WWPhmb2vf7vUzCTvwcA/nv8fAdF jtoWocmfwe9ke5CP4KvV1Gkz+dzfiosZkNZIB7dG6NrpUI4VrmZHoaTnKuaiW42RUZ6N g+cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=OABoHAQz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h14si17413989pfh.343.2022.02.09.02.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 02:24:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=OABoHAQz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B6CFDE04FF02; Wed, 9 Feb 2022 01:19:46 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357462AbiBHLWr (ORCPT + 99 others); Tue, 8 Feb 2022 06:22:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349291AbiBHJlp (ORCPT ); Tue, 8 Feb 2022 04:41:45 -0500 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61746C03FEC0; Tue, 8 Feb 2022 01:41:44 -0800 (PST) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 2186nISK021385; Tue, 8 Feb 2022 09:41:42 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=cGAUHhHkpAzrJb5ixZgckyR9+lpUn48KaDM46WHxkUQ=; b=OABoHAQzqYlDjS0hBslU3+kz8w1sQ3HosjzYjHYSkiFYPkRD6NGQhbEeRDqHoEwWQ9RU 8Gyt0EThBI6BBOQfMD3nkLv385TmqUtEloSUlTAb1whOlOV3VHwanytxUij6Xn1f0DBf VfjFz0ZPdT58cHBwd3ZylU20VBt1PNhtLmEeG090Y+cdHcLqfdXuwkqUuguaKNn+Ab8W qQsxBcl4Q41WvoheyDUuV2exjOmVFJ7aG8MAB4HXpc+x3KsHdtMUuE0FKrLJ4uuFMJrW s7OCJjrunYxCBz3K6EOyjT4V/TRhDmzmwjEphwj5GRpG+u0K2Z/LKdOjJ1CaCkFLAOT7 Mw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 3e3fm4qds2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Feb 2022 09:41:42 +0000 Received: from m0098414.ppops.net (m0098414.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 2189dgfU013180; Tue, 8 Feb 2022 09:41:41 GMT Received: from ppma02fra.de.ibm.com (47.49.7a9f.ip4.static.sl-reverse.com [159.122.73.71]) by mx0b-001b2d01.pphosted.com with ESMTP id 3e3fm4qdrf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Feb 2022 09:41:41 +0000 Received: from pps.filterd (ppma02fra.de.ibm.com [127.0.0.1]) by ppma02fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2189clVw018221; Tue, 8 Feb 2022 09:41:39 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma02fra.de.ibm.com with ESMTP id 3e1gv9b227-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Feb 2022 09:41:39 +0000 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2189fYwx35193340 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 8 Feb 2022 09:41:34 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 30CA642056; Tue, 8 Feb 2022 09:41:34 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7700442052; Tue, 8 Feb 2022 09:41:33 +0000 (GMT) Received: from [9.145.150.231] (unknown [9.145.150.231]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 8 Feb 2022 09:41:33 +0000 (GMT) Message-ID: Date: Tue, 8 Feb 2022 10:41:33 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v2 01/11] s390/uaccess: Add copy_from/to_user_key functions Content-Language: en-US To: Janis Schoetterl-Glausch , Christian Borntraeger , Heiko Carstens Cc: Alexander Gordeev , Claudio Imbrenda , David Hildenbrand , Jonathan Corbet , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, Paolo Bonzini , Sven Schnelle , Vasily Gorbik References: <20220207165930.1608621-1-scgl@linux.ibm.com> <20220207165930.1608621-2-scgl@linux.ibm.com> From: Janosch Frank In-Reply-To: <20220207165930.1608621-2-scgl@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: jzIv20Dpv3TK71FV7oDU1q5gkXGRdx2I X-Proofpoint-GUID: ClkEsWZKCa43gF0SkDozm-nPMc7pdPQ4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-08_02,2022-02-07_02,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 spamscore=0 bulkscore=0 impostorscore=0 priorityscore=1501 phishscore=0 mlxlogscore=999 clxscore=1011 adultscore=0 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080054 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/7/22 17:59, Janis Schoetterl-Glausch wrote: > Add copy_from/to_user_key functions, which perform storage key checking. > These functions can be used by KVM for emulating instructions that need > to be key checked. > These functions differ from their non _key counterparts in > include/linux/uaccess.h only in the additional key argument and must be > kept in sync with those. > > Since the existing uaccess implementation on s390 makes use of move > instructions that support having an additional access key supplied, > we can implement raw_copy_from/to_user_key by enhancing the > existing implementation. > > Signed-off-by: Janis Schoetterl-Glausch Acked-by: Janosch Frank > --- > arch/s390/include/asm/uaccess.h | 22 +++++++++ > arch/s390/lib/uaccess.c | 81 +++++++++++++++++++++++++-------- > 2 files changed, 85 insertions(+), 18 deletions(-) > > diff --git a/arch/s390/include/asm/uaccess.h b/arch/s390/include/asm/uaccess.h > index d74e26b48604..ba1bcb91af95 100644 > --- a/arch/s390/include/asm/uaccess.h > +++ b/arch/s390/include/asm/uaccess.h > @@ -44,6 +44,28 @@ raw_copy_to_user(void __user *to, const void *from, unsigned long n); > #define INLINE_COPY_TO_USER > #endif > > +unsigned long __must_check > +_copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key); > + > +static __always_inline unsigned long __must_check > +copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key) > +{ > + if (likely(check_copy_size(to, n, false))) > + n = _copy_from_user_key(to, from, n, key); > + return n; > +} > + > +unsigned long __must_check > +_copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key); > + > +static __always_inline unsigned long __must_check > +copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key) > +{ > + if (likely(check_copy_size(from, n, true))) > + n = _copy_to_user_key(to, from, n, key); > + return n; > +} > + > int __put_user_bad(void) __attribute__((noreturn)); > int __get_user_bad(void) __attribute__((noreturn)); > > diff --git a/arch/s390/lib/uaccess.c b/arch/s390/lib/uaccess.c > index 8a5d21461889..b709239feb5d 100644 > --- a/arch/s390/lib/uaccess.c > +++ b/arch/s390/lib/uaccess.c > @@ -59,11 +59,13 @@ static inline int copy_with_mvcos(void) > #endif > > static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr, > - unsigned long size) > + unsigned long size, unsigned long key) > { > unsigned long tmp1, tmp2; > union oac spec = { > + .oac2.key = key, > .oac2.as = PSW_BITS_AS_SECONDARY, > + .oac2.k = 1, > .oac2.a = 1, > }; > > @@ -94,19 +96,19 @@ static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr > } > > static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr, > - unsigned long size) > + unsigned long size, unsigned long key) > { > unsigned long tmp1, tmp2; > > tmp1 = -256UL; > asm volatile( > " sacf 0\n" > - "0: mvcp 0(%0,%2),0(%1),%3\n" > + "0: mvcp 0(%0,%2),0(%1),%[key]\n" > "7: jz 5f\n" > "1: algr %0,%3\n" > " la %1,256(%1)\n" > " la %2,256(%2)\n" > - "2: mvcp 0(%0,%2),0(%1),%3\n" > + "2: mvcp 0(%0,%2),0(%1),%[key]\n" > "8: jnz 1b\n" > " j 5f\n" > "3: la %4,255(%1)\n" /* %4 = ptr + 255 */ > @@ -115,7 +117,7 @@ static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr, > " slgr %4,%1\n" > " clgr %0,%4\n" /* copy crosses next page boundary? */ > " jnh 6f\n" > - "4: mvcp 0(%4,%2),0(%1),%3\n" > + "4: mvcp 0(%4,%2),0(%1),%[key]\n" > "9: slgr %0,%4\n" > " j 6f\n" > "5: slgr %0,%0\n" > @@ -123,24 +125,49 @@ static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr, > EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b) > EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b) > : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2) > - : : "cc", "memory"); > + : [key] "d" (key << 4) > + : "cc", "memory"); > return size; > } > > -unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n) > +static unsigned long raw_copy_from_user_key(void *to, const void __user *from, > + unsigned long n, unsigned long key) > { > if (copy_with_mvcos()) > - return copy_from_user_mvcos(to, from, n); > - return copy_from_user_mvcp(to, from, n); > + return copy_from_user_mvcos(to, from, n, key); > + return copy_from_user_mvcp(to, from, n, key); > +} > + > +unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n) > +{ > + return raw_copy_from_user_key(to, from, n, 0); > } > EXPORT_SYMBOL(raw_copy_from_user); > > +unsigned long _copy_from_user_key(void *to, const void __user *from, > + unsigned long n, unsigned long key) > +{ > + unsigned long res = n; > + > + might_fault(); > + if (!should_fail_usercopy()) { > + instrument_copy_from_user(to, from, n); > + res = raw_copy_from_user_key(to, from, n, key); > + } > + if (unlikely(res)) > + memset(to + (n - res), 0, res); > + return res; > +} > +EXPORT_SYMBOL(_copy_from_user_key); > + > static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x, > - unsigned long size) > + unsigned long size, unsigned long key) > { > unsigned long tmp1, tmp2; > union oac spec = { > + .oac1.key = key, > .oac1.as = PSW_BITS_AS_SECONDARY, > + .oac1.k = 1, > .oac1.a = 1, > }; > > @@ -171,19 +198,19 @@ static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x, > } > > static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x, > - unsigned long size) > + unsigned long size, unsigned long key) > { > unsigned long tmp1, tmp2; > > tmp1 = -256UL; > asm volatile( > " sacf 0\n" > - "0: mvcs 0(%0,%1),0(%2),%3\n" > + "0: mvcs 0(%0,%1),0(%2),%[key]\n" > "7: jz 5f\n" > "1: algr %0,%3\n" > " la %1,256(%1)\n" > " la %2,256(%2)\n" > - "2: mvcs 0(%0,%1),0(%2),%3\n" > + "2: mvcs 0(%0,%1),0(%2),%[key]\n" > "8: jnz 1b\n" > " j 5f\n" > "3: la %4,255(%1)\n" /* %4 = ptr + 255 */ > @@ -192,7 +219,7 @@ static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x, > " slgr %4,%1\n" > " clgr %0,%4\n" /* copy crosses next page boundary? */ > " jnh 6f\n" > - "4: mvcs 0(%4,%1),0(%2),%3\n" > + "4: mvcs 0(%4,%1),0(%2),%[key]\n" > "9: slgr %0,%4\n" > " j 6f\n" > "5: slgr %0,%0\n" > @@ -200,18 +227,36 @@ static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x, > EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b) > EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b) > : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2) > - : : "cc", "memory"); > + : [key] "d" (key << 4) > + : "cc", "memory"); > return size; > } > > -unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n) > +static unsigned long raw_copy_to_user_key(void __user *to, const void *from, > + unsigned long n, unsigned long key) > { > if (copy_with_mvcos()) > - return copy_to_user_mvcos(to, from, n); > - return copy_to_user_mvcs(to, from, n); > + return copy_to_user_mvcos(to, from, n, key); > + return copy_to_user_mvcs(to, from, n, key); > +} > + > +unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n) > +{ > + return raw_copy_to_user_key(to, from, n, 0); > } > EXPORT_SYMBOL(raw_copy_to_user); > > +unsigned long _copy_to_user_key(void __user *to, const void *from, > + unsigned long n, unsigned long key) > +{ > + might_fault(); > + if (should_fail_usercopy()) > + return n; > + instrument_copy_to_user(to, from, n); > + return raw_copy_to_user_key(to, from, n, key); > +} > +EXPORT_SYMBOL(_copy_to_user_key); > + > static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size) > { > unsigned long tmp1, tmp2;